1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/os.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
import '../../src/common.dart';
import '../../src/context.dart';
const String kExecutable = 'foo';
const String kPath1 = '/bar/bin/$kExecutable';
const String kPath2 = '/another/bin/$kExecutable';
void main() {
MockProcessManager mockProcessManager;
setUp(() {
mockProcessManager = MockProcessManager();
});
OperatingSystemUtils createOSUtils(Platform platform) {
return OperatingSystemUtils(
fileSystem: MemoryFileSystem.test(),
logger: BufferLogger.test(),
platform: platform,
processManager: mockProcessManager,
);
}
group('which on POSIX', () {
testWithoutContext('returns null when executable does not exist', () async {
when(mockProcessManager.runSync(<String>['which', kExecutable]))
.thenReturn(ProcessResult(0, 1, null, null));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
expect(utils.which(kExecutable), isNull);
});
testWithoutContext('returns exactly one result', () async {
when(mockProcessManager.runSync(<String>['which', 'foo']))
.thenReturn(ProcessResult(0, 0, kPath1, null));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
expect(utils.which(kExecutable).path, kPath1);
});
testWithoutContext('returns all results for whichAll', () async {
when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'linux'));
final List<File> result = utils.whichAll(kExecutable);
expect(result, hasLength(2));
expect(result[0].path, kPath1);
expect(result[1].path, kPath2);
});
});
group('which on Windows', () {
testWithoutContext('throws tool exit if where throws an argument error', () async {
when(mockProcessManager.runSync(<String>['where', kExecutable]))
.thenThrow(ArgumentError('Cannot find executable for where'));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
expect(() => utils.which(kExecutable), throwsA(isA<ToolExit>()));
});
testWithoutContext('returns null when executable does not exist', () async {
when(mockProcessManager.runSync(<String>['where', kExecutable]))
.thenReturn(ProcessResult(0, 1, null, null));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
expect(utils.which(kExecutable), isNull);
});
testWithoutContext('returns exactly one result', () async {
when(mockProcessManager.runSync(<String>['where', 'foo']))
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
expect(utils.which(kExecutable).path, kPath1);
});
testWithoutContext('returns all results for whichAll', () async {
when(mockProcessManager.runSync(<String>['where', kExecutable]))
.thenReturn(ProcessResult(0, 0, '$kPath1\n$kPath2', null));
final OperatingSystemUtils utils = createOSUtils(FakePlatform(operatingSystem: 'windows'));
final List<File> result = utils.whichAll(kExecutable);
expect(result, hasLength(2));
expect(result[0].path, kPath1);
expect(result[1].path, kPath2);
});
});
testWithoutContext('macos name', () async {
when(mockProcessManager.runSync(
<String>['sw_vers', '-productName'],
)).thenReturn(ProcessResult(0, 0, 'product', ''));
when(mockProcessManager.runSync(
<String>['sw_vers', '-productVersion'],
)).thenReturn(ProcessResult(0, 0, 'version', ''));
when(mockProcessManager.runSync(
<String>['sw_vers', '-buildVersion'],
)).thenReturn(ProcessResult(0, 0, 'build', ''));
when(mockProcessManager.runSync(
<String>['uname', '-m'],
)).thenReturn(ProcessResult(0, 0, 'arch', ''));
final MockFileSystem fileSystem = MockFileSystem();
final OperatingSystemUtils utils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'macos'),
processManager: mockProcessManager,
);
expect(utils.name, 'product version build arch');
});
testWithoutContext('If unzip fails, include stderr in exception text', () {
const String exceptionMessage = 'Something really bad happened.';
when(mockProcessManager.runSync(
<String>['unzip', '-o', '-q', null, '-d', null],
)).thenReturn(ProcessResult(0, 1, '', exceptionMessage));
final MockFileSystem fileSystem = MockFileSystem();
final MockFile mockFile = MockFile();
final MockDirectory mockDirectory = MockDirectory();
when(fileSystem.file(any)).thenReturn(mockFile);
when(mockFile.readAsBytesSync()).thenThrow(
const FileSystemException(exceptionMessage),
);
final OperatingSystemUtils osUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
processManager: mockProcessManager,
);
expect(
() => osUtils.unzip(mockFile, mockDirectory),
throwsProcessException(message: exceptionMessage),
);
});
testWithoutContext('If unzip throws an ArgumentError, display an install message', () {
final FileSystem fileSystem = MemoryFileSystem.test();
when(mockProcessManager.runSync(
<String>['unzip', '-o', '-q', 'foo.zip', '-d', fileSystem.currentDirectory.path],
)).thenThrow(ArgumentError());
final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
processManager: mockProcessManager,
);
expect(
() => linuxOsUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
throwsToolExit(
message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
'Consider running "sudo apt-get install unzip".'),
);
final OperatingSystemUtils macOSUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'macos'),
processManager: mockProcessManager,
);
expect(
() => macOSUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
throwsToolExit
(message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
'Consider running "brew install unzip".'),
);
final OperatingSystemUtils unknownOsUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'fuchsia'),
processManager: mockProcessManager,
);
expect(
() => unknownOsUtils.unzip(fileSystem.file('foo.zip'), fileSystem.currentDirectory),
throwsToolExit
(message: 'Missing "unzip" tool. Unable to extract foo.zip.\n'
'Please install unzip.'),
);
});
testWithoutContext('If zip throws an ArgumentError, display an install message', () {
final FileSystem fileSystem = MemoryFileSystem.test();
when(mockProcessManager.runSync(
<String>['zip', '-r', '-q', 'foo.zip', '.'],
workingDirectory: anyNamed('workingDirectory'),
)).thenThrow(ArgumentError());
final OperatingSystemUtils linuxOsUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
processManager: mockProcessManager,
);
expect(
() => linuxOsUtils.zip(fileSystem.currentDirectory, fileSystem.file('foo.zip')),
throwsToolExit(
message: 'Missing "zip" tool. Unable to compress /.\n'
'Consider running "sudo apt-get install zip".'),
);
final OperatingSystemUtils macOSUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'macos'),
processManager: mockProcessManager,
);
expect(
() => macOSUtils.zip(fileSystem.currentDirectory, fileSystem.file('foo.zip')),
throwsToolExit
(message: 'Missing "zip" tool. Unable to compress /.\n'
'Consider running "brew install zip".'),
);
final OperatingSystemUtils unknownOsUtils = OperatingSystemUtils(
fileSystem: fileSystem,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'fuchsia'),
processManager: mockProcessManager,
);
expect(
() => unknownOsUtils.zip(fileSystem.currentDirectory, fileSystem.file('foo.zip')),
throwsToolExit
(message: 'Missing "zip" tool. Unable to compress /.\n'
'Please install zip.'),
);
});
testWithoutContext('stream compression level', () {
expect(OperatingSystemUtils.gzipLevel1.level, equals(1));
});
}
class MockProcessManager extends Mock implements ProcessManager {}
class MockDirectory extends Mock implements Directory {}
class MockFileSystem extends Mock implements FileSystem {}
class MockFile extends Mock implements File {}