From 571e1af7be2d6011228665924ab55ed2c6fb78cc Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Wed, 26 Feb 2025 16:18:28 -0500 Subject: [PATCH] [tool] Move changed file detection to base command class Consolidates the code to find all changed file paths into the `PackageLoopingCommand` class that is the base of almost all of the repo tooling commands. This in a preparatory PR for a future change to allow each command to define a list of files or file patterns that definitively *don't* affect that test, so that CI can be smarter about what tests to run (e.g., not running expensive integration tests for README changes). A side effect of this change is that tests of almost all commands now need a mock `GitDir` instance. This would add a lot of copy/pasted boilerplate to the test setup, and there is already too much of that, so instead this refactors common test setup: - Creating a memory file system - Populating it with a packages directory - Creating a RecordingProcessRunner to mock out process calls - Creating a mock GitDir that forwards to a RecordingProcessRunner into a helper method (using records and destructuring to easily return multiple values). While some tests don't need all of these steps, those that don't can easily ignore parts of it, and it will make it much easier to update tests in the future if they need them, and it makes the setup much more consistent which makes it easier to reason about test setup in general. Prep for https://github.com/flutter/flutter/issues/136394 --- script/tool/lib/src/analyze_command.dart | 1 + .../tool/lib/src/build_examples_command.dart | 1 + .../lib/src/common/git_version_finder.dart | 9 - .../src/common/package_looping_command.dart | 17 ++ script/tool/lib/src/custom_test_command.dart | 1 + script/tool/lib/src/dart_test_command.dart | 1 + .../tool/lib/src/drive_examples_command.dart | 1 + .../src/federation_safety_check_command.dart | 3 +- script/tool/lib/src/fetch_deps_command.dart | 1 + .../lib/src/firebase_test_lab_command.dart | 1 + script/tool/lib/src/fix_command.dart | 1 + script/tool/lib/src/format_command.dart | 1 + script/tool/lib/src/gradle_check_command.dart | 5 +- script/tool/lib/src/lint_android_command.dart | 1 + script/tool/lib/src/native_test_command.dart | 1 + .../tool/lib/src/podspec_check_command.dart | 1 + .../tool/lib/src/publish_check_command.dart | 1 + script/tool/lib/src/publish_command.dart | 9 +- .../src/remove_dev_dependencies_command.dart | 5 +- .../lib/src/update_dependency_command.dart | 1 + .../tool/lib/src/update_min_sdk_command.dart | 5 +- .../lib/src/update_release_info_command.dart | 15 +- .../tool/lib/src/version_check_command.dart | 12 +- .../tool/lib/src/xcode_analyze_command.dart | 1 + script/tool/test/analyze_command_test.dart | 10 +- .../test/build_examples_command_test.dart | 10 +- .../test/common/git_version_finder_test.dart | 12 -- script/tool/test/common/gradle_test.dart | 31 ++- .../test/common/package_command_test.dart | 176 ++++++++--------- .../common/package_looping_command_test.dart | 8 +- .../test/common/package_state_utils_test.dart | 6 +- .../tool/test/common/plugin_utils_test.dart | 6 +- script/tool/test/common/pub_utils_test.dart | 7 +- .../test/common/repository_package_test.dart | 7 +- .../create_all_packages_app_command_test.dart | 11 +- .../tool/test/custom_test_command_test.dart | 17 +- script/tool/test/dart_test_command_test.dart | 10 +- .../test/dependabot_check_command_test.dart | 15 +- .../test/drive_examples_command_test.dart | 17 +- .../federation_safety_check_command_test.dart | 57 ++---- script/tool/test/fetch_deps_command_test.dart | 11 +- .../test/firebase_test_lab_command_test.dart | 10 +- script/tool/test/fix_command_test.dart | 10 +- script/tool/test/format_command_test.dart | 14 +- .../tool/test/gradle_check_command_test.dart | 10 +- .../tool/test/license_check_command_test.dart | 15 +- .../tool/test/lint_android_command_test.dart | 10 +- script/tool/test/list_command_test.dart | 6 +- .../make_deps_path_based_command_test.dart | 77 ++++---- script/tool/test/mocks.dart | 27 +++ .../tool/test/native_test_command_test.dart | 28 +-- .../tool/test/podspec_check_command_test.dart | 11 +- .../tool/test/publish_check_command_test.dart | 65 ++----- script/tool/test/publish_command_test.dart | 34 ++-- .../tool/test/pubspec_check_command_test.dart | 24 ++- .../tool/test/readme_check_command_test.dart | 13 +- .../remove_dev_dependencies_command_test.dart | 9 +- .../repo_package_info_check_command_test.dart | 15 +- .../test/update_dependency_command_test.dart | 10 +- .../test/update_excerpts_command_test.dart | 17 +- .../test/update_min_sdk_command_test.dart | 9 +- .../update_release_info_command_test.dart | 39 ++-- script/tool/test/util.dart | 59 ++++-- .../tool/test/version_check_command_test.dart | 183 +++++++++--------- .../tool/test/xcode_analyze_command_test.dart | 17 +- 65 files changed, 586 insertions(+), 632 deletions(-) diff --git a/script/tool/lib/src/analyze_command.dart b/script/tool/lib/src/analyze_command.dart index ec36d830b14..4651dbb42c7 100644 --- a/script/tool/lib/src/analyze_command.dart +++ b/script/tool/lib/src/analyze_command.dart @@ -17,6 +17,7 @@ class AnalyzeCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addMultiOption(_customAnalysisFlag, help: diff --git a/script/tool/lib/src/build_examples_command.dart b/script/tool/lib/src/build_examples_command.dart index d961201f716..4a5c0272853 100644 --- a/script/tool/lib/src/build_examples_command.dart +++ b/script/tool/lib/src/build_examples_command.dart @@ -48,6 +48,7 @@ class BuildExamplesCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addFlag(platformLinux); argParser.addFlag(platformMacOS); diff --git a/script/tool/lib/src/common/git_version_finder.dart b/script/tool/lib/src/common/git_version_finder.dart index 0956e3ca9ec..c07fee1505d 100644 --- a/script/tool/lib/src/common/git_version_finder.dart +++ b/script/tool/lib/src/common/git_version_finder.dart @@ -28,15 +28,6 @@ class GitVersionFinder { /// The base branche used to find a merge point if baseSha is not provided. final String _baseBranch; - static bool _isPubspec(String file) { - return file.trim().endsWith('pubspec.yaml'); - } - - /// Get a list of all the pubspec.yaml file that is changed. - Future> getChangedPubSpecs() async { - return (await getChangedFiles()).where(_isPubspec).toList(); - } - /// Get a list of all the changed files. Future> getChangedFiles( {bool includeUncommitted = false}) async { diff --git a/script/tool/lib/src/common/package_looping_command.dart b/script/tool/lib/src/common/package_looping_command.dart index b9c7a87c067..4ac90ae80b6 100644 --- a/script/tool/lib/src/common/package_looping_command.dart +++ b/script/tool/lib/src/common/package_looping_command.dart @@ -9,6 +9,7 @@ import 'package:path/path.dart' as p; import 'package:pub_semver/pub_semver.dart'; import 'core.dart'; +import 'git_version_finder.dart'; import 'output_utils.dart'; import 'package_command.dart'; import 'repository_package.dart'; @@ -226,6 +227,17 @@ abstract class PackageLoopingCommand extends PackageCommand { /// The suggested indentation for printed output. String get indentation => hasLongOutput ? '' : ' '; + /// The base SHA used to calculate changed files. + /// + /// This is guaranteed to be populated before [initializeRun] is called. + late String baseSha; + + /// The repo-relative paths (using Posix separators) of all files changed + /// relative to [baseSha]. + /// + /// This is guaranteed to be populated before [initializeRun] is called. + late List changedFiles; + // ---------------------------------------- @override @@ -264,6 +276,11 @@ abstract class PackageLoopingCommand extends PackageCommand { final DateTime runStart = DateTime.now(); + // Populate the list of changed files for subclasses to use. + final GitVersionFinder gitVersionFinder = await retrieveVersionFinder(); + baseSha = await gitVersionFinder.getBaseSha(); + changedFiles = await gitVersionFinder.getChangedFiles(); + await initializeRun(); final List targetPackages = diff --git a/script/tool/lib/src/custom_test_command.dart b/script/tool/lib/src/custom_test_command.dart index 35f74d0809e..bc81b27061e 100644 --- a/script/tool/lib/src/custom_test_command.dart +++ b/script/tool/lib/src/custom_test_command.dart @@ -21,6 +21,7 @@ class CustomTestCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }); @override diff --git a/script/tool/lib/src/dart_test_command.dart b/script/tool/lib/src/dart_test_command.dart index fcad751f471..5e99211169e 100644 --- a/script/tool/lib/src/dart_test_command.dart +++ b/script/tool/lib/src/dart_test_command.dart @@ -27,6 +27,7 @@ class DartTestCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addOption( kEnableExperiment, diff --git a/script/tool/lib/src/drive_examples_command.dart b/script/tool/lib/src/drive_examples_command.dart index 26c0173fd96..c26c4850072 100644 --- a/script/tool/lib/src/drive_examples_command.dart +++ b/script/tool/lib/src/drive_examples_command.dart @@ -27,6 +27,7 @@ class DriveExamplesCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addFlag(platformAndroid, help: 'Runs the Android implementation of the examples', diff --git a/script/tool/lib/src/federation_safety_check_command.dart b/script/tool/lib/src/federation_safety_check_command.dart index f4247de49b2..75b6edc2990 100644 --- a/script/tool/lib/src/federation_safety_check_command.dart +++ b/script/tool/lib/src/federation_safety_check_command.dart @@ -59,9 +59,8 @@ class FederationSafetyCheckCommand extends PackageLoopingCommand { @override Future initializeRun() async { final GitVersionFinder gitVersionFinder = await retrieveVersionFinder(); - final String baseSha = await gitVersionFinder.getBaseSha(); print('Validating changes relative to "$baseSha"\n'); - for (final String path in await gitVersionFinder.getChangedFiles()) { + for (final String path in changedFiles) { // Git output always uses Posix paths. final List allComponents = p.posix.split(path); final int packageIndex = allComponents.indexOf('packages'); diff --git a/script/tool/lib/src/fetch_deps_command.dart b/script/tool/lib/src/fetch_deps_command.dart index 61a82487c0f..a6ac9b7a520 100644 --- a/script/tool/lib/src/fetch_deps_command.dart +++ b/script/tool/lib/src/fetch_deps_command.dart @@ -27,6 +27,7 @@ class FetchDepsCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addFlag(_dartFlag, defaultsTo: true, help: 'Run "pub get"'); argParser.addFlag(_supportingTargetPlatformsOnlyFlag, diff --git a/script/tool/lib/src/firebase_test_lab_command.dart b/script/tool/lib/src/firebase_test_lab_command.dart index 5d2ab937c35..eb03f5c4906 100644 --- a/script/tool/lib/src/firebase_test_lab_command.dart +++ b/script/tool/lib/src/firebase_test_lab_command.dart @@ -23,6 +23,7 @@ class FirebaseTestLabCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addOption( _gCloudProjectArg, diff --git a/script/tool/lib/src/fix_command.dart b/script/tool/lib/src/fix_command.dart index 453434edc3d..fa5eea3349d 100644 --- a/script/tool/lib/src/fix_command.dart +++ b/script/tool/lib/src/fix_command.dart @@ -13,6 +13,7 @@ class FixCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }); @override diff --git a/script/tool/lib/src/format_command.dart b/script/tool/lib/src/format_command.dart index 5fbcd93265b..7d13919d96e 100644 --- a/script/tool/lib/src/format_command.dart +++ b/script/tool/lib/src/format_command.dart @@ -49,6 +49,7 @@ class FormatCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) { argParser.addFlag(_failonChangeArg, hide: true); argParser.addFlag(_dartArg, help: 'Format Dart files', defaultsTo: true); diff --git a/script/tool/lib/src/gradle_check_command.dart b/script/tool/lib/src/gradle_check_command.dart index c7486a9b09c..75ce3d5dbbc 100644 --- a/script/tool/lib/src/gradle_check_command.dart +++ b/script/tool/lib/src/gradle_check_command.dart @@ -19,7 +19,10 @@ final Version minKotlinVersion = Version(1, 7, 10); /// A command to enforce gradle file conventions and best practices. class GradleCheckCommand extends PackageLoopingCommand { /// Creates an instance of the gradle check command. - GradleCheckCommand(super.packagesDir); + GradleCheckCommand( + super.packagesDir, { + super.gitDir, + }); @override final String name = 'gradle-check'; diff --git a/script/tool/lib/src/lint_android_command.dart b/script/tool/lib/src/lint_android_command.dart index ed9394b8dbb..b893d9b0315 100644 --- a/script/tool/lib/src/lint_android_command.dart +++ b/script/tool/lib/src/lint_android_command.dart @@ -18,6 +18,7 @@ class LintAndroidCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }); @override diff --git a/script/tool/lib/src/native_test_command.dart b/script/tool/lib/src/native_test_command.dart index 508071c57e3..d5995a35cd2 100644 --- a/script/tool/lib/src/native_test_command.dart +++ b/script/tool/lib/src/native_test_command.dart @@ -45,6 +45,7 @@ class NativeTestCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, Abi? abi, }) : _abi = abi ?? Abi.current(), _xcode = Xcode(processRunner: processRunner, log: true) { diff --git a/script/tool/lib/src/podspec_check_command.dart b/script/tool/lib/src/podspec_check_command.dart index 01ff7d04fe2..bd52b547bbd 100644 --- a/script/tool/lib/src/podspec_check_command.dart +++ b/script/tool/lib/src/podspec_check_command.dart @@ -25,6 +25,7 @@ class PodspecCheckCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }); @override diff --git a/script/tool/lib/src/publish_check_command.dart b/script/tool/lib/src/publish_check_command.dart index 4dcaa7cf930..5184000522d 100644 --- a/script/tool/lib/src/publish_check_command.dart +++ b/script/tool/lib/src/publish_check_command.dart @@ -23,6 +23,7 @@ class PublishCheckCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, http.Client? httpClient, }) : _pubVersionFinder = PubVersionFinder(httpClient: httpClient ?? http.Client()) { diff --git a/script/tool/lib/src/publish_command.dart b/script/tool/lib/src/publish_command.dart index 39aadee79a1..fb0d4e1a4e4 100644 --- a/script/tool/lib/src/publish_command.dart +++ b/script/tool/lib/src/publish_command.dart @@ -17,7 +17,6 @@ import 'package:yaml/yaml.dart'; import 'common/core.dart'; import 'common/file_utils.dart'; -import 'common/git_version_finder.dart'; import 'common/output_utils.dart'; import 'common/package_command.dart'; import 'common/package_looping_command.dart'; @@ -175,12 +174,12 @@ class PublishCommand extends PackageLoopingCommand { @override Stream getPackagesToProcess() async* { if (getBoolArg(_allChangedFlag)) { - final GitVersionFinder gitVersionFinder = await retrieveVersionFinder(); - final String baseSha = await gitVersionFinder.getBaseSha(); print( 'Publishing all packages that have changed relative to "$baseSha"\n'); - final List changedPubspecs = - await gitVersionFinder.getChangedPubSpecs(); + + final List changedPubspecs = changedFiles + .where((String file) => file.trim().endsWith('pubspec.yaml')) + .toList(); for (final String pubspecPath in changedPubspecs) { // git outputs a relativa, Posix-style path. diff --git a/script/tool/lib/src/remove_dev_dependencies_command.dart b/script/tool/lib/src/remove_dev_dependencies_command.dart index 0b74e79f00d..41f6ff44fd7 100644 --- a/script/tool/lib/src/remove_dev_dependencies_command.dart +++ b/script/tool/lib/src/remove_dev_dependencies_command.dart @@ -15,7 +15,10 @@ import 'common/repository_package.dart'; /// clients of the library, but not for development of the library. class RemoveDevDependenciesCommand extends PackageLoopingCommand { /// Creates a publish metadata updater command instance. - RemoveDevDependenciesCommand(super.packagesDir); + RemoveDevDependenciesCommand( + super.packagesDir, { + super.gitDir, + }); @override final String name = 'remove-dev-dependencies'; diff --git a/script/tool/lib/src/update_dependency_command.dart b/script/tool/lib/src/update_dependency_command.dart index c48dedcd795..23a01eda173 100644 --- a/script/tool/lib/src/update_dependency_command.dart +++ b/script/tool/lib/src/update_dependency_command.dart @@ -32,6 +32,7 @@ class UpdateDependencyCommand extends PackageLoopingCommand { UpdateDependencyCommand( super.packagesDir, { super.processRunner, + super.gitDir, http.Client? httpClient, }) : _pubVersionFinder = PubVersionFinder(httpClient: httpClient ?? http.Client()) { diff --git a/script/tool/lib/src/update_min_sdk_command.dart b/script/tool/lib/src/update_min_sdk_command.dart index 603886ce94a..3e63c8c01e9 100644 --- a/script/tool/lib/src/update_min_sdk_command.dart +++ b/script/tool/lib/src/update_min_sdk_command.dart @@ -15,7 +15,10 @@ const int _exitUnknownVersion = 3; /// A command to update the minimum Flutter and Dart SDKs of packages. class UpdateMinSdkCommand extends PackageLoopingCommand { /// Creates a publish metadata updater command instance. - UpdateMinSdkCommand(super.packagesDir) { + UpdateMinSdkCommand( + super.packagesDir, { + super.gitDir, + }) { argParser.addOption(_flutterMinFlag, mandatory: true, help: 'The minimum version of Flutter to set SDK constraints to.'); diff --git a/script/tool/lib/src/update_release_info_command.dart b/script/tool/lib/src/update_release_info_command.dart index c35759668b6..d55d3d96b69 100644 --- a/script/tool/lib/src/update_release_info_command.dart +++ b/script/tool/lib/src/update_release_info_command.dart @@ -7,7 +7,6 @@ import 'package:file/file.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:yaml_edit/yaml_edit.dart'; -import 'common/git_version_finder.dart'; import 'common/output_utils.dart'; import 'common/package_looping_command.dart'; import 'common/package_state_utils.dart'; @@ -76,11 +75,6 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand { // If null, either there is no version change, or it is dynamic (`minimal`). _VersionIncrementType? _versionChange; - // The cache of changed files, for dynamic version change determination. - // - // Only set for `minimal` version change. - late final List _changedFiles; - @override final String name = 'update-release-info'; @@ -103,12 +97,6 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand { case _versionBugfix: _versionChange = _VersionIncrementType.bugfix; case _versionMinimal: - final GitVersionFinder gitVersionFinder = await retrieveVersionFinder(); - // If the line below fails with "Not a valid object name FETCH_HEAD" - // run "git fetch", FETCH_HEAD is a temporary reference that only exists - // after a fetch. This can happen when a branch is made locally and - // pushed but never fetched. - _changedFiles = await gitVersionFinder.getChangedFiles(); // Anothing other than a fixed change is null. _versionChange = null; case _versionNext: @@ -133,8 +121,7 @@ class UpdateReleaseInfoCommand extends PackageLoopingCommand { final String relativePackagePath = getRelativePosixPath(package.directory, from: gitRoot); final PackageChangeState state = await checkPackageChangeState(package, - changedPaths: _changedFiles, - relativePackagePath: relativePackagePath); + changedPaths: changedFiles, relativePackagePath: relativePackagePath); if (!state.hasChanges) { return PackageResult.skip('No changes to package'); diff --git a/script/tool/lib/src/version_check_command.dart b/script/tool/lib/src/version_check_command.dart index 267dd79fbf3..c3e36bb7087 100644 --- a/script/tool/lib/src/version_check_command.dart +++ b/script/tool/lib/src/version_check_command.dart @@ -154,8 +154,6 @@ class VersionCheckCommand extends PackageLoopingCommand { final PubVersionFinder _pubVersionFinder; late final GitVersionFinder _gitVersionFinder; - late final String _mergeBase; - late final List _changedFiles; late final Set _prLabels = _getPRLabels(); @@ -177,8 +175,6 @@ class VersionCheckCommand extends PackageLoopingCommand { @override Future initializeRun() async { _gitVersionFinder = await retrieveVersionFinder(); - _mergeBase = await _gitVersionFinder.getBaseSha(); - _changedFiles = await _gitVersionFinder.getChangedFiles(); } @override @@ -279,7 +275,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} final String gitPath = path.style == p.Style.windows ? p.posix.joinAll(path.split(relativePath)) : relativePath; - return _gitVersionFinder.getPackageVersion(gitPath, gitRef: _mergeBase); + return _gitVersionFinder.getPackageVersion(gitPath, gitRef: baseSha); } /// Returns the state of the verison of [package] relative to the comparison @@ -303,7 +299,7 @@ ${indentation}HTTP response: ${pubVersionFinderResponse.httpResponse.body} '$indentation${pubspec.name}: Current largest version on pub: $previousVersion'); } } else { - previousVersionSource = _mergeBase; + previousVersionSource = baseSha; previousVersion = await _getPreviousVersionFromGit(package) ?? Version.none; } @@ -535,7 +531,7 @@ ${indentation}The first version listed in CHANGELOG.md is $fromChangeLog. /// This should only be called if the version did not change. Future _checkForMissingChangeError(RepositoryPackage package) async { // Find the relative path to the current package, as it would appear at the - // beginning of a path reported by getChangedFiles() (which always uses + // beginning of a path reported by changedFiles (which always uses // Posix paths). final Directory gitRoot = packagesDir.fileSystem.directory((await gitDir).path); @@ -543,7 +539,7 @@ ${indentation}The first version listed in CHANGELOG.md is $fromChangeLog. getRelativePosixPath(package.directory, from: gitRoot); final PackageChangeState state = await checkPackageChangeState(package, - changedPaths: _changedFiles, + changedPaths: changedFiles, relativePackagePath: relativePackagePath, git: await retrieveVersionFinder()); diff --git a/script/tool/lib/src/xcode_analyze_command.dart b/script/tool/lib/src/xcode_analyze_command.dart index ba4b3fb5e95..6d6bd489649 100644 --- a/script/tool/lib/src/xcode_analyze_command.dart +++ b/script/tool/lib/src/xcode_analyze_command.dart @@ -16,6 +16,7 @@ class XcodeAnalyzeCommand extends PackageLoopingCommand { super.packagesDir, { super.processRunner, super.platform, + super.gitDir, }) : _xcode = Xcode(processRunner: processRunner, log: true) { argParser.addFlag(platformIOS, help: 'Analyze iOS'); argParser.addFlag(platformMacOS, help: 'Analyze macOS'); diff --git a/script/tool/test/analyze_command_test.dart b/script/tool/test/analyze_command_test.dart index 27e92dd5e00..f27503cabff 100644 --- a/script/tool/test/analyze_command_test.dart +++ b/script/tool/test/analyze_command_test.dart @@ -4,29 +4,29 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/analyze_command.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; 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 GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final AnalyzeCommand analyzeCommand = AnalyzeCommand( packagesDir, processRunner: processRunner, + gitDir: gitDir, platform: mockPlatform, ); diff --git a/script/tool/test/build_examples_command_test.dart b/script/tool/test/build_examples_command_test.dart index e2f78fcdc5b..2e2a2112baa 100644 --- a/script/tool/test/build_examples_command_test.dart +++ b/script/tool/test/build_examples_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -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:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -15,21 +15,21 @@ import 'util.dart'; void main() { group('build-example', () { - 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 GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final BuildExamplesCommand command = BuildExamplesCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/common/git_version_finder_test.dart b/script/tool/test/common/git_version_finder_test.dart index 5b2f30b13a3..c6b350f6898 100644 --- a/script/tool/test/common/git_version_finder_test.dart +++ b/script/tool/test/common/git_version_finder_test.dart @@ -56,18 +56,6 @@ file2/file2.cc expect(changedFiles, equals(['file1/file1.cc', 'file2/file2.cc'])); }); - test('get correct pubspec change based on git diff', () async { - gitDiffResponse = ''' -file1/pubspec.yaml -file2/file2.cc -'''; - final GitVersionFinder finder = - GitVersionFinder(gitDir, baseSha: 'some base sha'); - final List changedFiles = await finder.getChangedPubSpecs(); - - expect(changedFiles, equals(['file1/pubspec.yaml'])); - }); - test('use correct base sha if not specified', () async { mergeBaseResponse = 'shaqwiueroaaidf12312jnadf123nd'; gitDiffResponse = ''' diff --git a/script/tool/test/common/gradle_test.dart b/script/tool/test/common/gradle_test.dart index 4f06900eb8b..be184dfb4a0 100644 --- a/script/tool/test/common/gradle_test.dart +++ b/script/tool/test/common/gradle_test.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/gradle.dart'; import 'package:test/test.dart'; @@ -11,18 +10,17 @@ import '../mocks.dart'; import '../util.dart'; void main() { - late FileSystem fileSystem; + late Directory packagesDir; late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); - processRunner = RecordingProcessRunner(); + (:packagesDir, :processRunner, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(); }); group('isConfigured', () { test('reports true when configured on Windows', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/gradlew.bat']); final GradleProject project = GradleProject( plugin, @@ -34,8 +32,7 @@ void main() { }); test('reports true when configured on non-Windows', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/gradlew']); final GradleProject project = GradleProject( plugin, @@ -47,8 +44,7 @@ void main() { }); test('reports false when not configured on Windows', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/foo']); final GradleProject project = GradleProject( plugin, @@ -60,8 +56,7 @@ void main() { }); test('reports true when configured on non-Windows', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/foo']); final GradleProject project = GradleProject( plugin, @@ -75,8 +70,7 @@ void main() { group('runCommand', () { test('runs without arguments', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/gradlew']); final GradleProject project = GradleProject( plugin, @@ -103,8 +97,7 @@ void main() { }); test('runs with arguments', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/gradlew']); final GradleProject project = GradleProject( plugin, @@ -136,8 +129,7 @@ void main() { }); test('runs with the correct wrapper on Windows', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/gradlew.bat']); final GradleProject project = GradleProject( plugin, @@ -164,8 +156,7 @@ void main() { }); test('returns error codes', () async { - final RepositoryPackage plugin = createFakePlugin( - 'plugin', fileSystem.directory('/'), + final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, extraFiles: ['android/gradlew.bat']); final GradleProject project = GradleProject( plugin, diff --git a/script/tool/test/common/package_command_test.dart b/script/tool/test/common/package_command_test.dart index 099efee6b2e..c6ea0074e8d 100644 --- a/script/tool/test/common/package_command_test.dart +++ b/script/tool/test/common/package_command_test.dart @@ -4,53 +4,46 @@ import 'package:args/command_runner.dart'; 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/common/package_command.dart'; import 'package:git/git.dart'; import 'package:mockito/annotations.dart'; -import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; import '../mocks.dart'; import '../util.dart'; -import 'package_command_test.mocks.dart'; @GenerateMocks([GitDir]) void main() { + late GitDir gitDir; late RecordingProcessRunner processRunner; + late RecordingProcessRunner gitProcessRunner; late SamplePackageCommand command; late CommandRunner runner; - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late Directory thirdPartyPackagesDir; + SamplePackageCommand configureCommand({bool includeSubpackages = false}) { + final SamplePackageCommand command = SamplePackageCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + gitDir: gitDir, + includeSubpackages: includeSubpackages, + ); + return command; + } + setUp(() { - fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + (:packagesDir, :processRunner, :gitProcessRunner, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); thirdPartyPackagesDir = packagesDir.parent .childDirectory('third_party') .childDirectory('packages'); - final MockGitDir gitDir = MockGitDir(); - when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) - .thenAnswer((Invocation invocation) { - final List arguments = - invocation.positionalArguments[0]! as List; - // Attach the first argument to the command to make targeting the mock - // results easier. - final String gitCommand = arguments.removeAt(0); - return processRunner.run('git-$gitCommand', arguments); - }); - processRunner = RecordingProcessRunner(); - command = SamplePackageCommand( - packagesDir, - processRunner: processRunner, - platform: mockPlatform, - gitDir: gitDir, - ); + command = configureCommand(); runner = CommandRunner('common_command', 'Test for common functionality'); runner.addCommand(command); @@ -277,7 +270,8 @@ void main() { test( 'specifying the app-facing package of a federated plugin using its ' 'fully qualified name should include only that package', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1/plugin1.dart ''')), @@ -297,7 +291,8 @@ packages/plugin1/plugin1/plugin1.dart test( 'specifying a package of a federated plugin by its name should ' 'include only that package', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1/plugin1.dart ''')), @@ -320,13 +315,8 @@ packages/plugin1/plugin1/plugin1.dart }); test('returns subpackages after the enclosing package', () async { - final SamplePackageCommand localCommand = SamplePackageCommand( - packagesDir, - processRunner: processRunner, - platform: mockPlatform, - gitDir: MockGitDir(), - includeSubpackages: true, - ); + final SamplePackageCommand localCommand = + configureCommand(includeSubpackages: true); final CommandRunner localRunner = CommandRunner('common_command', 'subpackage testing'); localRunner.addCommand(localCommand); @@ -422,7 +412,7 @@ packages/plugin1/plugin1/plugin1.dart group('current-package', () { test('throws when run from outside of the packages directory', () async { - fileSystem.currentDirectory = packagesDir.parent; + packagesDir.fileSystem.currentDirectory = packagesDir.parent; Error? commandError; final List output = await runCapturingPrint(runner, [ @@ -442,7 +432,7 @@ packages/plugin1/plugin1/plugin1.dart }); test('throws when run directly in the packages directory', () async { - fileSystem.currentDirectory = packagesDir; + packagesDir.fileSystem.currentDirectory = packagesDir; Error? commandError; final List output = await runCapturingPrint(runner, [ @@ -465,7 +455,7 @@ packages/plugin1/plugin1/plugin1.dart final RepositoryPackage package = createFakePlugin('a_package', packagesDir); createFakePlugin('another_package', packagesDir); - fileSystem.currentDirectory = package.directory; + packagesDir.fileSystem.currentDirectory = package.directory; await runCapturingPrint( runner, ['sample', '--current-package']); @@ -478,7 +468,7 @@ packages/plugin1/plugin1/plugin1.dart final RepositoryPackage package = createFakePlugin('a_package', thirdPartyPackagesDir); createFakePlugin('another_package', thirdPartyPackagesDir); - fileSystem.currentDirectory = package.directory; + packagesDir.fileSystem.currentDirectory = package.directory; await runCapturingPrint( runner, ['sample', '--current-package']); @@ -493,7 +483,7 @@ packages/plugin1/plugin1/plugin1.dart createFakePlugin(pluginName, groupDir); createFakePlugin('${pluginName}_someplatform', groupDir); createFakePackage('${pluginName}_platform_interface', groupDir); - fileSystem.currentDirectory = package.directory; + packagesDir.fileSystem.currentDirectory = package.directory; await runCapturingPrint( runner, ['sample', '--current-package']); @@ -507,7 +497,8 @@ packages/plugin1/plugin1/plugin1.dart 'a_package', packagesDir, examples: ['a', 'b', 'c']); createFakePlugin('another_package', packagesDir); - fileSystem.currentDirectory = package.getExamples().first.directory; + packagesDir.fileSystem.currentDirectory = + package.getExamples().first.directory; await runCapturingPrint( runner, ['sample', '--current-package']); @@ -523,7 +514,7 @@ packages/plugin1/plugin1/plugin1.dart final RepositoryPackage plugin2 = createFakePlugin('a_plugin_bar', pluginGroup); createFakePlugin('unrelated_plugin', packagesDir); - fileSystem.currentDirectory = pluginGroup; + packagesDir.fileSystem.currentDirectory = pluginGroup; await runCapturingPrint( runner, ['sample', '--current-package']); @@ -549,7 +540,7 @@ packages/plugin1/plugin1/plugin1.dart test( 'all plugins should be tested if there are no plugin related changes.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: 'AUTHORS')), ]; @@ -565,7 +556,7 @@ packages/plugin1/plugin1/plugin1.dart }); test('all plugins should be tested if .ci.yaml changes', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' .ci.yaml @@ -591,7 +582,7 @@ packages/plugin1/CHANGELOG test('all plugins should be tested if anything in .ci/ changes', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' .ci/Dockerfile @@ -617,7 +608,7 @@ packages/plugin1/CHANGELOG test('all plugins should be tested if anything in script/ changes.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' script/tool/bin/flutter_plugin_tools.dart @@ -643,7 +634,7 @@ packages/plugin1/CHANGELOG test('all plugins should be tested if the root analysis options change.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' analysis_options.yaml @@ -669,7 +660,7 @@ packages/plugin1/CHANGELOG test('all plugins should be tested if formatting options change.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' .clang-format @@ -694,7 +685,7 @@ packages/plugin1/CHANGELOG }); test('Only changed plugin should be tested.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; @@ -716,7 +707,7 @@ packages/plugin1/CHANGELOG test('multiple files in one plugin should also test the plugin', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1.dart @@ -734,7 +725,7 @@ packages/plugin1/ios/plugin1.m test('multiple plugins changed should test all the changed plugins', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1.dart @@ -756,7 +747,7 @@ packages/plugin2/ios/plugin2.m test( 'multiple plugins inside the same plugin group changed should output the plugin group name', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1/plugin1.dart @@ -777,7 +768,7 @@ packages/plugin1/plugin1_web/plugin1_web.dart test( 'changing one plugin in a federated group should only include that plugin', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1/plugin1.dart @@ -795,7 +786,7 @@ packages/plugin1/plugin1/plugin1.dart }); test('honors --exclude flag', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1.dart @@ -818,7 +809,7 @@ packages/plugin3/plugin3.dart }); test('honors --filter-packages-to flag', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin1/plugin1.dart @@ -843,7 +834,7 @@ packages/plugin3/plugin3.dart test( 'honors --filter-packages-to flag when a file is changed that makes ' 'all packages potentially changed', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' .ci.yaml @@ -864,7 +855,7 @@ packages/plugin3/plugin3.dart }); test('--filter-packages-to handles federated plugin groups', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/a_plugin/a_plugin/lib/foo.dart @@ -893,7 +884,7 @@ packages/a_plugin/a_plugin_platform_interface/lib/foo.dart }); test('--filter-packages-to and --exclude work together', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' .ci.yaml @@ -927,7 +918,7 @@ packages/a_plugin/a_plugin_platform_interface/lib/foo.dart test( 'no packages should be tested if there are no plugin related changes.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: 'AUTHORS')), ]; @@ -940,7 +931,7 @@ packages/a_plugin/a_plugin_platform_interface/lib/foo.dart test('no packages should be tested even if special repo files change.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' .ci.yaml @@ -958,7 +949,7 @@ script/tool/bin/flutter_plugin_tools.dart }); test('Only changed packages should be tested.', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo( MockProcess(stdout: 'packages/a_package/lib/a_package.dart')), @@ -981,7 +972,7 @@ script/tool/bin/flutter_plugin_tools.dart test('multiple packages changed should test all the changed packages', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/a_package/lib/a_package.dart @@ -1001,7 +992,7 @@ packages/b_package/lib/src/foo.dart }); test('honors --exclude flag', () async { - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/a_package/lib/a_package.dart @@ -1026,14 +1017,15 @@ packages/b_package/lib/src/foo.dart group('--packages-for-branch', () { test('only tests changed packages relative to the merge base on a branch', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; - processRunner.mockProcessesForExecutable['git-rev-parse'] = + gitProcessRunner.mockProcessesForExecutable['git-rev-parse'] = [ FakeProcessInfo(MockProcess(stdout: 'a-branch')), ]; - processRunner.mockProcessesForExecutable['git-merge-base'] = + gitProcessRunner.mockProcessesForExecutable['git-merge-base'] = [ FakeProcessInfo(MockProcess(exitCode: 1), ['--is-ancestor']), FakeProcessInfo(MockProcess(stdout: 'abc123'), @@ -1056,7 +1048,7 @@ packages/b_package/lib/src/foo.dart ])); // Ensure that it's diffing against the merge-base. expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, contains( const ProcessCall( 'git-diff', ['--name-only', 'abc123', 'HEAD'], null), @@ -1065,10 +1057,11 @@ packages/b_package/lib/src/foo.dart test('only tests changed packages relative to the previous commit on main', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; - processRunner.mockProcessesForExecutable['git-rev-parse'] = + gitProcessRunner.mockProcessesForExecutable['git-rev-parse'] = [ FakeProcessInfo(MockProcess(stdout: 'main')), ]; @@ -1091,7 +1084,7 @@ packages/b_package/lib/src/foo.dart ])); // Ensure that it's diffing against the prior commit. expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, contains( const ProcessCall( 'git-diff', ['--name-only', 'HEAD~', 'HEAD'], null), @@ -1101,10 +1094,11 @@ packages/b_package/lib/src/foo.dart test( 'only tests changed packages relative to the previous commit if ' 'running on a specific hash from main', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; - processRunner.mockProcessesForExecutable['git-rev-parse'] = + gitProcessRunner.mockProcessesForExecutable['git-rev-parse'] = [ FakeProcessInfo(MockProcess(stdout: 'HEAD')), ]; @@ -1128,7 +1122,7 @@ packages/b_package/lib/src/foo.dart ])); // Ensure that it's diffing against the prior commit. expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, contains( const ProcessCall( 'git-diff', ['--name-only', 'HEAD~', 'HEAD'], null), @@ -1138,14 +1132,15 @@ packages/b_package/lib/src/foo.dart test( 'only tests changed packages relative to the previous commit if ' 'running on a specific hash from origin/main', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; - processRunner.mockProcessesForExecutable['git-rev-parse'] = + gitProcessRunner.mockProcessesForExecutable['git-rev-parse'] = [ FakeProcessInfo(MockProcess(stdout: 'HEAD')), ]; - processRunner.mockProcessesForExecutable['git-merge-base'] = + gitProcessRunner.mockProcessesForExecutable['git-merge-base'] = [ FakeProcessInfo(MockProcess(exitCode: 128), [ '--is-ancestor', @@ -1178,7 +1173,7 @@ packages/b_package/lib/src/foo.dart ])); // Ensure that it's diffing against the prior commit. expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, contains( const ProcessCall( 'git-diff', ['--name-only', 'HEAD~', 'HEAD'], null), @@ -1188,10 +1183,11 @@ packages/b_package/lib/src/foo.dart test( 'only tests changed packages relative to the previous commit on master', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; - processRunner.mockProcessesForExecutable['git-rev-parse'] = + gitProcessRunner.mockProcessesForExecutable['git-rev-parse'] = [ FakeProcessInfo(MockProcess(stdout: 'master')), ]; @@ -1214,7 +1210,7 @@ packages/b_package/lib/src/foo.dart ])); // Ensure that it's diffing against the prior commit. expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, contains( const ProcessCall( 'git-diff', ['--name-only', 'HEAD~', 'HEAD'], null), @@ -1222,10 +1218,11 @@ packages/b_package/lib/src/foo.dart }); test('throws if getting the branch fails', () async { - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: 'packages/plugin1/plugin1.dart')), ]; - processRunner.mockProcessesForExecutable['git-rev-parse'] = + gitProcessRunner.mockProcessesForExecutable['git-rev-parse'] = [ FakeProcessInfo(MockProcess(exitCode: 1)), ]; @@ -1268,12 +1265,7 @@ packages/b_package/lib/src/foo.dart ]; for (int i = 0; i < expectedShards.length; ++i) { - final SamplePackageCommand localCommand = SamplePackageCommand( - packagesDir, - processRunner: processRunner, - platform: mockPlatform, - gitDir: MockGitDir(), - ); + final SamplePackageCommand localCommand = configureCommand(); final CommandRunner localRunner = CommandRunner('common_command', 'Shard testing'); localRunner.addCommand(localCommand); @@ -1312,12 +1304,7 @@ packages/b_package/lib/src/foo.dart ]; for (int i = 0; i < expectedShards.length; ++i) { - final SamplePackageCommand localCommand = SamplePackageCommand( - packagesDir, - processRunner: processRunner, - platform: mockPlatform, - gitDir: MockGitDir(), - ); + final SamplePackageCommand localCommand = configureCommand(); final CommandRunner localRunner = CommandRunner('common_command', 'Shard testing'); localRunner.addCommand(localCommand); @@ -1365,12 +1352,7 @@ packages/b_package/lib/src/foo.dart createFakePackage('package9', packagesDir); for (int i = 0; i < expectedShards.length; ++i) { - final SamplePackageCommand localCommand = SamplePackageCommand( - packagesDir, - processRunner: processRunner, - platform: mockPlatform, - gitDir: MockGitDir(), - ); + final SamplePackageCommand localCommand = configureCommand(); final CommandRunner localRunner = CommandRunner('common_command', 'Shard testing'); localRunner.addCommand(localCommand); diff --git a/script/tool/test/common/package_looping_command_test.dart b/script/tool/test/common/package_looping_command_test.dart index 846a7a9ef9c..6474dff8025 100644 --- a/script/tool/test/common/package_looping_command_test.dart +++ b/script/tool/test/common/package_looping_command_test.dart @@ -7,7 +7,6 @@ import 'dart:io' as io; import 'package:args/command_runner.dart'; 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/common/output_utils.dart'; import 'package:flutter_plugin_tools/src/common/package_looping_command.dart'; @@ -77,17 +76,16 @@ String _filenameForType(_ResultFileType type) { } void main() { - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late Directory thirdPartyPackagesDir; setUp(() { + mockPlatform = MockPlatform(); + (:packagesDir, processRunner: _, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(platform: mockPlatform); // Correct color handling is part of the behavior being tested here. useColorForOutput = true; - fileSystem = MemoryFileSystem(); - mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); thirdPartyPackagesDir = packagesDir.parent .childDirectory('third_party') .childDirectory('packages'); diff --git a/script/tool/test/common/package_state_utils_test.dart b/script/tool/test/common/package_state_utils_test.dart index 9ec83de9a48..03f09c5b5a8 100644 --- a/script/tool/test/common/package_state_utils_test.dart +++ b/script/tool/test/common/package_state_utils_test.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/git_version_finder.dart'; import 'package:flutter_plugin_tools/src/common/package_state_utils.dart'; import 'package:test/fake.dart'; @@ -11,12 +10,11 @@ import 'package:test/test.dart'; import '../util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + (:packagesDir, processRunner: _, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(); }); group('checkPackageChangeState', () { diff --git a/script/tool/test/common/plugin_utils_test.dart b/script/tool/test/common/plugin_utils_test.dart index ff2ff6a3953..288e57a4784 100644 --- a/script/tool/test/common/plugin_utils_test.dart +++ b/script/tool/test/common/plugin_utils_test.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. 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/common/plugin_utils.dart'; import 'package:test/test.dart'; @@ -11,12 +10,11 @@ import 'package:test/test.dart'; import '../util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + (:packagesDir, processRunner: _, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(); }); group('pluginSupportsPlatform', () { diff --git a/script/tool/test/common/pub_utils_test.dart b/script/tool/test/common/pub_utils_test.dart index 58f451d7d58..af4c90ebe1c 100644 --- a/script/tool/test/common/pub_utils_test.dart +++ b/script/tool/test/common/pub_utils_test.dart @@ -3,7 +3,6 @@ // found in the LICENSE file. import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/pub_utils.dart'; import 'package:test/test.dart'; @@ -11,14 +10,12 @@ import '../mocks.dart'; import '../util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); + (:packagesDir, :processRunner, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(); }); test('runs with Dart for a non-Flutter package by default', () async { diff --git a/script/tool/test/common/repository_package_test.dart b/script/tool/test/common/repository_package_test.dart index d9667914d9c..ebc8f779cf5 100644 --- a/script/tool/test/common/repository_package_test.dart +++ b/script/tool/test/common/repository_package_test.dart @@ -10,12 +10,11 @@ import 'package:test/test.dart'; import '../util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + (:packagesDir, processRunner: _, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(); }); group('displayName', () { @@ -49,7 +48,7 @@ void main() { test('always uses Posix-style paths', () async { final Directory windowsPackagesDir = createPackagesDirectory( - fileSystem: MemoryFileSystem(style: FileSystemStyle.windows)); + MemoryFileSystem(style: FileSystemStyle.windows)); expect( RepositoryPackage(windowsPackagesDir.childDirectory('foo')).displayName, diff --git a/script/tool/test/create_all_packages_app_command_test.dart b/script/tool/test/create_all_packages_app_command_test.dart index b65371a1971..db577a35979 100644 --- a/script/tool/test/create_all_packages_app_command_test.dart +++ b/script/tool/test/create_all_packages_app_command_test.dart @@ -6,7 +6,6 @@ import 'dart:io' as io; import 'package:args/command_runner.dart'; 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/create_all_packages_app_command.dart'; import 'package:platform/platform.dart'; @@ -20,17 +19,15 @@ void main() { late CommandRunner runner; late CreateAllPackagesAppCommand command; late Platform mockPlatform; - late FileSystem fileSystem; late Directory testRoot; late Directory packagesDir; late RecordingProcessRunner processRunner; setUp(() { mockPlatform = MockPlatform(isMacOS: true); - fileSystem = MemoryFileSystem(); - testRoot = fileSystem.systemTempDirectory.createTempSync(); - packagesDir = testRoot.childDirectory('packages'); - processRunner = RecordingProcessRunner(); + (:packagesDir, :processRunner, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(platform: mockPlatform); + testRoot = packagesDir.parent; command = CreateAllPackagesAppCommand( packagesDir, @@ -530,7 +527,7 @@ android { test('handles --output-dir', () async { final Directory customOutputDir = - fileSystem.systemTempDirectory.createTempSync(); + testRoot.fileSystem.systemTempDirectory.createTempSync(); writeFakeFlutterCreateOutput(customOutputDir); createFakePlugin('plugina', packagesDir); diff --git a/script/tool/test/custom_test_command_test.dart b/script/tool/test/custom_test_command_test.dart index 1b6f0a470ec..590b5684efd 100644 --- a/script/tool/test/custom_test_command_test.dart +++ b/script/tool/test/custom_test_command_test.dart @@ -4,16 +4,15 @@ import 'package:args/command_runner.dart'; 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/custom_test_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; import 'util.dart'; void main() { - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late RecordingProcessRunner processRunner; @@ -21,14 +20,15 @@ void main() { group('posix', () { setUp(() { - fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final CustomTestCommand analyzeCommand = CustomTestCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( @@ -219,14 +219,15 @@ void main() { group('Windows', () { setUp(() { - fileSystem = MemoryFileSystem(style: FileSystemStyle.windows); mockPlatform = MockPlatform(isWindows: true); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final CustomTestCommand analyzeCommand = CustomTestCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/dart_test_command_test.dart b/script/tool/test/dart_test_command_test.dart index b5786797db5..0c12634a766 100644 --- a/script/tool/test/dart_test_command_test.dart +++ b/script/tool/test/dart_test_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/dart_test_command.dart'; +import 'package:git/git.dart'; import 'package:platform/platform.dart'; import 'package:test/test.dart'; @@ -16,21 +16,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 GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final DartTestCommand command = DartTestCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner('test_test', 'Test for $DartTestCommand'); diff --git a/script/tool/test/dependabot_check_command_test.dart b/script/tool/test/dependabot_check_command_test.dart index 08a10ae6cc9..d26690fc1b7 100644 --- a/script/tool/test/dependabot_check_command_test.dart +++ b/script/tool/test/dependabot_check_command_test.dart @@ -4,28 +4,23 @@ import 'package:args/command_runner.dart'; 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/dependabot_check_command.dart'; -import 'package:mockito/mockito.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'util.dart'; void main() { late CommandRunner runner; - late FileSystem fileSystem; late Directory root; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(); - root = fileSystem.currentDirectory; - packagesDir = root.childDirectory('packages'); - - final MockGitDir gitDir = MockGitDir(); - when(gitDir.path).thenReturn(root.path); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(); + root = packagesDir.parent; final DependabotCheckCommand command = DependabotCheckCommand( packagesDir, diff --git a/script/tool/test/drive_examples_command_test.dart b/script/tool/test/drive_examples_command_test.dart index 664481a4bf7..8ee59212135 100644 --- a/script/tool/test/drive_examples_command_test.dart +++ b/script/tool/test/drive_examples_command_test.dart @@ -8,10 +8,10 @@ import 'dart:io' as io; import 'package:args/command_runner.dart'; import 'package:fake_async/fake_async.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/drive_examples_command.dart'; +import 'package:git/git.dart'; import 'package:mockito/mockito.dart'; import 'package:platform/platform.dart'; import 'package:test/test.dart'; @@ -24,19 +24,22 @@ 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; setUp(() { - fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); - final DriveExamplesCommand command = DriveExamplesCommand(packagesDir, - processRunner: processRunner, platform: mockPlatform); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); + final DriveExamplesCommand command = DriveExamplesCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + gitDir: gitDir, + ); runner = CommandRunner( 'drive_examples_command', 'Test for drive_example_command'); diff --git a/script/tool/test/federation_safety_check_command_test.dart b/script/tool/test/federation_safety_check_command_test.dart index 93a21b76cdd..cb50eb716e1 100644 --- a/script/tool/test/federation_safety_check_command_test.dart +++ b/script/tool/test/federation_safety_check_command_test.dart @@ -4,42 +4,27 @@ import 'package:args/command_runner.dart'; 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/federation_safety_check_command.dart'; -import 'package:mockito/mockito.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; void main() { - FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; - late RecordingProcessRunner processRunner; + late RecordingProcessRunner gitProcessRunner; setUp(() { - fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - - final MockGitDir gitDir = MockGitDir(); - when(gitDir.path).thenReturn(packagesDir.parent.path); - when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) - .thenAnswer((Invocation invocation) { - final List arguments = - invocation.positionalArguments[0]! as List; - // Route git calls through the process runner, to make mock output - // consistent with other processes. Attach the first argument to the - // command to make targeting the mock results easier. - final String gitCommand = arguments.removeAt(0); - return processRunner.run('git-$gitCommand', arguments); - }); + final GitDir gitDir; + final RecordingProcessRunner processRunner; + (:packagesDir, :processRunner, :gitProcessRunner, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); - processRunner = RecordingProcessRunner(); final FederationSafetyCheckCommand command = FederationSafetyCheckCommand( packagesDir, processRunner: processRunner, @@ -57,7 +42,7 @@ void main() { final String changedFileOutput = [ package.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -80,7 +65,7 @@ void main() { final String changedFileOutput = [ package.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -105,7 +90,7 @@ void main() { final String changedFileOutput = [ platformInterface.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -134,7 +119,7 @@ void main() { platformInterface.libDirectory.childFile('foo.dart'), platformInterface.pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -174,7 +159,7 @@ void main() { appFacing.libDirectory.childFile('foo.dart'), implementation.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -225,7 +210,7 @@ index abc123..def456 100644 platformInterface.pubspecFile, platformInterface.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), // Ensure that a change with both a comment and non-comment addition is // counted, to validate change analysis. @@ -309,7 +294,7 @@ index abc123..def456 100644 platformInterface.pubspecFile, platformInterface.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), FakeProcessInfo(MockProcess(stdout: appFacingChanges), ['', 'HEAD', '--', '/packages/foo/foo/lib/foo.dart']), @@ -359,7 +344,7 @@ index abc123..def456 100644 platformInterface.pubspecFile, platformInterface.testDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -391,11 +376,11 @@ index abc123..def456 100644 platformInterface.pubspecFile, platformInterface.libDirectory.childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; // Simulate no change to the version in the interface's pubspec.yaml. - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess( stdout: platformInterface.pubspecFile.readAsStringSync())), ]; @@ -458,7 +443,7 @@ index abc123..def456 100644 // Do things '''; - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo( MockProcess(stdout: changedFileOutput), ['--name-only']), FakeProcessInfo(MockProcess(stdout: implementationChanges), @@ -526,7 +511,7 @@ index abc123..def456 100644 ... '''; - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo( MockProcess(stdout: changedFileOutput), ['--name-only']), FakeProcessInfo(MockProcess(stdout: implementationChanges), @@ -570,7 +555,7 @@ index abc123..def456 100644 otherPlugin1.libDirectory.childFile('bar.dart'), otherPlugin2.libDirectory.childFile('baz.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -599,7 +584,7 @@ index abc123..def456 100644 // This should be picked up as a change to 'foo', and not crash. plugin.directory.childFile('foo_bar.baz'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -636,7 +621,7 @@ index abc123..def456 100644 .childDirectory('lib') .childFile('foo.dart'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; diff --git a/script/tool/test/fetch_deps_command_test.dart b/script/tool/test/fetch_deps_command_test.dart index e3acecc234d..5dc6a0de86c 100644 --- a/script/tool/test/fetch_deps_command_test.dart +++ b/script/tool/test/fetch_deps_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/fetch_deps_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -15,21 +15,22 @@ import 'util.dart'; void main() { group('FetchDepsCommand', () { - FileSystem fileSystem; late Directory packagesDir; late CommandRunner runner; late MockPlatform mockPlatform; late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); mockPlatform = MockPlatform(); - processRunner = RecordingProcessRunner(); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); + final FetchDepsCommand command = FetchDepsCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = diff --git a/script/tool/test/firebase_test_lab_command_test.dart b/script/tool/test/firebase_test_lab_command_test.dart index 067bf9cbaf5..481c230526c 100644 --- a/script/tool/test/firebase_test_lab_command_test.dart +++ b/script/tool/test/firebase_test_lab_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/file_utils.dart'; import 'package:flutter_plugin_tools/src/firebase_test_lab_command.dart'; +import 'package:git/git.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -16,21 +16,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 GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final FirebaseTestLabCommand command = FirebaseTestLabCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/fix_command_test.dart b/script/tool/test/fix_command_test.dart index ad767968e25..de660b42b36 100644 --- a/script/tool/test/fix_command_test.dart +++ b/script/tool/test/fix_command_test.dart @@ -4,30 +4,30 @@ import 'package:args/command_runner.dart'; 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/fix_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; 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 GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final FixCommand command = FixCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner('fix_command', 'Test for fix_command'); diff --git a/script/tool/test/format_command_test.dart b/script/tool/test/format_command_test.dart index eb4f3bb7bcd..56911300912 100644 --- a/script/tool/test/format_command_test.dart +++ b/script/tool/test/format_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/file_utils.dart'; import 'package:flutter_plugin_tools/src/format_command.dart'; +import 'package:git/git.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; @@ -15,7 +15,6 @@ import 'mocks.dart'; import 'util.dart'; void main() { - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late RecordingProcessRunner processRunner; @@ -25,14 +24,15 @@ void main() { late String kotlinFormatPath; setUp(() { - fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); analyzeCommand = FormatCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); // Create the Java and Kotlin formatter files that the command checks for, @@ -40,11 +40,11 @@ void main() { final p.Context 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); + packagesDir.fileSystem.file(javaFormatPath).createSync(recursive: true); kotlinFormatPath = path.join( path.dirname(path.fromUri(mockPlatform.script)), 'ktfmt-0.46-jar-with-dependencies.jar'); - fileSystem.file(kotlinFormatPath).createSync(recursive: true); + packagesDir.fileSystem.file(kotlinFormatPath).createSync(recursive: true); runner = CommandRunner('format_command', 'Test for format_command'); runner.addCommand(analyzeCommand); diff --git a/script/tool/test/gradle_check_command_test.dart b/script/tool/test/gradle_check_command_test.dart index d5f33767ccc..ec6fd8d586b 100644 --- a/script/tool/test/gradle_check_command_test.dart +++ b/script/tool/test/gradle_check_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/gradle_check_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'util.dart'; @@ -16,15 +16,15 @@ const String _defaultFakeNamespace = 'dev.flutter.foo'; void main() { late CommandRunner runner; - late FileSystem fileSystem; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = fileSystem.currentDirectory.childDirectory('packages'); - createPackagesDirectory(parentDir: packagesDir.parent); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(); final GradleCheckCommand command = GradleCheckCommand( packagesDir, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/license_check_command_test.dart b/script/tool/test/license_check_command_test.dart index 0ad40e175e4..f78682e4f9f 100644 --- a/script/tool/test/license_check_command_test.dart +++ b/script/tool/test/license_check_command_test.dart @@ -4,34 +4,29 @@ import 'package:args/command_runner.dart'; 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/license_check_command.dart'; -import 'package:mockito/mockito.dart'; +import 'package:git/git.dart'; import 'package:platform/platform.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; void main() { group('LicenseCheckCommand', () { late CommandRunner runner; - late FileSystem fileSystem; late Platform platform; + late Directory packagesDir; late Directory root; setUp(() { - fileSystem = MemoryFileSystem(); platform = MockPlatformWithSeparator(); - final Directory packagesDir = - fileSystem.currentDirectory.childDirectory('packages'); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: platform); root = packagesDir.parent; - final MockGitDir gitDir = MockGitDir(); - when(gitDir.path).thenReturn(packagesDir.parent.path); - final LicenseCheckCommand command = LicenseCheckCommand( packagesDir, platform: platform, diff --git a/script/tool/test/lint_android_command_test.dart b/script/tool/test/lint_android_command_test.dart index 3c739af7e3e..8ac1bccfa82 100644 --- a/script/tool/test/lint_android_command_test.dart +++ b/script/tool/test/lint_android_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/lint_android_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -15,21 +15,21 @@ import 'util.dart'; void main() { group('LintAndroidCommand', () { - FileSystem fileSystem; late Directory packagesDir; late CommandRunner runner; late MockPlatform mockPlatform; late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); mockPlatform = MockPlatform(); - processRunner = RecordingProcessRunner(); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final LintAndroidCommand command = LintAndroidCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/list_command_test.dart b/script/tool/test/list_command_test.dart index f19215c89b9..c8ea6179661 100644 --- a/script/tool/test/list_command_test.dart +++ b/script/tool/test/list_command_test.dart @@ -4,7 +4,6 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/list_command.dart'; import 'package:test/test.dart'; @@ -13,15 +12,14 @@ 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); + (:packagesDir, processRunner: _, gitProcessRunner: _, gitDir: _) = + configureBaseCommandMocks(platform: mockPlatform); final ListCommand command = ListCommand(packagesDir, platform: mockPlatform); diff --git a/script/tool/test/make_deps_path_based_command_test.dart b/script/tool/test/make_deps_path_based_command_test.dart index 7e2625a49f3..232e971a1b0 100644 --- a/script/tool/test/make_deps_path_based_command_test.dart +++ b/script/tool/test/make_deps_path_based_command_test.dart @@ -4,44 +4,28 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/make_deps_path_based_command.dart'; -import 'package:mockito/mockito.dart'; +import 'package:git/git.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; void main() { - FileSystem fileSystem; late Directory packagesDir; late Directory thirdPartyPackagesDir; late CommandRunner runner; - late RecordingProcessRunner processRunner; + late RecordingProcessRunner gitProcessRunner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + final GitDir gitDir; + (:packagesDir, processRunner: _, :gitProcessRunner, :gitDir) = + configureBaseCommandMocks(); thirdPartyPackagesDir = packagesDir.parent .childDirectory('third_party') .childDirectory('packages'); - final MockGitDir gitDir = MockGitDir(); - when(gitDir.path).thenReturn(packagesDir.parent.path); - when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) - .thenAnswer((Invocation invocation) { - final List arguments = - invocation.positionalArguments[0]! as List; - // Route git calls through the process runner, to make mock output - // consistent with other processes. Attach the first argument to the - // command to make targeting the mock results easier. - final String gitCommand = arguments.removeAt(0); - return processRunner.run('git-$gitCommand', arguments); - }); - - processRunner = RecordingProcessRunner(); final MakeDepsPathBasedCommand command = MakeDepsPathBasedCommand(packagesDir, gitDir: gitDir); @@ -498,11 +482,13 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ package.pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; // Simulate no change to the version in the interface's pubspec.yaml. - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo( MockProcess(stdout: package.pubspecFile.readAsStringSync())), ]; @@ -525,7 +511,8 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} // A change for a file that's not on disk simulates a deletion. packagesDir.childDirectory('foo').childFile('pubspec.yaml'), ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; @@ -552,13 +539,15 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '1.0.0'); // Simulate no change to the version in the interface's pubspec.yaml. - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; @@ -584,13 +573,15 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '1.0.0'); // Simulate no change to the version in the interface's pubspec.yaml. - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; @@ -616,13 +607,15 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '1.0.0'); // Simulate no change to the version in the interface's pubspec.yaml. - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; @@ -648,13 +641,15 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '0.7.0'); // Simulate no change to the version in the interface's pubspec.yaml. - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; @@ -686,12 +681,14 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; @@ -726,12 +723,14 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; @@ -764,12 +763,14 @@ ${overrides.map((String dep) => ' $dep:\n path: $path').join('\n')} final String changedFileOutput = [ pubspecFile, ].map((File file) => file.path).join('\n'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: changedFileOutput)), ]; final String gitPubspecContents = pubspecFile.readAsStringSync().replaceAll(newVersion, '1.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: gitPubspecContents)), ]; diff --git a/script/tool/test/mocks.dart b/script/tool/test/mocks.dart index 2c84ecedd12..fbc2ffc11fc 100644 --- a/script/tool/test/mocks.dart +++ b/script/tool/test/mocks.dart @@ -7,9 +7,12 @@ import 'dart:convert'; import 'dart:io' as io; import 'package:file/file.dart'; +import 'package:flutter_plugin_tools/src/common/process_runner.dart'; import 'package:mockito/mockito.dart'; import 'package:platform/platform.dart'; +import 'common/package_command_test.mocks.dart'; + class MockPlatform extends Mock implements Platform { MockPlatform({ this.isLinux = false, @@ -93,3 +96,27 @@ class MockIOSink extends Mock implements IOSink { @override void writeln([Object? obj = '']) => lines.add(obj.toString()); } + +/// Creates a mockGitDir that uses [packagesDir]'s parent as its root, and +/// forwards any git commands to [processRunner] to make it easy to mock their +/// output the same way other process calls are mocked. +/// +/// The first argument to any `git` command is added to the command to make +/// targeting the mock results easier. For example, `git ls ...` will become +/// a `git-ls ...` call to [processRunner]. +/// +MockGitDir createForwardingMockGitDir({ + required Directory packagesDir, + required ProcessRunner processRunner, +}) { + final MockGitDir gitDir = MockGitDir(); + when(gitDir.path).thenReturn(packagesDir.parent.path); + when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) + .thenAnswer((Invocation invocation) { + final List arguments = + invocation.positionalArguments[0]! as List; + final String gitCommand = arguments.removeAt(0); + return processRunner.run('git-$gitCommand', arguments); + }); + return gitDir; +} diff --git a/script/tool/test/native_test_command_test.dart b/script/tool/test/native_test_command_test.dart index cdaa2a748bd..0a9924727ef 100644 --- a/script/tool/test/native_test_command_test.dart +++ b/script/tool/test/native_test_command_test.dart @@ -7,12 +7,12 @@ import 'dart:ffi'; import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/common/cmake.dart'; import 'package:flutter_plugin_tools/src/common/core.dart'; import 'package:flutter_plugin_tools/src/common/file_utils.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/native_test_command.dart'; +import 'package:git/git.dart'; import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; import 'package:test/test.dart'; @@ -80,22 +80,25 @@ void main() { const String kDestination = '--ios-destination'; group('test native_test_command on Posix', () { - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); // iOS and macOS tests expect macOS, Linux tests expect Linux; nothing // needs to distinguish between Linux and macOS, so set both to true to // allow them to share a setup group. mockPlatform = MockPlatform(isMacOS: true, isLinux: true); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); - final NativeTestCommand command = NativeTestCommand(packagesDir, - processRunner: processRunner, platform: mockPlatform); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); + final NativeTestCommand command = NativeTestCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + gitDir: gitDir, + ); runner = CommandRunner( 'native_test_command', 'Test for native_test_command'); @@ -1245,7 +1248,7 @@ public class FlutterActivityTest { }); // Tests behaviors of implementation that is shared between iOS and macOS. - group('iOS/macOS', () { + group('iOS or macOS', () { test('fails if xcrun fails', () async { createFakePlugin('plugin', packagesDir, platformSupport: { @@ -1855,17 +1858,16 @@ public class FlutterActivityTest { }); group('test native_test_command on Windows', () { - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; late RecordingProcessRunner processRunner; + late GitDir gitDir; setUp(() { - fileSystem = MemoryFileSystem(style: FileSystemStyle.windows); mockPlatform = MockPlatform(isWindows: true); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); }); // Returns the ProcessCall to expect for build the Windows unit tests for @@ -1895,6 +1897,7 @@ public class FlutterActivityTest { final NativeTestCommand command = NativeTestCommand(packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, abi: Abi.windowsX64); runner = CommandRunner( @@ -2165,6 +2168,7 @@ public class FlutterActivityTest { final NativeTestCommand command = NativeTestCommand(packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, abi: Abi.windowsArm64); runner = CommandRunner( diff --git a/script/tool/test/podspec_check_command_test.dart b/script/tool/test/podspec_check_command_test.dart index fe20e992ab8..c2506de6313 100644 --- a/script/tool/test/podspec_check_command_test.dart +++ b/script/tool/test/podspec_check_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/podspec_check_command.dart'; +import 'package:git/git.dart'; import 'package:platform/platform.dart'; import 'package:test/test.dart'; @@ -75,22 +75,21 @@ end void main() { group('PodspecCheckCommand', () { - FileSystem fileSystem; late Directory packagesDir; late CommandRunner runner; late MockPlatform mockPlatform; late RecordingProcessRunner processRunner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - mockPlatform = MockPlatform(isMacOS: true); - processRunner = RecordingProcessRunner(); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final PodspecCheckCommand command = PodspecCheckCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = diff --git a/script/tool/test/publish_check_command_test.dart b/script/tool/test/publish_check_command_test.dart index 0869888f058..117ad5b3eb6 100644 --- a/script/tool/test/publish_check_command_test.dart +++ b/script/tool/test/publish_check_command_test.dart @@ -6,9 +6,9 @@ import 'dart:convert'; import 'package:args/command_runner.dart'; 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/publish_check_command.dart'; +import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:test/test.dart'; @@ -18,21 +18,21 @@ import 'util.dart'; void main() { group('PublishCheckCommand tests', () { - FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late RecordingProcessRunner processRunner; + // Separate process runner for the mock gitDir to make asserting the + // expected command-specific calls easier. + late GitDir gitDir; late CommandRunner runner; - setUp(() { - fileSystem = MemoryFileSystem(); - mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); + CommandRunner configureRunner({MockClient? httpClient}) { final PublishCheckCommand publishCheckCommand = PublishCheckCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, + httpClient: httpClient, ); runner = CommandRunner( @@ -40,6 +40,15 @@ void main() { 'Test for publish-check command.', ); runner.addCommand(publishCheckCommand); + return runner; + } + + setUp(() { + mockPlatform = MockPlatform(); + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); + + runner = configureRunner(); }); test('publish check all packages', () async { @@ -282,14 +291,7 @@ void main() { return http.Response('', 500); }); - runner = CommandRunner( - 'publish_check_command', - 'Test for publish-check command.', - ); - runner.addCommand(PublishCheckCommand(packagesDir, - platform: mockPlatform, - processRunner: processRunner, - httpClient: mockClient)); + runner = configureRunner(httpClient: mockClient); processRunner.mockProcessesForExecutable['flutter'] = [ FakeProcessInfo(MockProcess(exitCode: 1, stdout: 'Some error from pub'), @@ -356,14 +358,7 @@ void main() { return http.Response('', 500); }); - runner = CommandRunner( - 'publish_check_command', - 'Test for publish-check command.', - ); - runner.addCommand(PublishCheckCommand(packagesDir, - platform: mockPlatform, - processRunner: processRunner, - httpClient: mockClient)); + runner = configureRunner(httpClient: mockClient); final List output = await runCapturingPrint(runner, ['publish-check']); @@ -532,14 +527,8 @@ void main() { } return http.Response('', 500); }); - final PublishCheckCommand command = PublishCheckCommand(packagesDir, - processRunner: processRunner, httpClient: mockClient); - runner = CommandRunner( - 'publish_check_command', - 'Test for publish-check command.', - ); - runner.addCommand(command); + runner = configureRunner(httpClient: mockClient); createFakePlugin('no_publish_a', packagesDir, version: '0.1.0'); createFakePlugin('no_publish_b', packagesDir, version: '0.2.0'); @@ -597,14 +586,8 @@ void main() { } return http.Response('', 500); }); - final PublishCheckCommand command = PublishCheckCommand(packagesDir, - processRunner: processRunner, httpClient: mockClient); - runner = CommandRunner( - 'publish_check_command', - 'Test for publish-check command.', - ); - runner.addCommand(command); + runner = configureRunner(httpClient: mockClient); createFakePlugin('no_publish_a', packagesDir, version: '0.1.0'); createFakePlugin('no_publish_b', packagesDir, version: '0.2.0'); @@ -664,14 +647,8 @@ void main() { } return http.Response('', 500); }); - final PublishCheckCommand command = PublishCheckCommand(packagesDir, - processRunner: processRunner, httpClient: mockClient); - runner = CommandRunner( - 'publish_check_command', - 'Test for publish-check command.', - ); - runner.addCommand(command); + runner = configureRunner(httpClient: mockClient); final RepositoryPackage plugin = createFakePlugin('no_publish_a', packagesDir, version: '0.1.0'); diff --git a/script/tool/test/publish_command_test.dart b/script/tool/test/publish_command_test.dart index 068ccb56569..9be0dd07c0b 100644 --- a/script/tool/test/publish_command_test.dart +++ b/script/tool/test/publish_command_test.dart @@ -8,42 +8,44 @@ import 'dart:io' as io; import 'package:args/command_runner.dart'; 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/publish_command.dart'; +import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; void main() { late MockPlatform platform; late Directory packagesDir; - late MockGitDir gitDir; late TestProcessRunner processRunner; late PublishCommand command; late CommandRunner commandRunner; late MockStdin mockStdin; - late FileSystem fileSystem; // Map of package name to mock response. late Map> mockHttpResponses; void createMockCredentialFile() { - fileSystem.file(command.credentialsPath) + packagesDir.fileSystem.file(command.credentialsPath) ..createSync(recursive: true) ..writeAsStringSync('some credential'); } setUp(() async { platform = MockPlatform(isLinux: true); - platform.environment['HOME'] = '/home'; - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); processRunner = TestProcessRunner(); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks( + platform: platform, + customProcessRunner: processRunner, + customGitProcessRunner: processRunner, + ); + platform.environment['HOME'] = '/home'; mockHttpResponses = >{}; final MockClient mockClient = MockClient((http.Request request) async { @@ -57,19 +59,6 @@ void main() { return http.Response('', 404); }); - gitDir = MockGitDir(); - when(gitDir.path).thenReturn(packagesDir.parent.path); - when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) - .thenAnswer((Invocation invocation) { - final List arguments = - invocation.positionalArguments[0]! as List; - // Route git calls through the process runner, to make mock output - // consistent with outer processes. Attach the first argument to the - // command to make targeting the mock results easier. - final String gitCommand = arguments.removeAt(0); - return processRunner.run('git-$gitCommand', arguments); - }); - mockStdin = MockStdin(); command = PublishCommand( packagesDir, @@ -358,7 +347,8 @@ void main() { '--server=bar' ]); - final File credentialFile = fileSystem.file(command.credentialsPath); + final File credentialFile = + packagesDir.fileSystem.file(command.credentialsPath); expect(credentialFile.existsSync(), true); expect(credentialFile.readAsStringSync(), credentials); }); diff --git a/script/tool/test/pubspec_check_command_test.dart b/script/tool/test/pubspec_check_command_test.dart index 07332131904..5f892cc4ed4 100644 --- a/script/tool/test/pubspec_check_command_test.dart +++ b/script/tool/test/pubspec_check_command_test.dart @@ -4,9 +4,9 @@ import 'package:args/command_runner.dart'; 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/pubspec_check_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -136,21 +136,20 @@ false_secrets: void main() { group('test pubspec_check_command', () { 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 RecordingProcessRunner processRunner; + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final PubspecCheckCommand command = PubspecCheckCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( @@ -1851,21 +1850,20 @@ ${_topicsSection()} group('test pubspec_check_command on Windows', () { late CommandRunner runner; - late RecordingProcessRunner processRunner; - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(style: FileSystemStyle.windows); mockPlatform = MockPlatform(isWindows: true); - packagesDir = fileSystem.currentDirectory.childDirectory('packages'); - createPackagesDirectory(parentDir: packagesDir.parent); - processRunner = RecordingProcessRunner(); + final RecordingProcessRunner processRunner; + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final PubspecCheckCommand command = PubspecCheckCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/readme_check_command_test.dart b/script/tool/test/readme_check_command_test.dart index 1cecedd88d0..ee7a0bfbdc5 100644 --- a/script/tool/test/readme_check_command_test.dart +++ b/script/tool/test/readme_check_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/readme_check_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -15,21 +15,20 @@ import 'util.dart'; 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 RecordingProcessRunner processRunner; + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); final ReadmeCheckCommand command = ReadmeCheckCommand( packagesDir, processRunner: processRunner, platform: mockPlatform, + gitDir: gitDir, ); runner = CommandRunner( diff --git a/script/tool/test/remove_dev_dependencies_command_test.dart b/script/tool/test/remove_dev_dependencies_command_test.dart index e453ff680e1..98b9168c6fc 100644 --- a/script/tool/test/remove_dev_dependencies_command_test.dart +++ b/script/tool/test/remove_dev_dependencies_command_test.dart @@ -4,23 +4,24 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/remove_dev_dependencies_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; late CommandRunner runner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(); final RemoveDevDependenciesCommand command = RemoveDevDependenciesCommand( packagesDir, + gitDir: gitDir, ); runner = CommandRunner('trim_dev_dependencies_command', 'Test for trim_dev_dependencies_command'); diff --git a/script/tool/test/repo_package_info_check_command_test.dart b/script/tool/test/repo_package_info_check_command_test.dart index 68f88cbccd8..16205a43a40 100644 --- a/script/tool/test/repo_package_info_check_command_test.dart +++ b/script/tool/test/repo_package_info_check_command_test.dart @@ -4,28 +4,23 @@ import 'package:args/command_runner.dart'; 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/repo_package_info_check_command.dart'; -import 'package:mockito/mockito.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'util.dart'; void main() { late CommandRunner runner; - late FileSystem fileSystem; late Directory root; late Directory packagesDir; setUp(() { - fileSystem = MemoryFileSystem(); - root = fileSystem.currentDirectory; - packagesDir = root.childDirectory('packages'); - - final MockGitDir gitDir = MockGitDir(); - when(gitDir.path).thenReturn(root.path); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(); + root = packagesDir.fileSystem.currentDirectory; final RepoPackageInfoCheckCommand command = RepoPackageInfoCheckCommand( packagesDir, diff --git a/script/tool/test/update_dependency_command_test.dart b/script/tool/test/update_dependency_command_test.dart index 2cb6b15b12c..2067ff32576 100644 --- a/script/tool/test/update_dependency_command_test.dart +++ b/script/tool/test/update_dependency_command_test.dart @@ -6,9 +6,9 @@ import 'dart:convert'; import 'package:args/command_runner.dart'; 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/update_dependency_command.dart'; +import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:pubspec_parse/pubspec_parse.dart'; @@ -18,19 +18,19 @@ import 'mocks.dart'; import 'util.dart'; void main() { - FileSystem fileSystem; late Directory packagesDir; late RecordingProcessRunner processRunner; late CommandRunner runner; Future Function(http.Request request)? mockHttpResponse; setUp(() { - fileSystem = MemoryFileSystem(); - processRunner = RecordingProcessRunner(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(); final UpdateDependencyCommand command = UpdateDependencyCommand( packagesDir, processRunner: processRunner, + gitDir: gitDir, httpClient: MockClient((http.Request request) => mockHttpResponse!(request)), ); diff --git a/script/tool/test/update_excerpts_command_test.dart b/script/tool/test/update_excerpts_command_test.dart index 187f0d8d598..1d29b6ce3f6 100644 --- a/script/tool/test/update_excerpts_command_test.dart +++ b/script/tool/test/update_excerpts_command_test.dart @@ -4,32 +4,29 @@ import 'package:args/command_runner.dart'; 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/update_excerpts_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; void runAllTests(MockPlatform platform) { - late FileSystem fileSystem; late Directory packagesDir; late CommandRunner runner; setUp(() { - fileSystem = MemoryFileSystem( - style: platform.isWindows - ? FileSystemStyle.windows - : FileSystemStyle.posix); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + final RecordingProcessRunner processRunner; + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: platform); runner = CommandRunner('', '') ..addCommand(UpdateExcerptsCommand( packagesDir, platform: platform, - processRunner: RecordingProcessRunner(), - gitDir: MockGitDir(), + processRunner: processRunner, + gitDir: gitDir, )); }); diff --git a/script/tool/test/update_min_sdk_command_test.dart b/script/tool/test/update_min_sdk_command_test.dart index d9bc93f2310..1e51c5bef4f 100644 --- a/script/tool/test/update_min_sdk_command_test.dart +++ b/script/tool/test/update_min_sdk_command_test.dart @@ -4,23 +4,24 @@ import 'package:args/command_runner.dart'; import 'package:file/file.dart'; -import 'package:file/memory.dart'; import 'package:flutter_plugin_tools/src/update_min_sdk_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; late CommandRunner runner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); + final GitDir gitDir; + (:packagesDir, processRunner: _, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(); final UpdateMinSdkCommand command = UpdateMinSdkCommand( packagesDir, + gitDir: gitDir, ); runner = CommandRunner( 'update_min_sdk_command', 'Test for update_min_sdk_command'); diff --git a/script/tool/test/update_release_info_command_test.dart b/script/tool/test/update_release_info_command_test.dart index d21a1367d2c..77e580cc230 100644 --- a/script/tool/test/update_release_info_command_test.dart +++ b/script/tool/test/update_release_info_command_test.dart @@ -4,40 +4,23 @@ import 'package:args/command_runner.dart'; 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/update_release_info_command.dart'; -import 'package:mockito/mockito.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; void main() { - late FileSystem fileSystem; late Directory packagesDir; - late MockGitDir gitDir; - late RecordingProcessRunner processRunner; + late RecordingProcessRunner gitProcessRunner; late CommandRunner runner; setUp(() { - fileSystem = MemoryFileSystem(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - processRunner = RecordingProcessRunner(); - - gitDir = MockGitDir(); - when(gitDir.path).thenReturn(packagesDir.parent.path); - when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) - .thenAnswer((Invocation invocation) { - final List arguments = - invocation.positionalArguments[0]! as List; - // Route git calls through a process runner, to make mock output - // consistent with other processes. Attach the first argument to the - // command to make targeting the mock results easier. - final String gitCommand = arguments.removeAt(0); - return processRunner.run('git-$gitCommand', arguments); - }); + final GitDir gitDir; + (:packagesDir, processRunner: _, :gitProcessRunner, :gitDir) = + configureBaseCommandMocks(); final UpdateReleaseInfoCommand command = UpdateReleaseInfoCommand( packagesDir, @@ -384,7 +367,8 @@ $originalChangelog'''; test('skips for "minimal" when there are no changes at all', () async { final RepositoryPackage package = createFakePackage('a_package', packagesDir, version: '1.0.1'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: ''' packages/different_package/lib/foo.dart ''')), @@ -412,7 +396,8 @@ packages/different_package/lib/foo.dart test('skips for "minimal" when there are only test changes', () async { final RepositoryPackage package = createFakePackage('a_package', packagesDir, version: '1.0.1'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: ''' packages/a_package/test/a_test.dart packages/a_package/example/integration_test/another_test.dart @@ -607,7 +592,8 @@ Some free-form text that isn't a list. () async { final RepositoryPackage package = createFakePackage('a_package', packagesDir, version: '1.0.1'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: ''' packages/a_package/lib/plugin.dart ''')), @@ -632,7 +618,8 @@ packages/a_package/lib/plugin.dart () async { final RepositoryPackage package = createFakePackage('a_package', packagesDir, version: '1.0.1'); - processRunner.mockProcessesForExecutable['git-diff'] = [ + gitProcessRunner.mockProcessesForExecutable['git-diff'] = + [ FakeProcessInfo(MockProcess(stdout: ''' packages/a_package/test/plugin_test.dart ''')), diff --git a/script/tool/test/util.dart b/script/tool/test/util.dart index 150508dbd16..a608c69339b 100644 --- a/script/tool/test/util.dart +++ b/script/tool/test/util.dart @@ -14,6 +14,7 @@ import 'package:flutter_plugin_tools/src/common/file_utils.dart'; import 'package:flutter_plugin_tools/src/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/common/process_runner.dart'; import 'package:flutter_plugin_tools/src/common/repository_package.dart'; +import 'package:git/git.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; import 'package:platform/platform.dart'; @@ -31,19 +32,11 @@ const String _defaultFlutterConstraint = '>=2.5.0'; 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, -/// otherwise [fileSystem] must be provided and it will be created an arbitrary -/// location in that filesystem. -Directory createPackagesDirectory( - {Directory? parentDir, FileSystem? fileSystem}) { - assert(parentDir != null || fileSystem != null, - 'One of parentDir or fileSystem must be provided'); - assert(fileSystem == null || fileSystem is MemoryFileSystem, - 'If using a real filesystem, parentDir must be provided'); +/// Creates a packages directory at an arbitrary location in the given +/// filesystem. +Directory createPackagesDirectory(FileSystem fileSystem) { final Directory packagesDir = - (parentDir ?? fileSystem!.currentDirectory).childDirectory('packages'); + fileSystem.currentDirectory.childDirectory('packages'); packagesDir.createSync(); return packagesDir; } @@ -494,3 +487,45 @@ class ProcessCall { return '"${command.join(' ')}" in $workingDir'; } } + +/// Sets up standard mocking common to most command unit test setUp methods, +/// including a packages directory in an in-memory filesystem, and a mock +/// process handling (including git commands sent by GitDir). +/// +/// The returned GitDir instance forwards to a mock process runner, as described +/// in [createForwardingMockGitDir]. This process runner is separate, so that +/// tests can easily treat most git commands called as internal implementation +/// details, and assert on the exact list of non-git commands that are run. +({ + Directory packagesDir, + RecordingProcessRunner processRunner, + RecordingProcessRunner gitProcessRunner, + GitDir gitDir, +}) configureBaseCommandMocks({ + Platform? platform, + RecordingProcessRunner? customProcessRunner, + RecordingProcessRunner? customGitProcessRunner, +}) { + final FileSystem fileSystem = MemoryFileSystem( + style: (platform?.isWindows ?? false) + ? FileSystemStyle.windows + : FileSystemStyle.posix); + final Directory packagesDir = createPackagesDirectory(fileSystem); + + final RecordingProcessRunner processRunner = + customProcessRunner ?? RecordingProcessRunner(); + + final RecordingProcessRunner gitProcessRunner = + customGitProcessRunner ?? RecordingProcessRunner(); + final GitDir gitDir = createForwardingMockGitDir( + packagesDir: packagesDir, + processRunner: gitProcessRunner, + ); + + return ( + packagesDir: packagesDir, + processRunner: processRunner, + gitProcessRunner: gitProcessRunner, + gitDir: gitDir, + ); +} diff --git a/script/tool/test/version_check_command_test.dart b/script/tool/test/version_check_command_test.dart index aea2a5fd08c..a2e04bd710e 100644 --- a/script/tool/test/version_check_command_test.dart +++ b/script/tool/test/version_check_command_test.dart @@ -6,16 +6,14 @@ import 'dart:convert'; import 'package:args/command_runner.dart'; 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/version_check_command.dart'; +import 'package:git/git.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; -import 'package:mockito/mockito.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:test/test.dart'; -import 'common/package_command_test.mocks.dart'; import 'mocks.dart'; import 'util.dart'; @@ -42,33 +40,20 @@ void testAllowedVersion( void main() { const String indentation = ' '; group('VersionCheckCommand', () { - late FileSystem fileSystem; late MockPlatform mockPlatform; late Directory packagesDir; late CommandRunner runner; - late RecordingProcessRunner processRunner; - late MockGitDir gitDir; + late RecordingProcessRunner gitProcessRunner; // Ignored if mockHttpResponse is set. int mockHttpStatus; Map? mockHttpResponse; setUp(() { - fileSystem = MemoryFileSystem(); mockPlatform = MockPlatform(); - packagesDir = createPackagesDirectory(fileSystem: fileSystem); - - gitDir = MockGitDir(); - when(gitDir.path).thenReturn(packagesDir.parent.path); - when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError'))) - .thenAnswer((Invocation invocation) { - final List arguments = - invocation.positionalArguments[0]! as List; - // Route git calls through the process runner, to make mock output - // consistent with other processes. Attach the first argument to the - // command to make targeting the mock results easier. - final String gitCommand = arguments.removeAt(0); - return processRunner.run('git-$gitCommand', arguments); - }); + final RecordingProcessRunner processRunner; + final GitDir gitDir; + (:packagesDir, :processRunner, :gitProcessRunner, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); // Default to simulating the plugin never having been published. mockHttpStatus = 404; @@ -78,7 +63,6 @@ void main() { mockHttpResponse == null ? mockHttpStatus : 200); }); - processRunner = RecordingProcessRunner(); final VersionCheckCommand command = VersionCheckCommand(packagesDir, processRunner: processRunner, platform: mockPlatform, @@ -92,7 +76,8 @@ void main() { test('allows valid version', () async { createFakePlugin('plugin', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; final List output = await runCapturingPrint( @@ -106,7 +91,7 @@ void main() { ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -115,7 +100,8 @@ void main() { test('denies invalid version', () async { createFakePlugin('plugin', packagesDir, version: '0.2.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 0.0.1')), ]; Error? commandError; @@ -132,7 +118,7 @@ void main() { contains('Incorrectly updated version.'), ])); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -141,12 +127,13 @@ void main() { test('uses merge-base without explicit base-sha', () async { createFakePlugin('plugin', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-merge-base'] = + gitProcessRunner.mockProcessesForExecutable['git-merge-base'] = [ FakeProcessInfo(MockProcess(stdout: 'abc123')), FakeProcessInfo(MockProcess(stdout: 'abc123')), ]; - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; final List output = @@ -160,7 +147,7 @@ void main() { ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-merge-base', ['--fork-point', 'FETCH_HEAD', 'HEAD'], null), @@ -185,7 +172,8 @@ void main() { test('allows likely reverts.', () async { createFakePlugin('plugin', packagesDir, version: '0.6.1'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 0.6.2')), ]; final List output = await runCapturingPrint( @@ -199,7 +187,7 @@ void main() { ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -208,7 +196,8 @@ void main() { test('denies lower version that could not be a simple revert', () async { createFakePlugin('plugin', packagesDir, version: '0.5.1'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 0.6.2')), ]; @@ -226,7 +215,7 @@ void main() { contains('Incorrectly updated version.'), ])); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -236,7 +225,8 @@ void main() { test('allows minor changes to platform interfaces', () async { createFakePlugin('plugin_platform_interface', packagesDir, version: '1.1.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; final List output = await runCapturingPrint( @@ -249,7 +239,7 @@ void main() { ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', @@ -264,7 +254,8 @@ void main() { () async { createFakePlugin('plugin_platform_interface', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; Error? commandError; @@ -285,7 +276,7 @@ void main() { 'for more information.'), ])); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', @@ -300,7 +291,8 @@ void main() { () async { createFakePlugin('plugin_platform_interface', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -319,7 +311,7 @@ void main() { ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', @@ -334,7 +326,8 @@ void main() { () async { createFakePlugin('plugin_platform_interface', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; final List output = await runCapturingPrint(runner, [ @@ -352,7 +345,7 @@ void main() { ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall( 'git-show', @@ -471,7 +464,8 @@ void main() { * Some other changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -523,7 +517,8 @@ void main() { const String version = '1.0.1'; final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, version: version); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -560,7 +555,8 @@ void main() { test('fails if the version increases without replacing NEXT', () async { final RepositoryPackage plugin = createFakePlugin('plugin', packagesDir, version: '1.0.1'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -602,7 +598,8 @@ void main() { '''; plugin.changelogFile.writeAsStringSync(changelog); plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.1')), ]; @@ -652,7 +649,8 @@ void main() { * Some earlier changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -689,7 +687,8 @@ void main() { * Some other changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -722,7 +721,8 @@ void main() { * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -766,11 +766,11 @@ void main() { * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: '')), ]; @@ -797,11 +797,11 @@ void main() { * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/lib/plugin.dart @@ -834,11 +834,11 @@ packages/plugin/lib/plugin.dart * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/lib/plugin.dart @@ -867,11 +867,11 @@ packages/plugin/pubspec.yaml * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin_a/lib/plugin.dart @@ -899,11 +899,11 @@ tool/plugin/lib/plugin.dart * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/example/android/.pluginToolsConfig.yaml @@ -937,11 +937,11 @@ packages/plugin/CHANGELOG.md * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/lib/plugin.dart @@ -973,11 +973,11 @@ packages/plugin/pubspec.yaml * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/example/lib/foo.dart @@ -1010,11 +1010,11 @@ packages/plugin/example/lib/foo.dart * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/example/lib/foo.dart @@ -1043,11 +1043,11 @@ packages/plugin/CHANGELOG.md * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/example/lib/foo.dart @@ -1079,11 +1079,11 @@ packages/another_plugin/CHANGELOG.md * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/example/lib/foo.dart @@ -1117,11 +1117,11 @@ packages/plugin/example/lib/foo.dart * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ // File list. FakeProcessInfo(MockProcess(stdout: ''' @@ -1158,11 +1158,11 @@ packages/plugin/android/build.gradle * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ // File list. FakeProcessInfo(MockProcess(stdout: ''' @@ -1193,11 +1193,11 @@ packages/plugin/run_tests.sh * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ // File list. FakeProcessInfo(MockProcess(stdout: ''' @@ -1230,11 +1230,11 @@ packages/plugin/lib/plugin.dart * Some changes. '''; plugin.changelogFile.writeAsStringSync(changelog); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; - processRunner.mockProcessesForExecutable['git-diff'] = + gitProcessRunner.mockProcessesForExecutable['git-diff'] = [ FakeProcessInfo(MockProcess(stdout: ''' packages/plugin/lib/plugin.dart @@ -1351,7 +1351,8 @@ ${indentation}HTTP response: null mockHttpStatus = 404; createFakePlugin('plugin', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = [ + gitProcessRunner.mockProcessesForExecutable['git-show'] = + [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; final List result = await runCapturingPrint(runner, @@ -1370,7 +1371,7 @@ ${indentation}HTTP response: null 'allow an otherwise-valid transition that also adds a pre-release component', () async { createFakePlugin('plugin', packagesDir, version: '2.0.0-dev'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.0.0')), ]; @@ -1385,7 +1386,7 @@ ${indentation}HTTP response: null ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -1394,7 +1395,7 @@ ${indentation}HTTP response: null test('allow releasing a pre-release', () async { createFakePlugin('plugin', packagesDir, version: '1.2.0'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.2.0-dev')), ]; @@ -1409,7 +1410,7 @@ ${indentation}HTTP response: null ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -1422,7 +1423,7 @@ ${indentation}HTTP response: null 'allow an otherwise-valid transition that also removes a pre-release component', () async { createFakePlugin('plugin', packagesDir, version: '2.0.0'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.2.0-dev')), ]; @@ -1437,7 +1438,7 @@ ${indentation}HTTP response: null ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -1446,7 +1447,7 @@ ${indentation}HTTP response: null test('allow changing only the pre-release version', () async { createFakePlugin('plugin', packagesDir, version: '1.2.0-dev.2'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 1.2.0-dev.1')), ]; @@ -1461,7 +1462,7 @@ ${indentation}HTTP response: null ]), ); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -1471,7 +1472,7 @@ ${indentation}HTTP response: null test('denies invalid version change that also adds a pre-release', () async { createFakePlugin('plugin', packagesDir, version: '0.2.0-dev'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 0.0.1')), ]; @@ -1489,7 +1490,7 @@ ${indentation}HTTP response: null contains('Incorrectly updated version.'), ])); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -1499,7 +1500,7 @@ ${indentation}HTTP response: null test('denies invalid version change that also removes a pre-release', () async { createFakePlugin('plugin', packagesDir, version: '0.2.0'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 0.0.1-dev')), ]; @@ -1517,7 +1518,7 @@ ${indentation}HTTP response: null contains('Incorrectly updated version.'), ])); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) @@ -1526,7 +1527,7 @@ ${indentation}HTTP response: null test('denies invalid version change between pre-releases', () async { createFakePlugin('plugin', packagesDir, version: '0.2.0-dev'); - processRunner.mockProcessesForExecutable['git-show'] = + gitProcessRunner.mockProcessesForExecutable['git-show'] = [ FakeProcessInfo(MockProcess(stdout: 'version: 0.0.1-dev')), ]; @@ -1544,7 +1545,7 @@ ${indentation}HTTP response: null contains('Incorrectly updated version.'), ])); expect( - processRunner.recordedCalls, + gitProcessRunner.recordedCalls, containsAllInOrder(const [ ProcessCall('git-show', ['main:packages/plugin/pubspec.yaml'], null) diff --git a/script/tool/test/xcode_analyze_command_test.dart b/script/tool/test/xcode_analyze_command_test.dart index cd4fb7306c4..a69ee23120d 100644 --- a/script/tool/test/xcode_analyze_command_test.dart +++ b/script/tool/test/xcode_analyze_command_test.dart @@ -4,10 +4,10 @@ import 'package:args/command_runner.dart'; 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/common/plugin_utils.dart'; import 'package:flutter_plugin_tools/src/xcode_analyze_command.dart'; +import 'package:git/git.dart'; import 'package:test/test.dart'; import 'mocks.dart'; @@ -17,19 +17,22 @@ import 'util.dart'; // doing all the process mocking and validation. void main() { group('test xcode_analyze_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 XcodeAnalyzeCommand command = XcodeAnalyzeCommand(packagesDir, - processRunner: processRunner, platform: mockPlatform); + final GitDir gitDir; + (:packagesDir, :processRunner, gitProcessRunner: _, :gitDir) = + configureBaseCommandMocks(platform: mockPlatform); + final XcodeAnalyzeCommand command = XcodeAnalyzeCommand( + packagesDir, + processRunner: processRunner, + platform: mockPlatform, + gitDir: gitDir, + ); runner = CommandRunner( 'xcode_analyze_command', 'Test for xcode_analyze_command');