Skip to content

Commit 0f3656e

Browse files
authored
[tool] Fix package matching logic for --package-tags filter (flutter#13472)
flutter/packages#12763 introduced a new `--package-tags` flag that can be used to specify which flags to filter a package's tests with. After some investigation, I realized it wasn't working on CI. This is because we were trying to match the package key set in the [YAML file](https://github.com/flutter/packages/blob/13d38e0d97a8e1227379f2887847bc178f22c73a/script/configs/web_dart_unit_test_tags.yaml#L5) (in this case `material_ui`) to [`RepositoryPackage.displayName`](https://github.com/flutter/packages/blob/9919dfd51491a82547033aa1a8aa9aa5a185c61c/script/tool/lib/src/common/repository_package.dart#L41) which removes everything in the path up to the first path component named `packages`. On CI, the repository is checked out into a directory named packages, so `RepositoryPackage.displayName` returned the package's path with a leading `packages/` (e.g. `packages/material_ui`). As a result, it never matched in CI. Tag lookup now uses `_packageTagKey`, which returns the package's path relative to the `packages/` directory that contains it (e.g. `material_ui`), regardless of where the repository is checked out. Work towards flutter#192308 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 6f61327 commit 0f3656e

4 files changed

Lines changed: 105 additions & 11 deletions

File tree

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
# Maps package names to test tag selectors for the web dart unit tests.
1+
# Maps packages to test tag selectors for the web dart unit tests.
22
#
3-
# Packages listed here will only run tests matching the specified tag.
3+
# Packages are identified by their path relative to the packages directory
4+
# (e.g., `foo` or `foo/foo_android`). Packages listed here, and any packages
5+
# nested inside them (such as examples), will only run tests matching the
6+
# specified tag.
47
# Note: Only one tag per package is supported.
58
material_ui: reduced-web-test-set

‎script/tool/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## NEXT
22

33
* Exempts `test_apps/` from requiring version and changelog changes.
4+
* Fixes `dart-test --package-tags` not matching any packages when the
5+
repository is checked out into a directory named `packages`, as it is in CI.
46

57
## 0.14.4+4
68

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:yaml/yaml.dart';
77

88
import 'common/core.dart';
99
import 'common/file_filters.dart';
10+
import 'common/file_utils.dart';
1011
import 'common/output_utils.dart';
1112
import 'common/package_looping_command.dart';
1213
import 'common/plugin_utils.dart';
@@ -45,8 +46,9 @@ class DartTestCommand extends PackageLoopingCommand {
4546
argParser.addOption(
4647
_packageTagsFlag,
4748
help:
48-
'Path to a YAML file that maps package names to tags to run for that '
49-
'package.',
49+
'Path to a YAML file that maps packages to tags to run for that '
50+
'package. Packages are identified by their path relative to the '
51+
'packages directory (e.g., "foo" or "foo/foo_android").',
5052
);
5153
}
5254

@@ -232,18 +234,21 @@ class DartTestCommand extends PackageLoopingCommand {
232234
/// Returns the tag to apply for [package] based on `--package-tags`, or null
233235
/// if none.
234236
///
235-
/// This checks the package's [RepositoryPackage.displayName], and falls back
236-
/// to checking the [RepositoryPackage.displayName] of any enclosing packages
237+
/// This checks the package's [_packageTagKey], and falls back
238+
/// to checking the [_packageTagKey] of any enclosing packages
237239
/// up the directory tree. This allows tagging an entire plugin (including its
238240
/// nested examples) by simply tagging the enclosing package.
239241
String? _getTagForPackage(RepositoryPackage package) {
240242
final Map<String, String>? packageTags = _packageTags;
241243
if (packageTags != null && packageTags.isNotEmpty) {
242-
final candidates = <String>{package.displayName};
243-
RepositoryPackage? enclosing = package.getEnclosingPackage();
244-
while (enclosing != null) {
245-
candidates.add(enclosing.displayName);
246-
enclosing = enclosing.getEnclosingPackage();
244+
final candidates = <String>{};
245+
RepositoryPackage? current = package;
246+
while (current != null) {
247+
final String? key = _packageTagKey(current);
248+
if (key != null) {
249+
candidates.add(key);
250+
}
251+
current = current.getEnclosingPackage();
247252
}
248253

249254
for (final candidate in candidates) {
@@ -256,6 +261,21 @@ class DartTestCommand extends PackageLoopingCommand {
256261
return null;
257262
}
258263

264+
/// Returns the key that a `--package-tags` entry uses to identify [package],
265+
/// or null if [package] is not in a packages directory.
266+
///
267+
/// The key is the package's path relative to the packages directory that
268+
/// contains it, using POSIX separators (e.g., `foo`, `foo/example`, or
269+
/// `foo/foo_android`).
270+
String? _packageTagKey(RepositoryPackage package) {
271+
for (final root in <Directory>[packagesDir, thirdPartyPackagesDir]) {
272+
if (path.isWithin(root.absolute.path, package.directory.absolute.path)) {
273+
return relativePosixPath(package.directory, from: root, platformContext: path);
274+
}
275+
}
276+
return null;
277+
}
278+
259279
/// Returns the required test environment, or null if none is specified.
260280
///
261281
/// Throws if the target is not recognized.

‎script/tool/test/dart_test_command_test.dart‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:args/command_runner.dart';
66
import 'package:file/file.dart';
7+
import 'package:file/memory.dart';
78
import 'package:flutter_plugin_tools/src/common/core.dart';
89
import 'package:flutter_plugin_tools/src/common/plugin_utils.dart';
910
import 'package:flutter_plugin_tools/src/dart_test_command.dart';
@@ -802,6 +803,74 @@ a: reduced-web-test-set
802803
);
803804
});
804805

806+
test('package-tags flag matches packages when the repository directory is '
807+
'named "packages"', () async {
808+
// CI checks the repository out into a directory named "packages", so
809+
// package paths look like .../packages/packages/<package>.
810+
final fileSystem = MemoryFileSystem();
811+
fileSystem.currentDirectory = fileSystem.directory('/b/packages')
812+
..createSync(recursive: true);
813+
final Directory nestedPackagesDir = createPackagesDirectory(fileSystem);
814+
final nestedProcessRunner = RecordingProcessRunner();
815+
final nestedRunner = CommandRunner<void>('test_test', 'Test for $DartTestCommand');
816+
nestedRunner.addCommand(
817+
DartTestCommand(
818+
nestedPackagesDir,
819+
processRunner: nestedProcessRunner,
820+
platform: mockPlatform,
821+
gitDir: createForwardingMockGitDir(
822+
packagesDir: nestedPackagesDir,
823+
processRunner: RecordingProcessRunner(),
824+
),
825+
),
826+
);
827+
828+
final RepositoryPackage package = createFakePackage(
829+
'a',
830+
nestedPackagesDir,
831+
isFlutter: true,
832+
extraFiles: <String>['test/empty_test.dart', 'example/test/empty_test.dart'],
833+
);
834+
final RepositoryPackage nestedApp = createFakePackage(
835+
'nested_app',
836+
package.directory.childDirectory('test_apps'),
837+
isFlutter: true,
838+
examples: <String>[],
839+
extraFiles: <String>['test/empty_test.dart'],
840+
);
841+
842+
final File configFile = fileSystem.file('/config.yaml');
843+
configFile.writeAsStringSync('''
844+
a: reduced-web-test-set
845+
''');
846+
847+
await runCapturingPrint(nestedRunner, <String>[
848+
'dart-test',
849+
'--package-tags=${configFile.path}',
850+
]);
851+
852+
expect(
853+
nestedProcessRunner.recordedCalls,
854+
orderedEquals(<ProcessCall>[
855+
ProcessCall(getFlutterCommand(mockPlatform), const <String>[
856+
'test',
857+
'--color',
858+
'--tags=reduced-web-test-set',
859+
], package.path),
860+
ProcessCall(getFlutterCommand(mockPlatform), const <String>[
861+
'test',
862+
'--color',
863+
'--tags=reduced-web-test-set',
864+
], getExampleDir(package).path),
865+
ProcessCall(getFlutterCommand(mockPlatform), const <String>[
866+
'test',
867+
'--color',
868+
'--tags=reduced-web-test-set',
869+
], nestedApp.path),
870+
]),
871+
);
872+
});
873+
805874
test('package-tags flag fails if configuration file does not exist', () async {
806875
createFakePlugin('a', packagesDir, extraFiles: <String>['test/empty_test.dart']);
807876

0 commit comments

Comments
 (0)