@@ -7,6 +7,7 @@ import 'package:file/file.dart';
7
7
import 'package:flutter_plugin_tools/src/common/core.dart' ;
8
8
import 'package:flutter_plugin_tools/src/license_check_command.dart' ;
9
9
import 'package:git/git.dart' ;
10
+ import 'package:path/path.dart' as p;
10
11
import 'package:platform/platform.dart' ;
11
12
import 'package:test/test.dart' ;
12
13
@@ -17,13 +18,14 @@ void main() {
17
18
group ('LicenseCheckCommand' , () {
18
19
late CommandRunner <void > runner;
19
20
late Platform platform;
21
+ late RecordingProcessRunner gitProcessRunner;
20
22
late Directory packagesDir;
21
23
late Directory root;
22
24
23
25
setUp (() {
24
26
platform = MockPlatformWithSeparator ();
25
27
final GitDir gitDir;
26
- (: packagesDir, processRunner: _, gitProcessRunner : _ , : gitDir) =
28
+ (: packagesDir, processRunner: _, : gitProcessRunner , : gitDir) =
27
29
configureBaseCommandMocks (platform: platform);
28
30
root = packagesDir.parent;
29
31
@@ -64,6 +66,22 @@ void main() {
64
66
file.writeAsStringSync (lines.join (newline) + suffix + newline);
65
67
}
66
68
69
+ /// Mocks `git ls-files` to return all files in `root` , simulating a
70
+ /// repository where everything present is checked in.
71
+ void mockGitFilesListWithAllFiles (Directory root) {
72
+ final String fileList = root
73
+ .listSync (recursive: true , followLinks: false )
74
+ .whereType <File >()
75
+ .map ((File f) => p.posix
76
+ .joinAll (p.split (p.relative (f.absolute.path, from: root.path))))
77
+ .join ('\n ' );
78
+
79
+ gitProcessRunner.mockProcessesForExecutable['git-ls-files' ] =
80
+ < FakeProcessInfo > [
81
+ FakeProcessInfo (MockProcess (stdout: '$fileList \n ' )),
82
+ ];
83
+ }
84
+
67
85
test ('looks at only expected extensions' , () async {
68
86
final Map <String , bool > extensions = < String , bool > {
69
87
'c' : true ,
@@ -88,6 +106,7 @@ void main() {
88
106
for (final String fileExtension in extensions.keys) {
89
107
root.childFile ('$filenameBase .$fileExtension ' ).createSync ();
90
108
}
109
+ mockGitFilesListWithAllFiles (root);
91
110
92
111
final List <String > output = await runCapturingPrint (
93
112
runner, < String > ['license-check' ], errorHandler: (Error e) {
@@ -121,6 +140,7 @@ void main() {
121
140
for (final String name in ignoredFiles) {
122
141
root.childFile (name).createSync ();
123
142
}
143
+ mockGitFilesListWithAllFiles (root);
124
144
125
145
final List <String > output =
126
146
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -157,7 +177,9 @@ void main() {
157
177
}
158
178
});
159
179
160
- test ('ignores FlutterGeneratedPluginSwiftPackage' , () async {
180
+ test ('ignores files that are not checked in' , () async {
181
+ mockGitFilesListWithAllFiles (root);
182
+ // Add files after creating the mock output from created files.
161
183
final Directory packageDir = root
162
184
.childDirectory ('FlutterGeneratedPluginSwiftPackage' )
163
185
..createSync ();
@@ -181,6 +203,7 @@ void main() {
181
203
writeLicense (checked);
182
204
final File notChecked = root.childFile ('not_checked.md' );
183
205
notChecked.createSync ();
206
+ mockGitFilesListWithAllFiles (root);
184
207
185
208
final List <String > output =
186
209
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -198,6 +221,7 @@ void main() {
198
221
final File checked = root.childFile ('checked.cc' );
199
222
checked.createSync ();
200
223
writeLicense (checked, useCrlf: true );
224
+ mockGitFilesListWithAllFiles (root);
201
225
202
226
final List <String > output =
203
227
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -221,6 +245,7 @@ void main() {
221
245
final File fileC = root.childFile ('file_c.html' );
222
246
fileC.createSync ();
223
247
writeLicense (fileC, comment: '' , prefix: '<!-- ' , suffix: ' -->' );
248
+ mockGitFilesListWithAllFiles (root);
224
249
225
250
final List <String > output =
226
251
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -245,6 +270,7 @@ void main() {
245
270
writeLicense (goodB);
246
271
root.childFile ('bad.cc' ).createSync ();
247
272
root.childFile ('bad.h' ).createSync ();
273
+ mockGitFilesListWithAllFiles (root);
248
274
249
275
Error ? commandError;
250
276
final List <String > output = await runCapturingPrint (
@@ -273,6 +299,7 @@ void main() {
273
299
final File bad = root.childFile ('bad.cc' );
274
300
bad.createSync ();
275
301
writeLicense (bad, copyright: '' );
302
+ mockGitFilesListWithAllFiles (root);
276
303
277
304
Error ? commandError;
278
305
final List <String > output = await runCapturingPrint (
@@ -300,6 +327,7 @@ void main() {
300
327
final File bad = root.childFile ('bad.cc' );
301
328
bad.createSync ();
302
329
writeLicense (bad, license: < String > []);
330
+ mockGitFilesListWithAllFiles (root);
303
331
304
332
Error ? commandError;
305
333
final List <String > output = await runCapturingPrint (
@@ -325,6 +353,7 @@ void main() {
325
353
final File thirdPartyFile = root.childFile ('third_party.cc' );
326
354
thirdPartyFile.createSync ();
327
355
writeLicense (thirdPartyFile, copyright: 'Copyright 2017 Someone Else' );
356
+ mockGitFilesListWithAllFiles (root);
328
357
329
358
Error ? commandError;
330
359
final List <String > output = await runCapturingPrint (
@@ -359,6 +388,7 @@ void main() {
359
388
'Licensed under the Apache License, Version 2.0 (the "License");' ,
360
389
'you may not use this file except in compliance with the License.'
361
390
]);
391
+ mockGitFilesListWithAllFiles (root);
362
392
363
393
final List <String > output =
364
394
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -381,6 +411,7 @@ void main() {
381
411
.childFile ('first_party.cc' );
382
412
firstPartyFileInThirdParty.createSync (recursive: true );
383
413
writeLicense (firstPartyFileInThirdParty);
414
+ mockGitFilesListWithAllFiles (root);
384
415
385
416
final List <String > output =
386
417
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -404,6 +435,7 @@ void main() {
404
435
'This program is free software: you can redistribute it and/or modify' ,
405
436
'it under the terms of the GNU General Public License' ,
406
437
]);
438
+ mockGitFilesListWithAllFiles (root);
407
439
408
440
Error ? commandError;
409
441
final List <String > output = await runCapturingPrint (
@@ -439,6 +471,7 @@ void main() {
439
471
'you may not use this file except in compliance with the License.'
440
472
],
441
473
);
474
+ mockGitFilesListWithAllFiles (root);
442
475
443
476
Error ? commandError;
444
477
final List <String > output = await runCapturingPrint (
@@ -464,6 +497,7 @@ void main() {
464
497
final File license = root.childFile ('LICENSE' );
465
498
license.createSync ();
466
499
license.writeAsStringSync (_correctLicenseFileText);
500
+ mockGitFilesListWithAllFiles (root);
467
501
468
502
final List <String > output =
469
503
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -482,6 +516,7 @@ void main() {
482
516
license.createSync ();
483
517
license
484
518
.writeAsStringSync (_correctLicenseFileText.replaceAll ('\n ' , '\r\n ' ));
519
+ mockGitFilesListWithAllFiles (root);
485
520
486
521
final List <String > output =
487
522
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -500,6 +535,7 @@ void main() {
500
535
final File license = root.childFile ('LICENSE' );
501
536
license.createSync ();
502
537
license.writeAsStringSync (_incorrectLicenseFileText);
538
+ mockGitFilesListWithAllFiles (root);
503
539
504
540
Error ? commandError;
505
541
final List <String > output = await runCapturingPrint (
@@ -516,6 +552,7 @@ void main() {
516
552
root.childDirectory ('third_party' ).childFile ('LICENSE' );
517
553
license.createSync (recursive: true );
518
554
license.writeAsStringSync (_incorrectLicenseFileText);
555
+ mockGitFilesListWithAllFiles (root);
519
556
520
557
final List <String > output =
521
558
await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -533,6 +570,7 @@ void main() {
533
570
final File license = root.childFile ('LICENSE' );
534
571
license.createSync ();
535
572
license.writeAsStringSync (_incorrectLicenseFileText);
573
+ mockGitFilesListWithAllFiles (root);
536
574
537
575
Error ? commandError;
538
576
final List <String > output = await runCapturingPrint (
@@ -563,7 +601,7 @@ void main() {
563
601
final File checked = root.childFile ('Package.swift' );
564
602
checked.createSync ();
565
603
writeLicense (checked, prefix: '// swift-tools-version: 5.9\n ' );
566
-
604
+ mockGitFilesListWithAllFiles (root);
567
605
final List <String > output =
568
606
await runCapturingPrint (runner, < String > ['license-check' ]);
569
607
0 commit comments