From 3d5afedce6fe9643cafbeb89af45ebe9754233c2 Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Fri, 16 Jun 2023 10:18:07 -0700 Subject: [PATCH] Fix root suite comparison on Windows (#2038) Fixes #2037 Canonicalize paths when checking whether a stack frame is coming from the test suite root. Add a test case which runs with an absolute path argument. On windows, this situation would have resulted in the `root_` location information being missing because of a difference in the stack frame paths and the suite path before canonicalization. This test fails on windows without the canonicalization. Make the test utils more flexible to handle the new output differences across operation systems. Allow a `Matcher` value for the `path` argument to `suiteJson`. This optional argument had previously been unused. Use `equalsIgnoreCase` because on windows there is a difference in the case of the drive letter. Allow a `Matcher` value for the `name` argument to `testStartJson`. Take an `Object` to allow either `Matcher` or `String` for existing uses. Check the "loading" output line loosely by checking only the start and end. --- pkgs/test/CHANGELOG.md | 2 + pkgs/test/test/runner/json_reporter_test.dart | 46 ++++++++++++++++++- .../test/test/runner/json_reporter_utils.dart | 9 ++-- pkgs/test_core/CHANGELOG.md | 2 + .../lib/src/runner/reporter/json.dart | 4 +- 5 files changed, 55 insertions(+), 8 deletions(-) diff --git a/pkgs/test/CHANGELOG.md b/pkgs/test/CHANGELOG.md index 1d2bf5f37..6ab538aef 100644 --- a/pkgs/test/CHANGELOG.md +++ b/pkgs/test/CHANGELOG.md @@ -3,6 +3,8 @@ * Drop support for null unsafe Dart, bump SDK constraint to `3.0.0`. * Make some annotation classes `final`: `OnPlatform`, `Retry`, `Skip`, `Tags`, `TestOn`, `Timeout`. +* Fix the `root_` fields in the JSON reporter when running a test on Windows + with an absolute path. ## 1.24.3 diff --git a/pkgs/test/test/runner/json_reporter_test.dart b/pkgs/test/test/runner/json_reporter_test.dart index b6b3317d7..934d89a63 100644 --- a/pkgs/test/test/runner/json_reporter_test.dart +++ b/pkgs/test/test/runner/json_reporter_test.dart @@ -512,7 +512,7 @@ void main() { args: ['-p', 'chrome']); }, tags: ['chrome'], skip: 'https://github.com/dart-lang/test/issues/872'); - test('the root suite if applicable', () { + test('the root suite from a relative path', () { return _expectReport( ''' customTest('success 1', () {}); @@ -543,6 +543,46 @@ void main() { 'common.dart': ''' import 'package:test/test.dart'; +void customTest(String name, dynamic Function() testFn) => test(name, testFn); +''', + }); + }); + + test('the root suite from an absolute path', () { + final path = p.prettyUri(p.join(d.sandbox, 'test.dart')); + return _expectReport( + ''' + customTest('success 1', () {}); + test('success 2', () {}); + ''', + useRelativePath: false, + [ + [ + suiteJson(0, path: equalsIgnoringCase(path)), + testStartJson( + 1, allOf(startsWith('loading '), endsWith('test.dart')), + groupIDs: []), + testDoneJson(1, hidden: true), + ], + [ + groupJson(2, testCount: 2), + testStartJson(3, 'success 1', + line: 3, + column: 60, + url: p.toUri(p.join(d.sandbox, 'common.dart')).toString(), + rootColumn: 7, + rootLine: 7, + rootUrl: p.toUri(p.join(d.sandbox, 'test.dart')).toString()), + testDoneJson(3), + testStartJson(4, 'success 2', line: 8, column: 7), + testDoneJson(4), + ] + ], + doneJson(), + externalLibraries: { + 'common.dart': ''' +import 'package:test/test.dart'; + void customTest(String name, dynamic Function() testFn) => test(name, testFn); ''', }); @@ -586,6 +626,7 @@ void customTest(String name, dynamic Function() testFn) => test(name, testFn); Future _expectReport(String tests, List> expected, Map done, {List args = const [], + bool useRelativePath = true, Map externalLibraries = const {}}) async { var testContent = StringBuffer(''' import 'dart:async'; @@ -603,8 +644,9 @@ import 'package:test/test.dart'; ..writeln('}'); await d.file('test.dart', testContent.toString()).create(); + var testPath = useRelativePath ? 'test.dart' : p.join(d.sandbox, 'test.dart'); - var test = await runTest(['test.dart', '--chain-stack-traces', ...args], + var test = await runTest([testPath, '--chain-stack-traces', ...args], reporter: 'json'); await test.shouldExit(); diff --git a/pkgs/test/test/runner/json_reporter_utils.dart b/pkgs/test/test/runner/json_reporter_utils.dart index c53562bf5..bbdbb78f0 100644 --- a/pkgs/test/test/runner/json_reporter_utils.dart +++ b/pkgs/test/test/runner/json_reporter_utils.dart @@ -61,15 +61,16 @@ Map _allSuitesJson({int count = 1}) => /// Returns the event emitted by the JSON reporter indicating that a suite has /// begun running. /// -/// The [platform] defaults to `"vm"`, the [path] defaults to `"test.dart"`. +/// The [platform] defaults to `'vm'`. +/// The [path] defaults to `equals('test.dart')`. Map suiteJson(int id, - {String platform = 'vm', String path = 'test.dart'}) => + {String platform = 'vm', Matcher? path}) => { 'type': 'suite', 'suite': { 'id': id, 'platform': platform, - 'path': path, + 'path': path ?? 'test.dart', } }; @@ -120,7 +121,7 @@ Map groupJson(int id, /// [skip] is `true`, the test is expected to be marked as skipped without a /// reason. If it's a [String], the test is expected to be marked as skipped /// with that reason. -Map testStartJson(int id, String name, +Map testStartJson(int id, Object /*String|Matcher*/ name, {int? suiteID, Iterable? groupIDs, int? line, diff --git a/pkgs/test_core/CHANGELOG.md b/pkgs/test_core/CHANGELOG.md index a5ff68161..e80bfbfd3 100644 --- a/pkgs/test_core/CHANGELOG.md +++ b/pkgs/test_core/CHANGELOG.md @@ -3,6 +3,8 @@ * Drop support for null unsafe Dart, bump SDK constraint to `3.0.0`. * Add `final` modifier on some implementation classes: `Configuration`, `CustomRuntime`,`RuntimeSettings`, `SuiteConfiguration`. +* Fix the `root_` fields in the JSON reporter when running a test on Windows + with an absolute path. ## 0.5.3 diff --git a/pkgs/test_core/lib/src/runner/reporter/json.dart b/pkgs/test_core/lib/src/runner/reporter/json.dart index 549781753..0683b60b9 100644 --- a/pkgs/test_core/lib/src/runner/reporter/json.dart +++ b/pkgs/test_core/lib/src/runner/reporter/json.dart @@ -303,7 +303,7 @@ class JsonReporter implements Reporter { /// all be `null`. Map _frameInfo(SuiteConfiguration suiteConfig, Trace? trace, Runtime runtime, String suitePath) { - var absoluteSuitePath = p.absolute(suitePath); + var absoluteSuitePath = p.canonicalize(p.absolute(suitePath)); var frame = trace?.frames.first; if (frame == null || (suiteConfig.jsTrace && runtime.isJS)) { return {'line': null, 'column': null, 'url': null}; @@ -311,7 +311,7 @@ class JsonReporter implements Reporter { var rootFrame = trace?.frames.firstWhereOrNull((frame) => frame.uri.scheme == 'file' && - frame.uri.toFilePath() == absoluteSuitePath); + p.canonicalize(frame.uri.toFilePath()) == absoluteSuitePath); return { 'line': frame.line, 'column': frame.column,