Skip to content

Commit a3c2fd9

Browse files
authored
feat: add support --platform in tests (#1359)
add platform option for test commands
1 parent a2f7c97 commit a3c2fd9

File tree

6 files changed

+142
-12
lines changed

6 files changed

+142
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ very_good test --recursive
136136

137137
# Run tests recursively (shorthand)
138138
very_good test -r
139+
140+
# Run tests on a specific platform
141+
very_good test --platform chrome
139142
```
140143

141144
### [`very_good packages get`](https://cli.vgv.dev/docs/commands/get_pkgs)

lib/src/commands/dart/commands/dart_test_command.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class DartTestOptions {
2020
required this.randomSeed,
2121
required this.optimizePerformance,
2222
required this.forceAnsi,
23+
required this.platform,
2324
required this.rest,
2425
required this.reportOn,
2526
});
@@ -41,6 +42,7 @@ class DartTestOptions {
4142
: randomOrderingSeed;
4243
final optimizePerformance = argResults['optimization'] as bool;
4344
final forceAnsi = argResults['force-ansi'] as bool?;
45+
final platform = argResults['platform'] as String?;
4446
final reportOn = argResults['report-on'] as String?;
4547
final rest = argResults.rest;
4648

@@ -54,6 +56,7 @@ class DartTestOptions {
5456
randomSeed: randomSeed,
5557
optimizePerformance: optimizePerformance,
5658
forceAnsi: forceAnsi,
59+
platform: platform,
5760
reportOn: reportOn,
5861
rest: rest,
5962
);
@@ -87,6 +90,9 @@ class DartTestOptions {
8790
/// default behavior based on stdout and stderr.
8891
final bool? forceAnsi;
8992

93+
/// The platform to run tests on (e.g., 'chrome', 'vm').
94+
final String? platform;
95+
9096
/// An optional file path to report coverage information to.
9197
final String? reportOn;
9298

@@ -142,13 +148,17 @@ class DartTestCommand extends Command<int> {
142148
..addFlag(
143149
'optimization',
144150
defaultsTo: true,
145-
help: 'Whether to apply optimizations for test performance.',
151+
help:
152+
'Whether to apply optimizations for test performance. '
153+
'Automatically disabled when --platform is specified.',
146154
)
147155
..addOption(
148156
'concurrency',
149157
abbr: 'j',
150158
defaultsTo: '4',
151-
help: 'The number of concurrent test suites run.',
159+
help:
160+
'The number of concurrent test suites run. '
161+
'Automatically set to 1 when --platform is specified.',
152162
)
153163
..addOption(
154164
'tags',
@@ -190,6 +200,11 @@ class DartTestCommand extends Command<int> {
190200
'An optional file path to report coverage information to. '
191201
'This should be a path relative to the current working directory.',
192202
valueHelp: 'lib/',
203+
)
204+
..addOption(
205+
'platform',
206+
help: 'The platform to run tests on. ',
207+
valueHelp: 'chrome|vm',
193208
);
194209
}
195210

@@ -231,7 +246,10 @@ This command should be run from the root of your Dart project.''');
231246
final results = await _dartTest(
232247
optimizePerformance:
233248
options.optimizePerformance &&
234-
!TestCLIRunner.isTargettingTestFiles(options.rest),
249+
!TestCLIRunner.isTargettingTestFiles(options.rest) &&
250+
// Disabled optimization when platform is specified
251+
// https://github.com/VeryGoodOpenSource/very_good_cli/issues/1363
252+
options.platform == null,
235253
recursive: recursive,
236254
logger: _logger,
237255
stdout: _logger.write,
@@ -245,7 +263,8 @@ This command should be run from the root of your Dart project.''');
245263
arguments: [
246264
if (options.excludeTags != null) ...['-x', options.excludeTags!],
247265
if (options.tags != null) ...['-t', options.tags!],
248-
...['-j', options.concurrency],
266+
if (options.platform != null) ...['--platform', options.platform!],
267+
if (options.platform == null) ...['-j', options.concurrency],
249268
...options.rest,
250269
],
251270
reportOn: options.reportOn,

lib/src/commands/test/test.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class FlutterTestOptions {
2323
required this.forceAnsi,
2424
required this.dartDefine,
2525
required this.dartDefineFromFile,
26+
required this.platform,
2627
required this.rest,
2728
});
2829

@@ -47,6 +48,7 @@ class FlutterTestOptions {
4748
final dartDefine = argResults['dart-define'] as List<String>?;
4849
final dartDefineFromFile =
4950
argResults['dart-define-from-file'] as List<String>?;
51+
final platform = argResults['platform'] as String?;
5052
final rest = argResults.rest;
5153

5254
return FlutterTestOptions._(
@@ -62,6 +64,7 @@ class FlutterTestOptions {
6264
forceAnsi: forceAnsi,
6365
dartDefine: dartDefine,
6466
dartDefineFromFile: dartDefineFromFile,
67+
platform: platform,
6568
rest: rest,
6669
);
6770
}
@@ -104,6 +107,9 @@ class FlutterTestOptions {
104107
/// Optional list of dart define from files
105108
final List<String>? dartDefineFromFile;
106109

110+
/// The platform to run tests on (e.g., 'chrome', 'vm', 'android', 'ios').
111+
final String? platform;
112+
107113
/// The remaining arguments passed to the test command.
108114
final List<String> rest;
109115
}
@@ -156,13 +162,17 @@ class TestCommand extends Command<int> {
156162
..addFlag(
157163
'optimization',
158164
defaultsTo: true,
159-
help: 'Whether to apply optimizations for test performance.',
165+
help:
166+
'Whether to apply optimizations for test performance. '
167+
'Automatically disabled when --platform is specified.',
160168
)
161169
..addOption(
162170
'concurrency',
163171
abbr: 'j',
164172
defaultsTo: '4',
165-
help: 'The number of concurrent test suites run.',
173+
help:
174+
'The number of concurrent test suites run. '
175+
'Automatically set to 1 when --platform is specified.',
166176
)
167177
..addOption(
168178
'tags',
@@ -227,6 +237,11 @@ class TestCommand extends Command<int> {
227237
'Entries from "--dart-define" with identical keys take '
228238
'precedence over entries from these files.',
229239
valueHelp: 'use-define-config.json|.env',
240+
)
241+
..addOption(
242+
'platform',
243+
help: 'The platform to run tests on. ',
244+
valueHelp: 'chrome|vm|android|ios',
230245
);
231246
}
232247

@@ -271,7 +286,10 @@ This command should be run from the root of your Flutter project.''');
271286
optimizePerformance:
272287
options.optimizePerformance &&
273288
!TestCLIRunner.isTargettingTestFiles(options.rest) &&
274-
!options.updateGoldens,
289+
!options.updateGoldens &&
290+
// Disabled optimization when platform is specified
291+
// https://github.com/VeryGoodOpenSource/very_good_cli/issues/1363
292+
options.platform == null,
275293
recursive: recursive,
276294
logger: _logger,
277295
stdout: _logger.write,
@@ -286,12 +304,13 @@ This command should be run from the root of your Flutter project.''');
286304
if (options.excludeTags != null) ...['-x', options.excludeTags!],
287305
if (options.tags != null) ...['-t', options.tags!],
288306
if (options.updateGoldens) '--update-goldens',
307+
if (options.platform != null) ...['--platform', options.platform!],
289308
if (options.dartDefine != null)
290309
for (final value in options.dartDefine!) '--dart-define=$value',
291310
if (options.dartDefineFromFile != null)
292311
for (final value in options.dartDefineFromFile!)
293312
'--dart-define-from-file=$value',
294-
...['-j', options.concurrency],
313+
if (options.platform == null) ...['-j', options.concurrency],
295314
'--no-pub',
296315
...options.rest,
297316
],

site/docs/commands/test.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ very_good test [arguments]
2626
--test-randomize-ordering-seed The seed to randomize the execution order of test cases within test files.
2727
--update-goldens Whether "matchesGoldenFile()" calls within your test methods should update the golden files.
2828
--force-ansi Whether to force ansi output. If not specified, it will maintain the default behavior based on stdout and stderr.
29+
--platform The platform to run tests on. For Flutter tests, this can be "chrome", "vm", "android", "ios", etc. For Dart tests, this can be "chrome", "vm", etc.
2930
--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.
3031

3132
Run "very_good help" to see global options.

test/src/commands/dart/commands/dart_test_test.dart

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const expectedTestUsage = [
2727
'-h, --help Print this usage information.\n'
2828
' --coverage Whether to collect coverage information.\n'
2929
'-r, --recursive Run tests recursively for all nested packages.\n'
30-
' --[no-]optimization Whether to apply optimizations for test performance.\n'
30+
' --[no-]optimization Whether to apply optimizations for test performance. Automatically disabled when --platform is specified.\n'
3131
' (defaults to on)\n'
32-
'-j, --concurrency The number of concurrent test suites run.\n'
32+
'-j, --concurrency The number of concurrent test suites run. Automatically set to 1 when --platform is specified.\n'
3333
' (defaults to "4")\n'
3434
'-t, --tags Run only tests associated with the specified tags.\n'
3535
' --exclude-coverage A glob which will be used to exclude files that match from the coverage.\n'
@@ -38,6 +38,7 @@ const expectedTestUsage = [
3838
' --test-randomize-ordering-seed The seed to randomize the execution order of test cases within test files.\n'
3939
' --force-ansi Whether to force ansi output. If not specified, it will maintain the default behavior based on stdout and stderr.\n'
4040
' --report-on=<lib/> An optional file path to report coverage information to. This should be a path relative to the current working directory.\n'
41+
' --platform=<chrome|vm> The platform to run tests on. \n'
4142
'\n'
4243
'Run "very_good help" to see global options.',
4344
];
@@ -106,6 +107,7 @@ void main() {
106107
when<dynamic>(() => argResults['recursive']).thenReturn(false);
107108
when<dynamic>(() => argResults['coverage']).thenReturn(false);
108109
when<dynamic>(() => argResults['optimization']).thenReturn(true);
110+
when<dynamic>(() => argResults['platform']).thenReturn(null);
109111
when(() => argResults.rest).thenReturn([]);
110112
});
111113

@@ -246,6 +248,48 @@ void main() {
246248
).called(1);
247249
});
248250

251+
test('completes normally --platform chrome', () async {
252+
when<dynamic>(() => argResults['platform']).thenReturn('chrome');
253+
final result = await testCommand.run();
254+
expect(result, equals(ExitCode.success.code));
255+
verify(
256+
() => dartTest(
257+
arguments: ['--platform', 'chrome'],
258+
logger: logger,
259+
stdout: logger.write,
260+
stderr: logger.err,
261+
),
262+
).called(1);
263+
});
264+
265+
test('disables optimization when --platform is specified', () async {
266+
when<dynamic>(() => argResults['platform']).thenReturn('chrome');
267+
final result = await testCommand.run();
268+
expect(result, equals(ExitCode.success.code));
269+
verify(
270+
() => dartTest(
271+
arguments: ['--platform', 'chrome'],
272+
logger: logger,
273+
stdout: logger.write,
274+
stderr: logger.err,
275+
),
276+
).called(1);
277+
});
278+
279+
test('disables concurrency when --platform is specified', () async {
280+
when<dynamic>(() => argResults['platform']).thenReturn('chrome');
281+
final result = await testCommand.run();
282+
expect(result, equals(ExitCode.success.code));
283+
verify(
284+
() => dartTest(
285+
arguments: ['--platform', 'chrome'],
286+
logger: logger,
287+
stdout: logger.write,
288+
stderr: logger.err,
289+
),
290+
).called(1);
291+
});
292+
249293
test('completes normally --test-randomize-ordering-seed random', () async {
250294
when<dynamic>(
251295
() => argResults['test-randomize-ordering-seed'],

test/src/commands/test/test_test.dart

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const expectedTestUsage = [
2727
'-h, --help Print this usage information.\n'
2828
' --coverage Whether to collect coverage information.\n'
2929
'-r, --recursive Run tests recursively for all nested packages.\n'
30-
' --[no-]optimization Whether to apply optimizations for test performance.\n'
30+
' --[no-]optimization Whether to apply optimizations for test performance. Automatically disabled when --platform is specified.\n'
3131
' (defaults to on)\n'
32-
'-j, --concurrency The number of concurrent test suites run.\n'
32+
'-j, --concurrency The number of concurrent test suites run. Automatically set to 1 when --platform is specified.\n'
3333
' (defaults to "4")\n'
3434
'-t, --tags Run only tests associated with the specified tags.\n'
3535
' --exclude-coverage A glob which will be used to exclude files that match from the coverage.\n'
@@ -40,6 +40,7 @@ const expectedTestUsage = [
4040
' --force-ansi Whether to force ansi output. If not specified, it will maintain the default behavior based on stdout and stderr.\n'
4141
' --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'
4242
' --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'
43+
' --platform=<chrome|vm|android|ios> The platform to run tests on. \n'
4344
'\n'
4445
'Run "very_good help" to see global options.',
4546
];
@@ -108,6 +109,7 @@ void main() {
108109
when<dynamic>(() => argResults['coverage']).thenReturn(false);
109110
when<dynamic>(() => argResults['update-goldens']).thenReturn(false);
110111
when<dynamic>(() => argResults['optimization']).thenReturn(true);
112+
when<dynamic>(() => argResults['platform']).thenReturn(null);
111113
when(() => argResults.rest).thenReturn([]);
112114
});
113115

@@ -262,6 +264,48 @@ void main() {
262264
).called(1);
263265
});
264266

267+
test('completes normally --platform chrome', () async {
268+
when<dynamic>(() => argResults['platform']).thenReturn('chrome');
269+
final result = await testCommand.run();
270+
expect(result, equals(ExitCode.success.code));
271+
verify(
272+
() => flutterTest(
273+
arguments: ['--platform', 'chrome', '--no-pub'],
274+
logger: logger,
275+
stdout: logger.write,
276+
stderr: logger.err,
277+
),
278+
).called(1);
279+
});
280+
281+
test('disables optimization when --platform is specified', () async {
282+
when<dynamic>(() => argResults['platform']).thenReturn('chrome');
283+
final result = await testCommand.run();
284+
expect(result, equals(ExitCode.success.code));
285+
verify(
286+
() => flutterTest(
287+
arguments: ['--platform', 'chrome', '--no-pub'],
288+
logger: logger,
289+
stdout: logger.write,
290+
stderr: logger.err,
291+
),
292+
).called(1);
293+
});
294+
295+
test('disables concurrency when --platform is specified', () async {
296+
when<dynamic>(() => argResults['platform']).thenReturn('chrome');
297+
final result = await testCommand.run();
298+
expect(result, equals(ExitCode.success.code));
299+
verify(
300+
() => flutterTest(
301+
arguments: ['--platform', 'chrome', '--no-pub'],
302+
logger: logger,
303+
stdout: logger.write,
304+
stderr: logger.err,
305+
),
306+
).called(1);
307+
});
308+
265309
test('completes normally --test-randomize-ordering-seed random', () async {
266310
when<dynamic>(
267311
() => argResults['test-randomize-ordering-seed'],

0 commit comments

Comments
 (0)