Fix local test failures in flutter_tools (#42015)
* Minor clean-up in terminal.dart
* Don't wrap text in tests (otherwise we'll be sensitive to the terminal width).
diff --git a/packages/flutter_tools/lib/src/base/terminal.dart b/packages/flutter_tools/lib/src/base/terminal.dart
index ed2e0ac..eb9bfdb 100644
--- a/packages/flutter_tools/lib/src/base/terminal.dart
+++ b/packages/flutter_tools/lib/src/base/terminal.dart
@@ -9,15 +9,6 @@
import 'context.dart';
import 'io.dart' as io;
import 'platform.dart';
-import 'utils.dart';
-
-final AnsiTerminal _kAnsiTerminal = AnsiTerminal();
-
-AnsiTerminal get terminal {
- return (context == null || context.get<AnsiTerminal>() == null)
- ? _kAnsiTerminal
- : context.get<AnsiTerminal>();
-}
enum TerminalColor {
red,
@@ -29,11 +20,15 @@
grey,
}
-final OutputPreferences _kOutputPreferences = OutputPreferences();
+AnsiTerminal get terminal {
+ return context?.get<AnsiTerminal>() ?? _defaultAnsiTerminal;
+}
+final AnsiTerminal _defaultAnsiTerminal = AnsiTerminal();
-OutputPreferences get outputPreferences => (context == null || context.get<OutputPreferences>() == null)
- ? _kOutputPreferences
- : context.get<OutputPreferences>();
+OutputPreferences get outputPreferences {
+ return context?.get<OutputPreferences>() ?? _defaultOutputPreferences;
+}
+final OutputPreferences _defaultOutputPreferences = OutputPreferences();
/// A class that contains the context settings for command text output to the
/// console.
@@ -42,31 +37,32 @@
bool wrapText,
int wrapColumn,
bool showColor,
- }) : wrapText = wrapText ?? io.stdio?.hasTerminal ?? const io.Stdio().hasTerminal,
+ }) : wrapText = wrapText ?? io.stdio.hasTerminal,
_overrideWrapColumn = wrapColumn,
showColor = showColor ?? platform.stdoutSupportsAnsi ?? false;
+ /// A version of this class for use in tests.
+ OutputPreferences.test() : wrapText = false, _overrideWrapColumn = null, showColor = false;
+
/// If [wrapText] is true, then any text sent to the context's [Logger]
/// instance (e.g. from the [printError] or [printStatus] functions) will be
/// wrapped (newlines added between words) to be no longer than the
/// [wrapColumn] specifies. Defaults to true if there is a terminal. To
/// determine if there's a terminal, [OutputPreferences] asks the context's
- /// stdio to see, and if that's not set, it tries creating a new [io.Stdio]
- /// and asks it if there is a terminal.
+ /// stdio.
final bool wrapText;
+ /// The terminal width used by the [wrapText] function if there is no terminal
+ /// attached to [io.Stdio], --wrap is on, and --wrap-columns was not specified.
+ static const int kDefaultTerminalColumns = 100;
+
/// The column at which output sent to the context's [Logger] instance
/// (e.g. from the [printError] or [printStatus] functions) will be wrapped.
/// Ignored if [wrapText] is false. Defaults to the width of the output
/// terminal, or to [kDefaultTerminalColumns] if not writing to a terminal.
- /// To find out if we're writing to a terminal, it tries the context's stdio,
- /// and if that's not set, it tries creating a new [io.Stdio] and asks it, if
- /// that doesn't have an idea of the terminal width, then we just use a
- /// default of 100. It will be ignored if [wrapText] is false.
final int _overrideWrapColumn;
int get wrapColumn {
- return _overrideWrapColumn ?? io.stdio?.terminalColumns
- ?? const io.Stdio().terminalColumns ?? kDefaultTerminalColumns;
+ return _overrideWrapColumn ?? io.stdio.terminalColumns ?? kDefaultTerminalColumns;
}
/// Whether or not to output ANSI color codes when writing to the output
diff --git a/packages/flutter_tools/lib/src/base/utils.dart b/packages/flutter_tools/lib/src/base/utils.dart
index 382060d..08db07c 100644
--- a/packages/flutter_tools/lib/src/base/utils.dart
+++ b/packages/flutter_tools/lib/src/base/utils.dart
@@ -307,9 +307,6 @@
Future<List<T>> waitGroup<T>(Iterable<Future<T>> futures) {
return Future.wait<T>(futures.where((Future<T> future) => future != null));
}
-/// The terminal width used by the [wrapText] function if there is no terminal
-/// attached to [io.Stdio], --wrap is on, and --wrap-columns was not specified.
-const int kDefaultTerminalColumns = 100;
/// Smallest column that will be used for text wrapping. If the requested column
/// width is smaller than this, then this is what will be used.
diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart
index c3519b1..0ecd81e 100644
--- a/packages/flutter_tools/test/src/context.dart
+++ b/packages/flutter_tools/test/src/context.dart
@@ -82,7 +82,7 @@
when(mock.getAttachedDevices()).thenAnswer((Invocation _) async => <IOSSimulator>[]);
return mock;
},
- OutputPreferences: () => OutputPreferences(showColor: false),
+ OutputPreferences: () => OutputPreferences.test(),
Logger: () => BufferLogger(),
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
SimControl: () => MockSimControl(),
diff --git a/packages/flutter_tools/test/src/testbed.dart b/packages/flutter_tools/test/src/testbed.dart
index 4c2fae1..b331604 100644
--- a/packages/flutter_tools/test/src/testbed.dart
+++ b/packages/flutter_tools/test/src/testbed.dart
@@ -33,7 +33,7 @@
FileSystem: () => MemoryFileSystem(style: platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix),
Logger: () => BufferLogger(), // Allows reading logs and prevents stdout.
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
- OutputPreferences: () => OutputPreferences(showColor: false), // configures BufferLogger to avoid color codes.
+ OutputPreferences: () => OutputPreferences.test(), // configures BufferLogger to avoid color codes.
Usage: () => NoOpUsage(), // prevent addition of analytics from burdening test mocks
FlutterVersion: () => FakeFlutterVersion(), // prevent requirement to mock git for test runner.
Signals: () => FakeSignals(), // prevent registering actual signal handlers.