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.
This commit is contained in:
Matan Lurey
2024-10-04 13:55:27 -07:00
committed by GitHub
parent 7f99da5b92
commit 9f49c8f4e3
7 changed files with 142 additions and 2 deletions

View File

@@ -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,
[

View File

@@ -46,6 +46,7 @@ template("gen_dartcli_call") {
[
"cwd",
"output",
"metadata",
"testonly",
"visibility",
])

View File

@@ -60,6 +60,7 @@ template("gen_executable_call") {
args = call_args
forward_variables_from(invoker,
[
"metadata",
"testonly",
"visibility",
])

View File

@@ -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,
};
}

View File

@@ -226,6 +226,44 @@ extension type const JsonObject(Map<String, Object?> _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<String, Object?>) {
_error(InvalidTypeJsonReadException(
this,
key,
expected: Map<String, Object?>,
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

View File

@@ -126,4 +126,41 @@ void main() {
isTrue,
);
});
test('parses a dart_test action as an executable', () async {
final testEnv = TestEnvironment.withTestEngine(
cannedProcesses: [
CannedProcess(
(List<String> 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',
),
);
});
}

View File

@@ -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<InvalidTypeJsonReadException>()),
);
});
});
group('JsonObject.map', () {
test('returns multiple fields', () {
final (String name, int age, bool isStudent) =