Skip to content

Commit 1efa5aa

Browse files
authored
feat(test): add --dart-define (#492)
1 parent a1a45b8 commit 1efa5aa

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/src/commands/test/test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ class TestCommand extends Command<int> {
9292
help: 'Whether "matchesGoldenFile()" calls within your test methods '
9393
'should update the golden files.',
9494
negatable: false,
95+
)
96+
..addMultiOption(
97+
'dart-define',
98+
help: 'Additional key-value pairs that will be available as constants '
99+
'from the String.fromEnvironment, bool.fromEnvironment, '
100+
'int.fromEnvironment, and double.fromEnvironment constructors. '
101+
'Multiple defines can be passed by repeating '
102+
'"--dart-define" multiple times.',
103+
valueHelp: 'foo=bar',
95104
);
96105
}
97106

@@ -142,6 +151,7 @@ This command should be run from the root of your Flutter project.''',
142151
: randomOrderingSeed;
143152
final optimizePerformance = _argResults['optimization'] as bool;
144153
final updateGoldens = _argResults['update-goldens'] as bool;
154+
final dartDefine = _argResults['dart-define'] as List<String>?;
145155

146156
if (isFlutterInstalled) {
147157
try {
@@ -160,6 +170,8 @@ This command should be run from the root of your Flutter project.''',
160170
if (excludeTags != null) ...['-x', excludeTags],
161171
if (tags != null) ...['-t', tags],
162172
if (updateGoldens) '--update-goldens',
173+
if (dartDefine != null)
174+
for (final value in dartDefine) '--dart-define=$value',
163175
...['-j', concurrency],
164176
'--no-pub',
165177
..._argResults.rest,

test/src/commands/test/test_test.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ const expectedTestUsage = [
1919
''' --coverage Whether to collect coverage information.\n'''
2020
'''-r, --recursive Run tests recursively for all nested packages.\n'''
2121
''' --[no-]optimization Whether to apply optimizations for test performance.\n'''
22-
' (defaults to on)\n'
22+
''' (defaults to on)\n'''
2323
'''-j, --concurrency The number of concurrent test suites run.\n'''
24-
' (defaults to "4")\n'
24+
''' (defaults to "4")\n'''
2525
'''-t, --tags Run only tests associated with the specified tags.\n'''
2626
''' --exclude-coverage A glob which will be used to exclude files that match from the coverage.\n'''
2727
'''-x, --exclude-tags Run only tests that do not have the specified tags.\n'''
2828
''' --min-coverage Whether to enforce a minimum coverage percentage.\n'''
2929
''' --test-randomize-ordering-seed The seed to randomize the execution order of test cases within test files.\n'''
3030
''' --update-goldens Whether "matchesGoldenFile()" calls within your test methods should update the golden files.\n'''
31+
''' --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'''
3132
'\n'
3233
'Run "very_good help" to see global options.'
3334
];
@@ -451,5 +452,26 @@ void main() {
451452
).called(1);
452453
verify(() => logger.err('$exception')).called(1);
453454
});
455+
456+
test('completes normally --dart-define', () async {
457+
when<dynamic>(
458+
() => argResults['dart-define'],
459+
).thenReturn(['FOO=bar', 'X=42']);
460+
final result = await testCommand.run();
461+
expect(result, equals(ExitCode.success.code));
462+
verify(
463+
() => flutterTest(
464+
optimizePerformance: true,
465+
arguments: [
466+
'--dart-define=FOO=bar',
467+
'--dart-define=X=42',
468+
...defaultArguments,
469+
],
470+
logger: logger,
471+
stdout: logger.write,
472+
stderr: logger.err,
473+
),
474+
).called(1);
475+
});
454476
});
455477
}

0 commit comments

Comments
 (0)