From eac215448ea9d252201664e02cbe085376ea4ab7 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Mon, 11 Oct 2021 16:10:22 -0400 Subject: [PATCH] [flutter_plugin_tools] Fix license-check on Windows Allow CRLF when checking licenses, since on Windows git can be configured to outconvert line endings locally. --- script/tool/CHANGELOG.md | 1 + .../tool/lib/src/license_check_command.dart | 10 ++++- .../tool/test/license_check_command_test.dart | 39 ++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/script/tool/CHANGELOG.md b/script/tool/CHANGELOG.md index a5263ba03965..6920dd62f6f3 100644 --- a/script/tool/CHANGELOG.md +++ b/script/tool/CHANGELOG.md @@ -9,6 +9,7 @@ - Added flags to `version-check` to allow overriding the platform interface major version change restriction. - Improved error handling and error messages in CHANGELOG version checks. +- Fix `license-check` when run on Windows with line ending conversion enabled. ## 0.7.1 diff --git a/script/tool/lib/src/license_check_command.dart b/script/tool/lib/src/license_check_command.dart index 8cee46b45a4c..2da02bdfb1d5 100644 --- a/script/tool/lib/src/license_check_command.dart +++ b/script/tool/lib/src/license_check_command.dart @@ -205,7 +205,10 @@ class LicenseCheckCommand extends PluginCommand { for (final File file in codeFiles) { print('Checking ${file.path}'); - final String content = await file.readAsString(); + // On Windows, git may auto-convert line endings on checkout; this should + // still pass since they will be converted back on commit. + final String content = + (await file.readAsString()).replaceAll('\r\n', '\n'); final String firstParyLicense = firstPartyLicenseBlockByExtension[p.extension(file.path)] ?? @@ -243,7 +246,10 @@ class LicenseCheckCommand extends PluginCommand { for (final File file in files) { print('Checking ${file.path}'); - if (!file.readAsStringSync().contains(_fullBsdLicenseText)) { + // On Windows, git may auto-convert line endings on checkout; this should + // still pass since they will be converted back on commit. + final String contents = file.readAsStringSync().replaceAll('\r\n', '\n'); + if (!contents.contains(_fullBsdLicenseText)) { incorrectLicenseFiles.add(file); } } diff --git a/script/tool/test/license_check_command_test.dart b/script/tool/test/license_check_command_test.dart index 288cf4696a59..d1bf233a1ee8 100644 --- a/script/tool/test/license_check_command_test.dart +++ b/script/tool/test/license_check_command_test.dart @@ -48,12 +48,14 @@ void main() { 'Use of this source code is governed by a BSD-style license that can be', 'found in the LICENSE file.', ], + bool useCrlf = false, }) { final List lines = ['$prefix$comment$copyright']; for (final String line in license) { lines.add('$comment$line'); } - file.writeAsStringSync(lines.join('\n') + suffix + '\n'); + final String newline = useCrlf ? '\r\n' : '\n'; + file.writeAsStringSync(lines.join(newline) + suffix + newline); } test('looks at only expected extensions', () async { @@ -139,6 +141,23 @@ void main() { ])); }); + test('passes correct license blocks on Windows', () async { + final File checked = root.childFile('checked.cc'); + checked.createSync(); + _writeLicense(checked, useCrlf: true); + + final List output = + await runCapturingPrint(runner, ['license-check']); + + // Sanity check that the test did actually check a file. + expect( + output, + containsAllInOrder([ + contains('Checking checked.cc'), + contains('All files passed validation!'), + ])); + }); + test('handles the comment styles for all supported languages', () async { final File fileA = root.childFile('file_a.cc'); fileA.createSync(); @@ -405,6 +424,24 @@ void main() { ])); }); + test('passes correct LICENSE files on Windows', () async { + final File license = root.childFile('LICENSE'); + license.createSync(); + license + .writeAsStringSync(_correctLicenseFileText.replaceAll('\n', '\r\n')); + + final List output = + await runCapturingPrint(runner, ['license-check']); + + // Sanity check that the test did actually check the file. + expect( + output, + containsAllInOrder([ + contains('Checking LICENSE'), + contains('All files passed validation!'), + ])); + }); + test('fails if any first-party LICENSE files are incorrectly formatted', () async { final File license = root.childFile('LICENSE');