Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[flutter_plugin_tools] Make unit tests pass on Windows #4149

Merged
merged 19 commits into from
Jul 9, 2021
Merged
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
13 changes: 7 additions & 6 deletions script/tool/lib/src/analyze_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:async';

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

import 'common/core.dart';
import 'common/package_looping_command.dart';
Expand All @@ -19,7 +19,8 @@ class AnalyzeCommand extends PackageLoopingCommand {
AnalyzeCommand(
Directory packagesDir, {
ProcessRunner processRunner = const ProcessRunner(),
}) : super(packagesDir, processRunner: processRunner) {
Platform platform = const LocalPlatform(),
}) : super(packagesDir, processRunner: processRunner, platform: platform) {
argParser.addMultiOption(_customAnalysisFlag,
help:
'Directories (comma separated) that are allowed to have their own analysis options.',
Expand Down Expand Up @@ -57,9 +58,8 @@ class AnalyzeCommand extends PackageLoopingCommand {

final bool allowed = (getStringListArg(_customAnalysisFlag)).any(
(String directory) =>
directory != null &&
directory.isNotEmpty &&
p.isWithin(
path.isWithin(
packagesDir.childDirectory(directory).path, file.path));
if (allowed) {
continue;
Expand Down Expand Up @@ -90,7 +90,7 @@ class AnalyzeCommand extends PackageLoopingCommand {
});
for (final Directory package in packageDirectories) {
final int exitCode = await processRunner.runAndStream(
'flutter', <String>['packages', 'get'],
flutterCommand, <String>['packages', 'get'],
workingDir: package);
if (exitCode != 0) {
return false;
Expand All @@ -109,7 +109,8 @@ class AnalyzeCommand extends PackageLoopingCommand {

// Use the Dart SDK override if one was passed in.
final String? dartSdk = argResults![_analysisSdk] as String?;
_dartBinaryPath = dartSdk == null ? 'dart' : p.join(dartSdk, 'bin', 'dart');
_dartBinaryPath =
dartSdk == null ? 'dart' : path.join(dartSdk, 'bin', 'dart');
}

@override
Expand Down
7 changes: 4 additions & 3 deletions script/tool/lib/src/build_examples_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:async';

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

import 'common/core.dart';
import 'common/package_looping_command.dart';
Expand All @@ -23,7 +23,8 @@ class BuildExamplesCommand extends PackageLoopingCommand {
BuildExamplesCommand(
Directory packagesDir, {
ProcessRunner processRunner = const ProcessRunner(),
}) : super(packagesDir, processRunner: processRunner) {
Platform platform = const LocalPlatform(),
}) : super(packagesDir, processRunner: processRunner, platform: platform) {
argParser.addFlag(kPlatformLinux);
argParser.addFlag(kPlatformMacos);
argParser.addFlag(kPlatformWeb);
Expand Down Expand Up @@ -127,7 +128,7 @@ class BuildExamplesCommand extends PackageLoopingCommand {

for (final Directory example in getExamplesForPlugin(package)) {
final String packageName =
p.relative(example.path, from: packagesDir.path);
getRelativePosixPath(example, from: packagesDir);

for (final _PlatformDetails platform in buildPlatforms) {
String buildPlatform = platform.label;
Expand Down
19 changes: 16 additions & 3 deletions script/tool/lib/src/common/package_looping_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:colorize/colorize.dart';
import 'package:file/file.dart';
import 'package:git/git.dart';
import 'package:path/path.dart' as p;
import 'package:platform/platform.dart';

import 'core.dart';
import 'plugin_command.dart';
Expand Down Expand Up @@ -63,8 +64,10 @@ abstract class PackageLoopingCommand extends PluginCommand {
PackageLoopingCommand(
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);

/// Packages that had at least one [logWarning] call.
final Set<Directory> _packagesWithWarnings = <Directory>{};
Expand Down Expand Up @@ -158,8 +161,8 @@ abstract class PackageLoopingCommand extends PluginCommand {
/// an exact format (e.g., published name, or basename) is required, that
/// should be used instead.
String getPackageDescription(Directory package) {
String packageName = p.relative(package.path, from: packagesDir.path);
final List<String> components = p.split(packageName);
String packageName = getRelativePosixPath(package, from: packagesDir);
final List<String> components = p.posix.split(packageName);
// For the common federated plugin pattern of `foo/foo_subpackage`, drop
// the first part since it's not useful.
if (components.length == 2 &&
Expand All @@ -169,6 +172,16 @@ abstract class PackageLoopingCommand extends PluginCommand {
return packageName;
}

/// Returns the relative path from [from] to [entity] in Posix style.
///
/// This should be used when, for example, printing package-relative paths in
/// status or error messages.
String getRelativePosixPath(
FileSystemEntity entity, {
required Directory from,
}) =>
p.posix.joinAll(path.split(path.relative(entity.path, from: from.path)));

/// The suggested indentation for printed output.
String get indentation => hasLongOutput ? '' : ' ';

Expand Down
18 changes: 13 additions & 5 deletions script/tool/lib/src/common/plugin_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class PluginCommand extends Command<void> {
PluginCommand(
this.packagesDir, {
this.processRunner = const ProcessRunner(),
this.platform = const LocalPlatform(),
GitDir? gitDir,
}) : _gitDir = gitDir {
argParser.addMultiOption(
Expand Down Expand Up @@ -79,6 +80,11 @@ abstract class PluginCommand extends Command<void> {
/// This can be overridden for testing.
final ProcessRunner processRunner;

/// The current platform.
///
/// This can be overridden for testing.
final Platform platform;

/// The git directory to use. If unset, [gitDir] populates it from the
/// packages directory's enclosing repository.
///
Expand All @@ -88,9 +94,11 @@ abstract class PluginCommand extends Command<void> {
int? _shardIndex;
int? _shardCount;

/// A context that matches the default for [platform].
p.Context get path => platform.isWindows ? p.windows : p.posix;

/// The command to use when running `flutter`.
String get flutterCommand =>
const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter';
String get flutterCommand => platform.isWindows ? 'flutter.bat' : 'flutter';

/// The shard of the overall command execution that this instance should run.
int get shardIndex {
Expand Down Expand Up @@ -240,9 +248,9 @@ abstract class PluginCommand extends Command<void> {
// plugins under 'my_plugin'. Also match if the exact plugin is
// passed.
final String relativePath =
p.relative(subdir.path, from: dir.path);
final String packageName = p.basename(subdir.path);
final String basenamePath = p.basename(entity.path);
path.relative(subdir.path, from: dir.path);
final String packageName = path.basename(subdir.path);
final String basenamePath = path.basename(entity.path);
if (!excludedPlugins.contains(basenamePath) &&
!excludedPlugins.contains(packageName) &&
!excludedPlugins.contains(relativePath) &&
Expand Down
22 changes: 19 additions & 3 deletions script/tool/lib/src/create_all_plugins_app_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:io' as io;

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

Expand Down Expand Up @@ -51,7 +52,7 @@ class CreateAllPluginsAppCommand extends PluginCommand {

Future<int> _createApp() async {
final io.ProcessResult result = io.Process.runSync(
'flutter',
flutterCommand,
<String>[
'create',
'--template=app',
Expand Down Expand Up @@ -156,7 +157,7 @@ class CreateAllPluginsAppCommand extends PluginCommand {
<String, PathDependency>{};

await for (final Directory package in getPlugins()) {
final String pluginName = package.path.split('/').last;
final String pluginName = package.basename;
final File pubspecFile = package.childFile('pubspec.yaml');
final Pubspec pubspec = Pubspec.parse(pubspecFile.readAsStringSync());

Expand All @@ -172,6 +173,7 @@ class CreateAllPluginsAppCommand extends PluginCommand {
### Generated file. Do not edit. Run `pub global run flutter_plugin_tools gen-pubspec` to update.
name: ${pubspec.name}
description: ${pubspec.description}
publish_to: none

version: ${pubspec.version}

Expand All @@ -197,7 +199,21 @@ dev_dependencies:${_pubspecMapString(pubspec.devDependencies)}
buffer.write(' ${entry.key}: \n sdk: ${dep.sdk}');
} else if (entry.value is PathDependency) {
final PathDependency dep = entry.value as PathDependency;
buffer.write(' ${entry.key}: \n path: ${dep.path}');
String depPath = dep.path;
if (path.style == p.Style.windows) {
// Posix-style path separators are preferred in pubspec.yaml (and
// using a consistent format makes unit testing simpler), so convert.
final List<String> components = path.split(depPath);
final String firstComponent = components.first;
// path.split leaves a \ on drive components that isn't necessary,
// and confuses pub, so remove it.
if (firstComponent.endsWith(r':\')) {
components[0] =
firstComponent.substring(0, firstComponent.length - 1);
}
depPath = p.posix.joinAll(components);
}
buffer.write(' ${entry.key}: \n path: $depPath');
} else {
throw UnimplementedError(
'Not available for type: ${entry.value.runtimeType}',
Expand Down
18 changes: 9 additions & 9 deletions script/tool/lib/src/drive_examples_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'dart:convert';
import 'dart:io';

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

import 'common/core.dart';
import 'common/package_looping_command.dart';
Expand All @@ -22,7 +22,8 @@ class DriveExamplesCommand extends PackageLoopingCommand {
DriveExamplesCommand(
Directory packagesDir, {
ProcessRunner processRunner = const ProcessRunner(),
}) : super(packagesDir, processRunner: processRunner) {
Platform platform = const LocalPlatform(),
}) : super(packagesDir, processRunner: processRunner, platform: platform) {
argParser.addFlag(kPlatformAndroid,
help: 'Runs the Android implementation of the examples');
argParser.addFlag(kPlatformIos,
Expand Down Expand Up @@ -148,7 +149,7 @@ class DriveExamplesCommand extends PackageLoopingCommand {
for (final Directory example in getExamplesForPlugin(package)) {
++examplesFound;
final String exampleName =
p.relative(example.path, from: packagesDir.path);
getRelativePosixPath(example, from: packagesDir);

final List<File> drivers = await _getDrivers(example);
if (drivers.isEmpty) {
Expand All @@ -172,11 +173,10 @@ class DriveExamplesCommand extends PackageLoopingCommand {

if (testTargets.isEmpty) {
final String driverRelativePath =
p.relative(driver.path, from: package.path);
getRelativePosixPath(driver, from: package);
printError(
'Found $driverRelativePath, but no integration_test/*_test.dart files.');
errors.add(
'No test files for ${p.relative(driver.path, from: package.path)}');
errors.add('No test files for $driverRelativePath');
continue;
}

Expand All @@ -185,7 +185,7 @@ class DriveExamplesCommand extends PackageLoopingCommand {
example, driver, testTargets,
deviceFlags: deviceFlags);
for (final File failingTarget in failingTargets) {
errors.add(p.relative(failingTarget.path, from: package.path));
errors.add(getRelativePosixPath(failingTarget, from: package));
}
}
}
Expand Down Expand Up @@ -296,9 +296,9 @@ class DriveExamplesCommand extends PackageLoopingCommand {
if (enableExperiment.isNotEmpty)
'--enable-experiment=$enableExperiment',
'--driver',
p.relative(driver.path, from: example.path),
getRelativePosixPath(driver, from: example),
'--target',
p.relative(target.path, from: example.path),
getRelativePosixPath(target, from: example),
],
workingDir: example);
if (exitCode != 0) {
Expand Down
14 changes: 8 additions & 6 deletions script/tool/lib/src/firebase_test_lab_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,16 +21,18 @@ 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',
help: 'The Firebase project name.',
);
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.');
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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',
Expand Down
Loading