Unverified Commit cd1e55b5 authored by Christopher Fujino's avatar Christopher Fujino Committed by GitHub

refactor `flutter upgrade` to be 2 part, with the second part re-entrant (#38325)

parent 9615eb99
......@@ -20,12 +20,19 @@ import 'channel.dart';
class UpgradeCommand extends FlutterCommand {
UpgradeCommand() {
argParser.addFlag(
'force',
abbr: 'f',
help: 'force upgrade the flutter branch, potentially discarding local changes.',
negatable: false,
);
argParser
..addFlag(
'force',
abbr: 'f',
help: 'Force upgrade the flutter branch, potentially discarding local changes.',
negatable: false,
)
..addFlag(
'continue',
hide: true,
negatable: false,
help: 'For the second half of the upgrade flow requiring the new version of Flutter. Should not be invoked manually, but re-entrantly by the standard upgrade command.',
);
}
@override
......@@ -45,7 +52,12 @@ class UpgradeCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> runCommand() async {
final UpgradeCommandRunner upgradeCommandRunner = UpgradeCommandRunner();
await upgradeCommandRunner.runCommand(argResults['force'], GitTagVersion.determine(), FlutterVersion.instance);
await upgradeCommandRunner.runCommand(
argResults['force'],
argResults['continue'],
GitTagVersion.determine(),
FlutterVersion.instance,
);
return null;
}
}
......@@ -53,7 +65,25 @@ class UpgradeCommand extends FlutterCommand {
@visibleForTesting
class UpgradeCommandRunner {
Future<FlutterCommandResult> runCommand(bool force, GitTagVersion gitTagVersion, FlutterVersion flutterVersion) async {
Future<FlutterCommandResult> runCommand(
bool force,
bool continueFlow,
GitTagVersion gitTagVersion,
FlutterVersion flutterVersion,
) async {
if (!continueFlow) {
await runCommandFirstHalf(force, gitTagVersion, flutterVersion);
} else {
await runCommandSecondHalf(flutterVersion);
}
return null;
}
Future<void> runCommandFirstHalf(
bool force,
GitTagVersion gitTagVersion,
FlutterVersion flutterVersion,
) async {
await verifyUpstreamConfigured();
if (!force && gitTagVersion == const GitTagVersion.unknown()) {
// If the commit is a recognized branch and not master,
......@@ -87,10 +117,31 @@ class UpgradeCommandRunner {
await resetChanges(gitTagVersion);
await upgradeChannel(flutterVersion);
await attemptFastForward();
await flutterUpgradeContinue();
}
Future<void> flutterUpgradeContinue() async {
final int code = await runCommandAndStreamOutput(
<String>[
fs.path.join('bin', 'flutter'),
'upgrade',
'--continue',
'--no-version-check',
],
workingDirectory: Cache.flutterRoot,
allowReentrantFlutter: true,
);
if (code != 0) {
throwToolExit(null, exitCode: code);
}
}
// This method should only be called if the upgrade command is invoked
// re-entrantly with the `--continue` flag
Future<void> runCommandSecondHalf(FlutterVersion flutterVersion) async {
await precacheArtifacts();
await updatePackages(flutterVersion);
await runDoctor();
return null;
}
Future<bool> hasUncomittedChanges() async {
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
......@@ -16,6 +18,21 @@ import 'package:process/process.dart';
import '../../src/common.dart';
import '../../src/context.dart';
Process createMockProcess({ int exitCode = 0, String stdout = '', String stderr = '' }) {
final Stream<List<int>> stdoutStream = Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(stdout),
]);
final Stream<List<int>> stderrStream = Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(stderr),
]);
final Process process = MockProcess();
when(process.stdout).thenAnswer((_) => stdoutStream);
when(process.stderr).thenAnswer((_) => stderrStream);
when(process.exitCode).thenAnswer((_) => Future<int>.value(exitCode));
return process;
}
void main() {
group('UpgradeCommandRunner', () {
FakeUpgradeCommandRunner fakeCommandRunner;
......@@ -29,11 +46,24 @@ void main() {
fakeCommandRunner = FakeUpgradeCommandRunner();
realCommandRunner = UpgradeCommandRunner();
processManager = MockProcessManager();
when(processManager.start(
<String>[
fs.path.join('bin', 'flutter'),
'upgrade',
'--continue',
'--no-version-check',
],
environment: anyNamed('environment'),
workingDirectory: anyNamed('workingDirectory'),
)).thenAnswer((Invocation invocation) async {
return Future<Process>.value(createMockProcess());
});
fakeCommandRunner.willHaveUncomittedChanges = false;
});
test('throws on unknown tag, official branch, noforce', () async {
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
false,
const GitTagVersion.unknown(),
flutterVersion,
......@@ -41,18 +71,22 @@ void main() {
expect(result, throwsA(isInstanceOf<ToolExit>()));
});
test('does not throw on unknown tag, official branch, force', () async {
testUsingContext('does not throw on unknown tag, official branch, force', () async {
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
true,
false,
const GitTagVersion.unknown(),
flutterVersion,
);
expect(await result, null);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
});
test('throws tool exit with uncommitted changes', () async {
fakeCommandRunner.willHaveUncomittedChanges = true;
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
false,
gitTagVersion,
flutterVersion,
......@@ -60,30 +94,37 @@ void main() {
expect(result, throwsA(isA<ToolExit>()));
});
test('does not throw tool exit with uncommitted changes and force', () async {
testUsingContext('does not throw tool exit with uncommitted changes and force', () async {
fakeCommandRunner.willHaveUncomittedChanges = true;
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
true,
false,
gitTagVersion,
flutterVersion,
);
expect(await result, null);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
});
test('Doesn\'t throw on known tag, dev branch, no force', () async {
testUsingContext('Doesn\'t throw on known tag, dev branch, no force', () async {
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
false,
gitTagVersion,
flutterVersion,
);
expect(await result, null);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
});
testUsingContext('verifyUpstreamConfigured', () async {
when(processManager.run(
<String>['git', 'rev-parse', '@{u}'],
environment:anyNamed('environment'),
workingDirectory: anyNamed('workingDirectory'))
workingDirectory: anyNamed('workingDirectory')),
).thenAnswer((Invocation invocation) async {
return FakeProcessResult()
..exitCode = 0;
......@@ -176,6 +217,7 @@ class FakeUpgradeCommandRunner extends UpgradeCommandRunner {
}
class MockFlutterVersion extends Mock implements FlutterVersion {}
class MockProcess extends Mock implements Process {}
class MockProcessManager extends Mock implements ProcessManager {}
class FakeProcessResult implements ProcessResult {
@override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment