Skip to content

Commit

Permalink
feat: add --dart-define-from-file support (#984)
Browse files Browse the repository at this point in the history
* Add --dart-define-from-file

* Fix lines_longer_than_80_chars

---------

Co-authored-by: Tom Arra <[email protected]>
  • Loading branch information
macoshita and tomarra committed Apr 4, 2024
1 parent dedb295 commit 9b5c89c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 15 deletions.
17 changes: 17 additions & 0 deletions lib/src/commands/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ class TestCommand extends Command<int> {
'Multiple defines can be passed by repeating '
'"--dart-define" multiple times.',
valueHelp: 'foo=bar',
)
..addMultiOption(
'dart-define-from-file',
help: 'The path of a .json or .env file containing key-value pairs '
'that will be available as environment variables. '
'These can be accessed using the String.fromEnvironment, '
'bool.fromEnvironment, and int.fromEnvironment constructors. '
'Multiple defines can be passed by repeating '
'"--dart-define-from-file" multiple times. '
'Entries from "--dart-define" with identical keys take '
'precedence over entries from these files.',
valueHelp: 'use-define-config.json|.env',
);
}

Expand Down Expand Up @@ -161,6 +173,8 @@ This command should be run from the root of your Flutter project.''',
final updateGoldens = _argResults['update-goldens'] as bool;
final forceAnsi = _argResults['force-ansi'] as bool?;
final dartDefine = _argResults['dart-define'] as List<String>?;
final dartDefineFromFile =
_argResults['dart-define-from-file'] as List<String>?;
final rest = _argResults.rest;

if (isFlutterInstalled) {
Expand All @@ -184,6 +198,9 @@ This command should be run from the root of your Flutter project.''',
if (updateGoldens) '--update-goldens',
if (dartDefine != null)
for (final value in dartDefine) '--dart-define=$value',
if (dartDefineFromFile != null)
for (final value in dartDefineFromFile)
'--dart-define-from-file=$value',
...['-j', concurrency],
'--no-pub',
...rest,
Expand Down
52 changes: 37 additions & 15 deletions test/src/commands/test/test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ const expectedTestUsage = [
'Run tests in a Dart or Flutter project.\n'
'\n'
'Usage: very_good test [arguments]\n'
'-h, --help Print this usage information.\n'
''' --coverage Whether to collect coverage information.\n'''
'''-r, --recursive Run tests recursively for all nested packages.\n'''
''' --[no-]optimization Whether to apply optimizations for test performance.\n'''
''' (defaults to on)\n'''
'''-j, --concurrency The number of concurrent test suites run.\n'''
''' (defaults to "4")\n'''
'''-t, --tags Run only tests associated with the specified tags.\n'''
''' --exclude-coverage A glob which will be used to exclude files that match from the coverage.\n'''
'''-x, --exclude-tags Run only tests that do not have the specified tags.\n'''
''' --min-coverage Whether to enforce a minimum coverage percentage.\n'''
''' --test-randomize-ordering-seed The seed to randomize the execution order of test cases within test files.\n'''
''' --update-goldens Whether "matchesGoldenFile()" calls within your test methods should update the golden files.\n'''
''' --force-ansi Whether to force ansi output. If not specified, it will maintain the default behavior based on stdout and stderr.\n'''
''' --dart-define=<foo=bar> Additional key-value pairs that will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors. Multiple defines can be passed by repeating "--dart-define" multiple times.\n'''
'''-h, --help Print this usage information.\n'''
''' --coverage Whether to collect coverage information.\n'''
'''-r, --recursive Run tests recursively for all nested packages.\n'''
''' --[no-]optimization Whether to apply optimizations for test performance.\n'''
''' (defaults to on)\n'''
'''-j, --concurrency The number of concurrent test suites run.\n'''
''' (defaults to "4")\n'''
'''-t, --tags Run only tests associated with the specified tags.\n'''
''' --exclude-coverage A glob which will be used to exclude files that match from the coverage.\n'''
'''-x, --exclude-tags Run only tests that do not have the specified tags.\n'''
''' --min-coverage Whether to enforce a minimum coverage percentage.\n'''
''' --test-randomize-ordering-seed The seed to randomize the execution order of test cases within test files.\n'''
''' --update-goldens Whether "matchesGoldenFile()" calls within your test methods should update the golden files.\n'''
''' --force-ansi Whether to force ansi output. If not specified, it will maintain the default behavior based on stdout and stderr.\n'''
''' --dart-define=<foo=bar> Additional key-value pairs that will be available as constants from the String.fromEnvironment, bool.fromEnvironment, int.fromEnvironment, and double.fromEnvironment constructors. Multiple defines can be passed by repeating "--dart-define" multiple times.\n'''
''' --dart-define-from-file=<use-define-config.json|.env> The path of a .json or .env file containing key-value pairs that will be available as environment variables. These can be accessed using the String.fromEnvironment, bool.fromEnvironment, and int.fromEnvironment constructors. Multiple defines can be passed by repeating "--dart-define-from-file" multiple times. Entries from "--dart-define" with identical keys take precedence over entries from these files.\n'''
'\n'
'Run "very_good help" to see global options.'
];
Expand Down Expand Up @@ -484,6 +485,27 @@ void main() {
).called(1);
});

test('completes normally --dart-define-from-file', () async {
when<dynamic>(
() => argResults['dart-define-from-file'],
).thenReturn(['defines/foo.json', 'bar.env']);
final result = await testCommand.run();
expect(result, equals(ExitCode.success.code));
verify(
() => flutterTest(
optimizePerformance: true,
arguments: [
'--dart-define-from-file=defines/foo.json',
'--dart-define-from-file=bar.env',
...defaultArguments,
],
logger: logger,
stdout: logger.write,
stderr: logger.err,
),
).called(1);
});

test('completes normally --force-ansi', () async {
when<dynamic>(() => argResults['force-ansi']).thenReturn(true);
final result = await testCommand.run();
Expand Down

0 comments on commit 9b5c89c

Please sign in to comment.