44
55import 'package:file/file.dart' ;
66import 'package:file/memory.dart' ;
7+ import 'package:flutter_tools/src/base/io.dart' ;
78import 'package:flutter_tools/src/base/file_system.dart' ;
89import 'package:flutter_tools/src/base/logger.dart' ;
910import 'package:flutter_tools/src/base/os.dart' ;
10- import 'package:flutter_tools/src/globals.dart' as globals;
1111import 'package:mockito/mockito.dart' ;
1212import 'package:process/process.dart' ;
1313import 'package:platform/platform.dart' ;
1414
1515import '../../src/common.dart' ;
1616import '../../src/context.dart' ;
17- import '../../src/fake_process_manager.dart' ;
1817
1918const String kExecutable = 'foo' ;
2019const String kPath1 = '/bar/bin/$kExecutable ' ;
@@ -23,51 +22,40 @@ const String kPath2 = '/another/bin/$kExecutable';
2322class MockLogger extends Mock implements Logger {}
2423
2524void main () {
26- FakeProcessManager fakeProcessManager;
25+ MockProcessManager mockProcessManager;
26+
27+ setUp (() {
28+ mockProcessManager = MockProcessManager ();
29+ });
2730
2831 OperatingSystemUtils createOSUtils (Platform platform) {
2932 return OperatingSystemUtils (
3033 fileSystem: MemoryFileSystem (),
3134 logger: MockLogger (),
3235 platform: platform,
33- processManager: fakeProcessManager ,
36+ processManager: mockProcessManager ,
3437 );
3538 }
3639
3740 group ('which on POSIX' , () {
3841 testWithoutContext ('returns null when executable does not exist' , () async {
39- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
40- const FakeCommand (command: < String > ['which' , kExecutable], exitCode: 1 ),
41- ]);
42- final OperatingSystemUtils utils = createOSUtils (
43- FakePlatform (operatingSystem: 'linux' ),
44- );
42+ when (mockProcessManager.runSync (< String > ['which' , kExecutable]))
43+ .thenReturn (ProcessResult (0 , 1 , null , null ));
44+ final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'linux' ));
4545 expect (utils.which (kExecutable), isNull);
4646 });
4747
4848 testWithoutContext ('returns exactly one result' , () async {
49- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
50- const FakeCommand (
51- command: < String > ['which' , kExecutable],
52- stdout: kPath1,
53- ),
54- ]);
55- final OperatingSystemUtils utils = createOSUtils (
56- FakePlatform (operatingSystem: 'linux' ),
57- );
49+ when (mockProcessManager.runSync (< String > ['which' , 'foo' ]))
50+ .thenReturn (ProcessResult (0 , 0 , kPath1, null ));
51+ final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'linux' ));
5852 expect (utils.which (kExecutable).path, kPath1);
5953 });
6054
6155 testWithoutContext ('returns all results for whichAll' , () async {
62- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
63- const FakeCommand (
64- command: < String > ['which' , '-a' , kExecutable],
65- stdout: '$kPath1 \n $kPath2 \n ' ,
66- ),
67- ]);
68- final OperatingSystemUtils utils = createOSUtils (
69- FakePlatform (operatingSystem: 'linux' ),
70- );
56+ when (mockProcessManager.runSync (< String > ['which' , '-a' , kExecutable]))
57+ .thenReturn (ProcessResult (0 , 0 , '$kPath1 \n $kPath2 ' , null ));
58+ final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'linux' ));
7159 final List <File > result = utils.whichAll (kExecutable);
7260 expect (result, hasLength (2 ));
7361 expect (result[0 ].path, kPath1);
@@ -77,129 +65,29 @@ void main() {
7765
7866 group ('which on Windows' , () {
7967 testWithoutContext ('returns null when executable does not exist' , () async {
80- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
81- const FakeCommand (
82- command: < String > ['where' , kExecutable],
83- exitCode: 1 ,
84- ),
85- ]);
86- final OperatingSystemUtils utils = createOSUtils (
87- FakePlatform (operatingSystem: 'windows' ),
88- );
68+ when (mockProcessManager.runSync (< String > ['where' , kExecutable]))
69+ .thenReturn (ProcessResult (0 , 1 , null , null ));
70+ final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'windows' ));
8971 expect (utils.which (kExecutable), isNull);
9072 });
9173
9274 testWithoutContext ('returns exactly one result' , () async {
93- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
94- const FakeCommand (
95- command: < String > ['where' , kExecutable],
96- stdout: '$kPath1 \n $kPath2 \n ' ,
97- ),
98- ]);
99- final OperatingSystemUtils utils = createOSUtils (
100- FakePlatform (operatingSystem: 'windows' ),
101- );
75+ when (mockProcessManager.runSync (< String > ['where' , 'foo' ]))
76+ .thenReturn (ProcessResult (0 , 0 , '$kPath1 \n $kPath2 ' , null ));
77+ final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'windows' ));
10278 expect (utils.which (kExecutable).path, kPath1);
10379 });
10480
10581 testWithoutContext ('returns all results for whichAll' , () async {
106- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
107- const FakeCommand (
108- command: < String > ['where' , kExecutable],
109- stdout: '$kPath1 \n $kPath2 \n ' ,
110- ),
111- ]);
112- final OperatingSystemUtils utils = createOSUtils (
113- FakePlatform (operatingSystem: 'windows' ),
114- );
82+ when (mockProcessManager.runSync (< String > ['where' , kExecutable]))
83+ .thenReturn (ProcessResult (0 , 0 , '$kPath1 \n $kPath2 ' , null ));
84+ final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'windows' ));
11585 final List <File > result = utils.whichAll (kExecutable);
11686 expect (result, hasLength (2 ));
11787 expect (result[0 ].path, kPath1);
11888 expect (result[1 ].path, kPath2);
11989 });
12090 });
121-
122- group ('name' , () {
123- testWithoutContext ('on Linux' , () {
124- final FakePlatform platform = FakePlatform (
125- operatingSystem: 'linux' ,
126- operatingSystemVersion: 'Linux 5.2.17-amd64 '
127- '#1 SMP Debian 5.2.17 (2019-10-21 > 2018)' ,
128- );
129- final OperatingSystemUtils utils = createOSUtils (platform);
130- expect (utils.name, 'Linux 5.2.17-amd64' );
131- });
132-
133- testWithoutContext ('on Mac' , () {
134- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
135- const FakeCommand (
136- command: < String > ['sw_vers' , '-productName' ],
137- stdout: 'Mac OS X\n ' ,
138- ),
139- const FakeCommand (
140- command: < String > ['sw_vers' , '-productVersion' ],
141- stdout: '10.14.6\n ' ,
142- ),
143- const FakeCommand (
144- command: < String > ['sw_vers' , '-buildVersion' ],
145- stdout: '16G2128\n ' ,
146- ),
147- ]);
148- final FakePlatform platform = FakePlatform (
149- operatingSystem: 'macos' ,
150- );
151- final OperatingSystemUtils utils = createOSUtils (platform);
152- expect (utils.name, 'Mac OS X 10.14.6 16G2128' );
153- });
154-
155- testWithoutContext ('on Windows' , () {
156- fakeProcessManager = FakeProcessManager .list (< FakeCommand > [
157- const FakeCommand (
158- command: < String > ['ver' ],
159- stdout: 'Microsoft Windows [Version 10.0.17763.740]' ,
160- ),
161- ]);
162- final FakePlatform platform = FakePlatform (
163- operatingSystem: 'windows' ,
164- );
165- final OperatingSystemUtils utils = createOSUtils (platform);
166- expect (utils.name, 'Microsoft Windows [Version 10.0.17763.740]' );
167- });
168- });
169-
170- group ('makeExecutable' , () {
171- Directory tempDir;
172-
173- setUp (() {
174- tempDir = globals.fs.systemTempDirectory.createTempSync (
175- 'flutter_tools_os_utils_test.' ,
176- );
177- });
178-
179- tearDown (() {
180- tryToDelete (tempDir);
181- });
182-
183- testUsingContext ('makeExecutable' , () async {
184- final File file = globals.fs.file (globals.fs.path.join (
185- tempDir.path,
186- 'foo.script' ,
187- ));
188- file.writeAsStringSync ('hello world' );
189- globals.os.makeExecutable (file);
190-
191- final String mode = file.statSync ().modeString ();
192- // rwxr--r--
193- expect (mode.substring (0 , 3 ), endsWith ('x' ));
194- }, overrides: < Type , Generator > {
195- OperatingSystemUtils : () => OperatingSystemUtils (
196- fileSystem: globals.fs,
197- logger: globals.logger,
198- platform: globals.platform,
199- processManager: globals.processManager,
200- ),
201- }, skip: const LocalPlatform ().isWindows);
202- });
20391}
20492
20593class MockProcessManager extends Mock implements ProcessManager {}
0 commit comments