-
Notifications
You must be signed in to change notification settings - Fork 224
Add .fromEnvironment flags to the global declarer #2561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
johnniwinther
wants to merge
3
commits into
master
Choose a base branch
from
fromEnvironment
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| ## 0.6.14 | ||
| * Support environment flags for configuring the global declarer. | ||
|
|
||
| ## 0.6.13 | ||
|
|
||
| * Require Dart 3.7 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:test/test.dart' as test_package; | ||
|
|
||
| typedef TestFunction = dynamic Function(); | ||
|
|
||
| class Test { | ||
| final String name; | ||
| final TestFunction function; | ||
|
|
||
| Test(this.name, this.function); | ||
| } | ||
|
|
||
| void main() { | ||
| var tests = [ | ||
| Test('a', () {}), | ||
| Test('b', () => throw test_package.TestFailure('b')), | ||
| Test('c', () {}), | ||
| Test('d', () => throw test_package.TestFailure('d')), | ||
| Test('e', () {}), | ||
| ]; | ||
|
|
||
| for (var test in tests) { | ||
| test_package.test(test.name, () async { | ||
| try { | ||
| await test.function(); | ||
| } finally {} | ||
| }); | ||
| } | ||
| } |
240 changes: 240 additions & 0 deletions
240
pkgs/test_core/test/scaffolding/global_declarer_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'dart:io'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| import 'package:test/test.dart'; | ||
|
|
||
| Future<void> main() async { | ||
| group('environment', () { | ||
| test('default', () async { | ||
| await _run([], contains: _generateExpectedOutput()); | ||
| }); | ||
|
|
||
| group('colors', () { | ||
| test('true', () async { | ||
| await _run([ | ||
| '-Dtest_core.colors=true', | ||
| ], contains: _generateExpectedOutput()); | ||
| }); | ||
| test('false', () async { | ||
| await _run([ | ||
| '-Dtest_core.colors=false', | ||
| ], contains: _generateExpectedOutput(colors: false)); | ||
| }); | ||
| }); | ||
|
|
||
| group('compactReporter', () { | ||
| test('false', () async { | ||
| await _run([ | ||
| '-Dtest_core.compactReporter=false', | ||
| ], contains: _generateExpectedOutput()); | ||
| }); | ||
| test('true', () async { | ||
| await _run([ | ||
| '-Dtest_core.compactReporter=true', | ||
| ], contains: _generateExpectedOutput(compactReporter: true)); | ||
| }); | ||
| }); | ||
|
|
||
| group('printPath', () { | ||
| test('false', () async { | ||
| await _run([ | ||
| '-Dtest_core.printPath=false', | ||
| ], contains: _generateExpectedOutput()); | ||
| }); | ||
| test('true', () async { | ||
| await _run([ | ||
| '-Dtest_core.printPath=true', | ||
| ], contains: _generateExpectedOutput(path: ': .')); | ||
| }); | ||
| }); | ||
|
|
||
| group('printPlatform', () { | ||
| test('false', () async { | ||
| await _run([ | ||
| '-Dtest_core.printPlatform=false', | ||
| ], contains: _generateExpectedOutput()); | ||
| }); | ||
| test('true', () async { | ||
| await _run([ | ||
| '-Dtest_core.printPlatform=true', | ||
| ], contains: _generateExpectedOutput(platform: ' [VM, Kernel]')); | ||
| }); | ||
| }); | ||
|
|
||
| group('mixed', () { | ||
| test('compactReporter, no colors', () async { | ||
| await _run( | ||
| ['-Dtest_core.compactReporter=true', '-Dtest_core.colors=false'], | ||
| contains: _generateExpectedOutput( | ||
| compactReporter: true, | ||
| colors: false, | ||
| ), | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| const boldCode = '\x1B[1m'; | ||
| const cyanCode = '\x1B[36m'; | ||
| const debugPrint = false; | ||
| const greenCode = '\x1B[32m'; | ||
| const noColorCode = '\x1B[0m'; | ||
| const redCode = '\x1B[31m'; | ||
|
|
||
| String _decode(dynamic stdout) { | ||
| if (stdout is String) { | ||
| return stdout; | ||
| } else { | ||
| return systemEncoding.decoder.convert(stdout as Uint8List); | ||
| } | ||
| } | ||
|
|
||
| List<Pattern> _generateExpectedOutput({ | ||
| bool colors = true, | ||
| bool compactReporter = false, | ||
| String path = '', | ||
| String platform = '', | ||
| }) { | ||
| String green(String text) => colors ? '$greenCode$text$noColorCode' : text; | ||
|
|
||
| String red(String text) => colors ? '$redCode$text$noColorCode' : text; | ||
|
|
||
| String bold(String text) => colors ? '$boldCode$text$noColorCode' : text; | ||
|
|
||
| String cyan(String text) => colors ? '$cyanCode$text$noColorCode' : text; | ||
|
|
||
| Pattern hidden(String text) => RegExp( | ||
| '${RegExp.escape(text)}${colors ? RegExp.escape(noColorCode) : ''} +\\r', | ||
| ); | ||
|
|
||
| Pattern line( | ||
| int success, | ||
| int fail, | ||
| String text, { | ||
| bool hide = false, | ||
| bool failure = false, | ||
| bool end = false, | ||
| }) { | ||
| var sb = StringBuffer(); | ||
| sb.write(green('+$success')); | ||
| if (fail != 0) { | ||
| sb.write(red(' $fail')); | ||
| } | ||
| if (end) { | ||
| sb.write(': ${red(text)}'); | ||
| } else { | ||
| sb.write('$path:$platform $text'); | ||
| if (failure) { | ||
| sb.write(' ${bold(red('[E]'))}'); | ||
| } | ||
| } | ||
| var result = sb.toString(); | ||
| if (hide) { | ||
| return hidden(result); | ||
| } else { | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| line(0, 0, 'a', hide: compactReporter), | ||
| line(1, 0, 'b', hide: compactReporter), | ||
| line(1, -1, 'b', failure: true), | ||
| if (compactReporter) cyan('To run this test again:'), | ||
| line(1, -1, 'c', hide: compactReporter), | ||
| line(2, -1, 'd', hide: compactReporter), | ||
| line(2, -2, 'd', failure: true), | ||
| if (compactReporter) cyan('To run this test again:'), | ||
| line(2, -2, 'e', hide: compactReporter), | ||
| line(3, -2, 'Some tests failed.', end: true), | ||
| ]; | ||
| } | ||
|
|
||
| Future<void> _run( | ||
| List<String> args, { | ||
| List<Pattern> contains = const [], | ||
| List<String>? doesNotContain, | ||
| }) async { | ||
| if (debugPrint) { | ||
| print( | ||
| '====================================================================', | ||
| ); | ||
| print('args: $args'); | ||
| } | ||
| var tester = File('test/scaffolding/fixtures/tester.dart'); | ||
| var result = await Process.run(Platform.executable, [ | ||
| ...args, | ||
| tester.absolute.path, | ||
| ]); | ||
|
|
||
| var stdout = _decode(result.stdout); | ||
| if (debugPrint) { | ||
| print(stdout); | ||
| } | ||
|
|
||
| expect(stdout, _stringMatchesInOrder(contains)); | ||
| if (doesNotContain != null) { | ||
| for (var text in doesNotContain) { | ||
| expect(stdout, isNot(stringContainsInOrder([text]))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Matcher _stringMatchesInOrder(List<Pattern> substrings) => | ||
| _StringMatchesInOrder(substrings); | ||
|
|
||
| class _StringMatchesInOrder implements Matcher { | ||
| final List<Pattern> _patterns; | ||
|
|
||
| const _StringMatchesInOrder(this._patterns); | ||
|
|
||
| @override | ||
| Description describe(Description description) => | ||
| description.addAll('a string containing ', ', ', ' in order', _patterns); | ||
|
|
||
| @override | ||
| Description describeMismatch( | ||
| dynamic item, | ||
| Description mismatchDescription, | ||
| Map<dynamic, dynamic> matchState, | ||
| bool verbose, | ||
| ) { | ||
| var patternIndex = _matches(item, matchState); | ||
| var pattern = _patterns[patternIndex!]; | ||
| return mismatchDescription.add( | ||
| 'Pattern #$patternIndex, $pattern, not found.', | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| bool matches(dynamic item, Map<dynamic, dynamic> matchState) { | ||
| return _matches(item, matchState) == null; | ||
| } | ||
|
|
||
| int? _matches(dynamic item, Map<dynamic, dynamic> matchState) { | ||
| item as String; | ||
| var fromIndex = 0; | ||
| for ( | ||
| var patternIndex = 0; | ||
| patternIndex < _patterns.length; | ||
| patternIndex++ | ||
| ) { | ||
| var s = _patterns[patternIndex]; | ||
| if (s is String) { | ||
| var index = item.indexOf(s, fromIndex); | ||
| if (index < 0) return patternIndex; | ||
| fromIndex = index + s.length; | ||
| } else { | ||
| var matches = s.allMatches(item, fromIndex); | ||
| if (matches.isEmpty) return patternIndex; | ||
| fromIndex = matches.first.end; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be a bit more explicit here?
It'd also be nice to update this section of the readme – so this feature isn't lost.
https://github.com/dart-lang/test/tree/master/pkgs/test#selecting-a-test-reporter
I have a few packages where I'd use this, too!