44
55import 'package:file/file.dart' ;
66import 'package:file/memory.dart' ;
7- import 'package:flutter_tools/src/base/io.dart' ;
87import 'package:flutter_tools/src/base/file_system.dart' ;
98import 'package:flutter_tools/src/base/logger.dart' ;
109import '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' ;
1718
1819const String kExecutable = 'foo' ;
1920const String kPath1 = '/bar/bin/$kExecutable ' ;
@@ -22,40 +23,51 @@ const String kPath2 = '/another/bin/$kExecutable';
2223class MockLogger extends Mock implements Logger {}
2324
2425void main () {
25- MockProcessManager mockProcessManager;
26-
27- setUp (() {
28- mockProcessManager = MockProcessManager ();
29- });
26+ FakeProcessManager fakeProcessManager;
3027
3128 OperatingSystemUtils createOSUtils (Platform platform) {
3229 return OperatingSystemUtils (
3330 fileSystem: MemoryFileSystem (),
3431 logger: MockLogger (),
3532 platform: platform,
36- processManager: mockProcessManager ,
33+ processManager: fakeProcessManager ,
3734 );
3835 }
3936
4037 group ('which on POSIX' , () {
4138 testWithoutContext ('returns null when executable does not exist' , () async {
42- when (mockProcessManager.runSync (< String > ['which' , kExecutable]))
43- .thenReturn (ProcessResult (0 , 1 , null , null ));
44- final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'linux' ));
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+ );
4545 expect (utils.which (kExecutable), isNull);
4646 });
4747
4848 testWithoutContext ('returns exactly one result' , () async {
49- when (mockProcessManager.runSync (< String > ['which' , 'foo' ]))
50- .thenReturn (ProcessResult (0 , 0 , kPath1, null ));
51- final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'linux' ));
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+ );
5258 expect (utils.which (kExecutable).path, kPath1);
5359 });
5460
5561 testWithoutContext ('returns all results for whichAll' , () async {
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' ));
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+ );
5971 final List <File > result = utils.whichAll (kExecutable);
6072 expect (result, hasLength (2 ));
6173 expect (result[0 ].path, kPath1);
@@ -65,29 +77,129 @@ void main() {
6577
6678 group ('which on Windows' , () {
6779 testWithoutContext ('returns null when executable does not exist' , () async {
68- when (mockProcessManager.runSync (< String > ['where' , kExecutable]))
69- .thenReturn (ProcessResult (0 , 1 , null , null ));
70- final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'windows' ));
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+ );
7189 expect (utils.which (kExecutable), isNull);
7290 });
7391
7492 testWithoutContext ('returns exactly one result' , () async {
75- when (mockProcessManager.runSync (< String > ['where' , 'foo' ]))
76- .thenReturn (ProcessResult (0 , 0 , '$kPath1 \n $kPath2 ' , null ));
77- final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'windows' ));
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+ );
78102 expect (utils.which (kExecutable).path, kPath1);
79103 });
80104
81105 testWithoutContext ('returns all results for whichAll' , () async {
82- when (mockProcessManager.runSync (< String > ['where' , kExecutable]))
83- .thenReturn (ProcessResult (0 , 0 , '$kPath1 \n $kPath2 ' , null ));
84- final OperatingSystemUtils utils = createOSUtils (FakePlatform (operatingSystem: 'windows' ));
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+ );
85115 final List <File > result = utils.whichAll (kExecutable);
86116 expect (result, hasLength (2 ));
87117 expect (result[0 ].path, kPath1);
88118 expect (result[1 ].path, kPath2);
89119 });
90120 });
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+ });
91203}
92204
93205class MockProcessManager extends Mock implements ProcessManager {}
0 commit comments