Skip to content

Commit 4b8cb3f

Browse files
[tool] Fix dart_test.yaml parsing (#12574)
Use YAML parsing rather than a regex to extract the `test_on` value from package-level dart_test.yaml files. This avoids failing to recognize valid values just because they don't match the exact regex format. Fixes flutter/flutter#191495
1 parent df2ba94 commit 4b8cb3f

2 files changed

Lines changed: 50 additions & 27 deletions

File tree

‎script/tool/lib/src/dart_test_command.dart‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:file/file.dart';
6+
import 'package:yaml/yaml.dart';
67

78
import 'common/core.dart';
89
import 'common/file_filters.dart';
@@ -177,33 +178,33 @@ class DartTestCommand extends PackageLoopingCommand {
177178
if (!testConfig.existsSync()) {
178179
return null;
179180
}
180-
final testOnRegex = RegExp(r'^test_on:\s*([a-z].*[a-z])\s*$');
181-
for (final String line in testConfig.readAsLinesSync()) {
182-
final RegExpMatch? match = testOnRegex.firstMatch(line);
183-
if (match != null) {
184-
final String targetFilter = match.group(1)!;
185-
// test_on lines can be very complex, but in pratice the packages in
186-
// this repo currently only need the ability to require vm or not, so a
187-
// simple one-target directive is all that's supported currently.
188-
// Making it deliberately strict avoids the possibility of accidentally
189-
// skipping vm coverage due to a complex expression that's not handled
190-
// correctly.
191-
switch (targetFilter) {
192-
case 'vm':
193-
return _TestPlatform.vm;
194-
case 'browser':
195-
return _TestPlatform.browser;
196-
default:
197-
printError(
198-
'Unknown "test_on" value: "$targetFilter"\n'
199-
"If this value needs to be supported for this package's tests, "
200-
'please update the repository tooling to support more test_on '
201-
'modes.',
202-
);
203-
throw ToolExit(_exitUnknownTestPlatform);
204-
}
205-
}
181+
final Object? root = loadYaml(testConfig.readAsStringSync());
182+
if (root is! YamlMap) {
183+
return null;
184+
}
185+
final Object? targetFilter = root['test_on'];
186+
if (targetFilter == null || targetFilter is! String) {
187+
return null;
188+
}
189+
// test_on lines can be very complex, but in pratice the packages in
190+
// this repo currently only need the ability to require vm or not, so a
191+
// simple one-target directive is all that's supported currently.
192+
// Making it deliberately strict avoids the possibility of accidentally
193+
// skipping vm coverage due to a complex expression that's not handled
194+
// correctly.
195+
switch (targetFilter) {
196+
case 'vm':
197+
return _TestPlatform.vm;
198+
case 'browser':
199+
return _TestPlatform.browser;
200+
default:
201+
printError(
202+
'Unknown "test_on" value: "$targetFilter"\n'
203+
"If this value needs to be supported for this package's tests, "
204+
'please update the repository tooling to support more test_on '
205+
'modes.',
206+
);
207+
throw ToolExit(_exitUnknownTestPlatform);
206208
}
207-
return null;
208209
}
209210
}

‎script/tool/test/dart_test_command_test.dart‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,28 @@ test_on: vm
597597
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
598598
});
599599

600+
test('skips browser testing regardless of the exact yaml formatting', () async {
601+
final RepositoryPackage package = createFakePackage(
602+
'a_package',
603+
packagesDir,
604+
extraFiles: <String>['test/empty_test.dart'],
605+
);
606+
package.directory.childFile('dart_test.yaml').writeAsStringSync('''
607+
test_on: "vm"
608+
''');
609+
610+
final List<String> output = await runCapturingPrint(runner, <String>[
611+
'dart-test',
612+
'--platform=chrome',
613+
]);
614+
615+
expect(
616+
output,
617+
containsAllInOrder(<Matcher>[contains('Package has opted out of non-vm testing.')]),
618+
);
619+
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
620+
});
621+
600622
test('does not skip running vm in vm mode', () async {
601623
final RepositoryPackage package = createFakePackage(
602624
'a_package',

0 commit comments

Comments
 (0)