Unverified Commit b416473b authored by Christopher Fujino's avatar Christopher Fujino Committed by GitHub

[flutter_tools] catch StdinException when setting terminal to SingleCharMode (#136283)

Fixes https://github.com/flutter/flutter/issues/129198
parent 54cf286e
...@@ -314,13 +314,19 @@ class AnsiTerminal implements Terminal { ...@@ -314,13 +314,19 @@ class AnsiTerminal implements Terminal {
return; return;
} }
final io.Stdin stdin = _stdio.stdin as io.Stdin; final io.Stdin stdin = _stdio.stdin as io.Stdin;
// The order of setting lineMode and echoMode is important on Windows.
if (value) { try {
stdin.echoMode = false; // The order of setting lineMode and echoMode is important on Windows.
stdin.lineMode = false; if (value) {
} else { stdin.echoMode = false;
stdin.lineMode = true; stdin.lineMode = false;
stdin.echoMode = true; } else {
stdin.lineMode = true;
stdin.echoMode = true;
}
} on io.StdinException {
// If the pipe to STDIN has been closed it's probably because the
// terminal has been closed, and there is nothing actionable to do here.
} }
} }
......
...@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/base/terminal.dart'; ...@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/base/terminal.dart';
import 'package:test/fake.dart'; import 'package:test/fake.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/fakes.dart';
void main() { void main() {
group('output preferences', () { group('output preferences', () {
...@@ -253,6 +254,16 @@ void main() { ...@@ -253,6 +254,16 @@ void main() {
expect(AnsiTerminal(stdio: stdio, platform: const LocalPlatform(), now: DateTime(2018, 1, 10, 23)).preferredStyle, 2); expect(AnsiTerminal(stdio: stdio, platform: const LocalPlatform(), now: DateTime(2018, 1, 10, 23)).preferredStyle, 2);
expect(AnsiTerminal(stdio: stdio, platform: const LocalPlatform(), now: DateTime(2018, 1, 11, 23)).preferredStyle, 3); expect(AnsiTerminal(stdio: stdio, platform: const LocalPlatform(), now: DateTime(2018, 1, 11, 23)).preferredStyle, 3);
}); });
testWithoutContext('set singleCharMode resilient to StdinException', () async {
final FakeStdio stdio = FakeStdio();
final AnsiTerminal terminal = AnsiTerminal(stdio: stdio, platform: const LocalPlatform());
stdio.stdinHasTerminal = true;
stdio._stdin = FakeStdin()..echoModeCallback = (bool _) => throw const StdinException(
'Error setting terminal echo mode, OS Error: The handle is invalid.',
);
terminal.singleCharMode = true;
});
} }
late Stream<String> mockStdInStream; late Stream<String> mockStdInStream;
...@@ -269,14 +280,36 @@ class TestTerminal extends AnsiTerminal { ...@@ -269,14 +280,36 @@ class TestTerminal extends AnsiTerminal {
return mockStdInStream; return mockStdInStream;
} }
bool _singleCharMode = false;
@override @override
bool singleCharMode = false; bool get singleCharMode => _singleCharMode;
void Function(bool newMode)? _singleCharModeCallback;
@override
set singleCharMode(bool newMode) {
_singleCharMode = newMode;
if (_singleCharModeCallback != null) {
_singleCharModeCallback!(newMode);
}
}
@override @override
int get preferredStyle => 0; int get preferredStyle => 0;
} }
class FakeStdio extends Fake implements Stdio { class FakeStdio extends Fake implements Stdio {
Stream<List<int>>? _stdin;
@override
Stream<List<int>> get stdin {
if (_stdin != null) {
return _stdin!;
}
throw UnimplementedError('stdin');
}
@override @override
bool stdinHasTerminal = false; bool stdinHasTerminal = false;
} }
...@@ -253,8 +253,20 @@ class FakeStdio extends Stdio { ...@@ -253,8 +253,20 @@ class FakeStdio extends Stdio {
class FakeStdin extends Fake implements Stdin { class FakeStdin extends Fake implements Stdin {
final StreamController<List<int>> controller = StreamController<List<int>>(); final StreamController<List<int>> controller = StreamController<List<int>>();
void Function(bool mode)? echoModeCallback;
bool _echoMode = true;
@override
bool get echoMode => _echoMode;
@override @override
bool echoMode = true; set echoMode(bool mode) {
_echoMode = mode;
if (echoModeCallback != null) {
echoModeCallback!(mode);
}
}
@override @override
bool lineMode = true; bool lineMode = true;
......
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