Skip to content

Commit dd93ee3

Browse files
author
Jonah Williams
authored
[flutter_tools] drive uses correct application package for prebuilt (flutter#68309)
Both start and stop app create an application package, but only start app used the application binary. Create the application package once and pass it to both start and stop app.
1 parent 8d73042 commit dd93ee3

File tree

2 files changed

+39
-42
lines changed

2 files changed

+39
-42
lines changed

packages/flutter_tools/lib/src/commands/drive.dart

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ class DriveCommand extends RunCommandBase {
181181
ResidentRunner residentRunner;
182182
final BuildInfo buildInfo = getBuildInfo();
183183
final bool isWebPlatform = await device.targetPlatform == TargetPlatform.web_javascript;
184+
final File applicationBinary = stringArg('use-application-binary') == null
185+
? null
186+
: globals.fs.file(stringArg('use-application-binary'));
187+
final ApplicationPackage package = await applicationPackages.getPackageForPlatform(
188+
await device.targetPlatform,
189+
buildInfo: buildInfo,
190+
applicationBinary: applicationBinary,
191+
);
184192
if (argResults['use-existing-app'] == null) {
185193
globals.printStatus('Starting application: $targetFile');
186194

@@ -238,7 +246,7 @@ class DriveCommand extends RunCommandBase {
238246
webUri = residentRunner.uri;
239247
}
240248

241-
final LaunchResult result = await appStarter(this, webUri);
249+
final LaunchResult result = await appStarter(this, webUri, package, applicationBinary != null);
242250
if (result == null) {
243251
throwToolExit('Application failed to start. Will not run test. Quitting.', exitCode: 1);
244252
}
@@ -364,7 +372,7 @@ $ex
364372
globals.printStatus('Leaving the application running.');
365373
} else {
366374
globals.printStatus('Stopping application instance.');
367-
await appStopper(this);
375+
await appStopper(this, package);
368376
}
369377
}
370378

@@ -417,7 +425,7 @@ $ex
417425

418426
Future<Device> findTargetDevice({ @required Duration timeout }) async {
419427
final DeviceManager deviceManager = globals.deviceManager;
420-
final List<Device> devices = await deviceManager.findTargetDevices(FlutterProject.current(), timeout: timeout);
428+
final List<Device> devices = await deviceManager.findTargetDevices(null, timeout: timeout);
421429

422430
if (deviceManager.hasSpecifiedDeviceId) {
423431
if (devices.isEmpty) {
@@ -444,7 +452,7 @@ Future<Device> findTargetDevice({ @required Duration timeout }) async {
444452
}
445453

446454
/// Starts the application on the device given command configuration.
447-
typedef AppStarter = Future<LaunchResult> Function(DriveCommand command, Uri webUri);
455+
typedef AppStarter = Future<LaunchResult> Function(DriveCommand command, Uri webUri, ApplicationPackage applicationPackage, bool prebuiltApplication);
448456

449457
AppStarter appStarter = _startApp; // (mutable for testing)
450458
void restoreAppStarter() {
@@ -453,26 +461,19 @@ void restoreAppStarter() {
453461

454462
Future<LaunchResult> _startApp(
455463
DriveCommand command,
456-
Uri webUri, {
457-
String userIdentifier,
458-
}) async {
464+
Uri webUri,
465+
ApplicationPackage applicationPackage,
466+
bool prebuiltApplication,
467+
) async {
459468
final String mainPath = findMainDartFile(command.targetFile);
460469
if (await globals.fs.type(mainPath) != FileSystemEntityType.file) {
461470
globals.printError('Tried to run $mainPath, but that file does not exist.');
462471
return null;
463472
}
464473

465474
globals.printTrace('Stopping previously running application, if any.');
466-
await appStopper(command);
467-
468-
final File applicationBinary = command.stringArg('use-application-binary') == null
469-
? null
470-
: globals.fs.file(command.stringArg('use-application-binary'));
471-
final ApplicationPackage package = await command.applicationPackages.getPackageForPlatform(
472-
await command.device.targetPlatform,
473-
buildInfo: command.getBuildInfo(),
474-
applicationBinary: applicationBinary,
475-
);
475+
await appStopper(command, applicationPackage);
476+
476477

477478
final Map<String, dynamic> platformArgs = <String, dynamic>{};
478479
if (command.traceStartup) {
@@ -491,13 +492,13 @@ Future<LaunchResult> _startApp(
491492
globals.printTrace('Starting application.');
492493

493494
// Forward device log messages to the terminal window running the "drive" command.
494-
final DeviceLogReader logReader = await command.device.getLogReader(app: package);
495+
final DeviceLogReader logReader = await command.device.getLogReader(app: applicationPackage);
495496
command._deviceLogSubscription = logReader
496497
.logLines
497498
.listen(globals.printStatus);
498499

499500
final LaunchResult result = await command.device.startApp(
500-
package,
501+
applicationPackage,
501502
mainPath: mainPath,
502503
route: command.route,
503504
debuggingOptions: DebuggingOptions.enabled(
@@ -512,8 +513,8 @@ Future<LaunchResult> _startApp(
512513
purgePersistentCache: command.purgePersistentCache,
513514
),
514515
platformArgs: platformArgs,
515-
userIdentifier: userIdentifier,
516-
prebuiltApplication: applicationBinary != null,
516+
userIdentifier: command.userIdentifier,
517+
prebuiltApplication: prebuiltApplication,
517518
);
518519

519520
if (!result.started) {
@@ -551,18 +552,14 @@ Future<void> _runTests(List<String> testArgs, Map<String, String> environment) a
551552

552553

553554
/// Stops the application.
554-
typedef AppStopper = Future<bool> Function(DriveCommand command);
555+
typedef AppStopper = Future<bool> Function(DriveCommand command, ApplicationPackage applicationPackage);
555556
AppStopper appStopper = _stopApp;
556557
void restoreAppStopper() {
557558
appStopper = _stopApp;
558559
}
559560

560-
Future<bool> _stopApp(DriveCommand command) async {
561+
Future<bool> _stopApp(DriveCommand command, ApplicationPackage package) async {
561562
globals.printTrace('Stopping application.');
562-
final ApplicationPackage package = await command.applicationPackages.getPackageForPlatform(
563-
await command.device.targetPlatform,
564-
buildInfo: command.getBuildInfo(),
565-
);
566563
final bool stopped = await command.device.stopApp(package, userIdentifier: command.userIdentifier);
567564
await command.device.uninstallApp(package);
568565
await command._deviceLogSubscription?.cancel();

packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ void main() {
4545
fs.file('pubspec.yaml').createSync();
4646
fs.file('.packages').createSync();
4747
setExitFunctionForTests();
48-
appStarter = (DriveCommand command, Uri webUri) {
48+
appStarter = (DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) {
4949
throw 'Unexpected call to appStarter';
5050
};
5151
testRunner = (List<String> testArgs, Map<String, String> environment) {
5252
throw 'Unexpected call to testRunner';
5353
};
54-
appStopper = (DriveCommand command) {
54+
appStopper = (DriveCommand command, ApplicationPackage package) {
5555
throw 'Unexpected call to appStopper';
5656
};
5757
command.applicationPackages = FakeApplicationPackageFactory();
@@ -99,7 +99,7 @@ void main() {
9999

100100
testUsingContext('returns 1 when app fails to run', () async {
101101
testDeviceManager.addDevice(MockDevice());
102-
appStarter = expectAsync2((DriveCommand command, Uri webUri) async => null);
102+
appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async => null);
103103

104104
final String testApp = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e.dart');
105105
final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
@@ -201,7 +201,7 @@ void main() {
201201
final String testApp = globals.fs.path.join(tempDir.path, 'test', 'e2e.dart');
202202
final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
203203

204-
appStarter = expectAsync2((DriveCommand command, Uri webUri) async {
204+
appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async {
205205
return LaunchResult.succeeded();
206206
});
207207
testRunner = expectAsync2((List<String> testArgs, Map<String, String> environment) async {
@@ -211,7 +211,7 @@ void main() {
211211
'VM_SERVICE_URL': 'null',
212212
});
213213
});
214-
appStopper = expectAsync1((DriveCommand command) async {
214+
appStopper = expectAsync2((DriveCommand command, ApplicationPackage package) async {
215215
return true;
216216
});
217217

@@ -242,13 +242,13 @@ void main() {
242242
final String testApp = globals.fs.path.join(tempDir.path, 'test', 'e2e.dart');
243243
final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
244244

245-
appStarter = expectAsync2((DriveCommand command, Uri webUri) async {
245+
appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async {
246246
return LaunchResult.succeeded();
247247
});
248248
testRunner = (List<String> testArgs, Map<String, String> environment) async {
249249
throwToolExit(null, exitCode: 123);
250250
};
251-
appStopper = expectAsync1((DriveCommand command) async {
251+
appStopper = expectAsync2((DriveCommand command, ApplicationPackage package) async {
252252
return true;
253253
});
254254

@@ -281,7 +281,7 @@ void main() {
281281
final String testApp = globals.fs.path.join(tempDir.path, 'test', 'e2e.dart');
282282
final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
283283

284-
appStarter = expectAsync2((DriveCommand command, Uri webUri) async {
284+
appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async {
285285
return LaunchResult.succeeded();
286286
});
287287
testRunner = expectAsync2((List<String> testArgs, Map<String, String> environment) async {
@@ -294,7 +294,7 @@ void main() {
294294
]
295295
);
296296
});
297-
appStopper = expectAsync1((DriveCommand command) async {
297+
appStopper = expectAsync2((DriveCommand command, ApplicationPackage package) async {
298298
return true;
299299
});
300300

@@ -324,7 +324,7 @@ void main() {
324324
final String testApp = globals.fs.path.join(tempDir.path, 'test', 'e2e.dart');
325325
final String testFile = globals.fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
326326

327-
appStarter = expectAsync2((DriveCommand command, Uri webUri) async {
327+
appStarter = expectAsync4((DriveCommand command, Uri webUri, ApplicationPackage package, bool prebuiltApplication) async {
328328
return LaunchResult.succeeded();
329329
});
330330
testRunner = expectAsync2((List<String> testArgs, Map<String, String> environment) async {
@@ -336,7 +336,7 @@ void main() {
336336
]
337337
);
338338
});
339-
appStopper = expectAsync1((DriveCommand command) async {
339+
appStopper = expectAsync2((DriveCommand command, ApplicationPackage package) async {
340340
return true;
341341
});
342342

@@ -493,8 +493,8 @@ void main() {
493493
testRunner = (List<String> testArgs, Map<String, String> environment) async {
494494
throwToolExit(null, exitCode: 123);
495495
};
496-
appStopper = expectAsync1(
497-
(DriveCommand command) async {
496+
appStopper = expectAsync2(
497+
(DriveCommand command, ApplicationPackage package) async {
498498
return true;
499499
},
500500
count: 2,
@@ -603,8 +603,8 @@ void main() {
603603
testRunner = (List<String> testArgs, Map<String, String> environment) async {
604604
throwToolExit(null, exitCode: 123);
605605
};
606-
appStopper = expectAsync1(
607-
(DriveCommand command) async {
606+
appStopper = expectAsync2(
607+
(DriveCommand command, ApplicationPackage package) async {
608608
return true;
609609
},
610610
count: 2,

0 commit comments

Comments
 (0)