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

Commit 4fedf9a

Browse files
authored
Handle new VM service message format (#364)
"Observatory listening on..." is eventually being updated to "Dart VM Service listening on...". This change allows for parsing the VM service URI from messages of either format. See dart-lang/sdk#46756
1 parent 9a43df0 commit 4fedf9a

File tree

8 files changed

+35
-26
lines changed

8 files changed

+35
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.1-dev
2+
3+
* Add support for scraping the service URI from the new Dart VM service message.
4+
15
## 1.1.0 - 2022-1-18
26

37
* Support function level coverage information, when running tests in the Dart

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Dart.
88

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

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

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

3838
If `collect_coverage` is invoked before the script from which coverage is to be
3939
collected, it will wait until it detects a VM observatory to which it can

lib/src/run_and_collect.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Future<Map<String, dynamic>> runAndCollect(String scriptPath,
3535
.transform(utf8.decoder)
3636
.transform(const LineSplitter())
3737
.listen((line) {
38-
final uri = extractObservatoryUri(line);
38+
final uri = extractVMServiceUri(line);
3939
if (uri != null) {
4040
serviceUriCompleter.complete(uri);
4141
}

lib/src/util.dart

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,19 @@ Future<dynamic> retry(Future Function() f, Duration interval,
3838
}, duration: timeout);
3939
}
4040

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

5756
/// Returns an open port by creating a temporary Socket

test/collect_coverage_api_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Future<Map<String, dynamic>> _collectCoverage(
116116
.transform(LineSplitter())
117117
.listen((line) {
118118
if (!serviceUriCompleter.isCompleted) {
119-
final serviceUri = extractObservatoryUri(line);
119+
final serviceUri = extractVMServiceUri(line);
120120
if (serviceUri != null) {
121121
serviceUriCompleter.complete(serviceUri);
122122
}

test/collect_coverage_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ Future<String> _collectCoverage(bool functionCoverage) async {
306306
.transform(LineSplitter())
307307
.listen((line) {
308308
if (!serviceUriCompleter.isCompleted) {
309-
final serviceUri = extractObservatoryUri(line);
309+
final serviceUri = extractVMServiceUri(line);
310310
if (serviceUri != null) {
311311
serviceUriCompleter.complete(serviceUri);
312312
}

test/lcov_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Future<Map<String, HitMap>> _getHitMap() async {
221221
.transform(LineSplitter())
222222
.listen((line) {
223223
if (!serviceUriCompleter.isCompleted) {
224-
final serviceUri = extractObservatoryUri(line);
224+
final serviceUri = extractVMServiceUri(line);
225225
if (serviceUri != null) {
226226
serviceUriCompleter.complete(serviceUri);
227227
}

test/util_test.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,43 @@ void main() {
9696
});
9797
});
9898

99-
group('extractObservatoryUri', () {
99+
group('extractVMServiceUri', () {
100100
test('returns null when not found', () {
101-
expect(extractObservatoryUri('foo bar baz'), isNull);
101+
expect(extractVMServiceUri('foo bar baz'), isNull);
102102
});
103103

104104
test('returns null for an incorrectly formatted URI', () {
105105
const msg = 'Observatory listening on :://';
106-
expect(extractObservatoryUri(msg), null);
106+
expect(extractVMServiceUri(msg), null);
107107
});
108108

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

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

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

125125
test('return URI with auth token embedded within string', () {
126126
const msg =
127127
'1985-10-26 Observatory listening on http://foo.bar:9999/cG90YXRv/ **';
128-
expect(extractObservatoryUri(msg),
129-
Uri.parse('http://foo.bar:9999/cG90YXRv/'));
128+
expect(
129+
extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/cG90YXRv/'));
130+
});
131+
132+
test('handles new Dart VM service message format', () {
133+
const msg = 'Dart VM Service listening on http://foo.bar:9999/cG90YXRv/';
134+
expect(
135+
extractVMServiceUri(msg), Uri.parse('http://foo.bar:9999/cG90YXRv/'));
130136
});
131137
});
132138

0 commit comments

Comments
 (0)