Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[flutter_plugin_tools] Fix license-check on Windows #4425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions script/tool/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `license-check` now validates Kotlin files.
- `pubspec-check` now checks that the description is of the pub-recommended
length.
- Fix `license-check` when run on Windows with line ending conversion enabled.

## 0.7.1

Expand Down
10 changes: 8 additions & 2 deletions script/tool/lib/src/license_check_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,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)] ??
Expand Down Expand Up @@ -244,7 +247,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);
}
}
Expand Down
39 changes: 38 additions & 1 deletion script/tool/test/license_check_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> lines = <String>['$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 {
Expand Down Expand Up @@ -140,6 +142,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<String> output =
await runCapturingPrint(runner, <String>['license-check']);

// Sanity check that the test did actually check a file.
expect(
output,
containsAllInOrder(<Matcher>[
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();
Expand Down Expand Up @@ -406,6 +425,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<String> output =
await runCapturingPrint(runner, <String>['license-check']);

// Sanity check that the test did actually check the file.
expect(
output,
containsAllInOrder(<Matcher>[
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');
Expand Down