Skip to content

Commit

Permalink
implement "--device" option for command "maestro drive"
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Jun 6, 2022
1 parent 183df80 commit d03ea2a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
19 changes: 15 additions & 4 deletions packages/maestro_cli/lib/src/commands/drive_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class DriveCommand extends Command<int> {
'driver',
abbr: 'd',
help: 'Dart file which starts flutter_driver.',
)
..addOption(
'device',
help: 'Serial number of ADB device to use.',
);
}

Expand Down Expand Up @@ -65,17 +69,24 @@ class DriveCommand extends Command<int> {
throw const FormatException('`driver` argument is not a string');
}

final device = argResults?['device'] as String?;
log.info('device: $device');

final options = MaestroDriveOptions(
host: host,
port: int.parse(portStr),
target: target,
driver: driver,
);

await adb.installApps();
await adb.forwardPorts(options.port);
await adb.runServer();
await flutter_driver.runTestsWithOutput(options.driver, options.target);
await adb.installApps(device: device);
await adb.forwardPorts(options.port, device: device);
await adb.runServer(device: device);
await flutter_driver.runTestsWithOutput(
options.driver,
options.target,
device: device,
);

return 0;
}
Expand Down
24 changes: 18 additions & 6 deletions packages/maestro_cli/lib/src/external/adb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import 'dart:io';
import 'package:maestro_cli/src/common/common.dart';
import 'package:path/path.dart' as path;

Future<void> installApps() async {
Future<void> installApps({String? device}) async {
final progress1 = log.progress('Installing server');
try {
await _installApk(serverArtifactFile);
await _installApk(serverArtifactFile, device: device);
} catch (err) {
progress1.fail('Failed to install server');
rethrow;
Expand All @@ -16,7 +16,7 @@ Future<void> installApps() async {

final progress2 = log.progress('Installing instrumentation');
try {
await _installApk(instrumentationArtifactFile);
await _installApk(instrumentationArtifactFile, device: device);
} catch (err) {
progress2.fail('Failed to install instrumentation');
rethrow;
Expand All @@ -25,12 +25,16 @@ Future<void> installApps() async {
progress2.complete('Installed instrumentation');
}

Future<void> forwardPorts(int port) async {
Future<void> forwardPorts(int port, {String? device}) async {
final progress = log.progress('Forwarding ports');

final result = await Process.run(
'adb',
[
if (device != null) ...[
'-s',
device,
],
'forward',
'tcp:$port',
'tcp:$port',
Expand All @@ -46,14 +50,18 @@ Future<void> forwardPorts(int port) async {
progress.complete('Forwarded ports');
}

Future<void> runServer() async {
Future<void> runServer({String? device}) async {
final progress = log.progress('Starting instrumentation server');

Process process;
try {
process = await Process.start(
'adb',
[
if (device != null) ...[
'-s',
device,
],
'shell',
'am',
'instrument',
Expand Down Expand Up @@ -82,10 +90,14 @@ Future<void> runServer() async {
progress.complete('Started instrumentation server');
}

Future<void> _installApk(String name) async {
Future<void> _installApk(String name, {String? device}) async {
final result = await Process.run(
'adb',
[
if (device != null) ...[
'-s',
device,
],
'install',
path.join(artifactPath, name),
],
Expand Down
35 changes: 30 additions & 5 deletions packages/maestro_cli/lib/src/external/flutter_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:maestro_cli/src/common/common.dart';

/// Runs flutter driver with the given [driver] and [target] and waits until the
/// drive is done.
Future<void> runTests(String driver, String target) async {
Future<void> runTests(String driver, String target, {String? device}) async {
log.info('Running tests...');

final res = await Process.run(
Expand All @@ -15,6 +15,10 @@ Future<void> runTests(String driver, String target) async {
driver,
'--target',
target,
if (device != null) ...[
'--device-id',
device,
],
],
runInShell: true,
);
Expand All @@ -28,7 +32,11 @@ Future<void> runTests(String driver, String target) async {
/// drive is done.
///
/// Prints standard output of "flutter drive".
Future<void> runTestsWithOutput(String driver, String target) async {
Future<void> runTestsWithOutput(
String driver,
String target, {
String? device,
}) async {
log.info('Running tests with output...');

final res = await Process.start(
Expand All @@ -39,11 +47,15 @@ Future<void> runTestsWithOutput(String driver, String target) async {
driver,
'--target',
target,
if (device != null) ...[
'--device-id',
device,
],
],
runInShell: true,
);

final sub = res.stdout.listen((msg) {
final stdOutSub = res.stdout.listen((msg) {
final text = 'driver: ${systemEncoding.decode(msg)}';
if (text.contains('I/flutter')) {
log.info(text);
Expand All @@ -52,6 +64,19 @@ Future<void> runTestsWithOutput(String driver, String target) async {
}
});

await res.exitCode;
await sub.cancel();
final stdErrSub = res.stderr.listen((msg) {
final text = 'driver: ${systemEncoding.decode(msg)}';
log.severe(text);
});

final exitCode = await res.exitCode;
await stdOutSub.cancel();
await stdErrSub.cancel();

final msg = 'flutter_driver exited with code $exitCode';
if (exitCode == 0) {
log.info(msg);
} else {
log.severe(msg);
}
}

0 comments on commit d03ea2a

Please sign in to comment.