Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Handle new VM service message format #364

Merged
merged 4 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.1-dev

* Add support for scraping the service URI from the new Dart VM service message.

## 1.1.0 - 2022-1-18

* Support function level coverage information, when running tests in the Dart
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Dart.

Tools
-----
`collect_coverage` collects coverage JSON from the Dart VM Observatory.
`collect_coverage` collects coverage JSON from the Dart VM Service.
`format_coverage` formats JSON coverage data into either
[LCOV](http://ltp.sourceforge.net/coverage/lcov.php) or pretty-printed format.

Expand All @@ -33,7 +33,7 @@ or if the `pub global run` executables are on your PATH,
collect_coverage --uri=http://... -o coverage.json --resume-isolates
```

where `--uri` specifies the Observatory URI emitted by the VM.
where `--uri` specifies the Dart VM Service URI emitted by the VM.

If `collect_coverage` is invoked before the script from which coverage is to be
collected, it will wait until it detects a VM observatory to which it can
Expand Down
2 changes: 1 addition & 1 deletion lib/src/run_and_collect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Future<Map<String, dynamic>> runAndCollect(String scriptPath,
.transform(utf8.decoder)
.transform(const LineSplitter())
.listen((line) {
final uri = extractObservatoryUri(line);
final uri = extractVMServiceUri(line);
if (uri != null) {
serviceUriCompleter.complete(uri);
}
Expand Down
21 changes: 10 additions & 11 deletions lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ Future<dynamic> retry(Future Function() f, Duration interval,
}, duration: timeout);
}

/// Scrapes and returns the observatory URI from a string, or null if not found.
/// Scrapes and returns the Dart VM service URI from a string, or null if not
/// found.
///
/// Potentially useful as a means to extract it from log statements.
Uri? extractObservatoryUri(String str) {
const kObservatoryListening = 'Observatory listening on ';
final msgPos = str.indexOf(kObservatoryListening);
if (msgPos == -1) return null;
final startPos = msgPos + kObservatoryListening.length;
final endPos = str.indexOf(RegExp(r'(\s|$)'), startPos);
try {
return Uri.parse(str.substring(startPos, endPos));
} on FormatException {
return null;
Uri? extractVMServiceUri(String str) {
final listeningMessageRegExp = RegExp(
r'(?:Observatory|Dart VM Service) listening on ((http|//)[a-zA-Z0-9:/=_\-\.\[\]]+)',
);
final match = listeningMessageRegExp.firstMatch(str);
if (match != null) {
return Uri.parse(match[1]!);
}
return null;
}

/// Returns an open port by creating a temporary Socket
Expand Down
2 changes: 1 addition & 1 deletion test/collect_coverage_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Future<Map<String, dynamic>> _collectCoverage(
.transform(LineSplitter())
.listen((line) {
if (!serviceUriCompleter.isCompleted) {
final serviceUri = extractObservatoryUri(line);
final serviceUri = extractVMServiceUri(line);
if (serviceUri != null) {
serviceUriCompleter.complete(serviceUri);
}
Expand Down
2 changes: 1 addition & 1 deletion test/collect_coverage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ Future<String> _collectCoverage(bool functionCoverage) async {
.transform(LineSplitter())
.listen((line) {
if (!serviceUriCompleter.isCompleted) {
final serviceUri = extractObservatoryUri(line);
final serviceUri = extractVMServiceUri(line);
if (serviceUri != null) {
serviceUriCompleter.complete(serviceUri);
}
Expand Down
2 changes: 1 addition & 1 deletion test/lcov_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ Future<Map<String, HitMap>> _getHitMap() async {
.transform(LineSplitter())
.listen((line) {
if (!serviceUriCompleter.isCompleted) {
final serviceUri = extractObservatoryUri(line);
final serviceUri = extractVMServiceUri(line);
if (serviceUri != null) {
serviceUriCompleter.complete(serviceUri);
}
Expand Down
24 changes: 15 additions & 9 deletions test/util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,37 +96,43 @@ void main() {
});
});

group('extractObservatoryUri', () {
group('extractVMServiceUri', () {
test('returns null when not found', () {
expect(extractObservatoryUri('foo bar baz'), isNull);
expect(extractVMServiceUri('foo bar baz'), isNull);
});

test('returns null for an incorrectly formatted URI', () {
const msg = 'Observatory listening on :://';
expect(extractObservatoryUri(msg), null);
expect(extractVMServiceUri(msg), null);
});

test('returns URI at end of string', () {
const msg = 'Observatory listening on http://foo.bar:9999/';
expect(extractObservatoryUri(msg), Uri.parse('http://foo.bar:9999/'));
expect(extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/'));
});

test('returns URI with auth token at end of string', () {
const msg = 'Observatory listening on http://foo.bar:9999/cG90YXRv/';
expect(extractObservatoryUri(msg),
Uri.parse('http://foo.bar:9999/cG90YXRv/'));
expect(
extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/cG90YXRv/'));
});

test('return URI embedded within string', () {
const msg = '1985-10-26 Observatory listening on http://foo.bar:9999/ **';
expect(extractObservatoryUri(msg), Uri.parse('http://foo.bar:9999/'));
expect(extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/'));
});

test('return URI with auth token embedded within string', () {
const msg =
'1985-10-26 Observatory listening on http://foo.bar:9999/cG90YXRv/ **';
expect(extractObservatoryUri(msg),
Uri.parse('http://foo.bar:9999/cG90YXRv/'));
expect(
extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/cG90YXRv/'));
});

test('handles new Dart VM service message format', () {
const msg = 'Dart VM Service listening on http://foo.bar:9999/cG90YXRv/';
expect(
extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/cG90YXRv/'));
});
});

Expand Down