Skip to content

Commit 0116b2e

Browse files
[flutter_tools] fix RangeError in flutter channel command (#103766)
1 parent 2ad7ced commit 0116b2e

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

packages/flutter_tools/lib/src/commands/channel.dart

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ class ChannelCommand extends FlutterCommand {
7979

8080
for (final String line in rawOutput) {
8181
final List<String> split = line.split('/');
82+
if (split.length != 2) {
83+
// We don't know how to parse this line, skip it.
84+
continue;
85+
}
8286
final String branch = split[1];
8387
if (split.length > 1) {
8488
final int index = officialChannels.indexOf(branch);

packages/flutter_tools/test/general.shard/channel_test.dart

+32
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,38 @@ void main() {
136136
FileSystem: () => MemoryFileSystem.test(),
137137
});
138138

139+
testUsingContext('ignores lines with unexpected output', () async {
140+
fakeProcessManager.addCommand(
141+
const FakeCommand(
142+
command: <String>['git', 'branch', '-r'],
143+
stdout: 'origin/beta\n'
144+
'origin/stable\n'
145+
'upstream/beta\n'
146+
'upstream/stable\n'
147+
'foo',
148+
),
149+
);
150+
151+
final ChannelCommand command = ChannelCommand();
152+
final CommandRunner<void> runner = createTestCommandRunner(command);
153+
await runner.run(<String>['channel']);
154+
155+
expect(fakeProcessManager.hasRemainingExpectations, isFalse);
156+
expect(testLogger.errorText, hasLength(0));
157+
158+
// format the status text for a simpler assertion.
159+
final Iterable<String> rows = testLogger.statusText
160+
.split('\n')
161+
.map((String line) => line.trim())
162+
.where((String line) => line?.isNotEmpty == true)
163+
.skip(1); // remove `Flutter channels:` line
164+
165+
expect(rows, <String>['beta', 'stable', 'Currently not on an official channel.']);
166+
}, overrides: <Type, Generator>{
167+
ProcessManager: () => fakeProcessManager,
168+
FileSystem: () => MemoryFileSystem.test(),
169+
});
170+
139171
testUsingContext('removes duplicates', () async {
140172
fakeProcessManager.addCommand(
141173
const FakeCommand(

0 commit comments

Comments
 (0)