From 1efa5aa8eeaeb0347ac1bdbb223f90e0e1404dc2 Mon Sep 17 00:00:00 2001 From: Giuseppe Cianci <39117631+Giuspepe@users.noreply.github.com> Date: Wed, 14 Sep 2022 20:25:11 +0200 Subject: [PATCH] feat(test): add --dart-define (#492) --- lib/src/commands/test/test.dart | 12 ++++++++++++ test/src/commands/test/test_test.dart | 26 ++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/src/commands/test/test.dart b/lib/src/commands/test/test.dart index 72df26e2..a4d249e1 100644 --- a/lib/src/commands/test/test.dart +++ b/lib/src/commands/test/test.dart @@ -92,6 +92,15 @@ class TestCommand extends Command { 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', ); } @@ -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?; if (isFlutterInstalled) { try { @@ -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, diff --git a/test/src/commands/test/test_test.dart b/test/src/commands/test/test_test.dart index f5cda0ad..1fccc5ad 100644 --- a/test/src/commands/test/test_test.dart +++ b/test/src/commands/test/test_test.dart @@ -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= 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.' ]; @@ -451,5 +452,26 @@ void main() { ).called(1); verify(() => logger.err('$exception')).called(1); }); + + test('completes normally --dart-define', () async { + when( + () => 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); + }); }); }