Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adb forward --list to adb wrapper #2332

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/adb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Bump minimum Dart SDK to version 3.2.0 (#1917)
- Add `getForwardedPorts` method to `Adb` (#2332)
- Bump minimum Dart SDK to version 3.3.0-0 (#1917)

## 0.3.0

Expand Down
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] ?? {};
}
}
4 changes: 2 additions & 2 deletions packages/adb/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: adb
description: Simple Dart wrapper around Android Debug Bridge.
version: 0.3.0
version: 0.4.0-0
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,
},
},
);
});
},
);
}
Loading