Skip to content

Commit

Permalink
Workaround for dependency issue, fixed support for missing license an…
Browse files Browse the repository at this point in the history
…d default replacement value
  • Loading branch information
mossmana committed Jan 6, 2025
1 parent bccb28a commit 5f0d5e7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
4 changes: 4 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
38 changes: 16 additions & 22 deletions tool/lib/license_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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 '<value>' 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
Expand Down Expand Up @@ -356,7 +353,7 @@ class LicenseHeader {
}
if (!updatedPathsList.contains(file.path)) {
final licenseHeaders = _processHeaders(
storedName: '',
storedName: _parseStoredName(replacementLicenseText),
existingLicenseText: '',
replacementLicenseText: replacementLicenseText,
content: '',
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions tool/test/license_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 <value4> multiline license
'''// This is some <value> 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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down

0 comments on commit 5f0d5e7

Please sign in to comment.