[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:
stuartmorgan
2021-07-09 16:38:13 -07:00
committed by GitHub
parent ac0eed1ac1
commit 77460f03f2
36 changed files with 428 additions and 250 deletions

View File

@ -6,7 +6,7 @@ import 'dart:async';
import 'dart:io' as io;
import 'package:file/file.dart';
import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';
import 'package:uuid/uuid.dart';
import 'common/core.dart';
@ -21,7 +21,8 @@ class FirebaseTestLabCommand extends PackageLoopingCommand {
FirebaseTestLabCommand(
Directory packagesDir, {
ProcessRunner processRunner = const ProcessRunner(),
}) : super(packagesDir, processRunner: processRunner) {
Platform platform = const LocalPlatform(),
}) : super(packagesDir, processRunner: processRunner, platform: platform) {
argParser.addOption(
'project',
defaultsTo: 'flutter-infra',
@ -29,8 +30,9 @@ class FirebaseTestLabCommand extends PackageLoopingCommand {
);
final String? homeDir = io.Platform.environment['HOME'];
argParser.addOption('service-key',
defaultsTo:
homeDir == null ? null : p.join(homeDir, 'gcloud-service-key.json'),
defaultsTo: homeDir == null
? null
: path.join(homeDir, 'gcloud-service-key.json'),
help: 'The path to the service key for gcloud authentication.\n'
r'If not provided, \$HOME/gcloud-service-key.json will be '
r'assumed if $HOME is set.');
@ -150,7 +152,7 @@ class FirebaseTestLabCommand extends PackageLoopingCommand {
// test file's run.
int resultsCounter = 0;
for (final File test in _findIntegrationTestFiles(package)) {
final String testName = p.relative(test.path, from: package.path);
final String testName = getRelativePosixPath(test, from: package);
print('Testing $testName...');
if (!await _runGradle(androidDirectory, 'app:assembleDebug',
testFile: test)) {
@ -203,7 +205,7 @@ class FirebaseTestLabCommand extends PackageLoopingCommand {
print('Running flutter build apk...');
final String experiment = getStringArg(kEnableExperiment);
final int exitCode = await processRunner.runAndStream(
'flutter',
flutterCommand,
<String>[
'build',
'apk',