[flutter_conductor] catch and warn when pushing working branch to mirror (#94506)

This commit is contained in:
Christopher Fujino
2021-12-06 16:59:05 -08:00
committed by GitHub
parent ad3fe21cbc
commit eea6c54dc2
4 changed files with 178 additions and 31 deletions

View File

@@ -3,6 +3,8 @@
// found in the LICENSE file.
import 'package:args/command_runner.dart';
import 'package:conductor_core/src/git.dart';
import 'package:conductor_core/src/globals.dart';
import 'package:conductor_core/src/next.dart';
import 'package:conductor_core/src/proto/conductor_state.pb.dart' as pb;
import 'package:conductor_core/src/proto/conductor_state.pbenum.dart' show ReleasePhase;
@@ -16,23 +18,24 @@ import 'package:platform/platform.dart';
import './common.dart';
void main() {
const String flutterRoot = '/flutter';
const String checkoutsParentDirectory = '$flutterRoot/dev/conductor';
const String candidateBranch = 'flutter-1.2-candidate.3';
const String workingBranch = 'cherrypicks-$candidateBranch';
const String remoteUrl = 'https://github.com/org/repo.git';
const String revision1 = 'd3af60d18e01fcb36e0c0fa06c8502e4935ed095';
const String revision2 = 'f99555c1e1392bf2a8135056b9446680c2af4ddf';
const String revision3 = '98a5ca242b9d270ce000b26309b8a3cdc9c89df5';
const String revision4 = '280e23318a0d8341415c66aa32581352a421d974';
const String releaseVersion = '1.2.0-3.0.pre';
const String releaseChannel = 'beta';
const String stateFile = '/state-file.json';
final String localPathSeparator = const LocalPlatform().pathSeparator;
final String localOperatingSystem = const LocalPlatform().pathSeparator;
group('next command', () {
const String flutterRoot = '/flutter';
const String checkoutsParentDirectory = '$flutterRoot/dev/conductor';
const String candidateBranch = 'flutter-1.2-candidate.3';
const String workingBranch = 'cherrypicks-$candidateBranch';
const String remoteUrl = 'https://github.com/org/repo.git';
final String localPathSeparator = const LocalPlatform().pathSeparator;
final String localOperatingSystem = const LocalPlatform().pathSeparator;
const String revision1 = 'd3af60d18e01fcb36e0c0fa06c8502e4935ed095';
const String revision2 = 'f99555c1e1392bf2a8135056b9446680c2af4ddf';
const String revision3 = '98a5ca242b9d270ce000b26309b8a3cdc9c89df5';
const String revision4 = '280e23318a0d8341415c66aa32581352a421d974';
const String releaseVersion = '1.2.0-3.0.pre';
const String releaseChannel = 'beta';
late MemoryFileSystem fileSystem;
late TestStdio stdio;
const String stateFile = '/state-file.json';
setUp(() {
stdio = TestStdio();
@@ -1102,6 +1105,68 @@ void main() {
);
});
});
group('.pushWorkingBranch()', () {
late MemoryFileSystem fileSystem;
late TestStdio stdio;
late Platform platform;
setUp(() {
stdio = TestStdio();
fileSystem = MemoryFileSystem.test();
platform = FakePlatform();
});
test('catches GitException if the push was rejected and instead throws a helpful ConductorException', () async {
const String gitPushErrorMessage = '''
To github.com:user/engine.git
! [rejected] HEAD -> cherrypicks-flutter-2.8-candidate.3 (non-fast-forward)
error: failed to push some refs to 'github.com:user/engine.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
''';
final Checkouts checkouts = Checkouts(
fileSystem: fileSystem,
parentDirectory: fileSystem.directory(checkoutsParentDirectory)..createSync(recursive: true),
platform: platform,
processManager: FakeProcessManager.empty(),
stdio: stdio,
);
final Repository testRepository = _TestRepository.fromCheckouts(checkouts);
final pb.Repository testPbRepository = pb.Repository();
(checkouts.processManager as FakeProcessManager).addCommands(<FakeCommand>[
FakeCommand(
command: <String>['git', 'clone', '--origin', 'upstream', '--', testRepository.upstreamRemote.url, '/flutter/dev/conductor/flutter_conductor_checkouts/test-repo/test-repo'],
),
const FakeCommand(
command: <String>['git', 'rev-parse', 'HEAD'],
stdout: revision1,
),
FakeCommand(
command: const <String>['git', 'push', '', 'HEAD:refs/heads/'],
exception: GitException(gitPushErrorMessage, <String>['git', 'push', '--force', '', 'HEAD:refs/heads/']),
)
]);
final NextContext nextContext = NextContext(
autoAccept: false,
checkouts: checkouts,
force: false,
stateFile: fileSystem.file(stateFile),
);
expect(
() => nextContext.pushWorkingBranch(testRepository, testPbRepository),
throwsA(isA<ConductorException>().having(
(ConductorException exception) => exception.message,
'has correct message',
contains('Re-run this command with --force to overwrite the remote branch'),
)),
);
});
});
}
/// A [Stdio] that will throw an exception if any of its methods are called.
@@ -1135,6 +1200,24 @@ class _UnimplementedStdio implements Stdio {
String readLineSync() => _throw();
}
class _TestRepository extends Repository {
_TestRepository.fromCheckouts(Checkouts checkouts, [String name = 'test-repo']) : super(
fileSystem: checkouts.fileSystem,
parentDirectory: checkouts.directory.childDirectory(name),
platform: checkouts.platform,
processManager: checkouts.processManager,
name: name,
requiredLocalBranches: <String>[],
stdio: checkouts.stdio,
upstreamRemote: const Remote(name: RemoteName.upstream, url: 'git@github.com:upstream/repo.git'),
);
@override
Future<_TestRepository> cloneRepository(String? cloneName) async {
throw Exception('Unimplemented!');
}
}
class _TestNextContext extends NextContext {
const _TestNextContext({
bool autoAccept = false,