From 739adf3d3e6c964622831a3f90e460a47320d52a Mon Sep 17 00:00:00 2001 From: Daniel Gomez Date: Tue, 23 Feb 2021 19:26:03 -0500 Subject: [PATCH] Add sharding options to test command line (#76382) --- .../flutter_tools/lib/src/commands/test.dart | 33 +++++++++++++ .../flutter_tools/lib/src/test/runner.dart | 8 ++++ .../commands.shard/hermetic/test_test.dart | 48 +++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/test.dart b/packages/flutter_tools/lib/src/commands/test.dart index 85c70e6e78..dc14d014b9 100644 --- a/packages/flutter_tools/lib/src/commands/test.dart +++ b/packages/flutter_tools/lib/src/commands/test.dart @@ -140,6 +140,16 @@ class TestCommand extends FlutterCommand { 'which indicates that a seed should be selected randomly. ' 'By default, tests run in the order they are declared.', ) + ..addOption('total-shards', + help: 'Tests can be sharded with the "--total-shards" and "--shard-index" ' + 'arguments, allowing you to split up your test suites and run ' + 'them separately.' + ) + ..addOption('shard-index', + help: 'Tests can be sharded with the "--total-shards" and "--shard-index" ' + 'arguments, allowing you to split up your test suites and run ' + 'them separately.' + ) ..addFlag('enable-vmservice', defaultsTo: false, hide: !verboseHelp, @@ -265,6 +275,27 @@ class TestCommand extends FlutterCommand { ]; } + final int shardIndex = int.tryParse(stringArg('shard-index') ?? ''); + if (shardIndex != null && (shardIndex < 0 || !shardIndex.isFinite)) { + throwToolExit( + 'Could not parse --shard-index=$shardIndex argument. It must be an integer greater than -1.'); + } + + final int totalShards = int.tryParse(stringArg('total-shards') ?? ''); + if (totalShards != null && (totalShards <= 0 || !totalShards.isFinite)) { + throwToolExit( + 'Could not parse --total-shards=$totalShards argument. It must be an integer greater than zero.'); + } + + if (totalShards != null && shardIndex == null) { + throwToolExit( + 'If you set --total-shards you need to also set --shard-index.'); + } + if (shardIndex != null && totalShards == null) { + throwToolExit( + 'If you set --shard-index you need to also set --total-shards.'); + } + final bool machine = boolArg('machine'); CoverageCollector collector; if (boolArg('coverage') || boolArg('merge-coverage')) { @@ -314,6 +345,8 @@ class TestCommand extends FlutterCommand { reporter: stringArg('reporter'), timeout: stringArg('timeout'), runSkipped: boolArg('run-skipped'), + shardIndex: shardIndex, + totalShards: totalShards, ); if (collector != null) { diff --git a/packages/flutter_tools/lib/src/test/runner.dart b/packages/flutter_tools/lib/src/test/runner.dart index 67492558c2..0fe639576c 100644 --- a/packages/flutter_tools/lib/src/test/runner.dart +++ b/packages/flutter_tools/lib/src/test/runner.dart @@ -51,6 +51,8 @@ abstract class FlutterTestRunner { String reporter, String timeout, bool runSkipped = false, + int shardIndex, + int totalShards, }); } @@ -83,6 +85,8 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner { String reporter, String timeout, bool runSkipped = false, + int shardIndex, + int totalShards, }) async { // Configure package:test to use the Flutter engine for child processes. final String shellPath = globals.artifacts.getArtifactPath(Artifact.flutterTester); @@ -112,6 +116,10 @@ class _FlutterTestRunnerImpl implements FlutterTestRunner { ...['--exclude-tags', excludeTags], if (runSkipped) '--run-skipped', + if (totalShards != null) + '--total-shards=$totalShards', + if (shardIndex != null) + '--shard-index=$shardIndex', ]; if (web) { final String tempBuildDir = globals.fs.systemTempDirectory diff --git a/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart index 5936c1722b..5c9f1505f5 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/test_test.dart @@ -59,6 +59,52 @@ void main() { Cache: () => FakeCache(), }); + group('shard-index and total-shards', () { + testUsingContext('with the params they are Piped to package:test', + () async { + final FakePackageTest fakePackageTest = FakePackageTest(); + + final TestCommand testCommand = TestCommand(testWrapper: fakePackageTest); + final CommandRunner commandRunner = + createTestCommandRunner(testCommand); + + await commandRunner.run(const [ + 'test', + '--total-shards=1', + '--shard-index=2', + '--no-pub', + ]); + + expect(fakePackageTest.lastArgs, contains('--total-shards=1')); + expect(fakePackageTest.lastArgs, contains('--shard-index=2')); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + Cache: () => FakeCache(), + }); + + testUsingContext('without the params they not Piped to package:test', + () async { + final FakePackageTest fakePackageTest = FakePackageTest(); + + final TestCommand testCommand = TestCommand(testWrapper: fakePackageTest); + final CommandRunner commandRunner = + createTestCommandRunner(testCommand); + + await commandRunner.run(const [ + 'test', + '--no-pub', + ]); + + expect(fakePackageTest.lastArgs, isNot(contains('--total-shards'))); + expect(fakePackageTest.lastArgs, isNot(contains('--shard-index'))); + }, overrides: { + FileSystem: () => fs, + ProcessManager: () => FakeProcessManager.any(), + Cache: () => FakeCache(), + }); + }); + testUsingContext('Supports coverage and machine', () async { final FakePackageTest fakePackageTest = FakePackageTest(); @@ -216,6 +262,8 @@ class FakeFlutterTestRunner implements FlutterTestRunner { String reporter, String timeout, bool runSkipped = false, + int shardIndex, + int totalShards, }) async { lastEnableObservatoryValue = enableObservatory; return exitCode;