Skip to content

Commit c4242e1

Browse files
authored
Modify test_and_fix to avoid traversing into .dart_tool directories (#568)
1 parent 84445fe commit c4242e1

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

.github/workflows/flutter_packages.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ jobs:
4444
env:
4545
GH_TOKEN: ${{ github.token }}
4646
run: |
47-
# Find all directories containing pubspec.yaml in packages and examples.
48-
ALL_DIRS=$(find packages examples -name pubspec.yaml -not -path "*/.dart_tool/*" -exec dirname {} \;)
47+
# Find all directories containing pubspec.yaml in packages, tools, and examples.
48+
ALL_DIRS=$(find packages examples tool -name pubspec.yaml -not -path "*/.dart_tool/*" -exec dirname {} \;)
4949
5050
# Get the list of changed files. The method depends on the event type.
5151
if [[ "${{ github.event_name }}" == "pull_request" ]]; then

tool/test_and_fix/lib/test_and_fix.dart

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,31 +121,56 @@ class TestAndFix {
121121
bool all = false,
122122
}) async {
123123
final projects = <Directory>[];
124-
await for (final FileSystemEntity entity in root.list(recursive: true)) {
125-
if (entity is! File || path.basename(entity.path) != 'pubspec.yaml') {
126-
continue;
127-
}
128-
final File pubspec = entity;
129-
final Directory projectDir = pubspec.parent;
130-
if (isProjectAllowed(projectDir, all: all)) {
131-
projects.add(projectDir);
124+
await _findProjectsRecursive(root, projects, all: all);
125+
return projects;
126+
}
127+
128+
Future<void> _findProjectsRecursive(
129+
Directory dir,
130+
List<Directory> projects, {
131+
required bool all,
132+
}) async {
133+
final Set<String> excludedDirs = _getExcludedDirectories(all: all);
134+
try {
135+
await for (final FileSystemEntity entity in dir.list(
136+
followLinks: false,
137+
)) {
138+
if (entity is File && fs.path.basename(entity.path) == 'pubspec.yaml') {
139+
final Directory projectDir = entity.parent;
140+
if (isProjectAllowed(projectDir, all: all)) {
141+
projects.add(projectDir);
142+
}
143+
} else if (entity is Directory) {
144+
if (!excludedDirs.contains(fs.path.basename(entity.path))) {
145+
await _findProjectsRecursive(entity, projects, all: all);
146+
}
147+
}
132148
}
149+
} on FileSystemException catch (exception) {
150+
print(
151+
'Warning: Failed to list directory contents while searching for '
152+
'projects: $exception',
153+
);
133154
}
134-
return projects;
135155
}
136156

137-
bool isProjectAllowed(Directory projectPath, {bool all = false}) {
138-
// Skip the things that we really don't ever want to traverse, but skip the
139-
// non-essential packages unless --all is specified.
140-
final excluded = [
157+
Set<String> _getExcludedDirectories({required bool all}) {
158+
return {
141159
'.dart_tool',
142160
'ephemeral',
143161
'firebase_core',
144162
'build',
145163
if (!all) 'spikes',
146164
if (!all) 'fix_copyright',
165+
if (!all) 'release',
147166
if (!all) 'test_and_fix',
148-
];
167+
};
168+
}
169+
170+
bool isProjectAllowed(Directory projectPath, {bool all = false}) {
171+
// Skip the things that we really don't ever want to traverse, but skip the
172+
// non-essential packages unless --all is specified.
173+
final Set<String> excluded = _getExcludedDirectories(all: all);
149174
final List<String> components = fs.path.split(projectPath.path);
150175
for (final exclude in excluded) {
151176
if (components.contains(exclude)) {

tool/test_and_fix/test/project_discovery_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ void main() {
6464
expect(projects, isEmpty);
6565
});
6666

67+
test('ignores nested projects in excluded directories', () async {
68+
final excluded = ['.dart_tool', 'build', 'ephemeral', 'firebase_core'];
69+
70+
for (final exclude in excluded) {
71+
final Directory project = fs.directory(
72+
path.join(root.path, exclude, 'nested_project'),
73+
)..createSync(recursive: true);
74+
fs
75+
.file(path.join(project.path, 'pubspec.yaml'))
76+
.writeAsStringSync('sdk: flutter');
77+
}
78+
79+
final List<Directory> projects = await testAndFix.findProjects(root);
80+
81+
expect(projects, isEmpty);
82+
});
83+
6784
test('ignores some excluded directories with --all', () async {
6885
final excluded = ['.dart_tool', 'ephemeral', 'firebase_core', 'build'];
6986

0 commit comments

Comments
 (0)