From 785386dc2fe323ece582e6888e803747e28912b9 Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Mon, 8 Apr 2024 20:26:57 +0000 Subject: [PATCH] [et] Run GN before looking for targets. Default to build config targets (flutter/engine#51956) Addresses the notes I left on https://github.com/flutter/engine/pull/51868. Mainly fixes an issue in which the `build`, `query`, and `test` commands would fail if GN had not yet been run for the requested configuration. Instead, in this PR, if the build output directory doesn't exist yet, then it will run GN before querying the targets. Additionally, for the `build` command, if no targets are specified on the command line, the PR uses the targets in the build config as the default rather than all targets. `query` and `test` keep the same behavior. --- .../engine_tool/lib/src/build_utils.dart | 40 ++++ .../lib/src/commands/build_command.dart | 34 +-- .../lib/src/commands/query_command.dart | 27 ++- .../lib/src/commands/test_command.dart | 25 +- .../tools/engine_tool/lib/src/gn_utils.dart | 65 +++++ .../engine_tool/test/build_command_test.dart | 224 ++++++++++++------ .../tools/engine_tool/test/gn_utils_test.dart | 58 ++++- .../engine_tool/test/query_command_test.dart | 194 +++++++-------- .../engine_tool/test/test_command_test.dart | 47 ++-- .../flutter/tools/engine_tool/test/utils.dart | 42 ++++ 10 files changed, 523 insertions(+), 233 deletions(-) diff --git a/engine/src/flutter/tools/engine_tool/lib/src/build_utils.dart b/engine/src/flutter/tools/engine_tool/lib/src/build_utils.dart index b04d442092..108bbb964a 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/build_utils.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/build_utils.dart @@ -176,3 +176,43 @@ Future runBuild(Environment environment, Build build, final bool buildResult = await buildRunner.run(handler); return buildResult ? 0 : 1; } + +/// Given a [Build] object, run only its GN step. +Future runGn( + Environment environment, + Build build, { + List extraGnArgs = const [], +}) async { + // If RBE config files aren't in the tree, then disable RBE. + final String rbeConfigPath = p.join( + environment.engine.srcDir.path, + 'flutter', + 'build', + 'rbe', + ); + final List gnArgs = [ + if (!io.Directory(rbeConfigPath).existsSync()) '--no-rbe', + ...extraGnArgs, + ]; + + final BuildRunner buildRunner = BuildRunner( + platform: environment.platform, + processRunner: environment.processRunner, + abi: environment.abi, + engineSrcDir: environment.engine.srcDir, + build: build, + extraGnArgs: gnArgs, + runNinja: false, + runGenerators: false, + runTests: false, + ); + + final bool buildResult = await buildRunner.run((RunnerEvent event) { + switch (event) { + case RunnerResult(ok: false): + environment.logger.error(event); + default: + } + }); + return buildResult ? 0 : 1; +} diff --git a/engine/src/flutter/tools/engine_tool/lib/src/commands/build_command.dart b/engine/src/flutter/tools/engine_tool/lib/src/commands/build_command.dart index e8ad7fd041..d965c5d9c4 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/commands/build_command.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/commands/build_command.dart @@ -2,10 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:io'; - import 'package:engine_build_configs/engine_build_configs.dart'; -import 'package:path/path.dart' as p; import '../build_utils.dart'; import '../gn_utils.dart'; @@ -63,22 +60,27 @@ et build //flutter/fml:fml_benchmarks # Build a specific target in `//flutter/f if (!useRbe) '--no-rbe', ]; - final Map allTargets = await findTargets(environment, - Directory(p.join(environment.engine.outDir.path, build.ninja.config))); - final Set selectedTargets = - selectTargets(argResults!.rest, allTargets); - if (selectedTargets.isEmpty) { - environment.logger.error( - 'No build targets matched ${argResults!.rest}\nRun `et query targets` to see list of targets.'); + final List? selectedTargets = await targetsFromCommandLine( + environment, + build, + argResults!.rest, + ); + if (selectedTargets == null) { + // The user typed something wrong and targetsFromCommandLine has already + // logged the error message. return 1; } // Chop off the '//' prefix. - final List buildTargets = selectedTargets - .map( - (BuildTarget target) => target.label.substring('//'.length)) - .toList(); - return runBuild(environment, build, - extraGnArgs: extraGnArgs, targets: buildTargets); + final List ninjaTargets = selectedTargets.map( + (BuildTarget target) => target.label.substring('//'.length), + ).toList(); + + return runBuild( + environment, + build, + extraGnArgs: extraGnArgs, + targets: ninjaTargets, + ); } } diff --git a/engine/src/flutter/tools/engine_tool/lib/src/commands/query_command.dart b/engine/src/flutter/tools/engine_tool/lib/src/commands/query_command.dart index e0b3879f64..a7a6d19d40 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/commands/query_command.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/commands/query_command.dart @@ -2,12 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:io'; - import 'package:engine_build_configs/engine_build_configs.dart'; -import 'package:path/path.dart' as p; - import '../build_utils.dart'; import '../gn_utils.dart'; import 'command.dart'; @@ -180,15 +176,24 @@ et query targets //flutter/fml/... # List all targets under `//flutter/fml` environment.logger.error('Could not find config $configName'); return 1; } - final Map allTargets = await findTargets(environment, - Directory(p.join(environment.engine.outDir.path, build.ninja.config))); - final Set selectedTargets = - selectTargets(argResults!.rest, allTargets); - if (selectedTargets.isEmpty) { - environment.logger.error( - 'No build targets matched ${argResults!.rest}\nRun `et query targets` to see list of targets.'); + + final List? selectedTargets = await targetsFromCommandLine( + environment, + build, + argResults!.rest, + defaultToAll: true, + ); + if (selectedTargets == null) { + // The user typed something wrong and targetsFromCommandLine has already + // logged the error message. return 1; } + if (selectedTargets.isEmpty) { + environment.logger.fatal( + 'targetsFromCommandLine unexpectedly returned an empty list', + ); + } + for (final BuildTarget target in selectedTargets) { if (testOnly && (!target.testOnly || target.type != BuildTargetType.executable)) { 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 a236087eb1..81e5514a17 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 @@ -2,10 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:io'; - import 'package:engine_build_configs/engine_build_configs.dart'; -import 'package:path/path.dart' as p; import '../build_utils.dart'; import '../gn_utils.dart'; @@ -54,15 +51,23 @@ et test //flutter/fml:fml_benchmarks # Run a single test target in `//flutter/f return 1; } - final Map allTargets = await findTargets(environment, - Directory(p.join(environment.engine.outDir.path, build.ninja.config))); - final Set selectedTargets = - selectTargets(argResults!.rest, allTargets); - if (selectedTargets.isEmpty) { - environment.logger.error( - 'No build targets matched ${argResults!.rest}\nRun `et query targets --testonly` to see list of targets.'); + final List? selectedTargets = await targetsFromCommandLine( + environment, + build, + argResults!.rest, + defaultToAll: true, + ); + if (selectedTargets == null) { + // The user typed something wrong and targetsFromCommandLine has already + // logged the error message. return 1; } + if (selectedTargets.isEmpty) { + environment.logger.fatal( + 'targetsFromCommandLine unexpectedly returned an empty list', + ); + } + for (final BuildTarget target in selectedTargets) { if (!target.testOnly || target.type != BuildTargetType.executable) { // Remove any targets that aren't testOnly and aren't executable. diff --git a/engine/src/flutter/tools/engine_tool/lib/src/gn_utils.dart b/engine/src/flutter/tools/engine_tool/lib/src/gn_utils.dart index 5a527a38e5..dc6887f74a 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/gn_utils.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/gn_utils.dart @@ -5,9 +5,11 @@ import 'dart:convert'; import 'dart:io'; +import 'package:engine_build_configs/engine_build_configs.dart'; import 'package:path/path.dart' as p; import 'package:process_runner/process_runner.dart'; +import 'build_utils.dart'; import 'environment.dart'; import 'json_utils.dart'; import 'proc_utils.dart'; @@ -178,3 +180,66 @@ Set selectTargets( } return selected; } + +/// Given a list of target specifications from the command line, return a list +/// of [BuildTarget]s from the [Build] that match those specifications. +/// +/// If `commandLineTargets` is empty, by default this will return the empty +/// list, which indicates that the caller should delegate the selection of +/// build targets to whatever is specified by the [Build] object. However, +/// if `defaultToAll` is `true`, then this function will return the list of +/// all build targets. +Future?> targetsFromCommandLine( + Environment environment, + Build build, + List commandLineTargets, { + bool defaultToAll = false, +}) async { + // If there are no targets specified on the command line, then delegate to + // the default targets specified in the Build object unless directed + // otherwise by the defaultToAll argument. + if (commandLineTargets.isEmpty && !defaultToAll) { + return []; + } + + final Directory buildDir = Directory(p.join( + environment.engine.outDir.path, + build.ninja.config, + )); + // If the expected build output directory doesn't exist yet, eagerly run + // the build's GN step to try to produce it. + if (!buildDir.existsSync()) { + environment.logger.status( + 'Build output directory at ${buildDir.path} not found. Running GN.', + ); + final int gnResult = await runGn(environment, build); + if (gnResult != 0 || !buildDir.existsSync()) { + environment.logger.error( + 'The specified build did not produce the expected build ' + 'output directory.', + ); + return null; + } + } + + // Find all targets buildable in the configuration. + final Map allTargets = await findTargets( + environment, + buildDir, + ); + // Use the targets specified on the command line to filter the list of + // all targets. + final Set selectedTargets = selectTargets( + commandLineTargets, + allTargets, + ); + // Report an error if applying the filter yields no results. + if (selectedTargets.isEmpty) { + environment.logger.error( + 'No build targets matched ${commandLineTargets.join(',')}\n' + 'Run `et query targets` to see list of targets.', + ); + return null; + } + return selectedTargets.toList(); +} diff --git a/engine/src/flutter/tools/engine_tool/test/build_command_test.dart b/engine/src/flutter/tools/engine_tool/test/build_command_test.dart index 13f7fb6cee..c28e24e414 100644 --- a/engine/src/flutter/tools/engine_tool/test/build_command_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/build_command_test.dart @@ -3,11 +3,8 @@ // found in the LICENSE file. import 'dart:convert' as convert; -import 'dart:ffi' as ffi show Abi; -import 'dart:io' as io; import 'package:engine_build_configs/engine_build_configs.dart'; -import 'package:engine_repo_tools/engine_repo_tools.dart'; import 'package:engine_tool/src/build_utils.dart'; import 'package:engine_tool/src/commands/command_runner.dart'; import 'package:engine_tool/src/environment.dart'; @@ -49,43 +46,23 @@ void main() { stdout: fixtures.gnDescOutput()), ]; - TestEnvironment makeTestEnv({bool withRbe = false}) { - final io.Directory rootDir = io.Directory.systemTemp.createTempSync('et'); - final TestEngine engine = TestEngine.createTemp(rootDir: rootDir); - if (withRbe) { - io.Directory(path.join( - engine.srcDir.path, - 'flutter', - 'build', - 'rbe', - )).createSync(recursive: true); - } - final TestEnvironment testEnvironment = TestEnvironment(engine, - abi: ffi.Abi.linuxX64, cannedProcesses: cannedProcesses); - return testEnvironment; - } - - void cleanupEnv(TestEnvironment testEnv) { - try { - testEnv.environment.engine.srcDir.parent.deleteSync(recursive: true); - } catch (_) { - // Ignore failure to clean up. - } - } - test('can find host runnable build', () async { - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final List result = runnableBuilds(testEnv.environment, configs); expect(result.length, equals(8)); expect(result[0].name, equals('ci/build_name')); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command invokes gn', () async { - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -96,17 +73,18 @@ void main() { '--config', 'ci/build_name', ]); - print(testEnv.processHistory); expect(result, equals(0)); expect(testEnv.processHistory.length, greaterThanOrEqualTo(1)); expect(testEnv.processHistory[0].command[0], contains('gn')); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command invokes ninja', () async { - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -119,14 +97,16 @@ void main() { ]); expect(result, equals(0)); expect(testEnv.processHistory.length, greaterThanOrEqualTo(2)); - expect(testEnv.processHistory[2].command[0], contains('ninja')); + expect(testEnv.processHistory[1].command[0], contains('ninja')); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command invokes generator', () async { - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -140,16 +120,18 @@ void main() { expect(result, equals(0)); expect(testEnv.processHistory.length, greaterThanOrEqualTo(3)); expect( - testEnv.processHistory[3].command, + testEnv.processHistory[2].command, containsStringsInOrder(['python3', 'gen/script.py']), ); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command does not invoke tests', () async { - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -163,12 +145,15 @@ void main() { expect(result, equals(0)); expect(testEnv.processHistory.length, lessThanOrEqualTo(4)); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command runs rbe on an rbe build', () async { - final TestEnvironment testEnv = makeTestEnv(withRbe: true); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + withRbe: true, + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -180,18 +165,21 @@ void main() { 'ci/android_debug_rbe_arm64', ]); expect(result, equals(0)); - expect(testEnv.processHistory[1].command[0], + expect(testEnv.processHistory[0].command[0], contains(path.join('tools', 'gn'))); - expect(testEnv.processHistory[1].command[4], equals('--rbe')); - expect(testEnv.processHistory[2].command[0], + expect(testEnv.processHistory[0].command[4], equals('--rbe')); + expect(testEnv.processHistory[1].command[0], contains(path.join('reclient', 'bootstrap'))); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command does not run rbe when disabled', () async { - final TestEnvironment testEnv = makeTestEnv(withRbe: true); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + withRbe: true, + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -204,20 +192,22 @@ void main() { '--no-rbe', ]); expect(result, equals(0)); - expect(testEnv.processHistory[1].command[0], + expect(testEnv.processHistory[0].command[0], contains(path.join('tools', 'gn'))); - expect(testEnv.processHistory[1].command, + expect(testEnv.processHistory[0].command, doesNotContainAny(['--rbe'])); - expect(testEnv.processHistory[2].command[0], + expect(testEnv.processHistory[1].command[0], contains(path.join('ninja', 'ninja'))); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('build command does not run rbe when rbe configs do not exist', () async { - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -229,35 +219,53 @@ void main() { 'ci/android_debug_rbe_arm64', ]); expect(result, equals(0)); - expect(testEnv.processHistory[1].command[0], + expect(testEnv.processHistory[0].command[0], contains(path.join('tools', 'gn'))); - expect(testEnv.processHistory[1].command, + expect(testEnv.processHistory[0].command, doesNotContainAny(['--rbe'])); - expect(testEnv.processHistory[2].command[0], + expect(testEnv.processHistory[1].command[0], contains(path.join('ninja', 'ninja'))); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); test('mangleConfigName removes the OS and adds ci/ as needed', () { - final TestEnvironment testEnv = makeTestEnv(); - final Environment env = testEnv.environment; - expect(mangleConfigName(env, 'linux/build'), equals('build')); - expect(mangleConfigName(env, 'ci/build'), equals('ci/build')); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final Environment env = testEnv.environment; + expect(mangleConfigName(env, 'linux/build'), equals('build')); + expect(mangleConfigName(env, 'ci/build'), equals('ci/build')); + } finally { + testEnv.cleanup(); + } }); test('mangleConfigName throws when the input config name is malformed', () { - final TestEnvironment testEnv = makeTestEnv(); - final Environment env = testEnv.environment; - expectArgumentError(() => mangleConfigName(env, 'build')); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final Environment env = testEnv.environment; + expectArgumentError(() => mangleConfigName(env, 'build')); + } finally { + testEnv.cleanup(); + } }); test('demangleConfigName adds the OS and removes ci/ as needed', () { - final TestEnvironment testEnv = makeTestEnv(); - final Environment env = testEnv.environment; - expect(demangleConfigName(env, 'build'), equals('linux/build')); - expect(demangleConfigName(env, 'ci/build'), equals('ci/build')); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final Environment env = testEnv.environment; + expect(demangleConfigName(env, 'build'), equals('linux/build')); + expect(demangleConfigName(env, 'ci/build'), equals('ci/build')); + } finally { + testEnv.cleanup(); + } }); test('local config name on the command line is correctly translated', @@ -270,7 +278,9 @@ void main() { final Map configs = { 'namespace_test_config': namespaceTestConfigs, }; - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); try { final ToolCommandRunner runner = ToolCommandRunner( environment: testEnv.environment, @@ -282,12 +292,12 @@ void main() { 'host_debug', ]); expect(result, equals(0)); - expect(testEnv.processHistory[2].command[0], + expect(testEnv.processHistory[1].command[0], contains(path.join('ninja', 'ninja'))); expect( - testEnv.processHistory[2].command[2], contains('local_host_debug')); + testEnv.processHistory[1].command[2], contains('local_host_debug')); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); } }); @@ -300,7 +310,9 @@ void main() { final Map configs = { 'namespace_test_config': namespaceTestConfigs, }; - final TestEnvironment testEnv = makeTestEnv(); + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); final Environment env = testEnv.environment; try { final ToolCommandRunner runner = ToolCommandRunner( @@ -313,11 +325,75 @@ void main() { 'ci/host_debug', ]); expect(result, equals(0)); - expect(testEnv.processHistory[2].command[0], + expect(testEnv.processHistory[1].command[0], contains(path.join('ninja', 'ninja'))); - expect(testEnv.processHistory[2].command[2], contains('ci/host_debug')); + expect(testEnv.processHistory[1].command[2], contains('ci/host_debug')); } finally { - cleanupEnv(testEnv); + testEnv.cleanup(); + } + }); + + test('build command invokes ninja with the specified target', () async { + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final ToolCommandRunner runner = ToolCommandRunner( + environment: testEnv.environment, + configs: configs, + ); + final int result = await runner.run([ + 'build', + '--config', + 'host_debug', + '//flutter/fml:fml_arc_unittests', + ]); + expect(result, equals(0)); + expect(testEnv.processHistory.length, greaterThanOrEqualTo(2)); + expect(testEnv.processHistory[3].command[0], contains('ninja')); + expect(testEnv.processHistory[3].command[2], endsWith('/host_debug')); + expect( + testEnv.processHistory[3].command[5], + equals('flutter/fml:fml_arc_unittests'), + ); + } finally { + testEnv.cleanup(); + } + }); + + test('build command invokes ninja with all matched targets', () async { + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final ToolCommandRunner runner = ToolCommandRunner( + environment: testEnv.environment, + configs: configs, + ); + final int result = await runner.run([ + 'build', + '--config', + 'host_debug', + '//flutter/...', + ]); + expect(result, equals(0)); + expect(testEnv.processHistory.length, greaterThanOrEqualTo(2)); + expect(testEnv.processHistory[3].command[0], contains('ninja')); + expect(testEnv.processHistory[3].command[2], endsWith('/host_debug')); + expect( + testEnv.processHistory[3].command[5], + equals('flutter/display_list:display_list_unittests'), + ); + expect( + testEnv.processHistory[3].command[6], + equals('flutter/flow:flow_unittests'), + ); + expect( + testEnv.processHistory[3].command[7], + equals('flutter/fml:fml_arc_unittests'), + ); + } finally { + testEnv.cleanup(); } }); } diff --git a/engine/src/flutter/tools/engine_tool/test/gn_utils_test.dart b/engine/src/flutter/tools/engine_tool/test/gn_utils_test.dart index e6ba96b04c..b73de10f19 100644 --- a/engine/src/flutter/tools/engine_tool/test/gn_utils_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/gn_utils_test.dart @@ -2,14 +2,18 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:convert' as convert; import 'dart:io' as io; +import 'package:engine_build_configs/engine_build_configs.dart'; import 'package:engine_repo_tools/engine_repo_tools.dart'; +import 'package:engine_tool/src/build_utils.dart'; import 'package:engine_tool/src/environment.dart'; import 'package:engine_tool/src/gn_utils.dart'; import 'package:litetest/litetest.dart'; +import 'package:platform/platform.dart'; -import 'fixtures.dart'; +import 'fixtures.dart' as fixtures; import 'utils.dart'; void main() { @@ -22,9 +26,19 @@ void main() { return; } + final BuilderConfig linuxTestConfig = BuilderConfig.fromJson( + path: 'ci/builders/linux_test_config.json', + map: convert.jsonDecode(fixtures.testConfig('Linux', Platform.linux)) + as Map, + ); + + final Map configs = { + 'linux_test_config': linuxTestConfig, + }; + final List cannedProcesses = [ CannedProcess((List command) => command.contains('desc'), - stdout: gnDescOutput()), + stdout: fixtures.gnDescOutput()), ]; test('find test targets', () async { @@ -63,4 +77,44 @@ void main() { .length, equals(1)); }); + + test('targetsFromCommandLine respects defaultToAll when false', () async { + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final Environment env = testEnv.environment; + final List builds = runnableBuilds(env, configs); + final Build? build = builds.where( + (Build build) => build.name == 'linux/host_debug', + ).firstOrNull; + final List? selectedTargets = await targetsFromCommandLine( + env, build!, [], + ); + expect(selectedTargets, isNotNull); + expect(selectedTargets, isEmpty); + } finally { + testEnv.cleanup(); + } + }); + + test('targetsFromCommandLine respects defaultToAll when true', () async { + final TestEnvironment testEnv = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, + ); + try { + final Environment env = testEnv.environment; + final List builds = runnableBuilds(env, configs); + final Build? build = builds.where( + (Build build) => build.name == 'linux/host_debug', + ).firstOrNull; + final List? selectedTargets = await targetsFromCommandLine( + env, build!, [], defaultToAll: true, + ); + expect(selectedTargets, isNotNull); + expect(selectedTargets, isNotEmpty); + } finally { + testEnv.cleanup(); + } + }); } diff --git a/engine/src/flutter/tools/engine_tool/test/query_command_test.dart b/engine/src/flutter/tools/engine_tool/test/query_command_test.dart index c00acca4f6..f9da60c6c8 100644 --- a/engine/src/flutter/tools/engine_tool/test/query_command_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/query_command_test.dart @@ -3,11 +3,8 @@ // found in the LICENSE file. import 'dart:convert' as convert; -import 'dart:ffi' as ffi show Abi; -import 'dart:io' as io; import 'package:engine_build_configs/engine_build_configs.dart'; -import 'package:engine_repo_tools/engine_repo_tools.dart'; import 'package:engine_tool/src/commands/command_runner.dart'; import 'package:engine_tool/src/environment.dart'; import 'package:litetest/litetest.dart'; @@ -18,15 +15,6 @@ import 'fixtures.dart' as fixtures; import 'utils.dart'; void main() { - final Engine engine; - try { - engine = Engine.findWithin(); - } catch (e) { - io.stderr.writeln(e); - io.exitCode = 1; - return; - } - final BuilderConfig linuxTestConfig = BuilderConfig.fromJson( path: 'ci/builders/linux_test_config.json', map: convert.jsonDecode(fixtures.testConfig('Linux', Platform.linux)) @@ -62,54 +50,21 @@ void main() { ]; test('query command returns builds for the host platform.', () async { - final TestEnvironment testEnvironment = TestEnvironment(engine, - abi: ffi.Abi.linuxX64, cannedProcesses: cannedProcesses); - final Environment env = testEnvironment.environment; - final ToolCommandRunner runner = ToolCommandRunner( - environment: env, - configs: configs, + final TestEnvironment testEnvironment = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, ); - final int result = await runner.run([ - 'query', - 'builders', - ]); - expect(result, equals(0)); - expect( - stringsFromLogs(env.logger.testLogs), - equals([ - 'Add --verbose to see detailed information about each builder\n', - '\n', - '"linux_test_config" builder:\n', - ' "ci/build_name" config\n', - ' "linux/host_debug" config\n', - ' "linux/android_debug_arm64" config\n', - ' "ci/android_debug_rbe_arm64" config\n', - '"linux_test_config2" builder:\n', - ' "ci/build_name" config\n', - ' "linux/host_debug" config\n', - ' "linux/android_debug_arm64" config\n', - ' "ci/android_debug_rbe_arm64" config\n', - ]), - ); - }); - - test('query command with --builder returns only from the named builder.', - () async { - final TestEnvironment testEnvironment = TestEnvironment(engine, - abi: ffi.Abi.linuxX64, cannedProcesses: cannedProcesses); - final Environment env = testEnvironment.environment; - final ToolCommandRunner runner = ToolCommandRunner( - environment: env, - configs: configs, - ); - final int result = await runner.run([ - 'query', - 'builders', - '--builder', - 'linux_test_config', - ]); - expect(result, equals(0)); - expect( + try { + final Environment env = testEnvironment.environment; + final ToolCommandRunner runner = ToolCommandRunner( + environment: env, + configs: configs, + ); + final int result = await runner.run([ + 'query', + 'builders', + ]); + expect(result, equals(0)); + expect( stringsFromLogs(env.logger.testLogs), equals([ 'Add --verbose to see detailed information about each builder\n', @@ -119,47 +74,100 @@ void main() { ' "linux/host_debug" config\n', ' "linux/android_debug_arm64" config\n', ' "ci/android_debug_rbe_arm64" config\n', - ])); + '"linux_test_config2" builder:\n', + ' "ci/build_name" config\n', + ' "linux/host_debug" config\n', + ' "linux/android_debug_arm64" config\n', + ' "ci/android_debug_rbe_arm64" config\n', + ]), + ); + } finally { + testEnvironment.cleanup(); + } + }); + + test('query command with --builder returns only from the named builder.', + () 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([ + 'query', + 'builders', + '--builder', + 'linux_test_config', + ]); + expect(result, equals(0)); + expect( + stringsFromLogs(env.logger.testLogs), + equals([ + 'Add --verbose to see detailed information about each builder\n', + '\n', + '"linux_test_config" builder:\n', + ' "ci/build_name" config\n', + ' "linux/host_debug" config\n', + ' "linux/android_debug_arm64" config\n', + ' "ci/android_debug_rbe_arm64" config\n', + ])); + } finally { + testEnvironment.cleanup(); + } }); test('query command with --all returns all builds.', () async { - final TestEnvironment testEnvironment = TestEnvironment(engine, - abi: ffi.Abi.linuxX64, cannedProcesses: cannedProcesses); - final Environment env = testEnvironment.environment; - final ToolCommandRunner runner = ToolCommandRunner( - environment: env, - configs: configs, - ); - final int result = await runner.run([ - 'query', - 'builders', - '--all', - ]); - expect(result, equals(0)); - expect( - env.logger.testLogs.length, - equals(30), + 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([ + 'query', + 'builders', + '--all', + ]); + expect(result, equals(0)); + expect( + env.logger.testLogs.length, + equals(30), + ); + } finally { + testEnvironment.cleanup(); + } }); test('query targets', () async { - final TestEnvironment testEnvironment = TestEnvironment(engine, - abi: ffi.Abi.linuxX64, cannedProcesses: cannedProcesses); - final Environment env = testEnvironment.environment; - final ToolCommandRunner runner = ToolCommandRunner( - environment: env, - configs: configs, + final TestEnvironment testEnvironment = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, ); - final int result = await runner.run([ - 'query', - 'targets', - ]); - expect(result, equals(0)); - expect( - env.logger.testLogs.length, - equals(3), - ); - expect(env.logger.testLogs[0].message, - startsWith('//flutter/display_list:display_list_unittests')); + try { + final Environment env = testEnvironment.environment; + final ToolCommandRunner runner = ToolCommandRunner( + environment: env, + configs: configs, + ); + final int result = await runner.run([ + 'query', + 'targets', + ]); + expect(result, equals(0)); + expect( + env.logger.testLogs.length, + equals(4), + ); + expect(env.logger.testLogs[1].message, + startsWith('//flutter/display_list:display_list_unittests')); + } finally { + testEnvironment.cleanup(); + } }); } 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 a08b258822..b8e5788b63 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 @@ -3,11 +3,8 @@ // found in the LICENSE file. import 'dart:convert' as convert; -import 'dart:ffi' as ffi show Abi; -import 'dart:io' as io; import 'package:engine_build_configs/engine_build_configs.dart'; -import 'package:engine_repo_tools/engine_repo_tools.dart'; import 'package:engine_tool/src/commands/command_runner.dart'; import 'package:engine_tool/src/environment.dart'; import 'package:litetest/litetest.dart'; @@ -17,15 +14,6 @@ import 'fixtures.dart' as fixtures; import 'utils.dart'; void main() { - final Engine engine; - try { - engine = Engine.findWithin(); - } catch (e) { - io.stderr.writeln(e); - io.exitCode = 1; - return; - } - final BuilderConfig linuxTestConfig = BuilderConfig.fromJson( path: 'ci/builders/linux_test_config.json', map: convert.jsonDecode(fixtures.testConfig('Linux', Platform.linux)) @@ -57,21 +45,26 @@ void main() { ]; test('test command executes test', () async { - final TestEnvironment testEnvironment = TestEnvironment(engine, - abi: ffi.Abi.linuxX64, cannedProcesses: cannedProcesses); - final Environment env = testEnvironment.environment; - final ToolCommandRunner runner = ToolCommandRunner( - environment: env, - configs: configs, + final TestEnvironment testEnvironment = TestEnvironment.withTestEngine( + cannedProcesses: cannedProcesses, ); - final int result = await runner.run([ - 'test', - '//flutter/display_list:display_list_unittests', - ]); - expect(result, equals(0)); - expect(testEnvironment.processHistory.length, greaterThan(3)); - final int offset = testEnvironment.processHistory.length - 1; - expect(testEnvironment.processHistory[offset].command[0], - endsWith('display_list_unittests')); + try { + final Environment env = testEnvironment.environment; + final ToolCommandRunner runner = ToolCommandRunner( + environment: env, + configs: configs, + ); + final int result = await runner.run([ + 'test', + '//flutter/display_list:display_list_unittests', + ]); + expect(result, equals(0)); + expect(testEnvironment.processHistory.length, greaterThan(3)); + final int offset = testEnvironment.processHistory.length - 1; + expect(testEnvironment.processHistory[offset].command[0], + endsWith('display_list_unittests')); + } finally { + testEnvironment.cleanup(); + } }); } diff --git a/engine/src/flutter/tools/engine_tool/test/utils.dart b/engine/src/flutter/tools/engine_tool/test/utils.dart index 6027e8674e..a6955933c6 100644 --- a/engine/src/flutter/tools/engine_tool/test/utils.dart +++ b/engine/src/flutter/tools/engine_tool/test/utils.dart @@ -8,6 +8,7 @@ import 'dart:io' as io; import 'package:engine_repo_tools/engine_repo_tools.dart'; import 'package:engine_tool/src/environment.dart'; import 'package:engine_tool/src/logger.dart'; +import 'package:path/path.dart' as path; import 'package:platform/platform.dart'; import 'package:process_fakes/process_fakes.dart'; import 'package:process_runner/process_runner.dart'; @@ -79,6 +80,47 @@ class TestEnvironment { ); } + factory TestEnvironment.withTestEngine({ + bool withRbe = false, + ffi.Abi abi = ffi.Abi.linuxX64, + List cannedProcesses = const [], + }) { + final io.Directory rootDir = io.Directory.systemTemp.createTempSync('et'); + final TestEngine engine = TestEngine.createTemp(rootDir: rootDir); + if (withRbe) { + io.Directory(path.join( + engine.srcDir.path, + 'flutter', + 'build', + 'rbe', + )).createSync(recursive: true); + } + // When GN runs, always try to create out/host_debug. + final CannedProcess cannedGn = CannedProcess((List command) { + if (command[0].endsWith('/gn') && !command.contains('desc')) { + io.Directory(path.join( + engine.outDir.path, + 'host_debug', + )).createSync(recursive: true); + return true; + } + return false; + }); + final TestEnvironment testEnvironment = TestEnvironment(engine, + abi: abi, cannedProcesses: cannedProcesses + [cannedGn]); + return testEnvironment; + } + + void cleanup() { + try { + if (environment.engine is TestEngine) { + environment.engine.srcDir.parent.deleteSync(recursive: true); + } + } catch (_) { + // Ignore failure to clean up. + } + } + /// Environment. late final Environment environment;