Skip to content

Supporting passing a filename that contains a list of arguments as the last argument #2485

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkgs/test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 1.25.16-wip

* Allow the final argument to be a filename prefixed with `@` that contains a
list of arguments as if they had been provided on the command line directly.

## 1.25.15

* Allow the latest version of `package:shelf_web_socket`.
Expand Down
37 changes: 37 additions & 0 deletions pkgs/test/test/runner/runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -868,4 +868,41 @@ void main() {
await test.shouldExit(0);
}, skip: 'https://github.com/dart-lang/test/issues/1803');
});

group('using @file as the final argument', () {
test('prints a suitable message if the file does not exist', () async {
var test = await runTest(['@mytestfile']);
expect(
test.stderr,
emitsThrough(
contains('Failed to read the file "mytestfile" for arguments')));
await test.shouldExit(64);
});

test('runs test suites provided in the file', () async {
await d.file('test.dart', _success).create();
await d.file('mytestargs.txt', 'test.dart').create();
var test = await runTest(['@mytestargs.txt']);
expect(test.stdout, emitsThrough(contains('+1: All tests passed!')));
await test.shouldExit(0);
});

test('handles empty lines in the file', () async {
await d.file('test.dart', _success).create();
await d.file('mytestargs.txt', '\n\r\ntest.dart\r\n').create();
var test = await runTest(['@mytestargs.txt']);
expect(test.stdout, emitsThrough(contains('+1: All tests passed!')));
await test.shouldExit(0);
});

test('handles other arguments in the file', () async {
await d.file('mytestargs.txt', '--help').create();
var test = await runTest(['@mytestargs.txt']);
expectStdoutEquals(test, '''
Runs tests in this package.

$_usage''');
await test.shouldExit(0);
});
});
}
5 changes: 4 additions & 1 deletion pkgs/test_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
## 0.6.9-wip

- Add support for native assets for `dart test` in pub workspaces.
* Add support for native assets for `dart test` in pub workspaces.
* Allow the final argument provided to the executable `main` method to be a
filename prefixed with `@` that contains a list of arguments as if they had
been provided on the command line directly.

## 0.6.8

Expand Down
23 changes: 23 additions & 0 deletions pkgs/test_core/lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ Future<void> _execute(List<String> args) async {
: StreamGroup.merge(
[ProcessSignal.sigterm.watch(), ProcessSignal.sigint.watch()]);

// The last arg can start with @ and is a file that contains a list
// of args that should replace the filename as if they were just provided
// inline.
// It is up to the calling code to clean up this file (if required) after
// the test process terminates.
if (args.isNotEmpty) {
args = args.toList(); // Ensure mutable
var lastArg = args.last;
if (lastArg.startsWith('@')) {
lastArg = lastArg.substring(1); // Strip @
var argsFile = File(lastArg); // Path must be relative to cwd
try {
args.removeLast();
args.addAll(
argsFile.readAsLinesSync().where((line) => line.trim().isNotEmpty));
} catch (e) {
_printUsage('Failed to read the file "$lastArg" for arguments: $e');
exitCode = exit_codes.usage;
return;
}
}
}

Configuration configuration;
try {
configuration = Configuration.parse(args);
Expand Down
Loading