Skip to content

feat(test): add --dart-define #492

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

Merged
merged 10 commits into from
Sep 14, 2022
12 changes: 12 additions & 0 deletions lib/src/commands/test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ class TestCommand extends Command<int> {
help: 'Whether "matchesGoldenFile()" calls within your test methods '
'should update the golden files.',
negatable: false,
)
..addMultiOption(
'dart-define',
help: '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.',
valueHelp: 'foo=bar',
);
}

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

if (isFlutterInstalled) {
try {
Expand All @@ -160,6 +170,8 @@ This command should be run from the root of your Flutter project.''',
if (excludeTags != null) ...['-x', excludeTags],
if (tags != null) ...['-t', tags],
if (updateGoldens) '--update-goldens',
if (dartDefine != null)
for (final value in dartDefine) '--dart-define=$value',
...['-j', concurrency],
'--no-pub',
..._argResults.rest,
Expand Down
26 changes: 24 additions & 2 deletions test/src/commands/test/test_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ const expectedTestUsage = [
''' --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'
''' (defaults to on)\n'''
'''-j, --concurrency The number of concurrent test suites run.\n'''
' (defaults to "4")\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'''
''' --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'''
'\n'
'Run "very_good help" to see global options.'
];
Expand Down Expand Up @@ -451,5 +452,26 @@ void main() {
).called(1);
verify(() => logger.err('$exception')).called(1);
});

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