From c9f3a89bf4a9551198c9189098a06cb0d8b4305c Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 19 Apr 2024 20:40:59 -0700 Subject: [PATCH] [et] Fix concurrent modification exception (flutter/engine#52247) We cannot modify the list of build targets as we're iterating over it. Instead of removing non-test/non-executable elements, instead we add executable test targets to a separate testTargets list and use that. Noticed while working on: https://github.com/flutter/flutter/issues/147071 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../lib/src/commands/test_command.dart | 15 ++++++++---- .../engine_tool/test/test_command_test.dart | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/engine/src/flutter/tools/engine_tool/lib/src/commands/test_command.dart b/engine/src/flutter/tools/engine_tool/lib/src/commands/test_command.dart index 20381fb171..63dd438c1d 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/commands/test_command.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/commands/test_command.dart @@ -70,10 +70,10 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f ); } + final List testTargets = []; for (final BuildTarget target in selectedTargets) { - if (!target.testOnly || target.type != BuildTargetType.executable) { - // Remove any targets that aren't testOnly and aren't executable. - selectedTargets.remove(target); + if (_isTestExecutable(target)) { + testTargets.add(target); } if (target.executable == null) { environment.logger.fatal( @@ -81,7 +81,7 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f } } // Chop off the '//' prefix. - final List buildTargets = selectedTargets + final List buildTargets = testTargets .map( (BuildTarget target) => target.label.substring('//'.length)) .toList(); @@ -95,11 +95,16 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f final WorkerPool workerPool = WorkerPool(environment, ProcessTaskProgressReporter(environment)); final Set tasks = {}; - for (final BuildTarget target in selectedTargets) { + for (final BuildTarget target in testTargets) { final List commandLine = [target.executable!.path]; tasks.add(ProcessTask( target.label, environment, environment.engine.srcDir, commandLine)); } return await workerPool.run(tasks) ? 0 : 1; } + + /// Returns true if `target` is a testonly executable. + static bool _isTestExecutable(BuildTarget target) { + return target.testOnly && target.type == BuildTargetType.executable; + } } diff --git a/engine/src/flutter/tools/engine_tool/test/test_command_test.dart b/engine/src/flutter/tools/engine_tool/test/test_command_test.dart index b8e5788b63..ecc86d3f8b 100644 --- a/engine/src/flutter/tools/engine_tool/test/test_command_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/test_command_test.dart @@ -67,4 +67,28 @@ void main() { testEnvironment.cleanup(); } }); + + test('test command skips non-testonly executables', () async { + final TestEnvironment testEnvironment = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final Environment env = testEnvironment.environment; + final ToolCommandRunner runner = ToolCommandRunner( + environment: env, + configs: configs, + ); + final int result = await runner.run([ + 'test', + '//third_party/protobuf:protoc', + ]); + expect(result, equals(1)); + expect(testEnvironment.processHistory.length, lessThan(3)); + expect(testEnvironment.processHistory.where((ExecutedProcess process) { + return process.command[0].contains('protoc'); + }), isEmpty); + } finally { + testEnvironment.cleanup(); + } + }); }