From a8a531bd1c19513b46ec8374709bf252bd013a2c Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 12:22:19 -0400 Subject: [PATCH 01/19] Make platform overridable in the base command for testing --- script/tool/lib/src/common/package_looping_command.dart | 5 ++++- script/tool/lib/src/common/plugin_command.dart | 9 +++++++-- script/tool/lib/src/lint_podspecs_command.dart | 7 ++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/script/tool/lib/src/common/package_looping_command.dart b/script/tool/lib/src/common/package_looping_command.dart index de1e3b861f59..718ff5917ed4 100644 --- a/script/tool/lib/src/common/package_looping_command.dart +++ b/script/tool/lib/src/common/package_looping_command.dart @@ -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'; @@ -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 _packagesWithWarnings = {}; diff --git a/script/tool/lib/src/common/plugin_command.dart b/script/tool/lib/src/common/plugin_command.dart index 74f607dde7cf..e587c871f500 100644 --- a/script/tool/lib/src/common/plugin_command.dart +++ b/script/tool/lib/src/common/plugin_command.dart @@ -21,6 +21,7 @@ abstract class PluginCommand extends Command { PluginCommand( this.packagesDir, { this.processRunner = const ProcessRunner(), + this.platform = const LocalPlatform(), GitDir? gitDir, }) : _gitDir = gitDir { argParser.addMultiOption( @@ -79,6 +80,11 @@ abstract class PluginCommand extends Command { /// 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. /// @@ -89,8 +95,7 @@ abstract class PluginCommand extends Command { int? _shardCount; /// 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 { diff --git a/script/tool/lib/src/lint_podspecs_command.dart b/script/tool/lib/src/lint_podspecs_command.dart index 82cce0bd13e6..1acc76cd94c8 100644 --- a/script/tool/lib/src/lint_podspecs_command.dart +++ b/script/tool/lib/src/lint_podspecs_command.dart @@ -25,8 +25,7 @@ class LintPodspecsCommand extends PackageLoopingCommand { Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), Platform platform = const LocalPlatform(), - }) : _platform = platform, - super(packagesDir, processRunner: processRunner) { + }) : super(packagesDir, processRunner: processRunner, platform: platform) { argParser.addMultiOption('ignore-warnings', help: 'Do not pass --allow-warnings flag to "pod lib lint" for podspecs ' @@ -45,11 +44,9 @@ class LintPodspecsCommand extends PackageLoopingCommand { 'Runs "pod lib lint" on all iOS and macOS plugin podspecs.\n\n' 'This command requires "pod" and "flutter" to be in your path. Runs on macOS only.'; - final Platform _platform; - @override Future initializeRun() async { - if (!_platform.isMacOS) { + if (!platform.isMacOS) { printError('This command is only supported on macOS'); throw ToolExit(_exitUnsupportedPlatform); } From 79c18904aba3fa408232db403cd34abb3e3f1226 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 12:28:07 -0400 Subject: [PATCH 02/19] Make a test utility for getting the Flutter command --- script/tool/test/util.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/script/tool/test/util.dart b/script/tool/test/util.dart index b65b1fcaa84a..1984a25cc430 100644 --- a/script/tool/test/util.dart +++ b/script/tool/test/util.dart @@ -14,8 +14,14 @@ import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/common/process_runner.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; +import 'package:platform/platform.dart'; import 'package:quiver/collection.dart'; +/// Returns the exe name that command will use when running Flutter on +/// [platform]. +String getFlutterCommand(Platform platform) => + platform.isWindows ? 'flutter.bat' : 'flutter'; + /// Creates a packages directory in the given location. /// /// If [parentDir] is set the packages directory will be created there, From 6614e6e81f9cc199ea9685df873a7502ce72cb4c Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 12:28:27 -0400 Subject: [PATCH 03/19] Switch all commands from hard-coded 'flutter' to flutterCommand --- script/tool/lib/src/analyze_command.dart | 2 +- script/tool/lib/src/create_all_plugins_app_command.dart | 2 +- script/tool/lib/src/firebase_test_lab_command.dart | 2 +- script/tool/lib/src/format_command.dart | 2 +- script/tool/lib/src/publish_check_command.dart | 2 +- script/tool/lib/src/publish_plugin_command.dart | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/script/tool/lib/src/analyze_command.dart b/script/tool/lib/src/analyze_command.dart index 2c4fc1b8376e..cffdab95ee08 100644 --- a/script/tool/lib/src/analyze_command.dart +++ b/script/tool/lib/src/analyze_command.dart @@ -90,7 +90,7 @@ class AnalyzeCommand extends PackageLoopingCommand { }); for (final Directory package in packageDirectories) { final int exitCode = await processRunner.runAndStream( - 'flutter', ['packages', 'get'], + flutterCommand, ['packages', 'get'], workingDir: package); if (exitCode != 0) { return false; diff --git a/script/tool/lib/src/create_all_plugins_app_command.dart b/script/tool/lib/src/create_all_plugins_app_command.dart index fab41bcf4ec4..0b38b4451d84 100644 --- a/script/tool/lib/src/create_all_plugins_app_command.dart +++ b/script/tool/lib/src/create_all_plugins_app_command.dart @@ -51,7 +51,7 @@ class CreateAllPluginsAppCommand extends PluginCommand { Future _createApp() async { final io.ProcessResult result = io.Process.runSync( - 'flutter', + flutterCommand, [ 'create', '--template=app', diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index 8253ceeda86b..7996d4c306bc 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -203,7 +203,7 @@ class FirebaseTestLabCommand extends PackageLoopingCommand { print('Running flutter build apk...'); final String experiment = getStringArg(kEnableExperiment); final int exitCode = await processRunner.runAndStream( - 'flutter', + flutterCommand, [ 'build', 'apk', diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart index 9d39d93b9118..7ec56cf4f822 100644 --- a/script/tool/lib/src/format_command.dart +++ b/script/tool/lib/src/format_command.dart @@ -156,7 +156,7 @@ class FormatCommand extends PluginCommand { // `flutter format` doesn't require the project to actually be a Flutter // project. final int exitCode = await processRunner.runAndStream( - 'flutter', ['format', ...dartFiles], + flutterCommand, ['format', ...dartFiles], workingDir: packagesDir); if (exitCode != 0) { printError('Failed to format Dart files: exit code $exitCode.'); diff --git a/script/tool/lib/src/publish_check_command.dart b/script/tool/lib/src/publish_check_command.dart index ccafabfddd1d..dce66db733c9 100644 --- a/script/tool/lib/src/publish_check_command.dart +++ b/script/tool/lib/src/publish_check_command.dart @@ -128,7 +128,7 @@ class PublishCheckCommand extends PackageLoopingCommand { Future _hasValidPublishCheckRun(Directory package) async { print('Running pub publish --dry-run:'); final io.Process process = await processRunner.start( - 'flutter', + flutterCommand, ['pub', 'publish', '--', '--dry-run'], workingDirectory: package, ); diff --git a/script/tool/lib/src/publish_plugin_command.dart b/script/tool/lib/src/publish_plugin_command.dart index 6de53ba2690a..abdd8c03242c 100644 --- a/script/tool/lib/src/publish_plugin_command.dart +++ b/script/tool/lib/src/publish_plugin_command.dart @@ -445,7 +445,7 @@ Safe to ignore if the package is deleted in this commit. } final io.Process publish = await processRunner.start( - 'flutter', ['pub', 'publish'] + publishFlags, + flutterCommand, ['pub', 'publish'] + publishFlags, workingDirectory: packageDir); publish.stdout .transform(utf8.decoder) From e52444080adb02de379914b7e8e6a231f06dc8fc Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 13:10:33 -0400 Subject: [PATCH 04/19] Fix build-example tests; make build-example always use Posix paths for display --- .../tool/lib/src/build_examples_command.dart | 7 +- .../test/build_examples_command_test.dart | 85 ++++++++++--------- script/tool/test/mocks.dart | 12 ++- 3 files changed, 59 insertions(+), 45 deletions(-) diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index 32905c83db91..6af57e821b44 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -6,6 +6,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'; @@ -23,7 +24,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); @@ -126,8 +128,9 @@ class BuildExamplesCommand extends PackageLoopingCommand { print(''); for (final Directory example in getExamplesForPlugin(package)) { + final p.Context posixContext = p.Context(style: p.Style.posix); final String packageName = - p.relative(example.path, from: packagesDir.path); + posixContext.relative(example.path, from: packagesDir.path); for (final _PlatformDetails platform in buildPlatforms) { String buildPlatform = platform.label; diff --git a/script/tool/test/build_examples_command_test.dart b/script/tool/test/build_examples_command_test.dart index c0c90a15c71c..27489a50228a 100644 --- a/script/tool/test/build_examples_command_test.dart +++ b/script/tool/test/build_examples_command_test.dart @@ -10,8 +10,6 @@ import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/build_examples_command.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; -import 'package:path/path.dart' as p; -import 'package:platform/platform.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -20,18 +18,21 @@ import 'util.dart'; void main() { group('build-example', () { late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; - final String flutterCommand = - const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter'; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final BuildExamplesCommand command = - BuildExamplesCommand(packagesDir, processRunner: processRunner); + final BuildExamplesCommand command = BuildExamplesCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner( 'build_examples_command', 'Test for build_example_command'); @@ -59,7 +60,9 @@ void main() { kPlatformIos: PlatformSupport.inline }); - processRunner.mockProcessesForExecutable['flutter'] = [ + processRunner + .mockProcessesForExecutable[getFlutterCommand(mockPlatform)] = + [ MockProcess.failing() // flutter packages get ]; @@ -81,6 +84,7 @@ void main() { test('building for iOS when plugin is not set up for iOS results in no-op', () async { + mockPlatform.isMacOS = true; createFakePlugin('plugin', packagesDir); final List output = @@ -99,7 +103,8 @@ void main() { expect(processRunner.recordedCalls, orderedEquals([])); }); - test('building for ios', () async { + test('building for iOS', () async { + mockPlatform.isMacOS = true; final Directory pluginDirectory = createFakePlugin('plugin', packagesDir, platformSupport: { kPlatformIos: PlatformSupport.inline @@ -110,13 +115,11 @@ void main() { final List output = await runCapturingPrint(runner, ['build-examples', '--ios', '--enable-experiment=exp1']); - final String packageName = - p.relative(pluginExampleDirectory.path, from: packagesDir.path); expect( output, containsAllInOrder([ - '\nBUILDING $packageName for iOS', + '\nBUILDING plugin/example for iOS', ]), ); @@ -124,7 +127,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'build', 'ios', @@ -138,6 +141,7 @@ void main() { test( 'building for Linux when plugin is not set up for Linux results in no-op', () async { + mockPlatform.isLinux = true; createFakePlugin('plugin', packagesDir); final List output = await runCapturingPrint( @@ -157,6 +161,7 @@ void main() { }); test('building for Linux', () async { + mockPlatform.isLinux = true; final Directory pluginDirectory = createFakePlugin('plugin', packagesDir, platformSupport: { kPlatformLinux: PlatformSupport.inline, @@ -167,26 +172,25 @@ void main() { final List output = await runCapturingPrint( runner, ['build-examples', '--linux']); - final String packageName = - p.relative(pluginExampleDirectory.path, from: packagesDir.path); expect( output, containsAllInOrder([ - '\nBUILDING $packageName for Linux', + '\nBUILDING plugin/example for Linux', ]), ); expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall(flutterCommand, const ['build', 'linux'], - pluginExampleDirectory.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['build', 'linux'], pluginExampleDirectory.path), ])); }); - test('building for macos with no implementation results in no-op', + test('building for macOS with no implementation results in no-op', () async { + mockPlatform.isMacOS = true; createFakePlugin('plugin', packagesDir); final List output = await runCapturingPrint( @@ -205,7 +209,8 @@ void main() { expect(processRunner.recordedCalls, orderedEquals([])); }); - test('building for macos', () async { + test('building for macOS', () async { + mockPlatform.isMacOS = true; final Directory pluginDirectory = createFakePlugin('plugin', packagesDir, platformSupport: { kPlatformMacos: PlatformSupport.inline, @@ -216,21 +221,19 @@ void main() { final List output = await runCapturingPrint( runner, ['build-examples', '--macos']); - final String packageName = - p.relative(pluginExampleDirectory.path, from: packagesDir.path); expect( output, containsAllInOrder([ - '\nBUILDING $packageName for macOS', + '\nBUILDING plugin/example for macOS', ]), ); expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall(flutterCommand, const ['build', 'macos'], - pluginExampleDirectory.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['build', 'macos'], pluginExampleDirectory.path), ])); }); @@ -264,27 +267,26 @@ void main() { final List output = await runCapturingPrint(runner, ['build-examples', '--web']); - final String packageName = - p.relative(pluginExampleDirectory.path, from: packagesDir.path); expect( output, containsAllInOrder([ - '\nBUILDING $packageName for web', + '\nBUILDING plugin/example for web', ]), ); expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall(flutterCommand, const ['build', 'web'], - pluginExampleDirectory.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['build', 'web'], pluginExampleDirectory.path), ])); }); test( 'building for Windows when plugin is not set up for Windows results in no-op', () async { + mockPlatform.isWindows = true; createFakePlugin('plugin', packagesDir); final List output = await runCapturingPrint( @@ -303,7 +305,8 @@ void main() { expect(processRunner.recordedCalls, orderedEquals([])); }); - test('building for windows', () async { + test('building for Windows', () async { + mockPlatform.isWindows = true; final Directory pluginDirectory = createFakePlugin('plugin', packagesDir, platformSupport: { kPlatformWindows: PlatformSupport.inline @@ -314,20 +317,20 @@ void main() { final List output = await runCapturingPrint( runner, ['build-examples', '--windows']); - final String packageName = - p.relative(pluginExampleDirectory.path, from: packagesDir.path); expect( output, containsAllInOrder([ - '\nBUILDING $packageName for Windows', + '\nBUILDING plugin/example for Windows', ]), ); expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall(flutterCommand, const ['build', 'windows'], + ProcessCall( + getFlutterCommand(mockPlatform), + const ['build', 'windows'], pluginExampleDirectory.path), ])); }); @@ -353,7 +356,7 @@ void main() { expect(processRunner.recordedCalls, orderedEquals([])); }); - test('building for android', () async { + test('building for Android', () async { final Directory pluginDirectory = createFakePlugin('plugin', packagesDir, platformSupport: { kPlatformAndroid: PlatformSupport.inline @@ -366,21 +369,19 @@ void main() { 'build-examples', '--apk', ]); - final String packageName = - p.relative(pluginExampleDirectory.path, from: packagesDir.path); expect( output, containsAllInOrder([ - '\nBUILDING $packageName for Android (apk)', + '\nBUILDING plugin/example for Android (apk)', ]), ); expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall(flutterCommand, const ['build', 'apk'], - pluginExampleDirectory.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['build', 'apk'], pluginExampleDirectory.path), ])); }); @@ -400,7 +401,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const ['build', 'apk', '--enable-experiment=exp1'], pluginExampleDirectory.path), ])); @@ -421,7 +422,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'build', 'ios', diff --git a/script/tool/test/mocks.dart b/script/tool/test/mocks.dart index 02b00658398e..042a455637b5 100644 --- a/script/tool/test/mocks.dart +++ b/script/tool/test/mocks.dart @@ -10,10 +10,20 @@ import 'package:mockito/mockito.dart'; import 'package:platform/platform.dart'; class MockPlatform extends Mock implements Platform { - MockPlatform({this.isMacOS = false}); + MockPlatform({ + this.isLinux = false, + this.isMacOS = false, + this.isWindows = false, + }); + + @override + bool isLinux; @override bool isMacOS; + + @override + bool isWindows; } class MockProcess extends Mock implements io.Process { From 174dcf8d51380627f9a9d1c9775f57d0d490fc56 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 13:52:56 -0400 Subject: [PATCH 05/19] Make a new helper to get a path context that matches the platform, and use that everywhere --- script/tool/lib/src/analyze_command.dart | 11 ++++++----- script/tool/lib/src/build_examples_command.dart | 3 +-- .../tool/lib/src/common/package_looping_command.dart | 5 ++--- script/tool/lib/src/common/plugin_command.dart | 9 ++++++--- script/tool/lib/src/drive_examples_command.dart | 8 ++++---- script/tool/lib/src/firebase_test_lab_command.dart | 8 ++++---- script/tool/lib/src/format_command.dart | 6 +++--- script/tool/lib/src/java_test_command.dart | 4 ++-- script/tool/lib/src/license_check_command.dart | 4 ++-- script/tool/lib/src/lint_podspecs_command.dart | 5 ++--- script/tool/lib/src/version_check_command.dart | 3 +-- script/tool/lib/src/xctest_command.dart | 3 +-- script/tool/test/analyze_command_test.dart | 9 +++++++-- 13 files changed, 41 insertions(+), 37 deletions(-) diff --git a/script/tool/lib/src/analyze_command.dart b/script/tool/lib/src/analyze_command.dart index cffdab95ee08..e56b95d88eb0 100644 --- a/script/tool/lib/src/analyze_command.dart +++ b/script/tool/lib/src/analyze_command.dart @@ -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'; @@ -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.', @@ -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; @@ -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 diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index 6af57e821b44..91e7499c39d1 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -128,9 +128,8 @@ class BuildExamplesCommand extends PackageLoopingCommand { print(''); for (final Directory example in getExamplesForPlugin(package)) { - final p.Context posixContext = p.Context(style: p.Style.posix); final String packageName = - posixContext.relative(example.path, from: packagesDir.path); + p.posix.relative(example.path, from: packagesDir.path); for (final _PlatformDetails platform in buildPlatforms) { String buildPlatform = platform.label; diff --git a/script/tool/lib/src/common/package_looping_command.dart b/script/tool/lib/src/common/package_looping_command.dart index 718ff5917ed4..b386bd37bac2 100644 --- a/script/tool/lib/src/common/package_looping_command.dart +++ b/script/tool/lib/src/common/package_looping_command.dart @@ -7,7 +7,6 @@ import 'dart:async'; 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'; @@ -161,8 +160,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 components = p.split(packageName); + String packageName = path.relative(package.path, from: packagesDir.path); + final List components = path.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 && diff --git a/script/tool/lib/src/common/plugin_command.dart b/script/tool/lib/src/common/plugin_command.dart index e587c871f500..ecdcb0565d35 100644 --- a/script/tool/lib/src/common/plugin_command.dart +++ b/script/tool/lib/src/common/plugin_command.dart @@ -94,6 +94,9 @@ abstract class PluginCommand extends Command { 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 => platform.isWindows ? 'flutter.bat' : 'flutter'; @@ -245,9 +248,9 @@ abstract class PluginCommand extends Command { // 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) && diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index df74119e4019..442b83c1a642 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -148,7 +148,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { for (final Directory example in getExamplesForPlugin(package)) { ++examplesFound; final String exampleName = - p.relative(example.path, from: packagesDir.path); + path.relative(example.path, from: packagesDir.path); final List drivers = await _getDrivers(example); if (drivers.isEmpty) { @@ -172,7 +172,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { if (testTargets.isEmpty) { final String driverRelativePath = - p.relative(driver.path, from: package.path); + path.relative(driver.path, from: package.path); printError( 'Found $driverRelativePath, but no integration_test/*_test.dart files.'); errors.add( @@ -296,9 +296,9 @@ class DriveExamplesCommand extends PackageLoopingCommand { if (enableExperiment.isNotEmpty) '--enable-experiment=$enableExperiment', '--driver', - p.relative(driver.path, from: example.path), + path.relative(driver.path, from: example.path), '--target', - p.relative(target.path, from: example.path), + path.relative(target.path, from: example.path), ], workingDir: example); if (exitCode != 0) { diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index 7996d4c306bc..6de095b68928 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -6,7 +6,6 @@ import 'dart:async'; import 'dart:io' as io; import 'package:file/file.dart'; -import 'package:path/path.dart' as p; import 'package:uuid/uuid.dart'; import 'common/core.dart'; @@ -29,8 +28,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 +150,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 = path.relative(test.path, from: package.path); print('Testing $testName...'); if (!await _runGradle(androidDirectory, 'app:assembleDebug', testFile: test)) { diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart index 7ec56cf4f822..4af2693e84a8 100644 --- a/script/tool/lib/src/format_command.dart +++ b/script/tool/lib/src/format_command.dart @@ -168,7 +168,7 @@ class FormatCommand extends PluginCommand { Future> _getFilteredFilePaths(Stream files) async { // Returns a pattern to check for [directories] as a subset of a file path. RegExp pathFragmentForDirectories(List directories) { - final String s = p.separator; + final String s = path.separator; return RegExp('(?:^|$s)${p.joinAll(directories)}$s'); } @@ -192,8 +192,8 @@ class FormatCommand extends PluginCommand { } Future _getGoogleFormatterPath() async { - final String javaFormatterPath = p.join( - p.dirname(p.fromUri(io.Platform.script)), + final String javaFormatterPath = path.join( + path.dirname(p.fromUri(io.Platform.script)), 'google-java-format-1.3-all-deps.jar'); final File javaFormatterFile = packagesDir.fileSystem.file(javaFormatterPath); diff --git a/script/tool/lib/src/java_test_command.dart b/script/tool/lib/src/java_test_command.dart index 352197be3057..75c187e041f1 100644 --- a/script/tool/lib/src/java_test_command.dart +++ b/script/tool/lib/src/java_test_command.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'package:file/file.dart'; -import 'package:path/path.dart' as p; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -50,7 +49,8 @@ class JavaTestCommand extends PackageLoopingCommand { final List errors = []; for (final Directory example in examplesWithTests) { - final String exampleName = p.relative(example.path, from: package.path); + final String exampleName = + path.relative(example.path, from: package.path); print('\nRUNNING JAVA TESTS for $exampleName'); final Directory androidDirectory = example.childDirectory('android'); diff --git a/script/tool/lib/src/license_check_command.dart b/script/tool/lib/src/license_check_command.dart index 1d3e49c6a7c6..093f8143df4f 100644 --- a/script/tool/lib/src/license_check_command.dart +++ b/script/tool/lib/src/license_check_command.dart @@ -112,7 +112,7 @@ class LicenseCheckCommand extends PluginCommand { !_shouldIgnoreFile(file)); final Iterable firstPartyLicenseFiles = (await _getAllFiles()).where( (File file) => - p.basename(file.basename) == 'LICENSE' && !_isThirdParty(file)); + path.basename(file.basename) == 'LICENSE' && !_isThirdParty(file)); final bool copyrightCheckSucceeded = await _checkCodeLicenses(codeFiles); print('\n=======================================\n'); @@ -246,7 +246,7 @@ class LicenseCheckCommand extends PluginCommand { } bool _isThirdParty(File file) { - return p.split(file.path).contains('third_party'); + return path.split(file.path).contains('third_party'); } Future> _getAllFiles() => packagesDir.parent diff --git a/script/tool/lib/src/lint_podspecs_command.dart b/script/tool/lib/src/lint_podspecs_command.dart index 1acc76cd94c8..d0d93fcb79b1 100644 --- a/script/tool/lib/src/lint_podspecs_command.dart +++ b/script/tool/lib/src/lint_podspecs_command.dart @@ -86,11 +86,10 @@ class LintPodspecsCommand extends PackageLoopingCommand { final List podspecs = await getFilesForPackage(package).where((File entity) { final String filePath = entity.path; - return p.extension(filePath) == '.podspec'; + return path.extension(filePath) == '.podspec'; }).toList(); - podspecs.sort( - (File a, File b) => p.basename(a.path).compareTo(p.basename(b.path))); + podspecs.sort((File a, File b) => a.basename.compareTo(b.basename)); return podspecs; } diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index f0902f016833..e23aca46a81b 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -6,7 +6,6 @@ import 'package:file/file.dart'; import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:meta/meta.dart'; -import 'package:path/path.dart' as p; import 'package:pub_semver/pub_semver.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; @@ -180,7 +179,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} }) async { final File pubspecFile = package.childFile('pubspec.yaml'); return await gitVersionFinder.getPackageVersion( - p.relative(pubspecFile.absolute.path, from: (await gitDir).path)); + path.relative(pubspecFile.absolute.path, from: (await gitDir).path)); } /// Returns true if the version of [package] is either unchanged relative to diff --git a/script/tool/lib/src/xctest_command.dart b/script/tool/lib/src/xctest_command.dart index cd3b674f8d3a..50b8f446fd33 100644 --- a/script/tool/lib/src/xctest_command.dart +++ b/script/tool/lib/src/xctest_command.dart @@ -6,7 +6,6 @@ import 'dart:convert'; import 'dart:io' as io; import 'package:file/file.dart'; -import 'package:path/path.dart' as p; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -142,7 +141,7 @@ class XCTestCommand extends PackageLoopingCommand { for (final Directory example in getExamplesForPlugin(plugin)) { // Running tests and static analyzer. final String examplePath = - p.relative(example.path, from: plugin.parent.path); + path.relative(example.path, from: plugin.parent.path); print('Running $platform tests and analyzer for $examplePath...'); int exitCode = await _runTests(true, example, platform, extraFlags: extraXcrunFlags); diff --git a/script/tool/test/analyze_command_test.dart b/script/tool/test/analyze_command_test.dart index adeaabaaca52..69a2c4f95523 100644 --- a/script/tool/test/analyze_command_test.dart +++ b/script/tool/test/analyze_command_test.dart @@ -16,16 +16,21 @@ import 'util.dart'; void main() { late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late RecordingProcessRunner processRunner; late CommandRunner runner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final AnalyzeCommand analyzeCommand = - AnalyzeCommand(packagesDir, processRunner: processRunner); + final AnalyzeCommand analyzeCommand = AnalyzeCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner('analyze_command', 'Test for analyze_command'); runner.addCommand(analyzeCommand); From 894bde46c36d07a286fddbf2cbd15e160e00b06c Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 14:14:14 -0400 Subject: [PATCH 06/19] Always use Posix-style paths in package descriptions --- .../src/common/package_looping_command.dart | 8 ++-- .../common/package_looping_command_test.dart | 37 ++++++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/script/tool/lib/src/common/package_looping_command.dart b/script/tool/lib/src/common/package_looping_command.dart index b386bd37bac2..276f21767d09 100644 --- a/script/tool/lib/src/common/package_looping_command.dart +++ b/script/tool/lib/src/common/package_looping_command.dart @@ -7,6 +7,7 @@ import 'dart:async'; 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'; @@ -160,15 +161,16 @@ 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 = path.relative(package.path, from: packagesDir.path); + final String packageName = + path.relative(package.path, from: packagesDir.path); final List components = path.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 && components[1].startsWith('${components[0]}_')) { - packageName = components[1]; + components.removeAt(0); } - return packageName; + return p.posix.joinAll(components); } /// The suggested indentation for printed output. diff --git a/script/tool/test/common/package_looping_command_test.dart b/script/tool/test/common/package_looping_command_test.dart index 917fbc0fd67a..542e91af6431 100644 --- a/script/tool/test/common/package_looping_command_test.dart +++ b/script/tool/test/common/package_looping_command_test.dart @@ -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(mockProcessResult.stdout as String?) .thenReturn(gitDiffResponse); } - return Future.value(mockProcessResult); + return Future.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 checkedPackages = []; final List capturedOutput = []; @@ -629,4 +654,4 @@ class TestPackageLoopingCommand extends PackageLoopingCommand { } } -class MockProcessResult extends Mock implements ProcessResult {} +class MockProcessResult extends Mock implements io.ProcessResult {} From d47d8ab7782dc9a80ad2d5093af8c101fcca49c5 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 14:20:14 -0400 Subject: [PATCH 07/19] Fix xctest tests --- script/tool/lib/src/xctest_command.dart | 4 +++- script/tool/test/xctest_command_test.dart | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/script/tool/lib/src/xctest_command.dart b/script/tool/lib/src/xctest_command.dart index 50b8f446fd33..144bf6d42042 100644 --- a/script/tool/lib/src/xctest_command.dart +++ b/script/tool/lib/src/xctest_command.dart @@ -6,6 +6,7 @@ import 'dart:convert'; import 'dart:io' as io; import 'package:file/file.dart'; +import 'package:platform/platform.dart'; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -31,7 +32,8 @@ class XCTestCommand extends PackageLoopingCommand { XCTestCommand( Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), - }) : super(packagesDir, processRunner: processRunner) { + Platform platform = const LocalPlatform(), + }) : super(packagesDir, processRunner: processRunner, platform: platform) { argParser.addOption( _kiOSDestination, help: diff --git a/script/tool/test/xctest_command_test.dart b/script/tool/test/xctest_command_test.dart index 10329b18980c..aa6d23fb56f5 100644 --- a/script/tool/test/xctest_command_test.dart +++ b/script/tool/test/xctest_command_test.dart @@ -90,16 +90,18 @@ void main() { group('test xctest_command', () { late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(isMacOS: true); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final XCTestCommand command = - XCTestCommand(packagesDir, processRunner: processRunner); + final XCTestCommand command = XCTestCommand(packagesDir, + processRunner: processRunner, platform: mockPlatform); runner = CommandRunner('xctest_command', 'Test for xctest_command'); runner.addCommand(command); From 7616eb5227caf0ee30bcc8fc48df2ef168415103 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 14:25:30 -0400 Subject: [PATCH 08/19] Fix lint tests --- script/tool/test/lint_podspecs_command_test.dart | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/script/tool/test/lint_podspecs_command_test.dart b/script/tool/test/lint_podspecs_command_test.dart index 1236ec0f5013..bd441857573f 100644 --- a/script/tool/test/lint_podspecs_command_test.dart +++ b/script/tool/test/lint_podspecs_command_test.dart @@ -94,7 +94,10 @@ void main() { [ 'lib', 'lint', - p.join(plugin1Dir.path, 'ios', 'plugin1.podspec'), + plugin1Dir + .childDirectory('ios') + .childFile('plugin1.podspec') + .path, '--configuration=Debug', '--skip-tests', '--use-modular-headers', @@ -106,7 +109,10 @@ void main() { [ 'lib', 'lint', - p.join(plugin1Dir.path, 'ios', 'plugin1.podspec'), + plugin1Dir + .childDirectory('ios') + .childFile('plugin1.podspec') + .path, '--configuration=Debug', '--skip-tests', '--use-modular-headers', @@ -136,7 +142,7 @@ void main() { [ 'lib', 'lint', - p.join(plugin1Dir.path, 'plugin1.podspec'), + plugin1Dir.childFile('plugin1.podspec').path, '--configuration=Debug', '--skip-tests', '--use-modular-headers', @@ -149,7 +155,7 @@ void main() { [ 'lib', 'lint', - p.join(plugin1Dir.path, 'plugin1.podspec'), + plugin1Dir.childFile('plugin1.podspec').path, '--configuration=Debug', '--skip-tests', '--use-modular-headers', From df757d165d4dd2a8c28fbe7c3d008486161822f3 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 15:43:44 -0400 Subject: [PATCH 09/19] Fix create all --- .../src/create_all_plugins_app_command.dart | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/script/tool/lib/src/create_all_plugins_app_command.dart b/script/tool/lib/src/create_all_plugins_app_command.dart index 0b38b4451d84..ed7014456086 100644 --- a/script/tool/lib/src/create_all_plugins_app_command.dart +++ b/script/tool/lib/src/create_all_plugins_app_command.dart @@ -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'; @@ -156,7 +157,7 @@ class CreateAllPluginsAppCommand extends PluginCommand { {}; 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()); @@ -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} @@ -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 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}', From 15fb1b8454943f3a7f7c4b2c7d5ba7b7da72746d Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 15:57:49 -0400 Subject: [PATCH 10/19] Fix version check tests --- script/tool/lib/src/version_check_command.dart | 10 ++++++++-- script/tool/test/lint_podspecs_command_test.dart | 1 - 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index e23aca46a81b..779c0dd9fca5 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -6,6 +6,7 @@ import 'package:file/file.dart'; import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:meta/meta.dart'; +import 'package:path/path.dart' as p; import 'package:pub_semver/pub_semver.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; @@ -178,8 +179,13 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} required GitVersionFinder gitVersionFinder, }) async { final File pubspecFile = package.childFile('pubspec.yaml'); - return await gitVersionFinder.getPackageVersion( - path.relative(pubspecFile.absolute.path, from: (await gitDir).path)); + final String relativePath = + path.relative(pubspecFile.absolute.path, from: (await gitDir).path); + // Use Posix-style paths for git. + final String gitPath = path.style == p.Style.windows + ? p.posix.joinAll(path.split(relativePath)) + : relativePath; + return await gitVersionFinder.getPackageVersion(gitPath); } /// Returns true if the version of [package] is either unchanged relative to diff --git a/script/tool/test/lint_podspecs_command_test.dart b/script/tool/test/lint_podspecs_command_test.dart index bd441857573f..c15df98acc46 100644 --- a/script/tool/test/lint_podspecs_command_test.dart +++ b/script/tool/test/lint_podspecs_command_test.dart @@ -9,7 +9,6 @@ import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/lint_podspecs_command.dart'; -import 'package:path/path.dart' as p; import 'package:test/test.dart'; import 'mocks.dart'; From c4379cc39e02e0fd45c24cdbcf071d18cf980b0e Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 22:28:42 -0400 Subject: [PATCH 11/19] Use Posix paths for most display strings --- .../tool/lib/src/build_examples_command.dart | 3 +-- .../src/common/package_looping_command.dart | 19 ++++++++++++++----- .../tool/lib/src/drive_examples_command.dart | 14 ++++++-------- .../lib/src/firebase_test_lab_command.dart | 2 +- script/tool/lib/src/java_test_command.dart | 3 +-- script/tool/lib/src/xctest_command.dart | 2 +- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index 91e7499c39d1..d652dc29343d 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -128,8 +128,7 @@ class BuildExamplesCommand extends PackageLoopingCommand { print(''); for (final Directory example in getExamplesForPlugin(package)) { - final String packageName = - p.posix.relative(example.path, from: packagesDir.path); + final String packageName = getRelativePosixPath(example, from: package); for (final _PlatformDetails platform in buildPlatforms) { String buildPlatform = platform.label; diff --git a/script/tool/lib/src/common/package_looping_command.dart b/script/tool/lib/src/common/package_looping_command.dart index 276f21767d09..9f4039ec7074 100644 --- a/script/tool/lib/src/common/package_looping_command.dart +++ b/script/tool/lib/src/common/package_looping_command.dart @@ -161,18 +161,27 @@ 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) { - final String packageName = - path.relative(package.path, from: packagesDir.path); - final List components = path.split(packageName); + String packageName = getRelativePosixPath(package, from: packagesDir); + final List 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 && components[1].startsWith('${components[0]}_')) { - components.removeAt(0); + packageName = components[1]; } - return p.posix.joinAll(components); + 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 ? '' : ' '; diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index 442b83c1a642..3a0512431dc6 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -147,8 +147,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { final List errors = []; for (final Directory example in getExamplesForPlugin(package)) { ++examplesFound; - final String exampleName = - path.relative(example.path, from: packagesDir.path); + final String exampleName = getRelativePosixPath(example, from: package); final List drivers = await _getDrivers(example); if (drivers.isEmpty) { @@ -172,11 +171,10 @@ class DriveExamplesCommand extends PackageLoopingCommand { if (testTargets.isEmpty) { final String driverRelativePath = - path.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; } @@ -185,7 +183,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)); } } } @@ -296,9 +294,9 @@ class DriveExamplesCommand extends PackageLoopingCommand { if (enableExperiment.isNotEmpty) '--enable-experiment=$enableExperiment', '--driver', - path.relative(driver.path, from: example.path), + getRelativePosixPath(driver, from: example), '--target', - path.relative(target.path, from: example.path), + getRelativePosixPath(target, from: example), ], workingDir: example); if (exitCode != 0) { diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index 6de095b68928..2a411350fa2d 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -150,7 +150,7 @@ class FirebaseTestLabCommand extends PackageLoopingCommand { // test file's run. int resultsCounter = 0; for (final File test in _findIntegrationTestFiles(package)) { - final String testName = path.relative(test.path, from: package.path); + final String testName = getRelativePosixPath(test, from: package); print('Testing $testName...'); if (!await _runGradle(androidDirectory, 'app:assembleDebug', testFile: test)) { diff --git a/script/tool/lib/src/java_test_command.dart b/script/tool/lib/src/java_test_command.dart index 75c187e041f1..e46eb230df64 100644 --- a/script/tool/lib/src/java_test_command.dart +++ b/script/tool/lib/src/java_test_command.dart @@ -49,8 +49,7 @@ class JavaTestCommand extends PackageLoopingCommand { final List errors = []; for (final Directory example in examplesWithTests) { - final String exampleName = - path.relative(example.path, from: package.path); + final String exampleName = getRelativePosixPath(example, from: package); print('\nRUNNING JAVA TESTS for $exampleName'); final Directory androidDirectory = example.childDirectory('android'); diff --git a/script/tool/lib/src/xctest_command.dart b/script/tool/lib/src/xctest_command.dart index 144bf6d42042..176adad39a09 100644 --- a/script/tool/lib/src/xctest_command.dart +++ b/script/tool/lib/src/xctest_command.dart @@ -143,7 +143,7 @@ class XCTestCommand extends PackageLoopingCommand { for (final Directory example in getExamplesForPlugin(plugin)) { // Running tests and static analyzer. final String examplePath = - path.relative(example.path, from: plugin.parent.path); + getRelativePosixPath(example, from: plugin.parent); print('Running $platform tests and analyzer for $examplePath...'); int exitCode = await _runTests(true, example, platform, extraFlags: extraXcrunFlags); From 23b94f8c0d6f3e6e8496d84ecaeabcd06d484229 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 22:36:46 -0400 Subject: [PATCH 12/19] Fix drive-examples --- .../tool/lib/src/build_examples_command.dart | 1 - .../tool/lib/src/drive_examples_command.dart | 4 +- .../test/drive_examples_command_test.dart | 91 ++++++++----------- 3 files changed, 38 insertions(+), 58 deletions(-) diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index d652dc29343d..7690815560c7 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -5,7 +5,6 @@ import 'dart:async'; import 'package:file/file.dart'; -import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'common/core.dart'; diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index 3a0512431dc6..e00c7561f226 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -6,7 +6,6 @@ import 'dart:convert'; import 'dart:io'; import 'package:file/file.dart'; -import 'package:path/path.dart' as p; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -147,7 +146,8 @@ class DriveExamplesCommand extends PackageLoopingCommand { final List errors = []; for (final Directory example in getExamplesForPlugin(package)) { ++examplesFound; - final String exampleName = getRelativePosixPath(example, from: package); + final String exampleName = + getRelativePosixPath(example, from: packagesDir); final List drivers = await _getDrivers(example); if (drivers.isEmpty) { diff --git a/script/tool/test/drive_examples_command_test.dart b/script/tool/test/drive_examples_command_test.dart index 681a9e0e5844..fdbd77dd8061 100644 --- a/script/tool/test/drive_examples_command_test.dart +++ b/script/tool/test/drive_examples_command_test.dart @@ -10,7 +10,6 @@ import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/drive_examples_command.dart'; -import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'package:test/test.dart'; @@ -63,7 +62,7 @@ void main() { final MockProcess mockDevicesProcess = MockProcess.succeeding(); mockDevicesProcess.stdoutController.close(); // ignore: unawaited_futures - processRunner.mockProcessesForExecutable['flutter'] = [ + processRunner.mockProcessesForExecutable[flutterCommand] = [ mockDevicesProcess ]; processRunner.resultStdout = output; @@ -150,7 +149,7 @@ void main() { test('fails for iOS if getting devices fails', () async { // Simulate failure from `flutter devices`. - processRunner.mockProcessesForExecutable['flutter'] = [ + processRunner.mockProcessesForExecutable[flutterCommand] = [ MockProcess.failing() ]; @@ -216,8 +215,6 @@ void main() { ]), ); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ @@ -225,14 +222,14 @@ void main() { flutterCommand, const ['devices', '--machine'], null), ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', _fakeIosDevice, '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -337,8 +334,6 @@ void main() { ]), ); - final String driverTestPath = - p.join('test_driver', 'integration_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ @@ -346,26 +341,26 @@ void main() { flutterCommand, const ['devices', '--machine'], null), ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', _fakeIosDevice, '--driver', - driverTestPath, + 'test_driver/integration_test.dart', '--target', - p.join('integration_test', 'bar_test.dart'), + 'integration_test/bar_test.dart', ], pluginExampleDirectory.path), ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', _fakeIosDevice, '--driver', - driverTestPath, + 'test_driver/integration_test.dart', '--target', - p.join('integration_test', 'foo_test.dart'), + 'integration_test/foo_test.dart', ], pluginExampleDirectory.path), ])); @@ -425,21 +420,19 @@ void main() { ]), ); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', 'linux', '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -500,21 +493,19 @@ void main() { ]), ); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', 'macos', '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -573,23 +564,21 @@ void main() { ]), ); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', 'web-server', '--web-port=7357', '--browser-name=chrome', '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -649,21 +638,19 @@ void main() { ]), ); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', 'windows', '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -699,8 +686,6 @@ void main() { ]), ); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ @@ -708,14 +693,14 @@ void main() { flutterCommand, const ['devices', '--machine'], null), ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', _fakeAndroidDevice, '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -833,8 +818,6 @@ void main() { '--enable-experiment=exp1', ]); - final String deviceTestPath = p.join('test_driver', 'plugin.dart'); - final String driverTestPath = p.join('test_driver', 'plugin_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ @@ -842,15 +825,15 @@ void main() { flutterCommand, const ['devices', '--machine'], null), ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', _fakeIosDevice, '--enable-experiment=exp1', '--driver', - driverTestPath, + 'test_driver/plugin_test.dart', '--target', - deviceTestPath + 'test_driver/plugin.dart' ], pluginExampleDirectory.path), ])); @@ -967,7 +950,7 @@ void main() { ); // Simulate failure from `flutter drive`. - processRunner.mockProcessesForExecutable['flutter'] = [ + processRunner.mockProcessesForExecutable[flutterCommand] = [ // No mock for 'devices', since it's running for macOS. MockProcess.failing(), // 'drive' #1 MockProcess.failing(), // 'drive' #2 @@ -994,33 +977,31 @@ void main() { final Directory pluginExampleDirectory = pluginDirectory.childDirectory('example'); - final String driverTestPath = - p.join('test_driver', 'integration_test.dart'); expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', 'macos', '--driver', - driverTestPath, + 'test_driver/integration_test.dart', '--target', - p.join('integration_test', 'bar_test.dart'), + 'integration_test/bar_test.dart', ], pluginExampleDirectory.path), ProcessCall( flutterCommand, - [ + const [ 'drive', '-d', 'macos', '--driver', - driverTestPath, + 'test_driver/integration_test.dart', '--target', - p.join('integration_test', 'foo_test.dart'), + 'integration_test/foo_test.dart', ], pluginExampleDirectory.path), ])); From 1b7383b47de6c2f5e1136766b327effd2ae22e92 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 22:38:42 -0400 Subject: [PATCH 13/19] Restore previous build relative path behavior to minimize behavioral changes in the PR --- script/tool/lib/src/build_examples_command.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index 7690815560c7..0cac09980c94 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -127,7 +127,8 @@ class BuildExamplesCommand extends PackageLoopingCommand { print(''); for (final Directory example in getExamplesForPlugin(package)) { - final String packageName = getRelativePosixPath(example, from: package); + final String packageName = + getRelativePosixPath(example, from: packagesDir); for (final _PlatformDetails platform in buildPlatforms) { String buildPlatform = platform.label; From a5ea741164538d56de76f15ad4bcd7cdc539243f Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 23:10:06 -0400 Subject: [PATCH 14/19] Fix format tests --- .../tool/lib/src/drive_examples_command.dart | 4 +- script/tool/lib/src/format_command.dart | 18 +++-- .../test/drive_examples_command_test.dart | 70 ++++++++++--------- script/tool/test/format_command_test.dart | 15 ++-- script/tool/test/mocks.dart | 5 ++ 5 files changed, 67 insertions(+), 45 deletions(-) diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index e00c7561f226..7e800ed54866 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -6,6 +6,7 @@ import 'dart:convert'; import 'dart:io'; import 'package:file/file.dart'; +import 'package:platform/platform.dart'; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -21,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, diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart index 4af2693e84a8..7954fd044ce4 100644 --- a/script/tool/lib/src/format_command.dart +++ b/script/tool/lib/src/format_command.dart @@ -7,7 +7,7 @@ import 'dart:io' as io; import 'package:file/file.dart'; import 'package:http/http.dart' as http; -import 'package:path/path.dart' as p; +import 'package:platform/platform.dart'; import 'package:quiver/iterables.dart'; import 'common/core.dart'; @@ -28,7 +28,8 @@ class FormatCommand extends PluginCommand { FormatCommand( Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), - }) : super(packagesDir, processRunner: processRunner) { + Platform platform = const LocalPlatform(), + }) : super(packagesDir, processRunner: processRunner, platform: platform) { argParser.addFlag('fail-on-change', hide: true); argParser.addOption('clang-format', defaultsTo: 'clang-format', @@ -168,8 +169,12 @@ class FormatCommand extends PluginCommand { Future> _getFilteredFilePaths(Stream files) async { // Returns a pattern to check for [directories] as a subset of a file path. RegExp pathFragmentForDirectories(List directories) { - final String s = path.separator; - return RegExp('(?:^|$s)${p.joinAll(directories)}$s'); + String s = path.separator; + // Escape the separator for use in the regex. + if (s == r'\') { + s = r'\\'; + } + return RegExp('(?:^|$s)${path.joinAll(directories)}$s'); } return files @@ -188,12 +193,13 @@ class FormatCommand extends PluginCommand { Iterable _getPathsWithExtensions( Iterable files, Set extensions) { - return files.where((String path) => extensions.contains(p.extension(path))); + return files.where( + (String filePath) => extensions.contains(path.extension(filePath))); } Future _getGoogleFormatterPath() async { final String javaFormatterPath = path.join( - path.dirname(p.fromUri(io.Platform.script)), + path.dirname(path.fromUri(platform.script)), 'google-java-format-1.3-all-deps.jar'); final File javaFormatterFile = packagesDir.fileSystem.file(javaFormatterPath); diff --git a/script/tool/test/drive_examples_command_test.dart b/script/tool/test/drive_examples_command_test.dart index fdbd77dd8061..c6893181e286 100644 --- a/script/tool/test/drive_examples_command_test.dart +++ b/script/tool/test/drive_examples_command_test.dart @@ -22,18 +22,18 @@ const String _fakeAndroidDevice = 'emulator-1234'; void main() { group('test drive_example_command', () { late FileSystem fileSystem; + late Platform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; - final String flutterCommand = - const LocalPlatform().isWindows ? 'flutter.bat' : 'flutter'; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final DriveExamplesCommand command = - DriveExamplesCommand(packagesDir, processRunner: processRunner); + final DriveExamplesCommand command = DriveExamplesCommand(packagesDir, + processRunner: processRunner, platform: mockPlatform); runner = CommandRunner( 'drive_examples_command', 'Test for drive_example_command'); @@ -62,9 +62,9 @@ void main() { final MockProcess mockDevicesProcess = MockProcess.succeeding(); mockDevicesProcess.stdoutController.close(); // ignore: unawaited_futures - processRunner.mockProcessesForExecutable[flutterCommand] = [ - mockDevicesProcess - ]; + processRunner + .mockProcessesForExecutable[getFlutterCommand(mockPlatform)] = + [mockDevicesProcess]; processRunner.resultStdout = output; } @@ -149,9 +149,9 @@ void main() { test('fails for iOS if getting devices fails', () async { // Simulate failure from `flutter devices`. - processRunner.mockProcessesForExecutable[flutterCommand] = [ - MockProcess.failing() - ]; + processRunner + .mockProcessesForExecutable[getFlutterCommand(mockPlatform)] = + [MockProcess.failing()]; Error? commandError; final List output = await runCapturingPrint( @@ -218,10 +218,10 @@ void main() { expect( processRunner.recordedCalls, orderedEquals([ + ProcessCall(getFlutterCommand(mockPlatform), + const ['devices', '--machine'], null), ProcessCall( - flutterCommand, const ['devices', '--machine'], null), - ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -337,10 +337,10 @@ void main() { expect( processRunner.recordedCalls, orderedEquals([ + ProcessCall(getFlutterCommand(mockPlatform), + const ['devices', '--machine'], null), ProcessCall( - flutterCommand, const ['devices', '--machine'], null), - ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -352,7 +352,7 @@ void main() { ], pluginExampleDirectory.path), ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -424,7 +424,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -497,7 +497,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -568,7 +568,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -642,7 +642,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -689,10 +689,10 @@ void main() { expect( processRunner.recordedCalls, orderedEquals([ + ProcessCall(getFlutterCommand(mockPlatform), + const ['devices', '--machine'], null), ProcessCall( - flutterCommand, const ['devices', '--machine'], null), - ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -734,8 +734,8 @@ void main() { // Output should be empty other than the device query. expect(processRunner.recordedCalls, [ - ProcessCall( - flutterCommand, const ['devices', '--machine'], null), + ProcessCall(getFlutterCommand(mockPlatform), + const ['devices', '--machine'], null), ]); }); @@ -767,8 +767,8 @@ void main() { // Output should be empty other than the device query. expect(processRunner.recordedCalls, [ - ProcessCall( - flutterCommand, const ['devices', '--machine'], null), + ProcessCall(getFlutterCommand(mockPlatform), + const ['devices', '--machine'], null), ]); }); @@ -821,10 +821,10 @@ void main() { expect( processRunner.recordedCalls, orderedEquals([ + ProcessCall(getFlutterCommand(mockPlatform), + const ['devices', '--machine'], null), ProcessCall( - flutterCommand, const ['devices', '--machine'], null), - ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -950,7 +950,9 @@ void main() { ); // Simulate failure from `flutter drive`. - processRunner.mockProcessesForExecutable[flutterCommand] = [ + processRunner + .mockProcessesForExecutable[getFlutterCommand(mockPlatform)] = + [ // No mock for 'devices', since it's running for macOS. MockProcess.failing(), // 'drive' #1 MockProcess.failing(), // 'drive' #2 @@ -981,7 +983,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', @@ -993,7 +995,7 @@ void main() { ], pluginExampleDirectory.path), ProcessCall( - flutterCommand, + getFlutterCommand(mockPlatform), const [ 'drive', '-d', diff --git a/script/tool/test/format_command_test.dart b/script/tool/test/format_command_test.dart index e7f4d795eb93..fabef31a1b64 100644 --- a/script/tool/test/format_command_test.dart +++ b/script/tool/test/format_command_test.dart @@ -17,21 +17,28 @@ import 'util.dart'; void main() { late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; + late p.Context path; late RecordingProcessRunner processRunner; late CommandRunner runner; late String javaFormatPath; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final FormatCommand analyzeCommand = - FormatCommand(packagesDir, processRunner: processRunner); + final FormatCommand analyzeCommand = FormatCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); // Create the java formatter file that the command checks for, to avoid // a download. - javaFormatPath = p.join(p.dirname(p.fromUri(io.Platform.script)), + path = analyzeCommand.path; + javaFormatPath = path.join(path.dirname(path.fromUri(mockPlatform.script)), 'google-java-format-1.3-all-deps.jar'); fileSystem.file(javaFormatPath).createSync(recursive: true); @@ -42,7 +49,7 @@ void main() { List _getAbsolutePaths( Directory package, List relativePaths) { return relativePaths - .map((String path) => p.join(package.path, path)) + .map((String relativePath) => path.join(package.path, relativePath)) .toList(); } diff --git a/script/tool/test/mocks.dart b/script/tool/test/mocks.dart index 042a455637b5..0dcdedd3db03 100644 --- a/script/tool/test/mocks.dart +++ b/script/tool/test/mocks.dart @@ -24,6 +24,11 @@ class MockPlatform extends Mock implements Platform { @override bool isWindows; + + @override + Uri get script => isWindows + ? Uri.file(r'C:\foo\bar', windows: true) + : Uri.file('/foo/bar', windows: false); } class MockProcess extends Mock implements io.Process { From a131620633ecf3f4f45e53d714bce0660e53d1cb Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 23:13:22 -0400 Subject: [PATCH 15/19] Fix test command tests --- script/tool/lib/src/test_command.dart | 4 +++- script/tool/test/test_command_test.dart | 32 +++++++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/script/tool/lib/src/test_command.dart b/script/tool/lib/src/test_command.dart index d06a2841812a..9dfe66b7926a 100644 --- a/script/tool/lib/src/test_command.dart +++ b/script/tool/lib/src/test_command.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:file/file.dart'; +import 'package:platform/platform.dart'; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -15,7 +16,8 @@ class TestCommand extends PackageLoopingCommand { TestCommand( Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), - }) : super(packagesDir, processRunner: processRunner) { + Platform platform = const LocalPlatform(), + }) : super(packagesDir, processRunner: processRunner, platform: platform) { argParser.addOption( kEnableExperiment, defaultsTo: '', diff --git a/script/tool/test/test_command_test.dart b/script/tool/test/test_command_test.dart index ac0ac4b3dd40..503e24d03056 100644 --- a/script/tool/test/test_command_test.dart +++ b/script/tool/test/test_command_test.dart @@ -10,6 +10,7 @@ import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/test_command.dart'; +import 'package:platform/platform.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -18,16 +19,21 @@ import 'util.dart'; void main() { group('$TestCommand', () { late FileSystem fileSystem; + late Platform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final TestCommand command = - TestCommand(packagesDir, processRunner: processRunner); + final TestCommand command = TestCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner('test_test', 'Test for $TestCommand'); runner.addCommand(command); @@ -44,10 +50,10 @@ void main() { expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall( - 'flutter', const ['test', '--color'], plugin1Dir.path), - ProcessCall( - 'flutter', const ['test', '--color'], plugin2Dir.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['test', '--color'], plugin1Dir.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['test', '--color'], plugin2Dir.path), ]), ); }); @@ -58,7 +64,9 @@ void main() { createFakePlugin('plugin2', packagesDir, extraFiles: ['test/empty_test.dart']); - processRunner.mockProcessesForExecutable['flutter'] = [ + processRunner + .mockProcessesForExecutable[getFlutterCommand(mockPlatform)] = + [ MockProcess.failing(), // plugin 1 test MockProcess.succeeding(), // plugin 2 test ]; @@ -88,8 +96,8 @@ void main() { expect( processRunner.recordedCalls, orderedEquals([ - ProcessCall( - 'flutter', const ['test', '--color'], plugin2Dir.path), + ProcessCall(getFlutterCommand(mockPlatform), + const ['test', '--color'], plugin2Dir.path), ]), ); }); @@ -107,7 +115,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - 'flutter', + getFlutterCommand(mockPlatform), const ['test', '--color', '--enable-experiment=exp1'], pluginDir.path), ProcessCall('dart', const ['pub', 'get'], packageDir.path), @@ -183,7 +191,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - 'flutter', + getFlutterCommand(mockPlatform), const ['test', '--color', '--platform=chrome'], pluginDir.path), ]), @@ -203,7 +211,7 @@ void main() { processRunner.recordedCalls, orderedEquals([ ProcessCall( - 'flutter', + getFlutterCommand(mockPlatform), const ['test', '--color', '--enable-experiment=exp1'], pluginDir.path), ProcessCall('dart', const ['pub', 'get'], packageDir.path), From f82f7c28be80a510beb86443aca16ff307284f59 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 23:16:53 -0400 Subject: [PATCH 16/19] Fix list tests --- script/tool/lib/src/list_command.dart | 6 +++++- script/tool/test/list_command_test.dart | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/script/tool/lib/src/list_command.dart b/script/tool/lib/src/list_command.dart index 39515cf686b0..20f01ff98f0e 100644 --- a/script/tool/lib/src/list_command.dart +++ b/script/tool/lib/src/list_command.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:file/file.dart'; +import 'package:platform/platform.dart'; import 'common/plugin_command.dart'; @@ -10,7 +11,10 @@ import 'common/plugin_command.dart'; class ListCommand extends PluginCommand { /// Creates an instance of the list command, whose behavior depends on the /// 'type' argument it provides. - ListCommand(Directory packagesDir) : super(packagesDir) { + ListCommand( + Directory packagesDir, { + Platform platform = const LocalPlatform(), + }) : super(packagesDir, platform: platform) { argParser.addOption( _type, defaultsTo: _plugin, diff --git a/script/tool/test/list_command_test.dart b/script/tool/test/list_command_test.dart index 836d06671c24..488fc9bcb1e4 100644 --- a/script/tool/test/list_command_test.dart +++ b/script/tool/test/list_command_test.dart @@ -8,18 +8,22 @@ import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/list_command.dart'; import 'package:test/test.dart'; +import 'mocks.dart'; import 'util.dart'; void main() { group('$ListCommand', () { late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); - final ListCommand command = ListCommand(packagesDir); + final ListCommand command = + ListCommand(packagesDir, platform: mockPlatform); runner = CommandRunner('list_test', 'Test for $ListCommand'); runner.addCommand(command); From 2d97a2a40dda7d11c46a807ac6544767c5c8d7b5 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 23:35:40 -0400 Subject: [PATCH 17/19] Fix several more tests --- .../lib/src/firebase_test_lab_command.dart | 4 +++- script/tool/lib/src/java_test_command.dart | 2 ++ .../tool/lib/src/publish_check_command.dart | 4 +++- .../test/firebase_test_lab_command_test.dart | 10 ++++++-- script/tool/test/java_test_command_test.dart | 24 ++++++++++++++----- .../tool/test/publish_check_command_test.dart | 19 +++++++++++---- 6 files changed, 48 insertions(+), 15 deletions(-) diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index 2a411350fa2d..5e4d9f080085 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:io' as io; import 'package:file/file.dart'; +import 'package:platform/platform.dart'; import 'package:uuid/uuid.dart'; import 'common/core.dart'; @@ -20,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', diff --git a/script/tool/lib/src/java_test_command.dart b/script/tool/lib/src/java_test_command.dart index e46eb230df64..b8d4ca566554 100644 --- a/script/tool/lib/src/java_test_command.dart +++ b/script/tool/lib/src/java_test_command.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import 'package:file/file.dart'; +import 'package:platform/platform.dart'; import 'common/core.dart'; import 'common/package_looping_command.dart'; @@ -14,6 +15,7 @@ class JavaTestCommand extends PackageLoopingCommand { JavaTestCommand( Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), + Platform platform = const LocalPlatform(), }) : super(packagesDir, processRunner: processRunner); static const String _gradleWrapper = 'gradlew'; diff --git a/script/tool/lib/src/publish_check_command.dart b/script/tool/lib/src/publish_check_command.dart index dce66db733c9..fda68a6a74a4 100644 --- a/script/tool/lib/src/publish_check_command.dart +++ b/script/tool/lib/src/publish_check_command.dart @@ -8,6 +8,7 @@ import 'dart:io' as io; import 'package:file/file.dart'; import 'package:http/http.dart' as http; +import 'package:platform/platform.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; @@ -22,10 +23,11 @@ class PublishCheckCommand extends PackageLoopingCommand { PublishCheckCommand( Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), + Platform platform = const LocalPlatform(), http.Client? httpClient, }) : _pubVersionFinder = PubVersionFinder(httpClient: httpClient ?? http.Client()), - super(packagesDir, processRunner: processRunner) { + super(packagesDir, processRunner: processRunner, platform: platform) { argParser.addFlag( _allowPrereleaseFlag, help: 'Allows the pre-release SDK warning to pass.\n' diff --git a/script/tool/test/firebase_test_lab_command_test.dart b/script/tool/test/firebase_test_lab_command_test.dart index 0199eba95983..fa60b1207bd1 100644 --- a/script/tool/test/firebase_test_lab_command_test.dart +++ b/script/tool/test/firebase_test_lab_command_test.dart @@ -9,6 +9,7 @@ import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/firebase_test_lab_command.dart'; +import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -17,16 +18,21 @@ import 'util.dart'; void main() { group('$FirebaseTestLabCommand', () { FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final FirebaseTestLabCommand command = - FirebaseTestLabCommand(packagesDir, processRunner: processRunner); + final FirebaseTestLabCommand command = FirebaseTestLabCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner( 'firebase_test_lab_command', 'Test for $FirebaseTestLabCommand'); diff --git a/script/tool/test/java_test_command_test.dart b/script/tool/test/java_test_command_test.dart index 9ae959710984..ffb2b10b8ad0 100644 --- a/script/tool/test/java_test_command_test.dart +++ b/script/tool/test/java_test_command_test.dart @@ -10,6 +10,7 @@ import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/java_test_command.dart'; +import 'package:mockito/mockito.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -19,16 +20,21 @@ import 'util.dart'; void main() { group('$JavaTestCommand', () { late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = RecordingProcessRunner(); - final JavaTestCommand command = - JavaTestCommand(packagesDir, processRunner: processRunner); + final JavaTestCommand command = JavaTestCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner('java_test_test', 'Test for $JavaTestCommand'); @@ -50,13 +56,16 @@ void main() { await runCapturingPrint(runner, ['java-test']); + final Directory androidFolder = + plugin.childDirectory('example').childDirectory('android'); + expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( - p.join(plugin.path, 'example/android/gradlew'), + androidFolder.childFile('gradlew').path, const ['testDebugUnitTest', '--info'], - p.join(plugin.path, 'example/android'), + androidFolder.path, ), ]), ); @@ -77,13 +86,16 @@ void main() { await runCapturingPrint(runner, ['java-test']); + final Directory androidFolder = + plugin.childDirectory('example').childDirectory('android'); + expect( processRunner.recordedCalls, orderedEquals([ ProcessCall( - p.join(plugin.path, 'example/android/gradlew'), + androidFolder.childFile('gradlew').path, const ['testDebugUnitTest', '--info'], - p.join(plugin.path, 'example/android'), + androidFolder.path, ), ]), ); diff --git a/script/tool/test/publish_check_command_test.dart b/script/tool/test/publish_check_command_test.dart index 5140316b4511..11de9f095481 100644 --- a/script/tool/test/publish_check_command_test.dart +++ b/script/tool/test/publish_check_command_test.dart @@ -21,16 +21,21 @@ import 'util.dart'; void main() { group('$PublishCheckProcessRunner tests', () { FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late PublishCheckProcessRunner processRunner; late CommandRunner runner; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = PublishCheckProcessRunner(); - final PublishCheckCommand publishCheckCommand = - PublishCheckCommand(packagesDir, processRunner: processRunner); + final PublishCheckCommand publishCheckCommand = PublishCheckCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner( 'publish_check_command', @@ -339,12 +344,16 @@ void main() { }); expect(hasError, isTrue); - expect(output.first, r''' + expect(output.first, contains(r''' { "status": "error", "humanMessage": [ "\n============================================================\n|| Running for no_publish_a\n============================================================\n", - "Failed to parse `pubspec.yaml` at /packages/no_publish_a/pubspec.yaml: ParsedYamlException: line 1, column 1: Not a map\n ╷\n1 │ bad-yaml\n │ ^^^^^^^^\n ╵}", + "Failed to parse `pubspec.yaml` at /packages/no_publish_a/pubspec.yaml: ParsedYamlException:''')); + // This is split into two checks since the details of the YamlException + // aren't controlled by this package, so asserting its exact format would + // make the test fragile to irrelevant changes in those details. + expect(output.first, contains(r''' "no pubspec", "\n============================================================\n|| Running for no_publish_b\n============================================================\n", "url https://pub.dev/packages/no_publish_b.json", @@ -356,7 +365,7 @@ void main() { " no_publish_a", "See above for full details." ] -}'''); +}''')); }); }); } From 14299e70875ffa9cb38031535f037e2f40f08242 Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Thu, 8 Jul 2021 23:53:56 -0400 Subject: [PATCH 18/19] Fix last tests --- script/tool/lib/src/publish_plugin_command.dart | 6 +++++- script/tool/test/publish_plugin_command_test.dart | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/script/tool/lib/src/publish_plugin_command.dart b/script/tool/lib/src/publish_plugin_command.dart index abdd8c03242c..8bcb9e37e8ef 100644 --- a/script/tool/lib/src/publish_plugin_command.dart +++ b/script/tool/lib/src/publish_plugin_command.dart @@ -208,9 +208,13 @@ class PublishPluginCommand extends PluginCommand { final List packagesFailed = []; for (final String pubspecPath in changedPubspecs) { + // Convert git's Posix-style paths to a path that matches the current + // filesystem. + final String localStylePubspecPath = + path.joinAll(p.posix.split(pubspecPath)); final File pubspecFile = packagesDir.fileSystem .directory(baseGitDir.path) - .childFile(pubspecPath); + .childFile(localStylePubspecPath); final _CheckNeedsReleaseResult result = await _checkNeedsRelease( pubspecFile: pubspecFile, existingTags: existingTags, diff --git a/script/tool/test/publish_plugin_command_test.dart b/script/tool/test/publish_plugin_command_test.dart index 497579b02f89..c7df81952641 100644 --- a/script/tool/test/publish_plugin_command_test.dart +++ b/script/tool/test/publish_plugin_command_test.dart @@ -16,6 +16,7 @@ import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:mockito/mockito.dart'; +import 'package:platform/platform.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -1091,7 +1092,7 @@ class TestProcessRunner extends ProcessRunner { {Directory? workingDirectory}) async { /// Never actually publish anything. Start is always and only used for this /// since it returns something we can route stdin through. - assert(executable == 'flutter' && + assert(executable == getFlutterCommand(const LocalPlatform()) && args.isNotEmpty && args[0] == 'pub' && args[1] == 'publish'); From dd1e333d67391bc55b825102f86dcb02f449967e Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Fri, 9 Jul 2021 00:04:48 -0400 Subject: [PATCH 19/19] Make all the rest of the non-real-FS tests use a mock platform --- script/tool/lib/src/java_test_command.dart | 2 +- script/tool/lib/src/pubspec_check_command.dart | 9 ++++++++- script/tool/lib/src/version_check_command.dart | 9 ++++++++- script/tool/test/common/plugin_command_test.dart | 9 ++++++++- script/tool/test/firebase_test_lab_command_test.dart | 1 - script/tool/test/java_test_command_test.dart | 2 -- script/tool/test/lint_podspecs_command_test.dart | 2 +- script/tool/test/pubspec_check_command_test.dart | 10 ++++++++-- script/tool/test/version_check_command_test.dart | 5 ++++- 9 files changed, 38 insertions(+), 11 deletions(-) diff --git a/script/tool/lib/src/java_test_command.dart b/script/tool/lib/src/java_test_command.dart index b8d4ca566554..b36d1102f109 100644 --- a/script/tool/lib/src/java_test_command.dart +++ b/script/tool/lib/src/java_test_command.dart @@ -16,7 +16,7 @@ class JavaTestCommand extends PackageLoopingCommand { Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), Platform platform = const LocalPlatform(), - }) : super(packagesDir, processRunner: processRunner); + }) : super(packagesDir, processRunner: processRunner, platform: platform); static const String _gradleWrapper = 'gradlew'; diff --git a/script/tool/lib/src/pubspec_check_command.dart b/script/tool/lib/src/pubspec_check_command.dart index 7d39c7322b71..539b170dbea1 100644 --- a/script/tool/lib/src/pubspec_check_command.dart +++ b/script/tool/lib/src/pubspec_check_command.dart @@ -4,6 +4,7 @@ import 'package:file/file.dart'; import 'package:git/git.dart'; +import 'package:platform/platform.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; import 'common/package_looping_command.dart'; @@ -19,8 +20,14 @@ class PubspecCheckCommand extends PackageLoopingCommand { PubspecCheckCommand( 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, + ); // Section order for plugins. Because the 'flutter' section is critical // information for plugins, and usually small, it goes near the top unlike in diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index 779c0dd9fca5..c08600c3f669 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -7,6 +7,7 @@ import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; +import 'package:platform/platform.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; @@ -77,11 +78,17 @@ class VersionCheckCommand extends PackageLoopingCommand { VersionCheckCommand( Directory packagesDir, { ProcessRunner processRunner = const ProcessRunner(), + Platform platform = const LocalPlatform(), GitDir? gitDir, http.Client? httpClient, }) : _pubVersionFinder = PubVersionFinder(httpClient: httpClient ?? http.Client()), - super(packagesDir, processRunner: processRunner, gitDir: gitDir) { + super( + packagesDir, + processRunner: processRunner, + platform: platform, + gitDir: gitDir, + ) { argParser.addFlag( _againstPubFlag, help: 'Whether the version check should run against the version on pub.\n' diff --git a/script/tool/test/common/plugin_command_test.dart b/script/tool/test/common/plugin_command_test.dart index 3f1f1adc4c19..fdab9612be3f 100644 --- a/script/tool/test/common/plugin_command_test.dart +++ b/script/tool/test/common/plugin_command_test.dart @@ -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 runner; late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late Directory thirdPartyPackagesDir; late List 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 _plugins; diff --git a/script/tool/test/firebase_test_lab_command_test.dart b/script/tool/test/firebase_test_lab_command_test.dart index fa60b1207bd1..c265868bbf3e 100644 --- a/script/tool/test/firebase_test_lab_command_test.dart +++ b/script/tool/test/firebase_test_lab_command_test.dart @@ -9,7 +9,6 @@ import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/firebase_test_lab_command.dart'; -import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import 'mocks.dart'; diff --git a/script/tool/test/java_test_command_test.dart b/script/tool/test/java_test_command_test.dart index ffb2b10b8ad0..13e0e7fc0f40 100644 --- a/script/tool/test/java_test_command_test.dart +++ b/script/tool/test/java_test_command_test.dart @@ -10,8 +10,6 @@ import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/java_test_command.dart'; -import 'package:mockito/mockito.dart'; -import 'package:path/path.dart' as p; import 'package:test/test.dart'; import 'mocks.dart'; diff --git a/script/tool/test/lint_podspecs_command_test.dart b/script/tool/test/lint_podspecs_command_test.dart index c15df98acc46..51a4e6267770 100644 --- a/script/tool/test/lint_podspecs_command_test.dart +++ b/script/tool/test/lint_podspecs_command_test.dart @@ -23,7 +23,7 @@ void main() { late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); + fileSystem = MemoryFileSystem(style: FileSystemStyle.posix); packagesDir = createPackagesDirectory(fileSystem: fileSystem); mockPlatform = MockPlatform(isMacOS: true); diff --git a/script/tool/test/pubspec_check_command_test.dart b/script/tool/test/pubspec_check_command_test.dart index 9e633e21b4ab..177ed7f25b4e 100644 --- a/script/tool/test/pubspec_check_command_test.dart +++ b/script/tool/test/pubspec_check_command_test.dart @@ -9,6 +9,7 @@ import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/pubspec_check_command.dart'; import 'package:test/test.dart'; +import 'mocks.dart'; import 'util.dart'; void main() { @@ -16,15 +17,20 @@ void main() { late CommandRunner runner; late RecordingProcessRunner processRunner; late FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = fileSystem.currentDirectory.childDirectory('packages'); createPackagesDirectory(parentDir: packagesDir.parent); processRunner = RecordingProcessRunner(); - final PubspecCheckCommand command = - PubspecCheckCommand(packagesDir, processRunner: processRunner); + final PubspecCheckCommand command = PubspecCheckCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + ); runner = CommandRunner( 'pubspec_check_command', 'Test for pubspec_check_command'); diff --git a/script/tool/test/version_check_command_test.dart b/script/tool/test/version_check_command_test.dart index 6fbed9c691b3..587de1a58cd9 100644 --- a/script/tool/test/version_check_command_test.dart +++ b/script/tool/test/version_check_command_test.dart @@ -18,6 +18,7 @@ import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; import 'common/plugin_command_test.mocks.dart'; +import 'mocks.dart'; import 'util.dart'; void testAllowedVersion( @@ -46,6 +47,7 @@ void main() { const String indentation = ' '; group('$VersionCheckCommand', () { FileSystem fileSystem; + late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; @@ -55,6 +57,7 @@ void main() { setUp(() { fileSystem = MemoryFileSystem(); + mockPlatform = MockPlatform(); packagesDir = createPackagesDirectory(fileSystem: fileSystem); gitDirCommands = >[]; gitShowResponses = {}; @@ -80,7 +83,7 @@ void main() { }); processRunner = RecordingProcessRunner(); final VersionCheckCommand command = VersionCheckCommand(packagesDir, - processRunner: processRunner, gitDir: gitDir); + processRunner: processRunner, platform: mockPlatform, gitDir: gitDir); runner = CommandRunner( 'version_check_command', 'Test for $VersionCheckCommand');