Unverified Commit d682259e authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Use a set to remove duplicate channel names (#15392)

* Use a set to remove duplicate channel names

* Add test for git command
parent 2f8474f9
......@@ -47,6 +47,7 @@ class ChannelCommand extends FlutterCommand {
// Beware: currentBranch could contain PII. See getBranchName().
final String currentChannel = FlutterVersion.instance.channel;
final String currentBranch = FlutterVersion.instance.getBranchName();
final Set<String> seenChannels = new Set<String>();
showAll = showAll || currentChannel != currentBranch;
......@@ -59,6 +60,10 @@ class ChannelCommand extends FlutterCommand {
if (split.length < 2)
return null;
final String branchName = split[1];
if (seenChannels.contains(branchName)) {
return null;
}
seenChannels.add(branchName);
if (branchName == currentBranch)
return '* $branchName';
if (!branchName.startsWith('HEAD ') &&
......
......@@ -2,16 +2,24 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/channel.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';
import 'package:process/process.dart';
import 'src/common.dart';
import 'src/context.dart';
void main() {
group('channel', () {
final MockProcessManager mockProcessManager = new MockProcessManager();
setUpAll(() {
Cache.disableLocking();
});
......@@ -26,5 +34,51 @@ void main() {
// so we check for the header text rather than any specific channel name.
expect(testLogger.statusText, contains('Flutter channels:'));
});
testUsingContext('removes duplicates', () async {
final Stream<List<int>> stdout = new Stream<List<int>>.fromIterable(<List<int>>[
utf8.encode(
'origin/dev\n'
'origin/beta\n'
'upstream/dev\n'
'upstream/beta\n'
),
]);
final Process process = new MockProcess();
when(process.stdout).thenReturn(stdout);
when(process.stderr).thenReturn(const Stream<List<int>>.empty());
when(process.exitCode).thenReturn(new Future<int>.value(0));
when(mockProcessManager.start(
<String>['git', 'branch', '-r'],
workingDirectory: typed(any, named: 'workingDirectory'),
environment: typed(any, named: 'environment')))
.thenReturn(new Future<Process>.value(process));
final ChannelCommand command = new ChannelCommand();
final CommandRunner<Null> runner = createTestCommandRunner(command);
await runner.run(<String>['channel']);
verify(mockProcessManager.start(<String>['git', 'branch', '-r'],
workingDirectory: typed(any, named: 'workingDirectory'),
environment: typed(any, named: 'environment'))).called(1);
expect(testLogger.errorText, hasLength(0));
// format the status text for a simpler assertion.
final Iterable<String> rows = testLogger.statusText
.split('\n')
.map((String line) => line.trim())
.where((String line) => line?.isNotEmpty == true)
.skip(1); // remove `Flutter channels:` line
expect(rows, <String>['dev', 'beta']);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
});
});
}
class MockProcessManager extends Mock implements ProcessManager {}
class MockProcess extends Mock implements Process {}
\ No newline at end of file
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