From 13fc1da249d13039b24a880ad99c78763a62ea5c Mon Sep 17 00:00:00 2001 From: Ann Mossman <233583+mossmana@users.noreply.github.com> Date: Mon, 6 Jan 2025 14:42:29 -0800 Subject: [PATCH] Workaround for dependency issue, fixed support for missing license and default replacement value --- pubspec.yaml | 4 ++++ tool/lib/license_utils.dart | 38 +++++++++++++------------------ tool/test/license_utils_test.dart | 30 ++++++++++++++++++++---- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index cef5b3b9205..6d300e2e0f3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,3 +12,7 @@ workspace: dev_dependencies: build_runner: ^2.3.3 flutter_lints: ^5.0.0 + +dependency_overrides: + analyzer: 6.11.0 + dart_style: 2.3.3 diff --git a/tool/lib/license_utils.dart b/tool/lib/license_utils.dart index c63338fe906..e8b8f5e8c5e 100644 --- a/tool/lib/license_utils.dart +++ b/tool/lib/license_utils.dart @@ -212,10 +212,10 @@ class LicenseHeader { ), ); await for (final content in stream) { - // Return just the license headers for the simple case with no stored - // value requested (i.e. content matches licenseText verbatim) + final storedName = _parseStoredName(replacementLicenseText); if (content.contains(existingLicenseText)) { - final storedName = _parseStoredName(replacementLicenseText); + // Return just the license headers for the simple case with no stored + // value requested (i.e. content matches licenseText verbatim) replacementLicenseText = replacementLicenseText.replaceAll( '<$storedName>', defaultStoredValue ?? DateTime.now().year.toString(), @@ -227,17 +227,14 @@ class LicenseHeader { } // Return a non-empty map for the case where there is a stored value // requested (i.e. when there is a '' defined in the license text) - final storedName = _parseStoredName(existingLicenseText); - if (storedName.isNotEmpty) { - return _processHeaders( - storedName: storedName, - existingLicenseText: existingLicenseText, - replacementLicenseText: replacementLicenseText, - content: content, - ); - } + return _processHeaders( + storedName: storedName, + existingLicenseText: existingLicenseText, + replacementLicenseText: replacementLicenseText, + content: content, + ); } - throw StateError('License header expected in ${file.path}, but not found!'); + throw StateError('License header could not be added to ${file.path}'); } /// Returns a copy of the given [file] with the [existingHeader] replaced by @@ -356,7 +353,7 @@ class LicenseHeader { } if (!updatedPathsList.contains(file.path)) { final licenseHeaders = _processHeaders( - storedName: '', + storedName: _parseStoredName(replacementLicenseText), existingLicenseText: '', replacementLicenseText: replacementLicenseText, content: '', @@ -401,13 +398,6 @@ class LicenseHeader { required String replacementLicenseText, required String content, }) { - if (existingLicenseText.isEmpty) { - final defaultReplacementHeader = replacementLicenseText.replaceAll( - '<$storedName>', - DateTime.now().year.toString(), - ); - return (existingHeader: '', replacementHeader: defaultReplacementHeader); - } final matchStr = RegExp.escape(existingLicenseText); final storedNameIndex = matchStr.indexOf('<$storedName>'); if (storedNameIndex != -1) { @@ -438,7 +428,11 @@ class LicenseHeader { ); } } - return const (existingHeader: '', replacementHeader: ''); + final defaultReplacementHeader = replacementLicenseText.replaceAll( + '<$storedName>', + DateTime.now().year.toString(), + ); + return (existingHeader: '', replacementHeader: defaultReplacementHeader); } // TODO(mossmana) Add support for multiple stored names diff --git a/tool/test/license_utils_test.dart b/tool/test/license_utils_test.dart index a149cade759..9b771ffb42c 100644 --- a/tool/test/license_utils_test.dart +++ b/tool/test/license_utils_test.dart @@ -174,10 +174,11 @@ text that should be removed from the file. */'''; const existingLicenseText = '''// This is some multiline license text to // remove that does not contain a stored value.'''; const replacementLicenseText = - '''// This is some multiline license + '''// This is some multiline license // text that should be added to the file.'''; - final replacementInfo = await _getTestReplacementInfo( + // Contains existing license + var replacementInfo = await _getTestReplacementInfo( testFile: testFile10, existingLicenseText: existingLicenseText, replacementLicenseText: replacementLicenseText, @@ -200,6 +201,20 @@ text that should be removed from the file. */'''; replacementInfo.replacementHeader, equals(expectedReplacementHeader), ); + + // Missing existing license + replacementInfo = await _getTestReplacementInfo( + testFile: testFile10, + existingLicenseText: existingLicenseText, + replacementLicenseText: replacementLicenseText, + ); + + expect(replacementInfo.existingHeader, equals(expectedExistingHeader)); + + expect( + replacementInfo.replacementHeader, + equals(expectedReplacementHeader), + ); }); test('stored value preserved in replacement header', () async { @@ -334,19 +349,19 @@ text that should be added to the file. */''', final config = LicenseConfig.fromYamlFile(configFile); final header = LicenseHeader(); - final contentsBeforeUpdate = testFile1.readAsStringSync(); + // missing license + final contentsBeforeUpdate = testFile9.readAsStringSync(); final results = await header.bulkUpdate( directory: testDirectory, config: config, ); - final contentsAfterUpdate = testFile1.readAsStringSync(); + final contentsAfterUpdate = testFile9.readAsStringSync(); final includedPaths = results.includedPaths; expect(includedPaths, isNotNull); expect(includedPaths.length, equals(9)); // Order is not guaranteed expect(includedPaths.contains(testFile1.path), true); - expect(contentsBeforeUpdate, isNot(equals(contentsAfterUpdate))); expect(includedPaths.contains(testFile2.path), true); expect(includedPaths.contains(testFile3.path), true); expect(includedPaths.contains(testFile7.path), true); @@ -369,6 +384,11 @@ text that should be added to the file. */''', expect(updatedPaths.contains(testFile10.path), true); expect(updatedPaths.contains(skipFile.path), false); expect(updatedPaths.contains(doNothingFile.path), false); + + expect(contentsBeforeUpdate, isNot(equals(contentsAfterUpdate))); + // There is an extremely rare failure case on the year boundary. + // TODO(mossmana): Handle running test on Dec 31 - Jan 1. + expect(contentsAfterUpdate, contains(DateTime.now().year.toString())); }); test('license headers bulk update can be dry run', () async {