Skip to content

Commit

Permalink
Add adb forward list to adb wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
gogolon committed Sep 9, 2024
1 parent 9f97af3 commit 6a66b13
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
46 changes: 46 additions & 0 deletions packages/adb/lib/src/adb.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io' as io;

import 'package:adb/adb.dart';
Expand Down Expand Up @@ -161,6 +162,25 @@ class Adb {
};
}

/// Returns a parsed result of `adb forward --list` command.
Future<AdbForwardList> getForwardedPorts() async {
await _adbInternals.ensureServerRunning();

final output = await io.Process.run(
'adb',
['forward', '--list'],
runInShell: true,
);

if (output.stdErr.isNotEmpty) {
_handleAdbExceptions(output.stdErr);

throw Exception(output.stdErr);
}

return AdbForwardList.parse(output.stdout as String);
}

/// Runs instrumentation test specified by [packageName] and [intentClass] on
/// the attached device.
///
Expand Down Expand Up @@ -243,3 +263,29 @@ class Adb {
}
}
}

/// Represents a parsed result of adb forward --list command.
extension type AdbForwardList._(Map<String, Map<int, int>> map) {
/// Parses the output of `adb forward --list` command into a map.
AdbForwardList.parse(String adbForwardOutput) : map = {} {
final nonEmptyLines = const LineSplitter()
.convert(adbForwardOutput)
.where((line) => line.isNotEmpty);
for (final line in nonEmptyLines) {
final parts = line.split(' ');
final device = parts[0];
final hostPort = int.parse(parts[1].split(':')[1]);
final devicePort = int.parse(parts[2].split(':')[1]);
if (map[device] case final deviceMap?) {
deviceMap[hostPort] = devicePort;
} else {
map[device] = {hostPort: devicePort};
}
}
}

/// Returns port mapping for a device with the specified id.
Map<int, int> getMappedPortsForDevice(String deviceId) {
return map[deviceId] ?? {};
}
}
2 changes: 1 addition & 1 deletion packages/adb/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repository: https://github.com/leancodepl/patrol/tree/master/packages/adb
issue_tracker: https://github.com/leancodepl/patrol/issues?q=is%3Aopen+is%3Aissue+label%3A%22package%3A+adb%22

environment:
sdk: '>=3.2.0 <4.0.0'
sdk: '>=3.3.0-0 <4.0.0'

dev_dependencies:
custom_lint: ^0.6.4
Expand Down
39 changes: 39 additions & 0 deletions packages/adb/test/adb_forward_list_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:adb/adb.dart';
import 'package:test/test.dart';

void main() {
group(
'AdbForwardList',
() {
test('returns an empty map if adb forward --list output is empty', () {
const output = '''
''';
expect(AdbForwardList.parse(output), <String, Map<int, int>>{});
});

test('parses adb forward --list output correctly', () {
const output = '''
emulator-5554 tcp:60000 tcp:60001
emulator-5554 tcp:61234 tcp:61235
emulator-5556 tcp:61341 tcp:62562
''';

final result = AdbForwardList.parse(output);
expect(
result,
{
'emulator-5554': {
60000: 60001,
61234: 61235,
},
'emulator-5556': {
61341: 62562,
},
},
);
});
},
);
}

0 comments on commit 6a66b13

Please sign in to comment.