From 9f49c8f4e3c14c9ed8132df7534ae1abf9285620 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Fri, 4 Oct 2024 13:55:27 -0700 Subject: [PATCH] Add support for `et test //flutter/path/to/dart_test` (flutter/engine#55638) Closes https://github.com/flutter/flutter/issues/147013. Closes https://github.com/flutter/flutter/issues/147071. /cc @reidbaker who I know wants to do this for the Java rules soon TM. /cc @jtmcdole, @zanderso for visibility. --- .../flutter/build/dart/internal/dart_test.gni | 3 ++ .../build/dart/internal/gen_dartcli_call.gni | 1 + .../dart/internal/gen_executable_call.gni | 1 + .../flutter/tools/engine_tool/lib/src/gn.dart | 37 +++++++++++++++++- .../tools/engine_tool/lib/src/typed_json.dart | 38 +++++++++++++++++++ .../test/external_tools/gn_test.dart | 37 ++++++++++++++++++ .../engine_tool/test/typed_json_test.dart | 27 +++++++++++++ 7 files changed, 142 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/build/dart/internal/dart_test.gni b/engine/src/flutter/build/dart/internal/dart_test.gni index 3e461e3105..6185e49ab3 100644 --- a/engine/src/flutter/build/dart/internal/dart_test.gni +++ b/engine/src/flutter/build/dart/internal/dart_test.gni @@ -27,6 +27,9 @@ template("dart_test") { invoker.main_dart, ] cwd = rebase_path(cwd) + metadata = { + action_type = [ "dart_test" ] + } testonly = true forward_variables_from(invoker, [ diff --git a/engine/src/flutter/build/dart/internal/gen_dartcli_call.gni b/engine/src/flutter/build/dart/internal/gen_dartcli_call.gni index 2745bc71a8..08b03612fc 100644 --- a/engine/src/flutter/build/dart/internal/gen_dartcli_call.gni +++ b/engine/src/flutter/build/dart/internal/gen_dartcli_call.gni @@ -46,6 +46,7 @@ template("gen_dartcli_call") { [ "cwd", "output", + "metadata", "testonly", "visibility", ]) diff --git a/engine/src/flutter/build/dart/internal/gen_executable_call.gni b/engine/src/flutter/build/dart/internal/gen_executable_call.gni index 451a73dae3..aaa92c8bea 100644 --- a/engine/src/flutter/build/dart/internal/gen_executable_call.gni +++ b/engine/src/flutter/build/dart/internal/gen_executable_call.gni @@ -60,6 +60,7 @@ template("gen_executable_call") { args = call_args forward_variables_from(invoker, [ + "metadata", "testonly", "visibility", ]) diff --git a/engine/src/flutter/tools/engine_tool/lib/src/gn.dart b/engine/src/flutter/tools/engine_tool/lib/src/gn.dart index fe73076f32..c2e6b71af5 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/gn.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/gn.dart @@ -123,6 +123,36 @@ sealed class BuildTarget { required this.testOnly, }); + factory BuildTarget._parseFromAction( + String label, { + required bool testOnly, + required JsonObject json, + }) { + final metadata = json.objectOrNull('metadata'); + if (metadata != null) { + final actionTypes = metadata.stringListOrNull('action_type'); + if (actionTypes != null && actionTypes.contains('dart_test')) { + final String executable; + final outputs = json.stringListOrNull('outputs'); + if (outputs == null || outputs.isEmpty) { + throw StateError('Expected at least one output for $label'); + } + + // Remove the leading // from the path. + executable = outputs.first.substring(2); + return ExecutableBuildTarget( + label: Label.parseGn(label), + testOnly: testOnly, + executable: executable, + ); + } + } + return ActionBuildTarget( + label: Label.parseGn(label), + testOnly: testOnly, + ); + } + /// Returns a build target from JSON originating from `gn desc --format=json`. /// /// If the JSON is not a supported build target, returns `null`. @@ -145,8 +175,11 @@ sealed class BuildTarget { label: Label.parseGn(label), testOnly: testOnly, ), - 'action' => - ActionBuildTarget(label: Label.parseGn(label), testOnly: testOnly), + 'action' => BuildTarget._parseFromAction( + label, + testOnly: testOnly, + json: json, + ), _ => null, }; } diff --git a/engine/src/flutter/tools/engine_tool/lib/src/typed_json.dart b/engine/src/flutter/tools/engine_tool/lib/src/typed_json.dart index ea749fd687..7a8d47598e 100644 --- a/engine/src/flutter/tools/engine_tool/lib/src/typed_json.dart +++ b/engine/src/flutter/tools/engine_tool/lib/src/typed_json.dart @@ -226,6 +226,44 @@ extension type const JsonObject(Map _object) { throw e; } + JsonObject? _getObjectOrNull(String key) { + final Object? value = _object[key]; + if (value == null && !_object.containsKey(key)) { + return null; + } else if (value is! Map) { + _error(InvalidTypeJsonReadException( + this, + key, + expected: Map, + actual: value.runtimeType, + )); + return const JsonObject({}); + } else { + return JsonObject(value); + } + } + + JsonObject _getObject(String key) { + final JsonObject? result = _getObjectOrNull(key); + if (result == null) { + _error(MissingKeyJsonReadException(this, key)); + return const JsonObject({}); + } + return result; + } + + /// Returns the value at the given [key] as a [JsonObject]. + /// + /// Throws a [JsonReadException] if the value is not found or cannot be cast. + JsonObject object(String key) => _getObject(key); + + /// Returns the value at the given [key] as a [JsonObject]. + /// + /// If the value is not found, returns `null`. + /// + /// Throws a [JsonReadException] if the value cannot be cast. + JsonObject? objectOrNull(String key) => _getObjectOrNull(key); + /// Returns the result of applying the given [mapper] to this JSON object. /// /// Any exception that otherwise would be thrown by the mapper is caught diff --git a/engine/src/flutter/tools/engine_tool/test/external_tools/gn_test.dart b/engine/src/flutter/tools/engine_tool/test/external_tools/gn_test.dart index d0b7a34b1f..efd1a8c463 100644 --- a/engine/src/flutter/tools/engine_tool/test/external_tools/gn_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/external_tools/gn_test.dart @@ -126,4 +126,41 @@ void main() { isTrue, ); }); + + test('parses a dart_test action as an executable', () async { + final testEnv = TestEnvironment.withTestEngine( + cannedProcesses: [ + CannedProcess( + (List command) => command.contains('desc'), + stdout: ''' + { + "//foo/bar:baz_test": { + "outputs": ["//out/host_debug/foo/bar/baz_test"], + "testonly": true, + "type": "action", + "metadata": { + "action_type": ["dart_test"] + } + } + } + ''', + ), + ], + ); + addTearDown(testEnv.cleanup); + + final gn = Gn.fromEnvironment(testEnv.environment); + final targets = await gn.desc('out/Release', TargetPattern('//foo', 'bar')); + expect(targets, hasLength(1)); + + final testTarget = targets.single; + expect( + testTarget, + ExecutableBuildTarget( + label: Label('//foo/bar', 'baz_test'), + testOnly: true, + executable: 'out/host_debug/foo/bar/baz_test', + ), + ); + }); } diff --git a/engine/src/flutter/tools/engine_tool/test/typed_json_test.dart b/engine/src/flutter/tools/engine_tool/test/typed_json_test.dart index ca2d2c9634..2a15a16488 100644 --- a/engine/src/flutter/tools/engine_tool/test/typed_json_test.dart +++ b/engine/src/flutter/tools/engine_tool/test/typed_json_test.dart @@ -188,6 +188,33 @@ void main() { }); }); + group('JsonObject.objectOrNull', () { + test('returns object value', () { + const jsonObject = JsonObject({ + 'key': {'nestedKey': 'nestedValue'} + }); + expect( + jsonObject.objectOrNull('key'), + const JsonObject({'nestedKey': 'nestedValue'}), + ); + }); + + test('returns null due to missing key', () { + const jsonObject = JsonObject({ + 'key': {'nestedKey': 'nestedValue'} + }); + expect(jsonObject.objectOrNull('missing'), isNull); + }); + + test('throws due to wrong type', () { + const jsonObject = JsonObject({'key': 'value'}); + expect( + () => jsonObject.objectOrNull('key'), + throwsA(const isInstanceOf()), + ); + }); + }); + group('JsonObject.map', () { test('returns multiple fields', () { final (String name, int age, bool isStudent) =