Skip to content

Commit c758345

Browse files
justinmcvictorsanni
authored andcommitted
[material_ui, cupertino_ui, go_router] Command to test dart fixes (flutter#12390)
This PR creates a new repo-level CI command to run the dart fix tests in relevant packages. Before this PR, go_router had its own script to run its dart fix tests, and material_ui and cupertino_ui had dart fix tests that were not being run. After this PR, all of the above are run via the new command. Part of flutter/flutter#182568
1 parent 14a44e4 commit c758345

6 files changed

Lines changed: 272 additions & 101 deletions

File tree

‎.ci/targets/dart_unit_tests.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ tasks:
1111
- name: Dart unit tests - pathified
1212
script: .ci/scripts/dart_unit_tests_pathified.sh
1313
args: ["--platform=vm"]
14+
- name: Dart fix tests
15+
script: .ci/scripts/tool_runner.sh
16+
args: ["test-dart-fixes"]

‎packages/go_router/tool/run_tests.dart‎

Lines changed: 0 additions & 101 deletions
This file was deleted.

‎script/tool/lib/src/common/repository_package.dart‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class RepositoryPackage {
7474
/// The test directory containing the package's Dart tests.
7575
Directory get testDirectory => directory.childDirectory('test');
7676

77+
/// The test directory containing the tests for the package's dart fixes.
78+
Directory get dartFixTestDirectory => directory.childDirectory('test_fixes');
79+
7780
/// The path to the script that is run by the `custom-test` command.
7881
File get customTestScript => directory.childDirectory('tool').childFile('run_tests.dart');
7982

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'podspec_check_command.dart';
3030
import 'publish_check_command.dart';
3131
import 'publish_command.dart';
3232
import 'remove_dev_dependencies_command.dart';
33+
import 'test_dart_fixes_command.dart';
3334
import 'update_dependency_command.dart';
3435
import 'update_excerpts_command.dart';
3536
import 'update_min_sdk_command.dart';
@@ -75,6 +76,7 @@ void main(List<String> args) {
7576
..addCommand(PublishCheckCommand(packagesDir))
7677
..addCommand(PublishCommand(packagesDir))
7778
..addCommand(RemoveDevDependenciesCommand(packagesDir))
79+
..addCommand(TestDartFixesCommand(packagesDir))
7880
..addCommand(UpdateDependencyCommand(packagesDir))
7981
..addCommand(UpdateExcerptsCommand(packagesDir))
8082
..addCommand(UpdateMinSdkCommand(packagesDir))
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:file/file.dart';
8+
import 'package:meta/meta.dart';
9+
import 'package:path/path.dart' as p;
10+
11+
import 'common/file_filters.dart';
12+
import 'common/package_looping_command.dart';
13+
import 'common/pub_utils.dart';
14+
import 'common/repository_package.dart';
15+
16+
/// A command to run dart fix tests for packages that have a test_fixes
17+
/// directory.
18+
class TestDartFixesCommand extends PackageLoopingCommand {
19+
/// Creates an instance of the test dart fixes command.
20+
TestDartFixesCommand(super.packagesDir, {super.processRunner, super.platform, super.gitDir});
21+
22+
/// A map of the test directory used for each package passed to runForPackage.
23+
@visibleForTesting
24+
final testDirectories = <String, Directory>{};
25+
26+
@override
27+
final String name = 'test-dart-fixes';
28+
29+
@override
30+
final String description =
31+
'Runs the Dart fix tests for all packages.\n\n'
32+
'This command requires "flutter" to be in your path.';
33+
34+
@override
35+
bool shouldIgnoreFile(String path) {
36+
return isRepoLevelNonCodeImpactingFile(path) ||
37+
isNativeCodeFile(path) ||
38+
isPackageSupportFile(path);
39+
}
40+
41+
@override
42+
Future<PackageResult> runForPackage(RepositoryPackage package) async {
43+
// Only run for packages that have a fix_tests directory.
44+
if (!package.dartFixTestDirectory.existsSync()) {
45+
final String localDartFixPath = p.relative(
46+
package.dartFixTestDirectory.path,
47+
from: package.directory.path,
48+
);
49+
return PackageResult.skip('No $localDartFixPath directory.');
50+
}
51+
52+
final Directory testDirectory;
53+
try {
54+
testDirectory = await _createTestDirectory(package);
55+
} catch (error) {
56+
return PackageResult.fail(['Failed to create temporary test directory: $error']);
57+
}
58+
59+
PackageResult result;
60+
try {
61+
final int exitCode = await _runDartFixTests(testDirectory);
62+
result = exitCode == 0
63+
? PackageResult.success()
64+
: PackageResult.fail(['Dart fix tests failed.']);
65+
} catch (error) {
66+
result = PackageResult.fail(['Dart fix tests failed: $error']);
67+
}
68+
if (testDirectory.existsSync()) {
69+
await testDirectory.delete(recursive: true);
70+
}
71+
testDirectories[package.displayName] = testDirectory;
72+
return result;
73+
}
74+
75+
/// Create and prepare a temporary directory in which to run the dart fix
76+
/// tests.
77+
///
78+
/// It is the responsibility of the caller to delete this directory and its
79+
/// contents when done.
80+
static Future<Directory> _createTestDirectory(RepositoryPackage package) async {
81+
final FileSystem fileSystem = package.directory.fileSystem;
82+
final Directory testDirectory = await fileSystem.systemTempDirectory.createTemp();
83+
84+
// Copy from `test_fixes/` to the temp directory.
85+
for (final FileSystemEntity entity in package.dartFixTestDirectory.listSync(recursive: true)) {
86+
final String relativePath = fileSystem.path.relative(
87+
entity.path,
88+
from: package.dartFixTestDirectory.path,
89+
);
90+
final String destPath = fileSystem.path.join(testDirectory.path, relativePath);
91+
if (entity is Directory) {
92+
fileSystem.directory(destPath).createSync(recursive: true);
93+
} else if (entity is File) {
94+
fileSystem.file(destPath).parent.createSync(recursive: true);
95+
entity.copySync(destPath);
96+
}
97+
}
98+
99+
// The pubspec.yaml file to create.
100+
final File targetPubspecFile = fileSystem.file(
101+
fileSystem.path.join(testDirectory.path, 'pubspec.yaml'),
102+
);
103+
104+
final targetYaml =
105+
'''
106+
name: test_fixes
107+
publish_to: "none"
108+
version: 1.0.0
109+
110+
environment:
111+
sdk: ">=2.18.0 <4.0.0"
112+
flutter: ">=3.3.0"
113+
114+
dependencies:
115+
flutter:
116+
sdk: flutter
117+
${package.directory.basename}:
118+
path: ${package.directory.path}
119+
''';
120+
121+
await targetPubspecFile.writeAsString(targetYaml);
122+
return testDirectory;
123+
}
124+
125+
/// Run the dart fix tests for the package in the given temporary directory.
126+
///
127+
/// Resolves with the exit code of the command.
128+
Future<int> _runDartFixTests(Directory testDirectory) async {
129+
// Run flutter pub get in the temp directory to set it up.
130+
final bool success = await runPubGet(RepositoryPackage(testDirectory), processRunner, platform);
131+
132+
if (!success) {
133+
throw Exception('Pub get failed.');
134+
}
135+
136+
// Run dart fix --compare-to-golden in the temp directory.
137+
return processRunner.runAndStream('dart', <String>[
138+
'fix',
139+
'--compare-to-golden',
140+
], workingDir: testDirectory);
141+
}
142+
}

0 commit comments

Comments
 (0)