Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2791e9b

Browse files
committedDec 18, 2024
skip processing files if the license already exists
1 parent 622dd4c commit 2791e9b

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed
 

‎tool/lib/license_utils.dart

+21-3
Original file line numberDiff line numberDiff line change
@@ -303,19 +303,37 @@ class LicenseHeader {
303303
// skip if add index doesn't exist for extension
304304
continue;
305305
}
306+
final fileLength = file.lengthSync();
307+
const bufferSize = 20;
306308
final replacementLicenseText = config.addLicenses[addIndex];
309+
final byteCount = min(
310+
bufferSize + replacementLicenseText.length,
311+
fileLength,
312+
);
313+
var replacementInfo = await getReplacementInfo(
314+
file: file,
315+
existingLicenseText: replacementLicenseText,
316+
replacementLicenseText: replacementLicenseText,
317+
byteCount: byteCount as int,
318+
);
319+
if (replacementInfo.existingHeader.isNotEmpty &&
320+
replacementInfo.replacementHeader.isNotEmpty &&
321+
replacementInfo.existingHeader ==
322+
replacementInfo.replacementHeader) {
323+
// Do nothing if the replacement header is the same as the
324+
// existing header
325+
continue;
326+
}
307327
final removeIndices = config.getRemoveIndicesForExtension(extension);
308328
for (final removeIndex in removeIndices) {
309329
final existingLicenseText = config.removeLicenses[removeIndex];
310-
final fileLength = file.lengthSync();
311-
const bufferSize = 20;
312330
// Assume that the license text will be near the start of the file,
313331
// but add in some buffer.
314332
final byteCount = min(
315333
bufferSize + existingLicenseText.length,
316334
fileLength,
317335
);
318-
final replacementInfo = await getReplacementInfo(
336+
replacementInfo = await getReplacementInfo(
319337
file: file,
320338
existingLicenseText: existingLicenseText,
321339
replacementLicenseText: replacementLicenseText,

‎tool/test/license_utils_test.dart

+21-27
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ late File testFile9;
5252
late File testFile10;
5353
late File excludeFile1;
5454
late File excludeFile2;
55-
late File skippedFile;
55+
late File skipFile;
56+
late File doNothingFile;
5657

5758
void main() {
5859
group('config file tests', () {
@@ -257,27 +258,6 @@ text that should be added to the file. */''',
257258
}
258259
});
259260

260-
test('update skipped if license text not found', () async {
261-
var errorMessage = '';
262-
final header = LicenseHeader();
263-
try {
264-
await header.getReplacementInfo(
265-
file: testFile9,
266-
existingLicenseText: 'test',
267-
replacementLicenseText: 'test',
268-
byteCount: 50,
269-
);
270-
} on StateError catch (e) {
271-
errorMessage = e.toString();
272-
}
273-
expect(
274-
errorMessage,
275-
equals(
276-
'Bad state: License header expected in ${testFile9.path}, but not found!',
277-
),
278-
);
279-
});
280-
281261
test("update skipped if file can't be read", () async {
282262
var errorMessage = '';
283263
final header = LicenseHeader();
@@ -363,7 +343,7 @@ text that should be added to the file. */''',
363343

364344
final includedPaths = results.includedPaths;
365345
expect(includedPaths, isNotNull);
366-
expect(includedPaths.length, equals(8));
346+
expect(includedPaths.length, equals(9));
367347
// Order is not guaranteed
368348
expect(includedPaths.contains(testFile1.path), true);
369349
expect(contentsBeforeUpdate, isNot(equals(contentsAfterUpdate)));
@@ -373,7 +353,8 @@ text that should be added to the file. */''',
373353
expect(includedPaths.contains(testFile8.path), true);
374354
expect(includedPaths.contains(testFile9.path), true);
375355
expect(includedPaths.contains(testFile10.path), true);
376-
expect(includedPaths.contains(skippedFile.path), true);
356+
expect(includedPaths.contains(skipFile.path), true);
357+
expect(includedPaths.contains(doNothingFile.path), true);
377358

378359
final updatedPaths = results.updatedPaths;
379360
expect(updatedPaths, isNotNull);
@@ -386,7 +367,8 @@ text that should be added to the file. */''',
386367
expect(updatedPaths.contains(testFile8.path), true);
387368
expect(updatedPaths.contains(testFile9.path), true);
388369
expect(updatedPaths.contains(testFile10.path), true);
389-
expect(updatedPaths.contains(skippedFile.path), false);
370+
expect(updatedPaths.contains(skipFile.path), false);
371+
expect(updatedPaths.contains(doNothingFile.path), false);
390372
});
391373

392374
test('license headers bulk update can be dry run', () async {
@@ -544,6 +526,10 @@ update_paths:
544526
ext2:
545527
remove:
546528
- 2
529+
add: 1
530+
ext3:
531+
remove:
532+
- 3
547533
add: 1''';
548534

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

591-
skippedFile = File(p.join(repoRoot.path, 'test.skip'))
577+
final licenseText = '''
578+
# This is other 2001 multiline license
579+
# text that should be added to the file.
580+
''';
581+
doNothingFile = File(p.join(repoRoot.path, 'doNothingFile.ext3'))
582+
..createSync(recursive: true);
583+
doNothingFile.writeAsStringSync(licenseText + extraText, flush: true);
584+
585+
skipFile = File(p.join(repoRoot.path, 'test.skip'))
592586
..createSync(recursive: true);
593-
skippedFile.writeAsStringSync(extraText, flush: true);
587+
skipFile.writeAsStringSync(extraText, flush: true);
594588

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

0 commit comments

Comments
 (0)
Please sign in to comment.