Skip to content

Commit

Permalink
feat(test): add --dart-define (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuspepe committed Sep 14, 2022
1 parent a1a45b8 commit 1efa5aa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/src/commands/test/test.dart
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
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);
});
});
}

0 comments on commit 1efa5aa

Please sign in to comment.