Skip to content

Commit 9a78953

Browse files
[flutter_plugin_tools] Fix license-check on Windows (flutter#4425)
1 parent fecd22e commit 9a78953

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

script/tool/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `license-check` now validates Kotlin files.
1414
- `pubspec-check` now checks that the description is of the pub-recommended
1515
length.
16+
- Fix `license-check` when run on Windows with line ending conversion enabled.
1617

1718
## 0.7.1
1819

script/tool/lib/src/license_check_command.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ class LicenseCheckCommand extends PluginCommand {
206206

207207
for (final File file in codeFiles) {
208208
print('Checking ${file.path}');
209-
final String content = await file.readAsString();
209+
// On Windows, git may auto-convert line endings on checkout; this should
210+
// still pass since they will be converted back on commit.
211+
final String content =
212+
(await file.readAsString()).replaceAll('\r\n', '\n');
210213

211214
final String firstParyLicense =
212215
firstPartyLicenseBlockByExtension[p.extension(file.path)] ??
@@ -244,7 +247,10 @@ class LicenseCheckCommand extends PluginCommand {
244247

245248
for (final File file in files) {
246249
print('Checking ${file.path}');
247-
if (!file.readAsStringSync().contains(_fullBsdLicenseText)) {
250+
// On Windows, git may auto-convert line endings on checkout; this should
251+
// still pass since they will be converted back on commit.
252+
final String contents = file.readAsStringSync().replaceAll('\r\n', '\n');
253+
if (!contents.contains(_fullBsdLicenseText)) {
248254
incorrectLicenseFiles.add(file);
249255
}
250256
}

script/tool/test/license_check_command_test.dart

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ void main() {
4848
'Use of this source code is governed by a BSD-style license that can be',
4949
'found in the LICENSE file.',
5050
],
51+
bool useCrlf = false,
5152
}) {
5253
final List<String> lines = <String>['$prefix$comment$copyright'];
5354
for (final String line in license) {
5455
lines.add('$comment$line');
5556
}
56-
file.writeAsStringSync(lines.join('\n') + suffix + '\n');
57+
final String newline = useCrlf ? '\r\n' : '\n';
58+
file.writeAsStringSync(lines.join(newline) + suffix + newline);
5759
}
5860

5961
test('looks at only expected extensions', () async {
@@ -140,6 +142,23 @@ void main() {
140142
]));
141143
});
142144

145+
test('passes correct license blocks on Windows', () async {
146+
final File checked = root.childFile('checked.cc');
147+
checked.createSync();
148+
_writeLicense(checked, useCrlf: true);
149+
150+
final List<String> output =
151+
await runCapturingPrint(runner, <String>['license-check']);
152+
153+
// Sanity check that the test did actually check a file.
154+
expect(
155+
output,
156+
containsAllInOrder(<Matcher>[
157+
contains('Checking checked.cc'),
158+
contains('All files passed validation!'),
159+
]));
160+
});
161+
143162
test('handles the comment styles for all supported languages', () async {
144163
final File fileA = root.childFile('file_a.cc');
145164
fileA.createSync();
@@ -406,6 +425,24 @@ void main() {
406425
]));
407426
});
408427

428+
test('passes correct LICENSE files on Windows', () async {
429+
final File license = root.childFile('LICENSE');
430+
license.createSync();
431+
license
432+
.writeAsStringSync(_correctLicenseFileText.replaceAll('\n', '\r\n'));
433+
434+
final List<String> output =
435+
await runCapturingPrint(runner, <String>['license-check']);
436+
437+
// Sanity check that the test did actually check the file.
438+
expect(
439+
output,
440+
containsAllInOrder(<Matcher>[
441+
contains('Checking LICENSE'),
442+
contains('All files passed validation!'),
443+
]));
444+
});
445+
409446
test('fails if any first-party LICENSE files are incorrectly formatted',
410447
() async {
411448
final File license = root.childFile('LICENSE');

0 commit comments

Comments
 (0)