diff --git a/CHANGELOG.md b/CHANGELOG.md index bbe9d37e..301d5775 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 6457e2e8..57ace835 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/lib/src/run_and_collect.dart b/lib/src/run_and_collect.dart index 6e52f4f4..b279f460 100644 --- a/lib/src/run_and_collect.dart +++ b/lib/src/run_and_collect.dart @@ -35,7 +35,7 @@ Future> 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); } diff --git a/lib/src/util.dart b/lib/src/util.dart index ff507d17..506d89c3 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -38,20 +38,19 @@ Future 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 diff --git a/test/collect_coverage_api_test.dart b/test/collect_coverage_api_test.dart index 608ccbfa..16cce21c 100644 --- a/test/collect_coverage_api_test.dart +++ b/test/collect_coverage_api_test.dart @@ -116,7 +116,7 @@ Future> _collectCoverage( .transform(LineSplitter()) .listen((line) { if (!serviceUriCompleter.isCompleted) { - final serviceUri = extractObservatoryUri(line); + final serviceUri = extractVMServiceUri(line); if (serviceUri != null) { serviceUriCompleter.complete(serviceUri); } diff --git a/test/collect_coverage_test.dart b/test/collect_coverage_test.dart index 72b3c7c2..05372354 100644 --- a/test/collect_coverage_test.dart +++ b/test/collect_coverage_test.dart @@ -306,7 +306,7 @@ Future _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); } diff --git a/test/lcov_test.dart b/test/lcov_test.dart index 7ce7334d..30035d64 100644 --- a/test/lcov_test.dart +++ b/test/lcov_test.dart @@ -221,7 +221,7 @@ Future> _getHitMap() async { .transform(LineSplitter()) .listen((line) { if (!serviceUriCompleter.isCompleted) { - final serviceUri = extractObservatoryUri(line); + final serviceUri = extractVMServiceUri(line); if (serviceUri != null) { serviceUriCompleter.complete(serviceUri); } diff --git a/test/util_test.dart b/test/util_test.dart index b8bf823c..403a022a 100644 --- a/test/util_test.dart +++ b/test/util_test.dart @@ -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/')); }); });