Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
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.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(TestDartFixes(packagesDir))
..addCommand(UpdateDependencyCommand(packagesDir))
..addCommand(UpdateExcerptsCommand(packagesDir))
..addCommand(UpdateMinSdkCommand(packagesDir))
Expand Down
147 changes: 147 additions & 0 deletions script/tool/lib/src/test_dart_fixes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// 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 'dart:io';

import 'package:file/file.dart';
import 'package:io/io.dart' as io;
import 'package:path/path.dart' as p;

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

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

@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.includeAllSubpackages;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could sub-packages ever have their own dart fixes? If not we only need to look at top-level packages.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's possible but doesn't happen in any of our packages. I'll set it to .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']);
}

late final PackageResult result;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually need the late keyword? I would expect it to work without it (with better safety).

try {
final int statusCode = await _runDartFixTests(package, testDirectory);
if (statusCode != 0) {
throw Exception('Status code $statusCode');
}
result = PackageResult.success();
} catch (error) {
result = PackageResult.fail(['Dart fix tests failed: $error']);
}
if (testDirectory.existsSync()) {
await testDirectory.delete(recursive: true);
}
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 testTempDirectory = await fileSystem.systemTempDirectory.createTemp();

// Copy from `test_fixes/` to the temp directory.
await io.copyPath(package.dartFixTestDirectory.path, testTempDirectory.path);

// The pubspec.yaml file to create.
final File targetPubspecFile = fileSystem.file(p.join(testTempDirectory.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 testTempDirectory;
}

/// Run the dart fix tests for the package in the given temporary directory.
///
/// Resolves with the status code of the command.
Future<int> _runDartFixTests(RepositoryPackage package, Directory testDirectory) async {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package is never used.

// Run dart pub get in the temp directory to set it up.
final int pubGetStatusCode = await _runProcess('dart', <String>[
'pub',
'get',
], workingDirectory: testDirectory);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have runPubGet to abstract this, in pub_utils.dart. You just need to construct a RepositoryPackage from the test directory.


if (pubGetStatusCode != 0) {
return pubGetStatusCode;
}

// Run dart fix --compare-to-golden in the temp directory.
return _runProcess('dart', <String>[
'fix',
'--compare-to-golden',
], workingDirectory: testDirectory);
}

Future<int> _runProcess(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These methods should be removed in favor of the command taking a ProcessRunner, and using processRunner.runAndStream. See dart_test_command.dart for an example (or almost any other command).

String command,
List<String> arguments, {
Directory? workingDirectory,
}) async {
final Process process = await _streamOutput(
processRunner.start(command, arguments, workingDirectory: workingDirectory),
);
return process.exitCode;
}

static Future<Process> _streamOutput(Future<Process> processFuture) async {
final Process process = await processFuture;
unawaited(stdout.addStream(process.stdout));
unawaited(stderr.addStream(process.stderr));
return process;
}
}
1 change: 1 addition & 0 deletions script/tool/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
glob: ^2.1.3
http: ^1.0.0
http_multi_server: ^3.0.1
io: ^1.0.5
meta: ^1.10.0
path: ^1.8.3
platform: ^3.0.2
Expand Down
Loading