From ddca3181d18541db4cc281d671850c3b64c05c23 Mon Sep 17 00:00:00 2001 From: Ann Mossman <233583+mossmana@users.noreply.github.com> Date: Tue, 17 Dec 2024 16:58:26 -0800 Subject: [PATCH] skip processing files if the license already exists --- tool/lib/license_utils.dart | 24 ++++++++++++++-- tool/test/license_utils_test.dart | 48 ++++++++++++++----------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/tool/lib/license_utils.dart b/tool/lib/license_utils.dart index cf2ad3ac20b..c63338fe906 100644 --- a/tool/lib/license_utils.dart +++ b/tool/lib/license_utils.dart @@ -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, diff --git a/tool/test/license_utils_test.dart b/tool/test/license_utils_test.dart index 8eb0e731034..a149cade759 100644 --- a/tool/test/license_utils_test.dart +++ b/tool/test/license_utils_test.dart @@ -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', () { @@ -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(); @@ -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))); @@ -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); @@ -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 { @@ -544,6 +526,10 @@ update_paths: ext2: remove: - 2 + add: 1 + ext3: + remove: + - 3 add: 1'''; configFile.writeAsStringSync(contents, flush: true); @@ -588,9 +574,17 @@ Future _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);