-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathcreate_all_packages_app_command_test.dart
605 lines (521 loc) · 20.2 KB
/
create_all_packages_app_command_test.dart
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// Copyright 2013 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 'dart:io' as io;
import 'package:args/command_runner.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_plugin_tools/src/common/core.dart';
import 'package:flutter_plugin_tools/src/create_all_packages_app_command.dart';
import 'package:platform/platform.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:test/test.dart';
import 'mocks.dart';
import 'util.dart';
void main() {
late CommandRunner<void> runner;
late CreateAllPackagesAppCommand command;
late Platform mockPlatform;
late FileSystem fileSystem;
late Directory testRoot;
late Directory packagesDir;
late RecordingProcessRunner processRunner;
setUp(() {
mockPlatform = MockPlatform(isMacOS: true);
fileSystem = MemoryFileSystem();
testRoot = fileSystem.systemTempDirectory.createTempSync();
packagesDir = testRoot.childDirectory('packages');
processRunner = RecordingProcessRunner();
command = CreateAllPackagesAppCommand(
packagesDir,
processRunner: processRunner,
platform: mockPlatform,
);
runner = CommandRunner<void>(
'create_all_test', 'Test for $CreateAllPackagesAppCommand');
runner.addCommand(command);
});
/// Simulates enough of `flutter create`s output to allow the modifications
/// made by the command to work.
void writeFakeFlutterCreateOutput(
Directory outputDirectory, {
String dartSdkConstraint = '>=3.0.0 <4.0.0',
String? appBuildGradleDependencies,
bool androidOnly = false,
}) {
final RepositoryPackage package = RepositoryPackage(
outputDirectory.childDirectory(allPackagesProjectName));
// Android
final String dependencies = appBuildGradleDependencies ??
r'''
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
''';
package
.platformDirectory(FlutterPlatform.android)
.childDirectory('app')
.childFile('build.gradle')
..createSync(recursive: true)
..writeAsStringSync('''
android {
namespace 'dev.flutter.packages.foo.example'
compileSdk flutter.compileSdkVersion
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
applicationId "dev.flutter.packages.foo.example"
minSdkVersion flutter.minSdkVersion
targetSdkVersion 32
}
}
$dependencies
''');
if (androidOnly) {
return;
}
// Non-platform-specific
package.pubspecFile
..createSync(recursive: true)
..writeAsStringSync('''
name: $allPackagesProjectName
description: Flutter app containing all 1st party plugins.
publish_to: none
version: 1.0.0
environment:
sdk: '$dartSdkConstraint'
dependencies:
flutter:
sdk: flutter
dev_dependencies:
flutter_test:
sdk: flutter
###
''');
// iOS
final Directory iOS = package.platformDirectory(FlutterPlatform.ios);
iOS.childDirectory('Runner.xcodeproj').childFile('project.pbxproj')
..createSync(recursive: true)
..writeAsStringSync('''
97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
};
name = Release;
};
''');
// macOS
final Directory macOS = package.platformDirectory(FlutterPlatform.macos);
macOS.childDirectory('Runner.xcodeproj').childFile('project.pbxproj')
..createSync(recursive: true)
..writeAsStringSync('''
97C147041CF9000F007C117D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.14;
};
name = Release;
};
''');
macOS.childFile('Podfile')
..createSync(recursive: true)
..writeAsStringSync('''
# platform :osx, '10.14'
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
project 'Runner', {
'Debug' => :debug,
'Profile' => :release,
'Release' => :release,
}
''');
}
group('non-macOS host', () {
setUp(() {
mockPlatform = MockPlatform(isLinux: true);
command = CreateAllPackagesAppCommand(
packagesDir,
processRunner: processRunner,
platform: mockPlatform,
);
runner = CommandRunner<void>(
'create_all_test', 'Test for $CreateAllPackagesAppCommand');
runner.addCommand(command);
});
test('calls "flutter create"', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
expect(
processRunner.recordedCalls,
contains(ProcessCall(
getFlutterCommand(mockPlatform),
<String>[
'create',
'--template=app',
'--project-name=$allPackagesProjectName',
testRoot.childDirectory(allPackagesProjectName).path,
],
null)));
});
test('pubspec includes all plugins', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
createFakePlugin('pluginb', packagesDir);
createFakePlugin('pluginc', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> pubspec = command.app.pubspecFile.readAsLinesSync();
expect(
pubspec,
containsAll(<Matcher>[
contains(RegExp('path: .*/packages/plugina')),
contains(RegExp('path: .*/packages/pluginb')),
contains(RegExp('path: .*/packages/pluginc')),
]));
});
test('pubspec has overrides for all plugins', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
createFakePlugin('pluginb', packagesDir);
createFakePlugin('pluginc', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> pubspec = command.app.pubspecFile.readAsLinesSync();
expect(
pubspec,
containsAllInOrder(<Matcher>[
contains('dependency_overrides:'),
contains(RegExp('path: .*/packages/plugina')),
contains(RegExp('path: .*/packages/pluginb')),
contains(RegExp('path: .*/packages/pluginc')),
]));
});
test(
'pubspec special-cases camera_android_camerax to remove it from deps but not overrides',
() async {
writeFakeFlutterCreateOutput(testRoot);
final Directory cameraDir = packagesDir.childDirectory('camera');
createFakePlugin('camera', cameraDir);
createFakePlugin('camera_android', cameraDir);
createFakePlugin('camera_android_camerax', cameraDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final Pubspec pubspec = command.app.parsePubspec();
final Dependency? cameraDependency = pubspec.dependencies['camera'];
final Dependency? cameraAndroidDependency =
pubspec.dependencies['camera_android'];
final Dependency? cameraCameraXDependency =
pubspec.dependencies['camera_android_camerax'];
expect(cameraDependency, isA<PathDependency>());
expect((cameraDependency! as PathDependency).path,
endsWith('/packages/camera/camera'));
expect(cameraAndroidDependency, isA<PathDependency>());
expect((cameraAndroidDependency! as PathDependency).path,
endsWith('/packages/camera/camera_android'));
expect(cameraCameraXDependency, null);
final Dependency? cameraCameraXOverride =
pubspec.dependencyOverrides['camera_android_camerax'];
expect(cameraCameraXOverride, isA<PathDependency>());
expect((cameraCameraXOverride! as PathDependency).path,
endsWith('/packages/camera/camera_android_camerax'));
});
test('legacy files are copied when requested', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
// Make a fake legacy source with all the necessary files, replacing one
// of them.
final Directory legacyDir = testRoot.childDirectory('legacy');
final RepositoryPackage legacySource =
RepositoryPackage(legacyDir.childDirectory(allPackagesProjectName));
writeFakeFlutterCreateOutput(legacyDir, androidOnly: true);
const String legacyAppBuildGradleContents = 'Fake legacy content';
final File legacyGradleFile = legacySource
.platformDirectory(FlutterPlatform.android)
.childFile('build.gradle');
legacyGradleFile.writeAsStringSync(legacyAppBuildGradleContents);
await runCapturingPrint(runner, <String>[
'create-all-packages-app',
'--legacy-source=${legacySource.path}',
]);
final File buildGradle = command.app
.platformDirectory(FlutterPlatform.android)
.childFile('build.gradle');
expect(buildGradle.readAsStringSync(), legacyAppBuildGradleContents);
});
test('legacy directory replaces, rather than overlaying', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
final File extraFile =
RepositoryPackage(testRoot.childDirectory(allPackagesProjectName))
.platformDirectory(FlutterPlatform.android)
.childFile('extra_file');
extraFile.createSync(recursive: true);
// Make a fake legacy source with all the necessary files, but not
// including the extra file.
final Directory legacyDir = testRoot.childDirectory('legacy');
final RepositoryPackage legacySource =
RepositoryPackage(legacyDir.childDirectory(allPackagesProjectName));
writeFakeFlutterCreateOutput(legacyDir, androidOnly: true);
await runCapturingPrint(runner, <String>[
'create-all-packages-app',
'--legacy-source=${legacySource.path}',
]);
expect(extraFile.existsSync(), false);
});
test('legacy files are modified as needed by the tool', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
// Make a fake legacy source with all the necessary files, replacing one
// of them.
final Directory legacyDir = testRoot.childDirectory('legacy');
final RepositoryPackage legacySource =
RepositoryPackage(legacyDir.childDirectory(allPackagesProjectName));
writeFakeFlutterCreateOutput(legacyDir, androidOnly: true);
const String legacyAppBuildGradleContents = '''
# This is the legacy file
android {
compileSdk flutter.compileSdkVersion
defaultConfig {
minSdkVersion flutter.minSdkVersion
}
}
''';
final File legacyGradleFile = legacySource
.platformDirectory(FlutterPlatform.android)
.childDirectory('app')
.childFile('build.gradle');
legacyGradleFile.writeAsStringSync(legacyAppBuildGradleContents);
await runCapturingPrint(runner, <String>[
'create-all-packages-app',
'--legacy-source=${legacySource.path}',
]);
final List<String> buildGradle = command.app
.platformDirectory(FlutterPlatform.android)
.childDirectory('app')
.childFile('build.gradle')
.readAsLinesSync();
expect(
buildGradle,
containsAll(<Matcher>[
contains('This is the legacy file'),
contains('compileSdk 34'),
]));
});
test('pubspec preserves existing Dart SDK version', () async {
const String existingSdkConstraint = '>=1.0.0 <99.0.0';
writeFakeFlutterCreateOutput(testRoot,
dartSdkConstraint: existingSdkConstraint);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final Pubspec generatedPubspec = command.app.parsePubspec();
const String dartSdkKey = 'sdk';
expect(generatedPubspec.environment[dartSdkKey].toString(),
existingSdkConstraint);
});
test('Android app gradle is modified as expected', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> buildGradle = command.app
.platformDirectory(FlutterPlatform.android)
.childDirectory('app')
.childFile('build.gradle')
.readAsLinesSync();
expect(
buildGradle,
containsAll(<Matcher>[
contains('compileSdk 34'),
contains('androidx.lifecycle:lifecycle-runtime'),
]));
});
// The template's app/build.gradle does not always have a dependencies
// section; ensure that the dependency is added if there is not one.
test('Android lifecyle dependency is added with no dependencies', () async {
writeFakeFlutterCreateOutput(testRoot, appBuildGradleDependencies: '');
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> buildGradle = command.app
.platformDirectory(FlutterPlatform.android)
.childDirectory('app')
.childFile('build.gradle')
.readAsLinesSync();
expect(
buildGradle,
containsAllInOrder(<Matcher>[
equals('dependencies {'),
contains('androidx.lifecycle:lifecycle-runtime'),
equals('}'),
]));
});
// Some versions of the template's app/build.gradle has an empty
// dependencies section; ensure that the dependency is added in that case.
test('Android lifecyle dependency is added with empty dependencies',
() async {
writeFakeFlutterCreateOutput(testRoot,
appBuildGradleDependencies: 'dependencies {}');
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> buildGradle = command.app
.platformDirectory(FlutterPlatform.android)
.childDirectory('app')
.childFile('build.gradle')
.readAsLinesSync();
expect(
buildGradle,
containsAllInOrder(<Matcher>[
equals('dependencies {'),
contains('androidx.lifecycle:lifecycle-runtime'),
equals('}'),
]));
});
test('macOS deployment target is modified in pbxproj', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> pbxproj = command.app
.platformDirectory(FlutterPlatform.macos)
.childDirectory('Runner.xcodeproj')
.childFile('project.pbxproj')
.readAsLinesSync();
expect(
pbxproj,
everyElement((String line) =>
!line.contains('MACOSX_DEPLOYMENT_TARGET') ||
line.contains('10.15')));
});
test('iOS deployment target is modified in pbxproj', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> pbxproj = command.app
.platformDirectory(FlutterPlatform.ios)
.childDirectory('Runner.xcodeproj')
.childFile('project.pbxproj')
.readAsLinesSync();
expect(
pbxproj,
everyElement((String line) =>
!line.contains('IPHONEOS_DEPLOYMENT_TARGET') ||
line.contains('14.0')));
});
test('calls flutter pub get', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>['create-all-packages-app']);
expect(
processRunner.recordedCalls,
contains(ProcessCall(
getFlutterCommand(mockPlatform),
const <String>['pub', 'get'],
testRoot.childDirectory(allPackagesProjectName).path)));
});
test('fails if flutter create fails', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
processRunner
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<FakeProcessInfo>[
FakeProcessInfo(MockProcess(exitCode: 1), <String>['create'])
];
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['create-all-packages-app'], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains('Failed to `flutter create`'),
]));
});
test('fails if flutter pub get fails', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
processRunner
.mockProcessesForExecutable[getFlutterCommand(mockPlatform)] =
<FakeProcessInfo>[
FakeProcessInfo(MockProcess(), <String>['create']),
FakeProcessInfo(MockProcess(exitCode: 1), <String>['pub', 'get'])
];
Error? commandError;
final List<String> output = await runCapturingPrint(
runner, <String>['create-all-packages-app'], errorHandler: (Error e) {
commandError = e;
});
expect(commandError, isA<ToolExit>());
expect(
output,
containsAllInOrder(<Matcher>[
contains(
"Failed to generate native build files via 'flutter pub get'"),
]));
},
// See comment about Windows in create_all_packages_app_command.dart
skip: io.Platform.isWindows);
test('handles --output-dir', () async {
final Directory customOutputDir =
fileSystem.systemTempDirectory.createTempSync();
writeFakeFlutterCreateOutput(customOutputDir);
createFakePlugin('plugina', packagesDir);
await runCapturingPrint(runner, <String>[
'create-all-packages-app',
'--output-dir=${customOutputDir.path}'
]);
expect(command.app.path,
customOutputDir.childDirectory(allPackagesProjectName).path);
});
test('logs exclusions', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
createFakePlugin('pluginb', packagesDir);
createFakePlugin('pluginc', packagesDir);
final List<String> output = await runCapturingPrint(runner,
<String>['create-all-packages-app', '--exclude=pluginb,pluginc']);
expect(
output,
containsAllInOrder(<String>[
'Exluding the following plugins from the combined build:',
' pluginb',
' pluginc',
]));
});
});
group('macOS host', () {
setUp(() {
command = CreateAllPackagesAppCommand(
packagesDir,
processRunner: processRunner,
platform: MockPlatform(isMacOS: true),
);
runner = CommandRunner<void>(
'create_all_test', 'Test for $CreateAllPackagesAppCommand');
runner.addCommand(command);
});
test('macOS deployment target is modified in Podfile', () async {
writeFakeFlutterCreateOutput(testRoot);
createFakePlugin('plugina', packagesDir);
final File podfileFile = RepositoryPackage(
command.packagesDir.parent.childDirectory(allPackagesProjectName))
.platformDirectory(FlutterPlatform.macos)
.childFile('Podfile');
podfileFile.createSync(recursive: true);
podfileFile.writeAsStringSync("""
platform :osx, '10.11'
# some other line
""");
await runCapturingPrint(runner, <String>['create-all-packages-app']);
final List<String> podfile = command.app
.platformDirectory(FlutterPlatform.macos)
.childFile('Podfile')
.readAsLinesSync();
expect(
podfile,
everyElement((String line) =>
!line.contains('platform :osx') || line.contains("'10.15'")));
},
// Podfile is only generated (and thus only edited) on macOS.
skip: !io.Platform.isMacOS);
});
}