@@ -7,6 +7,7 @@ import 'package:file/file.dart';
77import 'package:flutter_plugin_tools/src/common/core.dart' ;
88import 'package:flutter_plugin_tools/src/license_check_command.dart' ;
99import 'package:git/git.dart' ;
10+ import 'package:path/path.dart' as p;
1011import 'package:platform/platform.dart' ;
1112import 'package:test/test.dart' ;
1213
@@ -17,13 +18,14 @@ void main() {
1718 group ('LicenseCheckCommand' , () {
1819 late CommandRunner <void > runner;
1920 late Platform platform;
21+ late RecordingProcessRunner gitProcessRunner;
2022 late Directory packagesDir;
2123 late Directory root;
2224
2325 setUp (() {
2426 platform = MockPlatformWithSeparator ();
2527 final GitDir gitDir;
26- (: packagesDir, processRunner: _, gitProcessRunner : _ , : gitDir) =
28+ (: packagesDir, processRunner: _, : gitProcessRunner , : gitDir) =
2729 configureBaseCommandMocks (platform: platform);
2830 root = packagesDir.parent;
2931
@@ -64,6 +66,22 @@ void main() {
6466 file.writeAsStringSync (lines.join (newline) + suffix + newline);
6567 }
6668
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+
6785 test ('looks at only expected extensions' , () async {
6886 final Map <String , bool > extensions = < String , bool > {
6987 'c' : true ,
@@ -88,6 +106,7 @@ void main() {
88106 for (final String fileExtension in extensions.keys) {
89107 root.childFile ('$filenameBase .$fileExtension ' ).createSync ();
90108 }
109+ mockGitFilesListWithAllFiles (root);
91110
92111 final List <String > output = await runCapturingPrint (
93112 runner, < String > ['license-check' ], errorHandler: (Error e) {
@@ -121,6 +140,7 @@ void main() {
121140 for (final String name in ignoredFiles) {
122141 root.childFile (name).createSync ();
123142 }
143+ mockGitFilesListWithAllFiles (root);
124144
125145 final List <String > output =
126146 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -157,7 +177,9 @@ void main() {
157177 }
158178 });
159179
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.
161183 final Directory packageDir = root
162184 .childDirectory ('FlutterGeneratedPluginSwiftPackage' )
163185 ..createSync ();
@@ -181,6 +203,7 @@ void main() {
181203 writeLicense (checked);
182204 final File notChecked = root.childFile ('not_checked.md' );
183205 notChecked.createSync ();
206+ mockGitFilesListWithAllFiles (root);
184207
185208 final List <String > output =
186209 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -198,6 +221,7 @@ void main() {
198221 final File checked = root.childFile ('checked.cc' );
199222 checked.createSync ();
200223 writeLicense (checked, useCrlf: true );
224+ mockGitFilesListWithAllFiles (root);
201225
202226 final List <String > output =
203227 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -221,6 +245,7 @@ void main() {
221245 final File fileC = root.childFile ('file_c.html' );
222246 fileC.createSync ();
223247 writeLicense (fileC, comment: '' , prefix: '<!-- ' , suffix: ' -->' );
248+ mockGitFilesListWithAllFiles (root);
224249
225250 final List <String > output =
226251 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -245,6 +270,7 @@ void main() {
245270 writeLicense (goodB);
246271 root.childFile ('bad.cc' ).createSync ();
247272 root.childFile ('bad.h' ).createSync ();
273+ mockGitFilesListWithAllFiles (root);
248274
249275 Error ? commandError;
250276 final List <String > output = await runCapturingPrint (
@@ -273,6 +299,7 @@ void main() {
273299 final File bad = root.childFile ('bad.cc' );
274300 bad.createSync ();
275301 writeLicense (bad, copyright: '' );
302+ mockGitFilesListWithAllFiles (root);
276303
277304 Error ? commandError;
278305 final List <String > output = await runCapturingPrint (
@@ -300,6 +327,7 @@ void main() {
300327 final File bad = root.childFile ('bad.cc' );
301328 bad.createSync ();
302329 writeLicense (bad, license: < String > []);
330+ mockGitFilesListWithAllFiles (root);
303331
304332 Error ? commandError;
305333 final List <String > output = await runCapturingPrint (
@@ -325,6 +353,7 @@ void main() {
325353 final File thirdPartyFile = root.childFile ('third_party.cc' );
326354 thirdPartyFile.createSync ();
327355 writeLicense (thirdPartyFile, copyright: 'Copyright 2017 Someone Else' );
356+ mockGitFilesListWithAllFiles (root);
328357
329358 Error ? commandError;
330359 final List <String > output = await runCapturingPrint (
@@ -359,6 +388,7 @@ void main() {
359388 'Licensed under the Apache License, Version 2.0 (the "License");' ,
360389 'you may not use this file except in compliance with the License.'
361390 ]);
391+ mockGitFilesListWithAllFiles (root);
362392
363393 final List <String > output =
364394 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -381,6 +411,7 @@ void main() {
381411 .childFile ('first_party.cc' );
382412 firstPartyFileInThirdParty.createSync (recursive: true );
383413 writeLicense (firstPartyFileInThirdParty);
414+ mockGitFilesListWithAllFiles (root);
384415
385416 final List <String > output =
386417 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -404,6 +435,7 @@ void main() {
404435 'This program is free software: you can redistribute it and/or modify' ,
405436 'it under the terms of the GNU General Public License' ,
406437 ]);
438+ mockGitFilesListWithAllFiles (root);
407439
408440 Error ? commandError;
409441 final List <String > output = await runCapturingPrint (
@@ -439,6 +471,7 @@ void main() {
439471 'you may not use this file except in compliance with the License.'
440472 ],
441473 );
474+ mockGitFilesListWithAllFiles (root);
442475
443476 Error ? commandError;
444477 final List <String > output = await runCapturingPrint (
@@ -464,6 +497,7 @@ void main() {
464497 final File license = root.childFile ('LICENSE' );
465498 license.createSync ();
466499 license.writeAsStringSync (_correctLicenseFileText);
500+ mockGitFilesListWithAllFiles (root);
467501
468502 final List <String > output =
469503 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -482,6 +516,7 @@ void main() {
482516 license.createSync ();
483517 license
484518 .writeAsStringSync (_correctLicenseFileText.replaceAll ('\n ' , '\r\n ' ));
519+ mockGitFilesListWithAllFiles (root);
485520
486521 final List <String > output =
487522 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -500,6 +535,7 @@ void main() {
500535 final File license = root.childFile ('LICENSE' );
501536 license.createSync ();
502537 license.writeAsStringSync (_incorrectLicenseFileText);
538+ mockGitFilesListWithAllFiles (root);
503539
504540 Error ? commandError;
505541 final List <String > output = await runCapturingPrint (
@@ -516,6 +552,7 @@ void main() {
516552 root.childDirectory ('third_party' ).childFile ('LICENSE' );
517553 license.createSync (recursive: true );
518554 license.writeAsStringSync (_incorrectLicenseFileText);
555+ mockGitFilesListWithAllFiles (root);
519556
520557 final List <String > output =
521558 await runCapturingPrint (runner, < String > ['license-check' ]);
@@ -533,6 +570,7 @@ void main() {
533570 final File license = root.childFile ('LICENSE' );
534571 license.createSync ();
535572 license.writeAsStringSync (_incorrectLicenseFileText);
573+ mockGitFilesListWithAllFiles (root);
536574
537575 Error ? commandError;
538576 final List <String > output = await runCapturingPrint (
@@ -563,7 +601,7 @@ void main() {
563601 final File checked = root.childFile ('Package.swift' );
564602 checked.createSync ();
565603 writeLicense (checked, prefix: '// swift-tools-version: 5.9\n ' );
566-
604+ mockGitFilesListWithAllFiles (root);
567605 final List <String > output =
568606 await runCapturingPrint (runner, < String > ['license-check' ]);
569607
0 commit comments