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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// Copyright 2016 The Chromium 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 'dart:async';
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/base/file_system.dart' hide IOSink;
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/utils.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/packages.dart';
import 'package:flutter_tools/src/usage.dart';
import 'package:process/process.dart';
import '../src/common.dart';
import '../src/context.dart';
import '../src/mocks.dart' show MockProcessManager, MockStdio, PromptingProcess;
class AlwaysTrueBotDetector implements BotDetector {
const AlwaysTrueBotDetector();
@override
bool get isRunningOnBot => true;
}
class AlwaysFalseBotDetector implements BotDetector {
const AlwaysFalseBotDetector();
@override
bool get isRunningOnBot => false;
}
void main() {
Cache.disableLocking();
group('packages get/upgrade', () {
Directory tempDir;
setUp(() {
tempDir = fs.systemTempDirectory.createTempSync('flutter_tools_packages_test.');
});
tearDown(() {
tryToDelete(tempDir);
});
Future<String> createProjectWithPlugin(String plugin, { List<String> arguments }) async {
final String projectPath = await createProject(tempDir, arguments: arguments);
final File pubspec = fs.file(fs.path.join(projectPath, 'pubspec.yaml'));
String content = await pubspec.readAsString();
content = content.replaceFirst(
'\ndependencies:\n',
'\ndependencies:\n $plugin:\n',
);
await pubspec.writeAsString(content, flush: true);
return projectPath;
}
Future<PackagesCommand> runCommandIn(String projectPath, String verb, { List<String> args }) async {
final PackagesCommand command = PackagesCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
final List<String> commandArgs = <String>['packages', verb];
if (args != null)
commandArgs.addAll(args);
commandArgs.add(projectPath);
await runner.run(commandArgs);
return command;
}
void expectExists(String projectPath, String relPath) {
expect(
fs.isFileSync(fs.path.join(projectPath, relPath)),
true,
reason: '$projectPath/$relPath should exist, but does not',
);
}
void expectContains(String projectPath, String relPath, String substring) {
expectExists(projectPath, relPath);
expect(
fs.file(fs.path.join(projectPath, relPath)).readAsStringSync(),
contains(substring),
reason: '$projectPath/$relPath has unexpected content',
);
}
void expectNotExists(String projectPath, String relPath) {
expect(
fs.isFileSync(fs.path.join(projectPath, relPath)),
false,
reason: '$projectPath/$relPath should not exist, but does',
);
}
void expectNotContains(String projectPath, String relPath, String substring) {
expectExists(projectPath, relPath);
expect(
fs.file(fs.path.join(projectPath, relPath)).readAsStringSync(),
isNot(contains(substring)),
reason: '$projectPath/$relPath has unexpected content',
);
}
const List<String> pubOutput = <String>[
'.packages',
'pubspec.lock',
];
const List<String> pluginRegistrants = <String>[
'ios/Runner/GeneratedPluginRegistrant.h',
'ios/Runner/GeneratedPluginRegistrant.m',
'android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
];
const List<String> modulePluginRegistrants = <String>[
'.ios/Flutter/FlutterPluginRegistrant/Classes/GeneratedPluginRegistrant.h',
'.ios/Flutter/FlutterPluginRegistrant/Classes/GeneratedPluginRegistrant.m',
'.android/Flutter/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java',
];
const List<String> pluginWitnesses = <String>[
'.flutter-plugins',
'ios/Podfile',
];
const List<String> modulePluginWitnesses = <String>[
'.flutter-plugins',
'.ios/Podfile',
];
const Map<String, String> pluginContentWitnesses = <String, String>{
'ios/Flutter/Debug.xcconfig': '#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"',
'ios/Flutter/Release.xcconfig': '#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"',
};
const Map<String, String> modulePluginContentWitnesses = <String, String>{
'.ios/Config/Debug.xcconfig': '#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"',
'.ios/Config/Release.xcconfig': '#include "Pods/Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"',
};
void expectDependenciesResolved(String projectPath) {
for (String output in pubOutput) {
expectExists(projectPath, output);
}
}
void expectZeroPluginsInjected(String projectPath) {
for (final String registrant in modulePluginRegistrants) {
expectExists(projectPath, registrant);
}
for (final String witness in pluginWitnesses) {
expectNotExists(projectPath, witness);
}
modulePluginContentWitnesses.forEach((String witness, String content) {
expectNotContains(projectPath, witness, content);
});
}
void expectPluginInjected(String projectPath) {
for (final String registrant in pluginRegistrants) {
expectExists(projectPath, registrant);
}
for (final String witness in pluginWitnesses) {
expectExists(projectPath, witness);
}
pluginContentWitnesses.forEach((String witness, String content) {
expectContains(projectPath, witness, content);
});
}
void expectModulePluginInjected(String projectPath) {
for (final String registrant in modulePluginRegistrants) {
expectExists(projectPath, registrant);
}
for (final String witness in modulePluginWitnesses) {
expectExists(projectPath, witness);
}
modulePluginContentWitnesses.forEach((String witness, String content) {
expectContains(projectPath, witness, content);
});
}
void removeGeneratedFiles(String projectPath) {
final Iterable<String> allFiles = <List<String>>[
pubOutput,
modulePluginRegistrants,
pluginWitnesses,
].expand<String>((List<String> list) => list);
for (String path in allFiles) {
final File file = fs.file(fs.path.join(projectPath, path));
if (file.existsSync())
file.deleteSync();
}
}
testUsingContext('get fetches packages', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
removeGeneratedFiles(projectPath);
await runCommandIn(projectPath, 'get');
expectDependenciesResolved(projectPath);
expectZeroPluginsInjected(projectPath);
}, timeout: allowForRemotePubInvocation);
testUsingContext('get --offline fetches packages', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
removeGeneratedFiles(projectPath);
await runCommandIn(projectPath, 'get', args: <String>['--offline']);
expectDependenciesResolved(projectPath);
expectZeroPluginsInjected(projectPath);
}, timeout: allowForCreateFlutterProject);
testUsingContext('set the number of plugins as usage value', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
removeGeneratedFiles(projectPath);
final PackagesCommand command = await runCommandIn(projectPath, 'get');
final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;
expect(await getCommand.usageValues, containsPair(kCommandPackagesNumberPlugins, '0'));
}, timeout: allowForCreateFlutterProject);
testUsingContext('indicate that the project is not a module in usage value', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub']);
removeGeneratedFiles(projectPath);
final PackagesCommand command = await runCommandIn(projectPath, 'get');
final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;
expect(await getCommand.usageValues, containsPair(kCommandPackagesProjectModule, 'false'));
}, timeout: allowForCreateFlutterProject);
testUsingContext('indicate that the project is a module in usage value', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
removeGeneratedFiles(projectPath);
final PackagesCommand command = await runCommandIn(projectPath, 'get');
final PackagesGetCommand getCommand = command.subcommands['get'] as PackagesGetCommand;
expect(await getCommand.usageValues, containsPair(kCommandPackagesProjectModule, 'true'));
}, timeout: allowForCreateFlutterProject);
testUsingContext('upgrade fetches packages', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);
removeGeneratedFiles(projectPath);
await runCommandIn(projectPath, 'upgrade');
expectDependenciesResolved(projectPath);
expectZeroPluginsInjected(projectPath);
}, timeout: allowForRemotePubInvocation);
testUsingContext('get fetches packages and injects plugin', () async {
final String projectPath = await createProjectWithPlugin('path_provider',
arguments: <String>['--no-pub', '--template=module']);
removeGeneratedFiles(projectPath);
await runCommandIn(projectPath, 'get');
expectDependenciesResolved(projectPath);
expectModulePluginInjected(projectPath);
}, timeout: allowForRemotePubInvocation);
testUsingContext('get fetches packages and injects plugin in plugin project', () async {
final String projectPath = await createProject(
tempDir,
arguments: <String>['--template=plugin', '--no-pub'],
);
final String exampleProjectPath = fs.path.join(projectPath, 'example');
removeGeneratedFiles(projectPath);
removeGeneratedFiles(exampleProjectPath);
await runCommandIn(projectPath, 'get');
expectDependenciesResolved(projectPath);
await runCommandIn(exampleProjectPath, 'get');
expectDependenciesResolved(exampleProjectPath);
expectPluginInjected(exampleProjectPath);
}, timeout: allowForRemotePubInvocation);
});
group('packages test/pub', () {
MockProcessManager mockProcessManager;
MockStdio mockStdio;
setUp(() {
mockProcessManager = MockProcessManager();
mockStdio = MockStdio();
});
testUsingContext('test without bot', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(3));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], 'run');
expect(commands[2], 'test');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysFalseBotDetector(),
});
testUsingContext('test with bot', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'test']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(4));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'run');
expect(commands[3], 'test');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
testUsingContext('run', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', '--verbose', 'pub', 'run', '--foo', 'bar']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(4));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], 'run');
expect(commands[2], '--foo');
expect(commands[3], 'bar');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
});
testUsingContext('pub publish', () async {
final PromptingProcess process = PromptingProcess();
mockProcessManager.processFactory = (List<String> commands) => process;
final Future<void> runPackages = createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'pub', 'publish']);
final Future<void> runPrompt = process.showPrompt('Proceed (y/n)? ', <String>['hello', 'world']);
final Future<void> simulateUserInput = Future<void>(() {
mockStdio.simulateStdin('y');
});
await Future.wait<void>(<Future<void>>[runPackages, runPrompt, simulateUserInput]);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(2));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], 'publish');
final List<String> stdout = mockStdio.writtenToStdout;
expect(stdout, hasLength(4));
expect(stdout.sublist(0, 2), contains('Proceed (y/n)? '));
expect(stdout.sublist(0, 2), contains('y\n'));
expect(stdout[2], 'hello\n');
expect(stdout[3], 'world\n');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
});
testUsingContext('publish', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'publish']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(3));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'publish');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
testUsingContext('deps', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'deps']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(3));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'deps');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
testUsingContext('cache', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'cache']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(3));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'cache');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
testUsingContext('version', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'version']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(3));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'version');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
testUsingContext('uploader', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'uploader']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(3));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'uploader');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
testUsingContext('global', () async {
await createTestCommandRunner(PackagesCommand()).run(<String>['packages', 'global', 'list']);
final List<String> commands = mockProcessManager.commands;
expect(commands, hasLength(4));
expect(commands[0], matches(r'dart-sdk[\\/]bin[\\/]pub'));
expect(commands[1], '--trace');
expect(commands[2], 'global');
expect(commands[3], 'list');
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
Stdio: () => mockStdio,
BotDetector: () => const AlwaysTrueBotDetector(),
});
});
}