Skip to content

Commit

Permalink
skip processing files if the license already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
mossmana committed Dec 18, 2024
1 parent d628c45 commit ddca318
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
24 changes: 21 additions & 3 deletions tool/lib/license_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,37 @@ class LicenseHeader {
// skip if add index doesn't exist for extension
continue;
}
final fileLength = file.lengthSync();
const bufferSize = 20;
final replacementLicenseText = config.addLicenses[addIndex];
final byteCount = min(
bufferSize + replacementLicenseText.length,
fileLength,
);
var replacementInfo = await getReplacementInfo(
file: file,
existingLicenseText: replacementLicenseText,
replacementLicenseText: replacementLicenseText,
byteCount: byteCount as int,
);
if (replacementInfo.existingHeader.isNotEmpty &&
replacementInfo.replacementHeader.isNotEmpty &&
replacementInfo.existingHeader ==
replacementInfo.replacementHeader) {
// Do nothing if the replacement header is the same as the
// existing header
continue;
}
final removeIndices = config.getRemoveIndicesForExtension(extension);
for (final removeIndex in removeIndices) {
final existingLicenseText = config.removeLicenses[removeIndex];
final fileLength = file.lengthSync();
const bufferSize = 20;
// Assume that the license text will be near the start of the file,
// but add in some buffer.
final byteCount = min(
bufferSize + existingLicenseText.length,
fileLength,
);
final replacementInfo = await getReplacementInfo(
replacementInfo = await getReplacementInfo(
file: file,
existingLicenseText: existingLicenseText,
replacementLicenseText: replacementLicenseText,
Expand Down
48 changes: 21 additions & 27 deletions tool/test/license_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ late File testFile9;
late File testFile10;
late File excludeFile1;
late File excludeFile2;
late File skippedFile;
late File skipFile;
late File doNothingFile;

void main() {
group('config file tests', () {
Expand Down Expand Up @@ -257,27 +258,6 @@ text that should be added to the file. */''',
}
});

test('update skipped if license text not found', () async {
var errorMessage = '';
final header = LicenseHeader();
try {
await header.getReplacementInfo(
file: testFile9,
existingLicenseText: 'test',
replacementLicenseText: 'test',
byteCount: 50,
);
} on StateError catch (e) {
errorMessage = e.toString();
}
expect(
errorMessage,
equals(
'Bad state: License header expected in ${testFile9.path}, but not found!',
),
);
});

test("update skipped if file can't be read", () async {
var errorMessage = '';
final header = LicenseHeader();
Expand Down Expand Up @@ -363,7 +343,7 @@ text that should be added to the file. */''',

final includedPaths = results.includedPaths;
expect(includedPaths, isNotNull);
expect(includedPaths.length, equals(8));
expect(includedPaths.length, equals(9));
// Order is not guaranteed
expect(includedPaths.contains(testFile1.path), true);
expect(contentsBeforeUpdate, isNot(equals(contentsAfterUpdate)));
Expand All @@ -373,7 +353,8 @@ text that should be added to the file. */''',
expect(includedPaths.contains(testFile8.path), true);
expect(includedPaths.contains(testFile9.path), true);
expect(includedPaths.contains(testFile10.path), true);
expect(includedPaths.contains(skippedFile.path), true);
expect(includedPaths.contains(skipFile.path), true);
expect(includedPaths.contains(doNothingFile.path), true);

final updatedPaths = results.updatedPaths;
expect(updatedPaths, isNotNull);
Expand All @@ -386,7 +367,8 @@ text that should be added to the file. */''',
expect(updatedPaths.contains(testFile8.path), true);
expect(updatedPaths.contains(testFile9.path), true);
expect(updatedPaths.contains(testFile10.path), true);
expect(updatedPaths.contains(skippedFile.path), false);
expect(updatedPaths.contains(skipFile.path), false);
expect(updatedPaths.contains(doNothingFile.path), false);
});

test('license headers bulk update can be dry run', () async {
Expand Down Expand Up @@ -544,6 +526,10 @@ update_paths:
ext2:
remove:
- 2
add: 1
ext3:
remove:
- 3
add: 1''';

configFile.writeAsStringSync(contents, flush: true);
Expand Down Expand Up @@ -588,9 +574,17 @@ Future<void> _setupTestDirectoryStructure() async {
..createSync(recursive: true);
testFile2.writeAsStringSync(licenseText3 + extraText, flush: true);

skippedFile = File(p.join(repoRoot.path, 'test.skip'))
final licenseText = '''
# This is other 2001 multiline license
# text that should be added to the file.
''';
doNothingFile = File(p.join(repoRoot.path, 'doNothingFile.ext3'))
..createSync(recursive: true);
doNothingFile.writeAsStringSync(licenseText + extraText, flush: true);

skipFile = File(p.join(repoRoot.path, 'test.skip'))
..createSync(recursive: true);
skippedFile.writeAsStringSync(extraText, flush: true);
skipFile.writeAsStringSync(extraText, flush: true);

// Setup /repo_root/.hidden directory structure
Directory(p.join(repoRoot.path, '.hidden')).createSync(recursive: true);
Expand Down

0 comments on commit ddca318

Please sign in to comment.