mirror of
https://github.com/flutter/packages.git
synced 2025-08-24 03:18:54 +08:00

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
118 lines
3.8 KiB
Dart
118 lines
3.8 KiB
Dart
// Copyright 2013 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter_plugin_tools/src/common/git_version_finder.dart';
|
|
import 'package:mockito/mockito.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
import 'package_command_test.mocks.dart';
|
|
|
|
void main() {
|
|
late List<List<String>?> gitDirCommands;
|
|
late String gitDiffResponse;
|
|
late MockGitDir gitDir;
|
|
String mergeBaseResponse = '';
|
|
|
|
setUp(() {
|
|
gitDirCommands = <List<String>?>[];
|
|
gitDiffResponse = '';
|
|
gitDir = MockGitDir();
|
|
when(gitDir.runCommand(any, throwOnError: anyNamed('throwOnError')))
|
|
.thenAnswer((Invocation invocation) {
|
|
final List<String> arguments =
|
|
invocation.positionalArguments[0]! as List<String>;
|
|
gitDirCommands.add(arguments);
|
|
String? gitStdOut;
|
|
if (arguments[0] == 'diff') {
|
|
gitStdOut = gitDiffResponse;
|
|
} else if (arguments[0] == 'merge-base') {
|
|
gitStdOut = mergeBaseResponse;
|
|
}
|
|
return Future<ProcessResult>.value(
|
|
ProcessResult(0, 0, gitStdOut ?? '', ''));
|
|
});
|
|
});
|
|
|
|
test('No git diff should result no files changed', () async {
|
|
final GitVersionFinder finder =
|
|
GitVersionFinder(gitDir, baseSha: 'some base sha');
|
|
final List<String> changedFiles = await finder.getChangedFiles();
|
|
|
|
expect(changedFiles, isEmpty);
|
|
});
|
|
|
|
test('get correct files changed based on git diff', () async {
|
|
gitDiffResponse = '''
|
|
file1/file1.cc
|
|
file2/file2.cc
|
|
''';
|
|
final GitVersionFinder finder =
|
|
GitVersionFinder(gitDir, baseSha: 'some base sha');
|
|
final List<String> changedFiles = await finder.getChangedFiles();
|
|
|
|
expect(changedFiles, equals(<String>['file1/file1.cc', 'file2/file2.cc']));
|
|
});
|
|
|
|
test('use correct base sha if not specified', () async {
|
|
mergeBaseResponse = 'shaqwiueroaaidf12312jnadf123nd';
|
|
gitDiffResponse = '''
|
|
file1/pubspec.yaml
|
|
file2/file2.cc
|
|
''';
|
|
|
|
final GitVersionFinder finder = GitVersionFinder(gitDir);
|
|
await finder.getChangedFiles();
|
|
verify(gitDir.runCommand(
|
|
<String>['merge-base', '--fork-point', 'FETCH_HEAD', 'HEAD'],
|
|
throwOnError: false));
|
|
verify(gitDir.runCommand(
|
|
<String>['diff', '--name-only', mergeBaseResponse, 'HEAD']));
|
|
});
|
|
|
|
test('uses correct base branch to find base sha if specified', () async {
|
|
mergeBaseResponse = 'shaqwiueroaaidf12312jnadf123nd';
|
|
gitDiffResponse = '''
|
|
file1/pubspec.yaml
|
|
file2/file2.cc
|
|
''';
|
|
|
|
final GitVersionFinder finder =
|
|
GitVersionFinder(gitDir, baseBranch: 'upstream/main');
|
|
await finder.getChangedFiles();
|
|
verify(gitDir.runCommand(
|
|
<String>['merge-base', '--fork-point', 'upstream/main', 'HEAD'],
|
|
throwOnError: false));
|
|
verify(gitDir.runCommand(
|
|
<String>['diff', '--name-only', mergeBaseResponse, 'HEAD']));
|
|
});
|
|
|
|
test('use correct base sha if specified', () async {
|
|
const String customBaseSha = 'aklsjdcaskf12312';
|
|
gitDiffResponse = '''
|
|
file1/pubspec.yaml
|
|
file2/file2.cc
|
|
''';
|
|
final GitVersionFinder finder =
|
|
GitVersionFinder(gitDir, baseSha: customBaseSha);
|
|
await finder.getChangedFiles();
|
|
verify(gitDir
|
|
.runCommand(<String>['diff', '--name-only', customBaseSha, 'HEAD']));
|
|
});
|
|
|
|
test('include uncommitted files if requested', () async {
|
|
const String customBaseSha = 'aklsjdcaskf12312';
|
|
gitDiffResponse = '''
|
|
file1/pubspec.yaml
|
|
file2/file2.cc
|
|
''';
|
|
final GitVersionFinder finder =
|
|
GitVersionFinder(gitDir, baseSha: customBaseSha);
|
|
await finder.getChangedFiles(includeUncommitted: true);
|
|
// The call should not have HEAD as a final argument like the default diff.
|
|
verify(gitDir.runCommand(<String>['diff', '--name-only', customBaseSha]));
|
|
});
|
|
}
|