flutter/packages/flutter_tools/test/commands.shard/hermetic/downgrade_test.dart
Ian Hickson 9c7a9e779f
Give channel descriptions in flutter channel, use branch instead of upstream for channel name (#126936)
## How we determine the channel name

Historically, we used the current branch's upstream to figure out the current channel name. I have no idea why. I traced it back to https://github.com/flutter/flutter/pull/446/files where @abarth implement this and I reviewed that PR and left no comment on it at the time.

I think this is confusing. You can be on a branch and it tells you that your channel is different. That seems weird.

This PR changes the logic to uses the current branch as the channel name.

## How we display channels

The main reason this PR exists is to add channel descriptions to the `flutter channel` list:

```
ianh@burmese:~/dev/flutter/packages/flutter_tools$ flutter channel
Flutter channels:
  master (tip of tree, for contributors)
  main (tip of tree, follows master channel)
  beta (updated monthly, recommended for experienced users)
  stable (updated quarterly, for new users and for production app releases)
* foo_bar

Currently not on an official channel.
ianh@burmese:~/dev/flutter/packages/flutter_tools$
```

## Other changes

I made a few other changes while I was at it:

* If you're not on an official channel, we used to imply `--show-all`, but now we don't, we just show the official channels plus yours. This avoids flooding the screen in the case the user is on a weird channel and just wants to know what channel they're on.
* I made the tool more consistent about how it handles unofficial branches. Now it's always `[user branch]`.
* I slightly adjusted how unknown versions are rendered so it's clearer the version is unknown rather than just having the word "Unknown" floating in the output without context.
* Simplified some of the code.
* Made some of the tests more strict (checking all output rather than just some aspects of it).
* Changed the MockFlutterVersion to implement the FlutterVersion API more strictly.
* I made sure we escape the output to `.metadata` to avoid potential injection bugs (previously we just inlined the version and channel name verbatim with no escaping, which is super sketchy).
* Tweaked the help text for the `downgrade` command to be clearer.
* Removed some misleading text in some error messages.
* Made the `.metadata` generator consistent with the template file.
* Removed some obsolete code to do with the `dev` branch.

## Reviewer notes

I'm worried that there are implications to some of these changes that I am not aware of, so please don't assume I know what I'm doing when reviewing this code. :-)
2023-05-23 19:59:20 +00:00

239 lines
8.4 KiB
Dart

// Copyright 2014 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 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/downgrade.dart';
import 'package:flutter_tools/src/persistent_tool_state.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
import '../../src/context.dart';
import '../../src/fakes.dart';
import '../../src/test_flutter_command_runner.dart';
void main() {
late FileSystem fileSystem;
late BufferLogger bufferLogger;
late FakeTerminal terminal;
late ProcessManager processManager;
late FakeStdio stdio;
setUpAll(() {
Cache.disableLocking();
});
tearDownAll(() {
Cache.enableLocking();
});
setUp(() {
stdio = FakeStdio();
processManager = FakeProcessManager.any();
terminal = FakeTerminal();
fileSystem = MemoryFileSystem.test();
bufferLogger = BufferLogger.test(terminal: terminal);
});
testUsingContext('Downgrade exits on unknown channel', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(branch: 'WestSideStory'); // an unknown branch
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"invalid"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: processManager,
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
expect(createTestCommandRunner(command).run(const <String>['downgrade']),
throwsToolExit(message: 'Flutter is not currently on a known channel.'));
});
testUsingContext('Downgrade exits on no recorded version', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(branch: 'beta');
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"abcd"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: FakeProcessManager.list(<FakeCommand>[
const FakeCommand(
command: <String>[
'git', 'describe', '--tags', 'abcd',
],
stdout: 'v1.2.3',
),
]),
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
expect(createTestCommandRunner(command).run(const <String>['downgrade']),
throwsToolExit(message:
'There is no previously recorded version for channel "beta".\n'
'Channel "master" was previously on: v1.2.3.'
),
);
});
testUsingContext('Downgrade exits on unknown recorded version', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion();
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"invalid"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: FakeProcessManager.list(<FakeCommand>[
const FakeCommand(
command: <String>[
'git', 'describe', '--tags', 'invalid',
],
exitCode: 1,
),
]),
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
expect(createTestCommandRunner(command).run(const <String>['downgrade']),
throwsToolExit(message: 'Failed to parse version for downgrade'));
});
testUsingContext('Downgrade prompts for user input when terminal is attached - y', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion();
stdio.hasTerminal = true;
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: processManager,
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
terminal.addPrompt(const <String>['y', 'n'], 'y');
await createTestCommandRunner(command).run(const <String>['downgrade']);
expect(bufferLogger.statusText, contains('Success'));
});
testUsingContext('Downgrade prompts for user input when terminal is attached - n', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion();
stdio.hasTerminal = true;
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: processManager,
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
terminal.addPrompt(const <String>['y', 'n'], 'n');
await createTestCommandRunner(command).run(const <String>['downgrade']);
expect(bufferLogger.statusText, isNot(contains('Success')));
});
testUsingContext('Downgrade does not prompt when there is no terminal', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion();
stdio.hasTerminal = false;
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(
directory: fileSystem.currentDirectory,
logger: bufferLogger,
),
processManager: processManager,
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
await createTestCommandRunner(command).run(const <String>['downgrade']);
expect(bufferLogger.statusText, contains('Success'));
});
testUsingContext('Downgrade performs correct git commands', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion();
stdio.hasTerminal = false;
fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(
directory: fileSystem.currentDirectory,
logger: bufferLogger,
),
processManager: FakeProcessManager.list(<FakeCommand>[
const FakeCommand(
command: <String>[
'git', 'describe', '--tags', 'g6b00b5e88',
],
stdout: 'v1.2.3',
),
const FakeCommand(
command: <String>[
'git', 'reset', '--hard', 'g6b00b5e88',
],
),
const FakeCommand(
command: <String>[
'git', 'checkout', 'master', '--',
],
),
]),
terminal: terminal,
stdio: stdio,
flutterVersion: fakeFlutterVersion,
logger: bufferLogger,
);
await createTestCommandRunner(command).run(const <String>['downgrade']);
expect(bufferLogger.statusText, contains('Success'));
});
}
class FakeTerminal extends Fake implements Terminal {
@override
bool usesTerminalUi = false;
void addPrompt(List<String> characters, String selected) {
_characters = characters;
_selected = selected;
}
List<String>? _characters;
late String _selected;
@override
Future<String> promptForCharInput(List<String> acceptedCharacters, {Logger? logger, String? prompt, int? defaultChoiceIndex, bool displayAcceptedCharacters = true}) async {
expect(acceptedCharacters, _characters);
return _selected;
}
}
class FakeStdio extends Fake implements Stdio {
@override
bool hasTerminal = true;
}