diff --git a/packages/mason_logger/lib/src/mason_logger.dart b/packages/mason_logger/lib/src/mason_logger.dart index e489e91b9..70d1ccbef 100644 --- a/packages/mason_logger/lib/src/mason_logger.dart +++ b/packages/mason_logger/lib/src/mason_logger.dart @@ -3,7 +3,6 @@ import 'dart:convert'; import 'dart:io' as io; import 'package:mason_logger/mason_logger.dart'; -import 'package:mason_logger/src/stdio_overrides.dart'; part 'progress.dart'; diff --git a/packages/mason_logger/lib/src/progress.dart b/packages/mason_logger/lib/src/progress.dart index 1af5309ab..94947f512 100644 --- a/packages/mason_logger/lib/src/progress.dart +++ b/packages/mason_logger/lib/src/progress.dart @@ -52,11 +52,9 @@ class Progress { ..reset() ..start(); - final stdioType = StdioOverrides.current?.stdioType ?? io.stdioType; - // The animation is only shown when it would be meaningful. // Do not animate if the stdio type is not a terminal. - if (stdioType(_stdout) != io.StdioType.terminal) { + if (!_stdout.hasTerminal) { final frames = _options.animation.frames; final char = frames.isEmpty ? '' : frames.first; final prefix = char.isEmpty ? char : '${lightGreen.wrap(char)} '; diff --git a/packages/mason_logger/lib/src/stdio_overrides.dart b/packages/mason_logger/lib/src/stdio_overrides.dart deleted file mode 100644 index c9e90f3c4..000000000 --- a/packages/mason_logger/lib/src/stdio_overrides.dart +++ /dev/null @@ -1,49 +0,0 @@ -import 'dart:async'; -import 'dart:io' as io; - -const _asyncRunZoned = runZoned; - -/// This class facilitates overriding [io.stdioType]. -abstract class StdioOverrides { - static final _token = Object(); - - /// Returns the current [StdioOverrides] instance. - /// - /// This will return `null` if the current [Zone] does not contain - /// any [StdioOverrides]. - /// - /// See also: - /// * [StdioOverrides.runZoned] to provide [StdioOverrides] - /// in a fresh [Zone]. - /// - static StdioOverrides? get current { - return Zone.current[_token] as StdioOverrides?; - } - - /// Runs [body] in a fresh [Zone] using the provided overrides. - static R runZoned( - R Function() body, { - io.StdioType Function(dynamic object) Function()? stdioType, - }) { - final overrides = _StdioOverridesScope(stdioType); - return _asyncRunZoned( - body, - zoneValues: {_token: overrides}, - ); - } - - /// The [io.stdioType] that will be used for errors within the current [Zone]. - io.StdioType Function(dynamic object) get stdioType => io.stdioType; -} - -class _StdioOverridesScope extends StdioOverrides { - _StdioOverridesScope(this._stdioType); - - final StdioOverrides? _previous = StdioOverrides.current; - final io.StdioType Function(dynamic object) Function()? _stdioType; - - @override - io.StdioType Function(dynamic object) get stdioType { - return _stdioType?.call() ?? _previous?.stdioType ?? super.stdioType; - } -} diff --git a/packages/mason_logger/test/src/mason_logger_test.dart b/packages/mason_logger/test/src/mason_logger_test.dart index 155a688b2..423d4a263 100644 --- a/packages/mason_logger/test/src/mason_logger_test.dart +++ b/packages/mason_logger/test/src/mason_logger_test.dart @@ -503,6 +503,7 @@ void main() { group('.progress', () { test('writes lines to stdout', () async { + when(() => stdout.hasTerminal).thenReturn(true); await IOOverrides.runZoned( () async { const time = '(0.Xs)'; diff --git a/packages/mason_logger/test/src/progress_test.dart b/packages/mason_logger/test/src/progress_test.dart index 236012d9c..1fbbd3885 100644 --- a/packages/mason_logger/test/src/progress_test.dart +++ b/packages/mason_logger/test/src/progress_test.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'dart:io'; import 'package:mason_logger/mason_logger.dart'; -import 'package:mason_logger/src/stdio_overrides.dart'; import 'package:mocktail/mocktail.dart'; import 'package:test/test.dart'; @@ -13,12 +12,11 @@ class MockStdin extends Mock implements Stdin {} void main() { group('Progress', () { late Stdout stdout; - late StdioType Function(dynamic) stdioType; setUp(() { stdout = MockStdout(); when(() => stdout.supportsAnsiEscapes).thenReturn(true); - stdioType = (dynamic _) => StdioType.terminal; + when(() => stdout.hasTerminal).thenReturn(true); }); test('writes ms when elapsed time is less than 0.1s', () async { @@ -38,6 +36,7 @@ void main() { }); test('writes static message when stdioType is not terminal', () async { + when(() => stdout.hasTerminal).thenReturn(false); await _runZoned( () async { const message = 'test message'; @@ -54,7 +53,6 @@ void main() { ]); }, stdout: () => stdout, - stdioType: () => (dynamic _) => StdioType.other, zoneValues: {AnsiCode: true}, ); }); @@ -95,7 +93,6 @@ void main() { }, stdout: () => stdout, stdin: () => stdin, - stdioType: () => stdioType, ); }); @@ -135,7 +132,6 @@ void main() { }, stdout: () => stdout, stdin: () => stdin, - stdioType: () => stdioType, ); }); @@ -170,7 +166,6 @@ void main() { ).called(1); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -185,7 +180,6 @@ void main() { verifyNever(() => stdout.write(any())); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -231,7 +225,6 @@ void main() { ).called(1); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -286,7 +279,6 @@ void main() { ).called(1); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -301,7 +293,6 @@ void main() { verifyNever(() => stdout.write(any())); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -344,7 +335,6 @@ void main() { ).called(1); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -359,7 +349,6 @@ void main() { verifyNever(() => stdout.write(any())); }, stdout: () => stdout, - stdioType: () => stdioType, zoneValues: {AnsiCode: true}, ); }); @@ -370,19 +359,11 @@ void main() { T _runZoned( T Function() body, { Map? zoneValues, - StdioType Function(dynamic) Function()? stdioType, Stdin Function()? stdin, Stdout Function()? stdout, }) { return runZoned( - () { - return StdioOverrides.runZoned( - () { - return IOOverrides.runZoned(body, stdout: stdout, stdin: stdin); - }, - stdioType: stdioType, - ); - }, + () => IOOverrides.runZoned(body, stdout: stdout, stdin: stdin), zoneValues: zoneValues, ); } diff --git a/packages/mason_logger/test/src/stdio_overrides_test.dart b/packages/mason_logger/test/src/stdio_overrides_test.dart deleted file mode 100644 index 2decb03f7..000000000 --- a/packages/mason_logger/test/src/stdio_overrides_test.dart +++ /dev/null @@ -1,69 +0,0 @@ -import 'dart:io'; - -import 'package:mason_logger/src/stdio_overrides.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -class FakeStdout extends Fake implements Stdout {} - -class FakeStdin extends Fake implements Stdin {} - -void main() { - group('StdioOverrides', () { - group('runZoned', () { - test('uses default stdioType when not specified', () { - StdioOverrides.runZoned(() { - final overrides = StdioOverrides.current; - expect(overrides!.stdioType, equals(stdioType)); - }); - }); - - test('uses custom stdioType when specified', () { - StdioType customStdioType(dynamic _) => StdioType.pipe; - StdioOverrides.runZoned( - () { - final overrides = StdioOverrides.current; - expect(overrides!.stdioType, equals(customStdioType)); - }, - stdioType: () => customStdioType, - ); - }); - - test( - 'uses current stdioType when not specified ' - 'and zone already contains a stdioType', () { - StdioType customStdioType(dynamic _) => StdioType.pipe; - StdioOverrides.runZoned( - () { - StdioOverrides.runZoned(() { - final overrides = StdioOverrides.current; - expect(overrides!.stdioType, equals(customStdioType)); - }); - }, - stdioType: () => customStdioType, - ); - }); - - test( - 'uses nested stdioType when specified ' - 'and zone already contains a stdioType', () { - StdioType rootStdioType(dynamic _) => StdioType.pipe; - StdioOverrides.runZoned( - () { - StdioType nestedStdioType(dynamic _) => StdioType.other; - final overrides = StdioOverrides.current; - expect(overrides!.stdioType, equals(rootStdioType)); - StdioOverrides.runZoned( - () { - final overrides = StdioOverrides.current; - expect(overrides!.stdioType, equals(nestedStdioType)); - }, - stdioType: () => nestedStdioType, - ); - }, - stdioType: () => rootStdioType, - ); - }); - }); - }); -}