Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .ci/targets/dart_unit_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ tasks:
- name: Dart unit tests - pathified
script: .ci/scripts/dart_unit_tests_pathified.sh
args: ["--platform=vm"]
- name: Dart fix tests
script: .ci/scripts/tool_runner.sh
args: ["test-dart-fixes"]
101 changes: 0 additions & 101 deletions packages/go_router/tool/run_tests.dart

This file was deleted.

3 changes: 3 additions & 0 deletions script/tool/lib/src/common/repository_package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class RepositoryPackage {
/// The test directory containing the package's Dart tests.
Directory get testDirectory => directory.childDirectory('test');

/// The test directory containing the tests for the package's dart fixes.
Directory get dartFixTestDirectory => directory.childDirectory('test_fixes');

/// The path to the script that is run by the `custom-test` command.
File get customTestScript => directory.childDirectory('tool').childFile('run_tests.dart');

Expand Down
2 changes: 2 additions & 0 deletions script/tool/lib/src/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'podspec_check_command.dart';
import 'publish_check_command.dart';
import 'publish_command.dart';
import 'remove_dev_dependencies_command.dart';
import 'test_dart_fixes_command.dart';
import 'update_dependency_command.dart';
import 'update_excerpts_command.dart';
import 'update_min_sdk_command.dart';
Expand Down Expand Up @@ -75,6 +76,7 @@ void main(List<String> args) {
..addCommand(PublishCheckCommand(packagesDir))
..addCommand(PublishCommand(packagesDir))
..addCommand(RemoveDevDependenciesCommand(packagesDir))
..addCommand(TestDartFixesCommand(packagesDir))
..addCommand(UpdateDependencyCommand(packagesDir))
..addCommand(UpdateExcerptsCommand(packagesDir))
..addCommand(UpdateMinSdkCommand(packagesDir))
Expand Down
146 changes: 146 additions & 0 deletions script/tool/lib/src/test_dart_fixes_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Copyright 2013 The Flutter Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:file/file.dart';
import 'package:meta/meta.dart';

import 'common/file_filters.dart';
import 'common/package_looping_command.dart';
import 'common/pub_utils.dart';
import 'common/repository_package.dart';

/// A command to run dart fix tests for packages that have a test_fixes
/// directory.
class TestDartFixesCommand extends PackageLoopingCommand {
/// Creates an instance of the test dart fixes command.
TestDartFixesCommand(super.packagesDir, {super.processRunner, super.platform, super.gitDir});

/// A map of the test directory used for each package passed to runForPackage.
@visibleForTesting
final testDirectories = <String, Directory>{};

@override
final String name = 'test-dart-fixes';

@override
List<String> get aliases => <String>[];

@override
final String description =
'Runs the Dart fix tests for all packages.\n\n'
'This command requires "flutter" to be in your path.';

@override
PackageLoopingType get packageLoopingType => PackageLoopingType.topLevelOnly;

@override
bool shouldIgnoreFile(String path) {
return isRepoLevelNonCodeImpactingFile(path) ||
isNativeCodeFile(path) ||
isPackageSupportFile(path);
}

@override
Future<PackageResult> runForPackage(RepositoryPackage package) async {
// Only run for packages that have a fix_tests directory.
if (!package.dartFixTestDirectory.existsSync()) {
return PackageResult.skip('No ${package.dartFixTestDirectory} directory.');
}

final Directory testDirectory;
try {
testDirectory = await _createTestDirectory(package);
} catch (error) {
return PackageResult.fail(['Failed to create temporary test directory: $error']);
}

PackageResult result;
try {
final bool success = await _runDartFixTests(testDirectory);
if (!success) {
throw Exception('Failed to run dart fix tests.');
}
result = PackageResult.success();
} catch (error) {
result = PackageResult.fail(['Dart fix tests failed: $error']);
}
if (testDirectory.existsSync()) {
await testDirectory.delete(recursive: true);
}
testDirectories[package.displayName] = testDirectory;
return result;
}

/// Create and prepare a temporary directory in which to run the dart fix
/// tests.
///
/// It is the responsibility of the caller to delete this directory and its
/// contents when done.
static Future<Directory> _createTestDirectory(RepositoryPackage package) async {
final FileSystem fileSystem = package.directory.fileSystem;
final Directory testDirectory = await fileSystem.systemTempDirectory.createTemp();

// Copy from `test_fixes/` to the temp directory.
for (final FileSystemEntity entity in package.dartFixTestDirectory.listSync(recursive: true)) {
final String relativePath = fileSystem.path.relative(
entity.path,
from: package.dartFixTestDirectory.path,
);
final String destPath = fileSystem.path.join(testDirectory.path, relativePath);
if (entity is Directory) {
fileSystem.directory(destPath).createSync(recursive: true);
} else if (entity is File) {
fileSystem.file(destPath).parent.createSync(recursive: true);
entity.copySync(destPath);
}
}

// The pubspec.yaml file to create.
final File targetPubspecFile = fileSystem.file(
fileSystem.path.join(testDirectory.path, 'pubspec.yaml'),
);

final targetYaml =
'''
name: test_fixes
publish_to: "none"
version: 1.0.0

environment:
sdk: ">=2.18.0 <4.0.0"
flutter: ">=3.3.0"

dependencies:
flutter:
sdk: flutter
${package.directory.basename}:
path: ${package.directory.path}
''';

await targetPubspecFile.writeAsString(targetYaml);
return testDirectory;
}

/// Run the dart fix tests for the package in the given temporary directory.
///
/// Resolves with the status code of the command.
Future<bool> _runDartFixTests(Directory testDirectory) async {
// Run flutter pub get in the temp directory to set it up.
final bool success = await runPubGet(RepositoryPackage(testDirectory), processRunner, platform);

if (!success) {
return success;
}

// Run dart fix --compare-to-golden in the temp directory.
final int exitCode = await processRunner.runAndStream('dart', <String>[
'fix',
'--compare-to-golden',
], workingDir: testDirectory);

return exitCode == 0;
}
}
Loading
Loading