mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
[flutter_tools] catch StdinException when setting terminal to SingleCharMode (#136283)
Fixes https://github.com/flutter/flutter/issues/129198
This commit is contained in:
parent
54cf286e1a
commit
b416473bbb
@ -314,13 +314,19 @@ class AnsiTerminal implements Terminal {
|
||||
return;
|
||||
}
|
||||
final io.Stdin stdin = _stdio.stdin as io.Stdin;
|
||||
// The order of setting lineMode and echoMode is important on Windows.
|
||||
if (value) {
|
||||
stdin.echoMode = false;
|
||||
stdin.lineMode = false;
|
||||
} else {
|
||||
stdin.lineMode = true;
|
||||
stdin.echoMode = true;
|
||||
|
||||
try {
|
||||
// The order of setting lineMode and echoMode is important on Windows.
|
||||
if (value) {
|
||||
stdin.echoMode = false;
|
||||
stdin.lineMode = false;
|
||||
} 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';
|
||||
import 'package:test/fake.dart';
|
||||
|
||||
import '../../src/common.dart';
|
||||
import '../../src/fakes.dart';
|
||||
|
||||
void main() {
|
||||
group('output preferences', () {
|
||||
@ -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, 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;
|
||||
@ -269,14 +280,36 @@ class TestTerminal extends AnsiTerminal {
|
||||
return mockStdInStream;
|
||||
}
|
||||
|
||||
bool _singleCharMode = false;
|
||||
|
||||
@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
|
||||
int get preferredStyle => 0;
|
||||
}
|
||||
|
||||
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
|
||||
bool stdinHasTerminal = false;
|
||||
}
|
||||
|
@ -253,8 +253,20 @@ class FakeStdio extends Stdio {
|
||||
class FakeStdin extends Fake implements Stdin {
|
||||
final StreamController<List<int>> controller = StreamController<List<int>>();
|
||||
|
||||
void Function(bool mode)? echoModeCallback;
|
||||
|
||||
bool _echoMode = true;
|
||||
|
||||
@override
|
||||
bool echoMode = true;
|
||||
bool get echoMode => _echoMode;
|
||||
|
||||
@override
|
||||
set echoMode(bool mode) {
|
||||
_echoMode = mode;
|
||||
if (echoModeCallback != null) {
|
||||
echoModeCallback!(mode);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool lineMode = true;
|
||||
|
Loading…
Reference in New Issue
Block a user