Unverified Commit 71c42c9c authored by includecmath's avatar includecmath Committed by GitHub

[flutter_tools] Add channel order aware version_test (#62417)

parent a2406601
...@@ -72,7 +72,7 @@ class ChannelCommand extends FlutterCommand { ...@@ -72,7 +72,7 @@ class ChannelCommand extends FlutterCommand {
throwToolExit('List channels failed: $result$details', exitCode: result); throwToolExit('List channels failed: $result$details', exitCode: result);
} }
final List<String> officialChannels = FlutterVersion.officialChannels.toList(); final List<String> officialChannels = kOfficialChannels.toList();
final List<bool> availableChannels = List<bool>.filled(officialChannels.length, false); final List<bool> availableChannels = List<bool>.filled(officialChannels.length, false);
for (final String line in rawOutput) { for (final String line in rawOutput) {
...@@ -116,10 +116,10 @@ class ChannelCommand extends FlutterCommand { ...@@ -116,10 +116,10 @@ class ChannelCommand extends FlutterCommand {
Future<void> _switchChannel(String branchName) async { Future<void> _switchChannel(String branchName) async {
globals.printStatus("Switching to flutter channel '$branchName'..."); globals.printStatus("Switching to flutter channel '$branchName'...");
if (FlutterVersion.obsoleteBranches.containsKey(branchName)) { if (kObsoleteBranches.containsKey(branchName)) {
final String alternative = FlutterVersion.obsoleteBranches[branchName]; final String alternative = kObsoleteBranches[branchName];
globals.printStatus("This channel is obsolete. Consider switching to the '$alternative' channel instead."); globals.printStatus("This channel is obsolete. Consider switching to the '$alternative' channel instead.");
} else if (!FlutterVersion.officialChannels.contains(branchName)) { } else if (!kOfficialChannels.contains(branchName)) {
globals.printStatus('This is not an official channel. For a list of available channels, try "flutter channel".'); globals.printStatus('This is not an official channel. For a list of available channels, try "flutter channel".');
} }
await _checkout(branchName); await _checkout(branchName);
...@@ -129,8 +129,8 @@ class ChannelCommand extends FlutterCommand { ...@@ -129,8 +129,8 @@ class ChannelCommand extends FlutterCommand {
static Future<void> upgradeChannel() async { static Future<void> upgradeChannel() async {
final String channel = globals.flutterVersion.channel; final String channel = globals.flutterVersion.channel;
if (FlutterVersion.obsoleteBranches.containsKey(channel)) { if (kObsoleteBranches.containsKey(channel)) {
final String alternative = FlutterVersion.obsoleteBranches[channel]; final String alternative = kObsoleteBranches[channel];
globals.printStatus("Transitioning from '$channel' to '$alternative'..."); globals.printStatus("Transitioning from '$channel' to '$alternative'...");
return _checkout(alternative); return _checkout(alternative);
} }
......
...@@ -107,7 +107,7 @@ class UpgradeCommandRunner { ...@@ -107,7 +107,7 @@ class UpgradeCommandRunner {
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,
// explain that we are avoiding potential damage. // explain that we are avoiding potential damage.
if (flutterVersion.channel != 'master' && FlutterVersion.officialChannels.contains(flutterVersion.channel)) { if (flutterVersion.channel != 'master' && kOfficialChannels.contains(flutterVersion.channel)) {
throwToolExit( throwToolExit(
'Unknown flutter tag. Abandoning upgrade to avoid destroying local ' 'Unknown flutter tag. Abandoning upgrade to avoid destroying local '
'changes. It is recommended to use git directly if not working on ' 'changes. It is recommended to use git directly if not working on '
......
...@@ -13,6 +13,20 @@ import 'cache.dart'; ...@@ -13,6 +13,20 @@ import 'cache.dart';
import 'convert.dart'; import 'convert.dart';
import 'globals.dart' as globals; import 'globals.dart' as globals;
/// The flutter GitHub repository.
String get _flutterGit => globals.platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git';
/// This maps old branch names to the names of branches that replaced them.
///
/// For example, in early 2018 we changed from having an "alpha" branch to
/// having a "dev" branch, so anyone using "alpha" now gets transitioned to
/// "dev".
const Map<String, String> kObsoleteBranches = <String, String>{
'alpha': 'dev',
'hackathon': 'dev',
'codelab': 'dev',
};
/// The names of each channel/branch in order of increasing stability. /// The names of each channel/branch in order of increasing stability.
enum Channel { enum Channel {
master, master,
...@@ -21,23 +35,28 @@ enum Channel { ...@@ -21,23 +35,28 @@ enum Channel {
stable, stable,
} }
/// The flutter GitHub repository. // Beware: Keep order in accordance with stability
String get _flutterGit => globals.platform.environment['FLUTTER_GIT_URL'] ?? 'https://github.com/flutter/flutter.git'; const Set<String> kOfficialChannels = <String>{
'master',
'dev',
'beta',
'stable',
};
/// Retrieve a human-readable name for a given [channel]. /// Retrieve a human-readable name for a given [channel].
/// ///
/// Requires [FlutterVersion.officialChannels] to be correctly ordered. /// Requires [kOfficialChannels] to be correctly ordered.
String getNameForChannel(Channel channel) { String getNameForChannel(Channel channel) {
return FlutterVersion.officialChannels.elementAt(channel.index); return kOfficialChannels.elementAt(channel.index);
} }
/// Retrieve the [Channel] representation for a string [name]. /// Retrieve the [Channel] representation for a string [name].
/// ///
/// Returns `null` if [name] is not in the list of official channels, according /// Returns `null` if [name] is not in the list of official channels, according
/// to [FlutterVersion.officialChannels]. /// to [kOfficialChannels].
Channel getChannelForName(String name) { Channel getChannelForName(String name) {
if (FlutterVersion.officialChannels.contains(name)) { if (kOfficialChannels.contains(name)) {
return Channel.values[FlutterVersion.officialChannels.toList().indexOf(name)]; return Channel.values[kOfficialChannels.toList().indexOf(name)];
} }
return null; return null;
} }
...@@ -84,25 +103,6 @@ class FlutterVersion { ...@@ -84,25 +103,6 @@ class FlutterVersion {
return !<String>['dev', 'beta', 'stable'].contains(branchName); return !<String>['dev', 'beta', 'stable'].contains(branchName);
} }
// Beware: Keep order in accordance with stability
static const Set<String> officialChannels = <String>{
'master',
'dev',
'beta',
'stable',
};
/// This maps old branch names to the names of branches that replaced them.
///
/// For example, in early 2018 we changed from having an "alpha" branch to
/// having a "dev" branch, so anyone using "alpha" now gets transitioned to
/// "dev".
static Map<String, String> obsoleteBranches = <String, String>{
'alpha': 'dev',
'hackathon': 'dev',
'codelab': 'dev',
};
String _channel; String _channel;
/// The channel is the upstream branch. /// The channel is the upstream branch.
/// `master`, `dev`, `beta`, `stable`; or old ones, like `alpha`, `hackathon`, ... /// `master`, `dev`, `beta`, `stable`; or old ones, like `alpha`, `hackathon`, ...
...@@ -298,8 +298,8 @@ class FlutterVersion { ...@@ -298,8 +298,8 @@ class FlutterVersion {
}(); }();
if (redactUnknownBranches || _branch.isEmpty) { if (redactUnknownBranches || _branch.isEmpty) {
// Only return the branch names we know about; arbitrary branch names might contain PII. // Only return the branch names we know about; arbitrary branch names might contain PII.
if (!officialChannels.contains(_branch) && if (!kOfficialChannels.contains(_branch) &&
!obsoleteBranches.containsKey(_branch)) { !kObsoleteBranches.containsKey(_branch)) {
return '[user-branch]'; return '[user-branch]';
} }
} }
...@@ -388,7 +388,7 @@ class FlutterVersion { ...@@ -388,7 +388,7 @@ class FlutterVersion {
/// writes shared cache files. /// writes shared cache files.
Future<void> checkFlutterVersionFreshness() async { Future<void> checkFlutterVersionFreshness() async {
// Don't perform update checks if we're not on an official channel. // Don't perform update checks if we're not on an official channel.
if (!officialChannels.contains(channel)) { if (!kOfficialChannels.contains(channel)) {
return; return;
} }
......
...@@ -86,7 +86,7 @@ void main() { ...@@ -86,7 +86,7 @@ void main() {
final Iterable<String> rows = testLogger.statusText final Iterable<String> rows = testLogger.statusText
.split('\n') .split('\n')
.map((String line) => line.substring(2)); // remove '* ' or ' ' from output .map((String line) => line.substring(2)); // remove '* ' or ' ' from output
expect(rows, containsAllInOrder(FlutterVersion.officialChannels)); expect(rows, containsAllInOrder(kOfficialChannels));
// clear buffer for next process // clear buffer for next process
testLogger.clear(); testLogger.clear();
...@@ -107,7 +107,7 @@ void main() { ...@@ -107,7 +107,7 @@ void main() {
final Iterable<String> rows2 = testLogger.statusText final Iterable<String> rows2 = testLogger.statusText
.split('\n') .split('\n')
.map((String line) => line.substring(2)); // remove '* ' or ' ' from output .map((String line) => line.substring(2)); // remove '* ' or ' ' from output
expect(rows2, containsAllInOrder(FlutterVersion.officialChannels)); expect(rows2, containsAllInOrder(kOfficialChannels));
// clear buffer for next process // clear buffer for next process
testLogger.clear(); testLogger.clear();
...@@ -127,7 +127,7 @@ void main() { ...@@ -127,7 +127,7 @@ void main() {
// check if available official channels are in order of stability // check if available official channels are in order of stability
int prev = -1; int prev = -1;
int next = -1; int next = -1;
for (final String branch in FlutterVersion.officialChannels) { for (final String branch in kOfficialChannels) {
next = testLogger.statusText.indexOf(branch); next = testLogger.statusText.indexOf(branch);
if (next != -1) { if (next != -1) {
expect(prev < next, isTrue); expect(prev < next, isTrue);
......
...@@ -36,7 +36,15 @@ void main() { ...@@ -36,7 +36,15 @@ void main() {
mockCache = MockCache(); mockCache = MockCache();
}); });
for (final String channel in FlutterVersion.officialChannels) { testUsingContext('Channel enum and string transform to each other', () {
for (final Channel channel in Channel.values) {
expect(getNameForChannel(channel), kOfficialChannels.toList()[channel.index]);
}
expect(kOfficialChannels.toList().map((String str) => getChannelForName(str)).toList(),
Channel.values);
});
for (final String channel in kOfficialChannels) {
DateTime getChannelUpToDateVersion() { DateTime getChannelUpToDateVersion() {
return _testClock.ago(FlutterVersion.versionAgeConsideredUpToDate(channel) ~/ 2); return _testClock.ago(FlutterVersion.versionAgeConsideredUpToDate(channel) ~/ 2);
} }
......
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