Skip to content

Commit

Permalink
Add the platform to test names in the github reporter when applicable (
Browse files Browse the repository at this point in the history
…#1715)

Fixes #1714

Uses the same logic as the expanded reporter, outputting names when more than one platform is being ran.
  • Loading branch information
jakemac53 committed Jun 8, 2022
1 parent 611a2ba commit 2d67647
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
2 changes: 2 additions & 0 deletions pkgs/test/CHANGELOG.md
@@ -1,6 +1,8 @@
## 1.21.2-dev

* Add `Target` to restrict `TestOn` annotation to library level.
* Update the github reporter to output the platform in the test names when
multiple platforms are used.
* Fix `spawnHybridUri` support for `package:` uris.

## 1.21.1
Expand Down
54 changes: 43 additions & 11 deletions pkgs/test/test/runner/github_reporter_test.dart
Expand Up @@ -36,6 +36,20 @@ void main() {
🎉 3 tests passed.''');
});

test('includes the platform name when multiple platforms are ran', () {
return _expectReportLines('''
test('success 1', () {});''', [
'::group::✅ [VM] success 1',
'::endgroup::',
'::group::✅ [Chrome] success 1',
'::endgroup::',
'🎉 2 tests passed.',
], args: [
'-p',
'vm,chrome'
]);
});

test('runs several failing tests and reports when each fails', () {
return _expectReport('''
test('failure 1', () => throw TestFailure('oh no'));
Expand Down Expand Up @@ -306,12 +320,36 @@ void main() {
});
}

/// Expects exactly [expected] to appear in the test output.
///
/// If [useContains] is passed, then the output only must contain [expected].
Future<void> _expectReport(
String tests,
String expected, {
List<String> args = const [],
bool useContains = false,
}) async {
expected = expected.split('\n').map(_unindent).join('\n');

var actual = (await _reportLines(tests, args)).join('\n');

expect(actual, useContains ? contains(expected) : equals(expected));
}

/// Expects all of [expected] lines to appear in the test output, but additional
/// output is allowed.
Future<void> _expectReportLines(
String tests,
List<String> expected, {
List<String> args = const [],
}) async {
expected = [for (var line in expected) _unindent(line)];
var actual = await _reportLines(tests, args);
expect(actual, containsAllInOrder(expected));
}

/// All the output lines from running [tests].
Future<List<String>> _reportLines(String tests, List<String> args) async {
await d.file('test.dart', '''
import 'dart:async';
Expand All @@ -329,17 +367,11 @@ $tests
await test.shouldExit();

var stdoutLines = await test.stdoutStream().toList();
var actual = stdoutLines
return stdoutLines
.map((line) => line.trim())
.where((line) => line.isNotEmpty)
.join('\n');

// Un-indent the expected string.
var indentation = expected.indexOf(RegExp('[^ ]'));
expected = expected.split('\n').map((line) {
if (line.isEmpty) return line;
return line.substring(indentation);
}).join('\n');

expect(actual, useContains ? contains(expected) : equals(expected));
.toList();
}

/// Removes all leading space from [line].
String _unindent(String line) => line.trimLeft();
2 changes: 2 additions & 0 deletions pkgs/test_core/CHANGELOG.md
@@ -1,5 +1,7 @@
## 0.4.14-dev

* Update the github reporter to output the platform in the test names when
multiple platforms are used.
* Fix `spawnHybridUri` support for `package:` uris.

## 0.4.13
Expand Down
3 changes: 2 additions & 1 deletion pkgs/test_core/lib/src/runner/configuration/reporters.dart
Expand Up @@ -48,7 +48,8 @@ final _allReporters = <String, ReporterDetails>{
'A custom reporter for GitHub Actions (the default reporter when running on GitHub Actions).',
(config, engine, sink) => GithubReporter.watch(engine, sink,
printPath: config.paths.length > 1 ||
Directory(config.paths.single.testPath).existsSync())),
Directory(config.paths.single.testPath).existsSync(),
printPlatform: config.suiteDefaults.runtimes.length > 1)),
'json': ReporterDetails(
'A machine-readable format (see '
'https://dart.dev/go/test-docs/json_reporter.md).',
Expand Down
12 changes: 10 additions & 2 deletions pkgs/test_core/lib/src/runner/reporter/github.dart
Expand Up @@ -29,6 +29,9 @@ class GithubReporter implements Reporter {
/// Whether the path to each test's suite should be printed.
final bool _printPath;

/// Whether the platform each test is running on should be printed.
final bool _printPlatform;

/// Whether the reporter is paused.
var _paused = false;

Expand All @@ -46,10 +49,12 @@ class GithubReporter implements Reporter {
Engine engine,
StringSink sink, {
required bool printPath,
required bool printPlatform,
}) =>
GithubReporter._(engine, sink, printPath);
GithubReporter._(engine, sink, printPath, printPlatform);

GithubReporter._(this._engine, this._sink, this._printPath) {
GithubReporter._(
this._engine, this._sink, this._printPath, this._printPlatform) {
_subscriptions.add(_engine.onTestStarted.listen(_onTestStarted));
_subscriptions.add(_engine.success.asStream().listen(_onDone));

Expand Down Expand Up @@ -143,6 +148,9 @@ class GithubReporter implements Reporter {
name = '${test.suite.path}: $name';
}
}
if (_printPlatform) {
name = '[${test.suite.platform.runtime.name}] $name';
}
_sink.writeln(_GithubMarkup.startGroup('$prefix $name$statusSuffix'));
for (var message in messages) {
_sink.writeln(message.text);
Expand Down

0 comments on commit 2d67647

Please sign in to comment.