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'; ...@@ -20,12 +20,19 @@ import 'channel.dart';
class UpgradeCommand extends FlutterCommand { class UpgradeCommand extends FlutterCommand {
UpgradeCommand() { UpgradeCommand() {
argParser.addFlag( argParser
'force', ..addFlag(
abbr: 'f', 'force',
help: 'force upgrade the flutter branch, potentially discarding local changes.', abbr: 'f',
negatable: false, 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 @override
...@@ -45,7 +52,12 @@ class UpgradeCommand extends FlutterCommand { ...@@ -45,7 +52,12 @@ class UpgradeCommand extends FlutterCommand {
@override @override
Future<FlutterCommandResult> runCommand() async { Future<FlutterCommandResult> runCommand() async {
final UpgradeCommandRunner upgradeCommandRunner = UpgradeCommandRunner(); 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; return null;
} }
} }
...@@ -53,7 +65,25 @@ class UpgradeCommand extends FlutterCommand { ...@@ -53,7 +65,25 @@ class UpgradeCommand extends FlutterCommand {
@visibleForTesting @visibleForTesting
class UpgradeCommandRunner { 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(); await verifyUpstreamConfigured();
if (!force && gitTagVersion == const GitTagVersion.unknown()) { if (!force && gitTagVersion == const GitTagVersion.unknown()) {
// If the commit is a recognized branch and not master, // If the commit is a recognized branch and not master,
...@@ -87,10 +117,31 @@ class UpgradeCommandRunner { ...@@ -87,10 +117,31 @@ class UpgradeCommandRunner {
await resetChanges(gitTagVersion); await resetChanges(gitTagVersion);
await upgradeChannel(flutterVersion); await upgradeChannel(flutterVersion);
await attemptFastForward(); 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 precacheArtifacts();
await updatePackages(flutterVersion); await updatePackages(flutterVersion);
await runDoctor(); await runDoctor();
return null;
} }
Future<bool> hasUncomittedChanges() async { Future<bool> hasUncomittedChanges() async {
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:convert';
import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
...@@ -16,6 +18,21 @@ import 'package:process/process.dart'; ...@@ -16,6 +18,21 @@ import 'package:process/process.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.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() { void main() {
group('UpgradeCommandRunner', () { group('UpgradeCommandRunner', () {
FakeUpgradeCommandRunner fakeCommandRunner; FakeUpgradeCommandRunner fakeCommandRunner;
...@@ -29,11 +46,24 @@ void main() { ...@@ -29,11 +46,24 @@ void main() {
fakeCommandRunner = FakeUpgradeCommandRunner(); fakeCommandRunner = FakeUpgradeCommandRunner();
realCommandRunner = UpgradeCommandRunner(); realCommandRunner = UpgradeCommandRunner();
processManager = MockProcessManager(); 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; fakeCommandRunner.willHaveUncomittedChanges = false;
}); });
test('throws on unknown tag, official branch, noforce', () async { test('throws on unknown tag, official branch, noforce', () async {
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand( final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
false, false,
const GitTagVersion.unknown(), const GitTagVersion.unknown(),
flutterVersion, flutterVersion,
...@@ -41,18 +71,22 @@ void main() { ...@@ -41,18 +71,22 @@ void main() {
expect(result, throwsA(isInstanceOf<ToolExit>())); 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( final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
true, true,
false,
const GitTagVersion.unknown(), const GitTagVersion.unknown(),
flutterVersion, flutterVersion,
); );
expect(await result, null); expect(await result, null);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
}); });
test('throws tool exit with uncommitted changes', () async { test('throws tool exit with uncommitted changes', () async {
fakeCommandRunner.willHaveUncomittedChanges = true; fakeCommandRunner.willHaveUncomittedChanges = true;
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand( final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
false, false,
gitTagVersion, gitTagVersion,
flutterVersion, flutterVersion,
...@@ -60,30 +94,37 @@ void main() { ...@@ -60,30 +94,37 @@ void main() {
expect(result, throwsA(isA<ToolExit>())); 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; fakeCommandRunner.willHaveUncomittedChanges = true;
final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand( final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
true, true,
false,
gitTagVersion, gitTagVersion,
flutterVersion, flutterVersion,
); );
expect(await result, null); 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( final Future<FlutterCommandResult> result = fakeCommandRunner.runCommand(
false,
false, false,
gitTagVersion, gitTagVersion,
flutterVersion, flutterVersion,
); );
expect(await result, null); expect(await result, null);
}, overrides: <Type, Generator>{
ProcessManager: () => processManager,
}); });
testUsingContext('verifyUpstreamConfigured', () async { testUsingContext('verifyUpstreamConfigured', () async {
when(processManager.run( when(processManager.run(
<String>['git', 'rev-parse', '@{u}'], <String>['git', 'rev-parse', '@{u}'],
environment:anyNamed('environment'), environment:anyNamed('environment'),
workingDirectory: anyNamed('workingDirectory')) workingDirectory: anyNamed('workingDirectory')),
).thenAnswer((Invocation invocation) async { ).thenAnswer((Invocation invocation) async {
return FakeProcessResult() return FakeProcessResult()
..exitCode = 0; ..exitCode = 0;
...@@ -176,6 +217,7 @@ class FakeUpgradeCommandRunner extends UpgradeCommandRunner { ...@@ -176,6 +217,7 @@ class FakeUpgradeCommandRunner extends UpgradeCommandRunner {
} }
class MockFlutterVersion extends Mock implements FlutterVersion {} class MockFlutterVersion extends Mock implements FlutterVersion {}
class MockProcess extends Mock implements Process {}
class MockProcessManager extends Mock implements ProcessManager {} class MockProcessManager extends Mock implements ProcessManager {}
class FakeProcessResult implements ProcessResult { class FakeProcessResult implements ProcessResult {
@override @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