mirror of
https://github.com/flutter/packages.git
synced 2025-08-06 08:53:11 +08:00
[flutter_plugin_tools] Make unit tests pass on Windows (#4149)
The purpose of this PR is to make running all unit tests on Windows pass (vs failing a large portion of the tests as currently happens). This does not mean that the commands actually work when run on Windows, or that Windows support is tested, only that it's possible to actually run the tests themselves. This is prep for actually supporting parts of the tool on Windows in future PRs. Major changes: - Make the tests significantly more hermetic: - Make almost all tools take a `Platform` constructor argument that can be used to inject a mock platform to control what OS the command acts like it is running on under test. - Add a path `Context` object to the base command, whose style matches the `Platform`, and use that almost everywhere instead of the top-level `path` functions. - In cases where Posix behavior is always required (such as parsing `git` output), explicitly use the `posix` context object for `path` functions. - Start laying the groundwork for actual Windows support: - Replace all uses of `flutter` as a command with a getter that returns `flutter` or `flutter.bat` as appropriate. - For user messages that include relative paths, use a helper that always uses Posix-style relative paths for consistent output. This bumps the version since quite a few changes have built up, and having a cut point before starting to make more changes to the commands to support Windows seems like a good idea. Part of https://github.com/flutter/flutter/issues/86113
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:io' as io;
|
||||
|
||||
import 'package:args/command_runner.dart';
|
||||
import 'package:file/file.dart';
|
||||
@ -13,8 +13,10 @@ import 'package:flutter_plugin_tools/src/common/package_looping_command.dart';
|
||||
import 'package:flutter_plugin_tools/src/common/process_runner.dart';
|
||||
import 'package:git/git.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../mocks.dart';
|
||||
import '../util.dart';
|
||||
import 'plugin_command_test.mocks.dart';
|
||||
|
||||
@ -36,11 +38,13 @@ const String _warningFile = 'warnings';
|
||||
|
||||
void main() {
|
||||
late FileSystem fileSystem;
|
||||
late MockPlatform mockPlatform;
|
||||
late Directory packagesDir;
|
||||
late Directory thirdPartyPackagesDir;
|
||||
|
||||
setUp(() {
|
||||
fileSystem = MemoryFileSystem();
|
||||
mockPlatform = MockPlatform();
|
||||
packagesDir = createPackagesDirectory(fileSystem: fileSystem);
|
||||
thirdPartyPackagesDir = packagesDir.parent
|
||||
.childDirectory('third_party')
|
||||
@ -69,11 +73,12 @@ void main() {
|
||||
when<String?>(mockProcessResult.stdout as String?)
|
||||
.thenReturn(gitDiffResponse);
|
||||
}
|
||||
return Future<ProcessResult>.value(mockProcessResult);
|
||||
return Future<io.ProcessResult>.value(mockProcessResult);
|
||||
});
|
||||
|
||||
return TestPackageLoopingCommand(
|
||||
packagesDir,
|
||||
platform: mockPlatform,
|
||||
hasLongOutput: hasLongOutput,
|
||||
includeSubpackages: includeSubpackages,
|
||||
failsDuringInit: failsDuringInit,
|
||||
@ -502,7 +507,25 @@ void main() {
|
||||
test('getPackageDescription prints packageDir-relative paths by default',
|
||||
() async {
|
||||
final TestPackageLoopingCommand command =
|
||||
TestPackageLoopingCommand(packagesDir);
|
||||
TestPackageLoopingCommand(packagesDir, platform: mockPlatform);
|
||||
|
||||
expect(
|
||||
command.getPackageDescription(packagesDir.childDirectory('foo')),
|
||||
'foo',
|
||||
);
|
||||
expect(
|
||||
command.getPackageDescription(packagesDir
|
||||
.childDirectory('foo')
|
||||
.childDirectory('bar')
|
||||
.childDirectory('baz')),
|
||||
'foo/bar/baz',
|
||||
);
|
||||
});
|
||||
|
||||
test('getPackageDescription always uses Posix-style paths', () async {
|
||||
mockPlatform.isWindows = true;
|
||||
final TestPackageLoopingCommand command =
|
||||
TestPackageLoopingCommand(packagesDir, platform: mockPlatform);
|
||||
|
||||
expect(
|
||||
command.getPackageDescription(packagesDir.childDirectory('foo')),
|
||||
@ -521,7 +544,7 @@ void main() {
|
||||
'getPackageDescription elides group name in grouped federated plugin structure',
|
||||
() async {
|
||||
final TestPackageLoopingCommand command =
|
||||
TestPackageLoopingCommand(packagesDir);
|
||||
TestPackageLoopingCommand(packagesDir, platform: mockPlatform);
|
||||
|
||||
expect(
|
||||
command.getPackageDescription(packagesDir
|
||||
@ -542,6 +565,7 @@ void main() {
|
||||
class TestPackageLoopingCommand extends PackageLoopingCommand {
|
||||
TestPackageLoopingCommand(
|
||||
Directory packagesDir, {
|
||||
required Platform platform,
|
||||
this.hasLongOutput = true,
|
||||
this.includeSubpackages = false,
|
||||
this.customFailureListHeader,
|
||||
@ -552,7 +576,8 @@ class TestPackageLoopingCommand extends PackageLoopingCommand {
|
||||
this.captureOutput = false,
|
||||
ProcessRunner processRunner = const ProcessRunner(),
|
||||
GitDir? gitDir,
|
||||
}) : super(packagesDir, processRunner: processRunner, gitDir: gitDir);
|
||||
}) : super(packagesDir,
|
||||
processRunner: processRunner, platform: platform, gitDir: gitDir);
|
||||
|
||||
final List<String> checkedPackages = <String>[];
|
||||
final List<String> capturedOutput = <String>[];
|
||||
@ -629,4 +654,4 @@ class TestPackageLoopingCommand extends PackageLoopingCommand {
|
||||
}
|
||||
}
|
||||
|
||||
class MockProcessResult extends Mock implements ProcessResult {}
|
||||
class MockProcessResult extends Mock implements io.ProcessResult {}
|
||||
|
@ -12,8 +12,10 @@ import 'package:flutter_plugin_tools/src/common/process_runner.dart';
|
||||
import 'package:git/git.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../mocks.dart';
|
||||
import '../util.dart';
|
||||
import 'plugin_command_test.mocks.dart';
|
||||
|
||||
@ -22,6 +24,7 @@ void main() {
|
||||
late RecordingProcessRunner processRunner;
|
||||
late CommandRunner<void> runner;
|
||||
late FileSystem fileSystem;
|
||||
late MockPlatform mockPlatform;
|
||||
late Directory packagesDir;
|
||||
late Directory thirdPartyPackagesDir;
|
||||
late List<String> plugins;
|
||||
@ -30,6 +33,7 @@ void main() {
|
||||
|
||||
setUp(() {
|
||||
fileSystem = MemoryFileSystem();
|
||||
mockPlatform = MockPlatform();
|
||||
packagesDir = createPackagesDirectory(fileSystem: fileSystem);
|
||||
thirdPartyPackagesDir = packagesDir.parent
|
||||
.childDirectory('third_party')
|
||||
@ -54,6 +58,7 @@ void main() {
|
||||
plugins,
|
||||
packagesDir,
|
||||
processRunner: processRunner,
|
||||
platform: mockPlatform,
|
||||
gitDir: gitDir,
|
||||
);
|
||||
runner =
|
||||
@ -414,8 +419,10 @@ class SamplePluginCommand extends PluginCommand {
|
||||
this._plugins,
|
||||
Directory packagesDir, {
|
||||
ProcessRunner processRunner = const ProcessRunner(),
|
||||
Platform platform = const LocalPlatform(),
|
||||
GitDir? gitDir,
|
||||
}) : super(packagesDir, processRunner: processRunner, gitDir: gitDir);
|
||||
}) : super(packagesDir,
|
||||
processRunner: processRunner, platform: platform, gitDir: gitDir);
|
||||
|
||||
final List<String> _plugins;
|
||||
|
||||
|
Reference in New Issue
Block a user