Skip to content

Commit

Permalink
fix Adb crashing (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia authored Jul 7, 2022
1 parent 5b2e432 commit 35d0398
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 60 deletions.
111 changes: 61 additions & 50 deletions packages/maestro_cli/lib/src/features/drive/adb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,73 @@ import 'package:maestro_cli/src/common/common.dart';
import 'package:maestro_cli/src/features/drive/constants.dart';
import 'package:path/path.dart' as path;

Future<void> installApps({String? device, bool debug = false}) async {
final serverInstallProgress = log.progress('Installing server');
try {
final p = path.join(
artifactPath,
debug ? debugServerArtifactFile : serverArtifactFile,
);
await Adb().forceInstallApk(p, device: device);
} catch (err) {
serverInstallProgress.fail('Failed to install server');
rethrow;
}
serverInstallProgress.complete('Installed server');
class MaestroAdb {
MaestroAdb() : _adb = Adb();

final instrumentInstallProgress = log.progress('Installing instrumentation');
try {
final p = path.join(
artifactPath,
debug ? debugInstrumentationArtifactFile : instrumentationArtifactFile,
);
await Adb().forceInstallApk(p, device: device);
} catch (err) {
instrumentInstallProgress.fail('Failed to install instrumentation');
rethrow;
final Adb _adb;

Future<void> init() => _adb.init();

Future<List<String>> devices() => _adb.devices();

Future<void> installApps({String? device, bool debug = false}) async {
final serverInstallProgress = log.progress('Installing server');
try {
final p = path.join(
artifactPath,
debug ? debugServerArtifactFile : serverArtifactFile,
);
await _adb.forceInstallApk(p, device: device);
} catch (err) {
serverInstallProgress.fail('Failed to install server');
rethrow;
}
serverInstallProgress.complete('Installed server');

final instrumentInstallProgress =
log.progress('Installing instrumentation');
try {
final p = path.join(
artifactPath,
debug ? debugInstrumentationArtifactFile : instrumentationArtifactFile,
);
await _adb.forceInstallApk(p, device: device);
} catch (err) {
instrumentInstallProgress.fail('Failed to install instrumentation');
rethrow;
}

instrumentInstallProgress.complete('Installed instrumentation');
}

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

Future<void> forwardPorts(int port, {String? device}) async {
final progress = log.progress('Forwarding ports');
try {
await _adb.forwardPorts(
fromHost: port,
toDevice: port,
device: device,
);
} catch (err) {
progress.fail('Failed to forward ports');
rethrow;
}

progress.complete('Forwarded ports');
}

try {
await Adb().forwardPorts(
fromHost: port,
toDevice: port,
void runServer({
required String? device,
required int port,
}) {
_adb.instrument(
packageName: 'pl.leancode.automatorserver.test',
intentClass: 'androidx.test.runner.AndroidJUnitRunner',
device: device,
onStdout: log.info,
onStderr: log.severe,
arguments: {envPortKey: port.toString()},
);
} catch (err) {
progress.fail('Failed to forward ports');
rethrow;
}

progress.complete('Forwarded ports');
}

void runServer({
required String? device,
required int port,
}) {
Adb().instrument(
packageName: 'pl.leancode.automatorserver.test',
intentClass: 'androidx.test.runner.AndroidJUnitRunner',
device: device,
onStdout: log.info,
onStderr: log.severe,
arguments: {envPortKey: port.toString()},
);
}
24 changes: 14 additions & 10 deletions packages/maestro_cli/lib/src/features/drive/drive_command.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:io';

import 'package:adb/adb.dart';
import 'package:args/command_runner.dart';
import 'package:maestro_cli/src/command_runner.dart';
import 'package:maestro_cli/src/common/common.dart';
import 'package:maestro_cli/src/features/drive/adb.dart' as drive_adb;
import 'package:maestro_cli/src/features/drive/adb.dart';
import 'package:maestro_cli/src/features/drive/flutter_driver.dart'
as flutter_driver;
import 'package:maestro_cli/src/maestro_config.dart';
Expand Down Expand Up @@ -59,8 +58,13 @@ class DriveCommand extends Command<int> {
@override
String get description => 'Drive the app using flutter_driver.';

late MaestroAdb _adb;

@override
Future<int> run() async {
_adb = MaestroAdb();
await _adb.init();

final toml = File(configFileName).readAsStringSync();
final config = MaestroConfig.fromToml(toml);

Expand Down Expand Up @@ -163,9 +167,9 @@ class DriveCommand extends Command<int> {
}) async {
await Future.wait(
devices.map((device) async {
await drive_adb.installApps(device: device, debug: debugFlag);
await drive_adb.forwardPorts(port, device: device);
drive_adb.runServer(device: device, port: port);
await _adb.installApps(device: device, debug: debugFlag);
await _adb.forwardPorts(port, device: device);
_adb.runServer(device: device, port: port);
await flutter_driver.runTestsWithOutput(
driver: driver,
target: target,
Expand All @@ -191,9 +195,9 @@ class DriveCommand extends Command<int> {
Map<String, String> dartDefines = const {},
}) async {
for (final device in devices) {
await drive_adb.installApps(device: device, debug: debugFlag);
await drive_adb.forwardPorts(port, device: device);
drive_adb.runServer(device: device, port: port);
await _adb.installApps(device: device, debug: debugFlag);
await _adb.forwardPorts(port, device: device);
_adb.runServer(device: device, port: port);
await flutter_driver.runTestsWithOutput(
driver: driver,
target: target,
Expand All @@ -209,7 +213,7 @@ class DriveCommand extends Command<int> {

Future<List<String>> _parseDevices(List<String>? devicesArg) async {
if (devicesArg == null || devicesArg.isEmpty) {
final adbDevices = await Adb().devices();
final adbDevices = await _adb.devices();

if (adbDevices.isEmpty) {
throw Exception('No devices attached');
Expand All @@ -228,7 +232,7 @@ class DriveCommand extends Command<int> {
}

if (devicesArg.contains('all')) {
return Adb().devices();
return _adb.devices();
}

return devicesArg;
Expand Down

0 comments on commit 35d0398

Please sign in to comment.