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

365 lines
13 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:args/command_runner.dart';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/channel.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/version.dart';
import '../src/common.dart';
import '../src/context.dart';
import '../src/fake_process_manager.dart';
import '../src/fakes.dart' show FakeFlutterVersion;
import '../src/test_flutter_command_runner.dart';
void main() {
group('channel', () {
late FakeProcessManager fakeProcessManager;
setUp(() {
fakeProcessManager = FakeProcessManager.empty();
});
setUpAll(() {
Cache.disableLocking();
});
Future<void> simpleChannelTest(List<String> args) async {
fakeProcessManager.addCommands(const <FakeCommand>[
FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout:
' origin/branch-1\n'
' origin/branch-2\n'
' origin/master\n'
' origin/main\n'
' origin/stable\n'
' origin/beta',
),
]);
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(args);
expect(testLogger.errorText, hasLength(0));
// The bots may return an empty list of channels (network hiccup?)
// and when run locally the list of branches might be different
// so we check for the header text rather than any specific channel name.
expect(
testLogger.statusText,
containsIgnoringWhitespace('Flutter channels:'),
);
}
testUsingContext('list', () async {
await simpleChannelTest(<String>['channel']);
}, overrides: <Type, Generator>{
ProcessManager: () => fakeProcessManager,
FileSystem: () => MemoryFileSystem.test(),
});
testUsingContext('verbose list', () async {
await simpleChannelTest(<String>['channel', '-v']);
}, overrides: <Type, Generator>{
ProcessManager: () => fakeProcessManager,
FileSystem: () => MemoryFileSystem.test(),
});
testUsingContext('sorted by stability', () async {
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
fakeProcessManager.addCommand(
const FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout: 'origin/beta\n'
'origin/master\n'
'origin/main\n'
'origin/stable\n',
),
);
await runner.run(<String>['channel']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(testLogger.errorText, hasLength(0));
expect(testLogger.statusText,
'Flutter channels:\n'
'* master (latest development branch, for contributors)\n'
' main (latest development branch, follows master channel)\n'
' beta (updated monthly, recommended for experienced users)\n'
' stable (updated quarterly, for new users and for production app releases)\n',
);
// clear buffer for next process
testLogger.clear();
// Extra branches.
fakeProcessManager.addCommand(
const FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout: 'origin/beta\n'
'origin/master\n'
'origin/dependabot/bundler\n'
'origin/main\n'
'origin/v1.4.5-hotfixes\n'
'origin/stable\n',
),
);
await runner.run(<String>['channel']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(testLogger.errorText, hasLength(0));
expect(testLogger.statusText,
'Flutter channels:\n'
'* master (latest development branch, for contributors)\n'
' main (latest development branch, follows master channel)\n'
' beta (updated monthly, recommended for experienced users)\n'
' stable (updated quarterly, for new users and for production app releases)\n',
);
// clear buffer for next process
testLogger.clear();
// Missing branches.
fakeProcessManager.addCommand(
const FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout: 'origin/master\n'
'origin/dependabot/bundler\n'
'origin/v1.4.5-hotfixes\n'
'origin/stable\n'
'origin/beta\n',
),
);
await runner.run(<String>['channel']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(testLogger.errorText, hasLength(0));
// check if available official channels are in order of stability
int prev = -1;
int next = -1;
for (final String branch in kOfficialChannels) {
next = testLogger.statusText.indexOf(branch);
if (next != -1) {
expect(prev < next, isTrue);
prev = next;
}
}
}, overrides: <Type, Generator>{
ProcessManager: () => fakeProcessManager,
FileSystem: () => MemoryFileSystem.test(),
});
testUsingContext('ignores lines with unexpected output', () async {
fakeProcessManager.addCommand(
const FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout: 'origin/beta\n'
'origin/stable\n'
'upstream/beta\n'
'upstream/stable\n'
'foo',
),
);
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['channel']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(testLogger.errorText, hasLength(0));
expect(testLogger.statusText,
'Flutter channels:\n'
'* beta (updated monthly, recommended for experienced users)\n'
' stable (updated quarterly, for new users and for production app releases)\n'
);
}, overrides: <Type, Generator>{
ProcessManager: () => fakeProcessManager,
FileSystem: () => MemoryFileSystem.test(),
FlutterVersion: () => FakeFlutterVersion(branch: 'beta'),
});
testUsingContext('handles custom branches', () async {
fakeProcessManager.addCommand(
const FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout: 'origin/beta\n'
'origin/stable\n'
'origin/foo',
),
);
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['channel']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(testLogger.errorText, hasLength(0));
expect(testLogger.statusText,
'Flutter channels:\n'
' beta (updated monthly, recommended for experienced users)\n'
' stable (updated quarterly, for new users and for production app releases)\n'
'* foo\n'
'\n'
'Currently not on an official channel.\n',
);
}, overrides: <Type, Generator>{
ProcessManager: () => fakeProcessManager,
FileSystem: () => MemoryFileSystem.test(),
FlutterVersion: () => FakeFlutterVersion(branch: 'foo'),
});
testUsingContext('removes duplicates', () async {
fakeProcessManager.addCommand(
const FakeCommand(
command: <String>['git', 'branch', '-r'],
stdout: 'origin/beta\n'
'origin/stable\n'
'upstream/beta\n'
'upstream/stable\n',
),
);
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['channel']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(testLogger.errorText, hasLength(0));
expect(testLogger.statusText,
'Flutter channels:\n'
'* beta (updated monthly, recommended for experienced users)\n'
' stable (updated quarterly, for new users and for production app releases)\n'
);
}, overrides: <Type, Generator>{
ProcessManager: () => fakeProcessManager,
FileSystem: () => MemoryFileSystem.test(),
FlutterVersion: () => FakeFlutterVersion(branch: 'beta'),
});
testUsingContext('can switch channels', () async {
fakeProcessManager.addCommands(<FakeCommand>[
const FakeCommand(
command: <String>['git', 'fetch'],
),
const FakeCommand(
command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/beta'],
),
const FakeCommand(
command: <String>['git', 'checkout', 'beta', '--']
),
]);
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['channel', 'beta']);
expect(fakeProcessManager, hasNoRemainingExpectations);
expect(
testLogger.statusText,
containsIgnoringWhitespace("Switching to flutter channel 'beta'..."),
);
expect(testLogger.errorText, hasLength(0));
fakeProcessManager.addCommands(<FakeCommand>[
const FakeCommand(
command: <String>['git', 'fetch'],
),
const FakeCommand(
command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/stable'],
),
const FakeCommand(
command: <String>['git', 'checkout', 'stable', '--']
),
]);
await runner.run(<String>['channel', 'stable']);
expect(fakeProcessManager, hasNoRemainingExpectations);
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => fakeProcessManager,
});
testUsingContext('switching channels prompts to run flutter upgrade', () async {
fakeProcessManager.addCommands(<FakeCommand>[
const FakeCommand(
command: <String>['git', 'fetch'],
),
const FakeCommand(
command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/beta'],
),
const FakeCommand(
command: <String>['git', 'checkout', 'beta', '--']
),
]);
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['channel', 'beta']);
expect(
testLogger.statusText,
containsIgnoringWhitespace("Successfully switched to flutter channel 'beta'."),
);
expect(
testLogger.statusText,
containsIgnoringWhitespace(
"To ensure that you're on the latest build "
"from this channel, run 'flutter upgrade'"),
);
expect(testLogger.errorText, hasLength(0));
expect(fakeProcessManager, hasNoRemainingExpectations);
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => fakeProcessManager,
});
// This verifies that bug https://github.com/flutter/flutter/issues/21134
// doesn't return.
testUsingContext('removes version stamp file when switching channels', () async {
fakeProcessManager.addCommands(<FakeCommand>[
const FakeCommand(
command: <String>['git', 'fetch'],
),
const FakeCommand(
command: <String>['git', 'show-ref', '--verify', '--quiet', 'refs/heads/beta'],
),
const FakeCommand(
command: <String>['git', 'checkout', 'beta', '--']
),
]);
final File versionCheckFile = globals.cache.getStampFileFor(
VersionCheckStamp.flutterVersionCheckStampFile,
);
/// Create a bogus "leftover" version check file to make sure it gets
/// removed when the channel changes. The content doesn't matter.
versionCheckFile.createSync(recursive: true);
versionCheckFile.writeAsStringSync('''
{
"lastTimeVersionWasChecked": "2151-08-29 10:17:30.763802",
"lastKnownRemoteVersion": "2151-09-26 15:56:19.000Z"
}
''');
final ChannelCommand command = ChannelCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['channel', 'beta']);
expect(testLogger.statusText, isNot(contains('A new version of Flutter')));
expect(testLogger.errorText, hasLength(0));
expect(versionCheckFile.existsSync(), isFalse);
expect(fakeProcessManager, hasNoRemainingExpectations);
}, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem.test(),
ProcessManager: () => fakeProcessManager,
});
});
}