From 0116b2e77dc054a9fa021d75e481606534996660 Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Tue, 17 May 2022 16:22:12 -0700 Subject: [PATCH] [flutter_tools] fix RangeError in flutter channel command (#103766) --- .../lib/src/commands/channel.dart | 4 +++ .../test/general.shard/channel_test.dart | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/packages/flutter_tools/lib/src/commands/channel.dart b/packages/flutter_tools/lib/src/commands/channel.dart index 82f8edbf6d..9f1780cd29 100644 --- a/packages/flutter_tools/lib/src/commands/channel.dart +++ b/packages/flutter_tools/lib/src/commands/channel.dart @@ -79,6 +79,10 @@ class ChannelCommand extends FlutterCommand { for (final String line in rawOutput) { final List split = line.split('/'); + if (split.length != 2) { + // We don't know how to parse this line, skip it. + continue; + } final String branch = split[1]; if (split.length > 1) { final int index = officialChannels.indexOf(branch); diff --git a/packages/flutter_tools/test/general.shard/channel_test.dart b/packages/flutter_tools/test/general.shard/channel_test.dart index c075791061..ca26708cbb 100644 --- a/packages/flutter_tools/test/general.shard/channel_test.dart +++ b/packages/flutter_tools/test/general.shard/channel_test.dart @@ -136,6 +136,38 @@ void main() { FileSystem: () => MemoryFileSystem.test(), }); + testUsingContext('ignores lines with unexpected output', () async { + fakeProcessManager.addCommand( + const FakeCommand( + command: ['git', 'branch', '-r'], + stdout: 'origin/beta\n' + 'origin/stable\n' + 'upstream/beta\n' + 'upstream/stable\n' + 'foo', + ), + ); + + final ChannelCommand command = ChannelCommand(); + final CommandRunner runner = createTestCommandRunner(command); + await runner.run(['channel']); + + expect(fakeProcessManager.hasRemainingExpectations, isFalse); + expect(testLogger.errorText, hasLength(0)); + + // format the status text for a simpler assertion. + final Iterable rows = testLogger.statusText + .split('\n') + .map((String line) => line.trim()) + .where((String line) => line?.isNotEmpty == true) + .skip(1); // remove `Flutter channels:` line + + expect(rows, ['beta', 'stable', 'Currently not on an official channel.']); + }, overrides: { + ProcessManager: () => fakeProcessManager, + FileSystem: () => MemoryFileSystem.test(), + }); + testUsingContext('removes duplicates', () async { fakeProcessManager.addCommand( const FakeCommand(