Unverified Commit 07161e82 authored by Zachary Anderson's avatar Zachary Anderson Committed by GitHub

[flutter_tool] Don't use context in ProcessUtils (#48444)

parent 4e6d649f
...@@ -255,7 +255,7 @@ Future<int> _exit(int code) async { ...@@ -255,7 +255,7 @@ Future<int> _exit(int code) async {
} }
// Run shutdown hooks before flushing logs // Run shutdown hooks before flushing logs
await runShutdownHooks(); await shutdownHooks.runShutdownHooks();
final Completer<void> completer = Completer<void>(); final Completer<void> completer = Completer<void>();
......
...@@ -907,7 +907,7 @@ Directory _getLocalEngineRepo({ ...@@ -907,7 +907,7 @@ Directory _getLocalEngineRepo({
.createTempSync('flutter_tool_local_engine_repo.'); .createTempSync('flutter_tool_local_engine_repo.');
// Remove the local engine repo before the tool exits. // Remove the local engine repo before the tool exits.
addShutdownHook( shutdownHooks.addShutdownHook(
() => localEngineRepo.deleteSync(recursive: true), () => localEngineRepo.deleteSync(recursive: true),
ShutdownStage.CLEANUP, ShutdownStage.CLEANUP,
); );
......
...@@ -288,7 +288,7 @@ abstract class IOSApp extends ApplicationPackage { ...@@ -288,7 +288,7 @@ abstract class IOSApp extends ApplicationPackage {
} else { } else {
// Try to unpack as an ipa. // Try to unpack as an ipa.
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_app.'); final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_app.');
addShutdownHook(() async { shutdownHooks.addShutdownHook(() async {
await tempDir.delete(recursive: true); await tempDir.delete(recursive: true);
}, ShutdownStage.STILL_RECORDING); }, ShutdownStage.STILL_RECORDING);
os.unzip(globals.fs.file(applicationBinary), tempDir); os.unzip(globals.fs.file(applicationBinary), tempDir);
......
...@@ -4,11 +4,14 @@ ...@@ -4,11 +4,14 @@
import 'dart:async'; import 'dart:async';
import 'package:meta/meta.dart';
import 'package:process/process.dart';
import '../convert.dart'; import '../convert.dart';
import '../globals.dart' as globals;
import 'common.dart'; import 'common.dart';
import 'context.dart'; import 'context.dart';
import 'io.dart'; import 'io.dart';
import 'logger.dart';
import 'utils.dart'; import 'utils.dart';
typedef StringConverter = String Function(String string); typedef StringConverter = String Function(String string);
...@@ -51,50 +54,80 @@ class ShutdownStage implements Comparable<ShutdownStage> { ...@@ -51,50 +54,80 @@ class ShutdownStage implements Comparable<ShutdownStage> {
int compareTo(ShutdownStage other) => priority.compareTo(other.priority); int compareTo(ShutdownStage other) => priority.compareTo(other.priority);
} }
Map<ShutdownStage, List<ShutdownHook>> _shutdownHooks = <ShutdownStage, List<ShutdownHook>>{}; ShutdownHooks get shutdownHooks => ShutdownHooks.instance;
bool _shutdownHooksRunning = false;
abstract class ShutdownHooks {
/// Registers a [ShutdownHook] to be executed before the VM exits. factory ShutdownHooks({
/// @required Logger logger,
/// If [stage] is specified, the shutdown hook will be run during the specified }) => _DefaultShutdownHooks(
/// stage. By default, the shutdown hook will be run during the logger: logger,
/// [ShutdownStage.CLEANUP] stage. );
void addShutdownHook(
ShutdownHook shutdownHook, [ static ShutdownHooks get instance => context.get<ShutdownHooks>();
ShutdownStage stage = ShutdownStage.CLEANUP,
]) { /// Registers a [ShutdownHook] to be executed before the VM exits.
assert(!_shutdownHooksRunning); ///
_shutdownHooks.putIfAbsent(stage, () => <ShutdownHook>[]).add(shutdownHook); /// If [stage] is specified, the shutdown hook will be run during the specified
/// stage. By default, the shutdown hook will be run during the
/// [ShutdownStage.CLEANUP] stage.
void addShutdownHook(
ShutdownHook shutdownHook, [
ShutdownStage stage = ShutdownStage.CLEANUP,
]);
/// Runs all registered shutdown hooks and returns a future that completes when
/// all such hooks have finished.
///
/// Shutdown hooks will be run in groups by their [ShutdownStage]. All shutdown
/// hooks within a given stage will be started in parallel and will be
/// guaranteed to run to completion before shutdown hooks in the next stage are
/// started.
Future<void> runShutdownHooks();
} }
/// Runs all registered shutdown hooks and returns a future that completes when class _DefaultShutdownHooks implements ShutdownHooks {
/// all such hooks have finished. _DefaultShutdownHooks({
/// @required Logger logger,
/// Shutdown hooks will be run in groups by their [ShutdownStage]. All shutdown }) : _logger = logger;
/// hooks within a given stage will be started in parallel and will be
/// guaranteed to run to completion before shutdown hooks in the next stage are final Logger _logger;
/// started.
Future<void> runShutdownHooks() async { final Map<ShutdownStage, List<ShutdownHook>> _shutdownHooks = <ShutdownStage, List<ShutdownHook>>{};
globals.printTrace('Running shutdown hooks');
_shutdownHooksRunning = true; bool _shutdownHooksRunning = false;
try {
for (final ShutdownStage stage in _shutdownHooks.keys.toList()..sort()) { @override
globals.printTrace('Shutdown hook priority ${stage.priority}'); void addShutdownHook(
final List<ShutdownHook> hooks = _shutdownHooks.remove(stage); ShutdownHook shutdownHook, [
final List<Future<dynamic>> futures = <Future<dynamic>>[]; ShutdownStage stage = ShutdownStage.CLEANUP,
for (final ShutdownHook shutdownHook in hooks) { ]) {
final FutureOr<dynamic> result = shutdownHook(); assert(!_shutdownHooksRunning);
if (result is Future<dynamic>) { _shutdownHooks.putIfAbsent(stage, () => <ShutdownHook>[]).add(shutdownHook);
futures.add(result); }
@override
Future<void> runShutdownHooks() async {
_logger.printTrace('Running shutdown hooks');
_shutdownHooksRunning = true;
try {
for (final ShutdownStage stage in _shutdownHooks.keys.toList()..sort()) {
_logger.printTrace('Shutdown hook priority ${stage.priority}');
final List<ShutdownHook> hooks = _shutdownHooks.remove(stage);
final List<Future<dynamic>> futures = <Future<dynamic>>[];
for (final ShutdownHook shutdownHook in hooks) {
final FutureOr<dynamic> result = shutdownHook();
if (result is Future<dynamic>) {
futures.add(result);
}
} }
await Future.wait<dynamic>(futures);
} }
await Future.wait<dynamic>(futures); } finally {
_shutdownHooksRunning = false;
} }
} finally { assert(_shutdownHooks.isEmpty);
_shutdownHooksRunning = false; _logger.printTrace('Shutdown hooks complete');
} }
assert(_shutdownHooks.isEmpty);
globals.printTrace('Shutdown hooks complete');
} }
class ProcessExit implements Exception { class ProcessExit implements Exception {
...@@ -150,7 +183,13 @@ typedef RunResultChecker = bool Function(int); ...@@ -150,7 +183,13 @@ typedef RunResultChecker = bool Function(int);
ProcessUtils get processUtils => ProcessUtils.instance; ProcessUtils get processUtils => ProcessUtils.instance;
abstract class ProcessUtils { abstract class ProcessUtils {
factory ProcessUtils() => _DefaultProcessUtils(); factory ProcessUtils({
@required ProcessManager processManager,
@required Logger logger,
}) => _DefaultProcessUtils(
processManager: processManager,
logger: logger,
);
static ProcessUtils get instance => context.get<ProcessUtils>(); static ProcessUtils get instance => context.get<ProcessUtils>();
...@@ -239,6 +278,16 @@ abstract class ProcessUtils { ...@@ -239,6 +278,16 @@ abstract class ProcessUtils {
} }
class _DefaultProcessUtils implements ProcessUtils { class _DefaultProcessUtils implements ProcessUtils {
_DefaultProcessUtils({
@required ProcessManager processManager,
@required Logger logger,
}) : _processManager = processManager,
_logger = logger;
final ProcessManager _processManager;
final Logger _logger;
@override @override
Future<RunResult> run( Future<RunResult> run(
List<String> cmd, { List<String> cmd, {
...@@ -259,15 +308,15 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -259,15 +308,15 @@ class _DefaultProcessUtils implements ProcessUtils {
_traceCommand(cmd, workingDirectory: workingDirectory); _traceCommand(cmd, workingDirectory: workingDirectory);
// When there is no timeout, there's no need to kill a running process, so // When there is no timeout, there's no need to kill a running process, so
// we can just use globals.processManager.run(). // we can just use _processManager.run().
if (timeout == null) { if (timeout == null) {
final ProcessResult results = await globals.processManager.run( final ProcessResult results = await _processManager.run(
cmd, cmd,
workingDirectory: workingDirectory, workingDirectory: workingDirectory,
environment: _environment(allowReentrantFlutter, environment), environment: _environment(allowReentrantFlutter, environment),
); );
final RunResult runResult = RunResult(results, cmd); final RunResult runResult = RunResult(results, cmd);
globals.printTrace(runResult.toString()); _logger.printTrace(runResult.toString());
if (throwOnError && runResult.exitCode != 0 && if (throwOnError && runResult.exitCode != 0 &&
(whiteListFailures == null || !whiteListFailures(runResult.exitCode))) { (whiteListFailures == null || !whiteListFailures(runResult.exitCode))) {
runResult.throwException('Process exited abnormally:\n$runResult'); runResult.throwException('Process exited abnormally:\n$runResult');
...@@ -276,7 +325,7 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -276,7 +325,7 @@ class _DefaultProcessUtils implements ProcessUtils {
} }
// When there is a timeout, we have to kill the running process, so we have // When there is a timeout, we have to kill the running process, so we have
// to use globals.processManager.start() through _runCommand() above. // to use _processManager.start() through _runCommand() above.
while (true) { while (true) {
assert(timeoutRetries >= 0); assert(timeoutRetries >= 0);
timeoutRetries = timeoutRetries - 1; timeoutRetries = timeoutRetries - 1;
...@@ -302,7 +351,7 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -302,7 +351,7 @@ class _DefaultProcessUtils implements ProcessUtils {
int exitCode; int exitCode;
exitCode = await process.exitCode.timeout(timeout, onTimeout: () { exitCode = await process.exitCode.timeout(timeout, onTimeout: () {
// The process timed out. Kill it. // The process timed out. Kill it.
globals.processManager.killPid(process.pid); _processManager.killPid(process.pid);
return null; return null;
}); });
...@@ -331,7 +380,7 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -331,7 +380,7 @@ class _DefaultProcessUtils implements ProcessUtils {
// If the process did not timeout. We are done. // If the process did not timeout. We are done.
if (exitCode != null) { if (exitCode != null) {
globals.printTrace(runResult.toString()); _logger.printTrace(runResult.toString());
if (throwOnError && runResult.exitCode != 0 && if (throwOnError && runResult.exitCode != 0 &&
(whiteListFailures == null || !whiteListFailures(exitCode))) { (whiteListFailures == null || !whiteListFailures(exitCode))) {
runResult.throwException('Process exited abnormally:\n$runResult'); runResult.throwException('Process exited abnormally:\n$runResult');
...@@ -345,8 +394,10 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -345,8 +394,10 @@ class _DefaultProcessUtils implements ProcessUtils {
} }
// Log the timeout with a trace message in verbose mode. // Log the timeout with a trace message in verbose mode.
globals.printTrace('Process "${cmd[0]}" timed out. $timeoutRetries attempts left:\n' _logger.printTrace(
'$runResult'); 'Process "${cmd[0]}" timed out. $timeoutRetries attempts left:\n'
'$runResult',
);
} }
// Unreachable. // Unreachable.
...@@ -363,14 +414,14 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -363,14 +414,14 @@ class _DefaultProcessUtils implements ProcessUtils {
bool allowReentrantFlutter = false, bool allowReentrantFlutter = false,
}) { }) {
_traceCommand(cmd, workingDirectory: workingDirectory); _traceCommand(cmd, workingDirectory: workingDirectory);
final ProcessResult results = globals.processManager.runSync( final ProcessResult results = _processManager.runSync(
cmd, cmd,
workingDirectory: workingDirectory, workingDirectory: workingDirectory,
environment: _environment(allowReentrantFlutter, environment), environment: _environment(allowReentrantFlutter, environment),
); );
final RunResult runResult = RunResult(results, cmd); final RunResult runResult = RunResult(results, cmd);
globals.printTrace('Exit code ${runResult.exitCode} from: ${cmd.join(' ')}'); _logger.printTrace('Exit code ${runResult.exitCode} from: ${cmd.join(' ')}');
bool failedExitCode = runResult.exitCode != 0; bool failedExitCode = runResult.exitCode != 0;
if (whiteListFailures != null && failedExitCode) { if (whiteListFailures != null && failedExitCode) {
...@@ -379,17 +430,17 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -379,17 +430,17 @@ class _DefaultProcessUtils implements ProcessUtils {
if (runResult.stdout.isNotEmpty && !hideStdout) { if (runResult.stdout.isNotEmpty && !hideStdout) {
if (failedExitCode && throwOnError) { if (failedExitCode && throwOnError) {
globals.printStatus(runResult.stdout.trim()); _logger.printStatus(runResult.stdout.trim());
} else { } else {
globals.printTrace(runResult.stdout.trim()); _logger.printTrace(runResult.stdout.trim());
} }
} }
if (runResult.stderr.isNotEmpty) { if (runResult.stderr.isNotEmpty) {
if (failedExitCode && throwOnError) { if (failedExitCode && throwOnError) {
globals.printError(runResult.stderr.trim()); _logger.printError(runResult.stderr.trim());
} else { } else {
globals.printTrace(runResult.stderr.trim()); _logger.printTrace(runResult.stderr.trim());
} }
} }
...@@ -408,7 +459,7 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -408,7 +459,7 @@ class _DefaultProcessUtils implements ProcessUtils {
Map<String, String> environment, Map<String, String> environment,
}) { }) {
_traceCommand(cmd, workingDirectory: workingDirectory); _traceCommand(cmd, workingDirectory: workingDirectory);
return globals.processManager.start( return _processManager.start(
cmd, cmd,
workingDirectory: workingDirectory, workingDirectory: workingDirectory,
environment: _environment(allowReentrantFlutter, environment), environment: _environment(allowReentrantFlutter, environment),
...@@ -443,9 +494,9 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -443,9 +494,9 @@ class _DefaultProcessUtils implements ProcessUtils {
if (line != null) { if (line != null) {
final String message = '$prefix$line'; final String message = '$prefix$line';
if (trace) { if (trace) {
globals.printTrace(message); _logger.printTrace(message);
} else { } else {
globals.printStatus(message, wrap: false); _logger.printStatus(message, wrap: false);
} }
} }
}); });
...@@ -458,7 +509,7 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -458,7 +509,7 @@ class _DefaultProcessUtils implements ProcessUtils {
line = mapFunction(line); line = mapFunction(line);
} }
if (line != null) { if (line != null) {
globals.printError('$prefix$line', wrap: false); _logger.printError('$prefix$line', wrap: false);
} }
}); });
...@@ -487,9 +538,9 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -487,9 +538,9 @@ class _DefaultProcessUtils implements ProcessUtils {
}) { }) {
_traceCommand(cli); _traceCommand(cli);
try { try {
return globals.processManager.runSync(cli, environment: environment).exitCode == 0; return _processManager.runSync(cli, environment: environment).exitCode == 0;
} catch (error) { } catch (error) {
globals.printTrace('$cli failed with $error'); _logger.printTrace('$cli failed with $error');
return false; return false;
} }
} }
...@@ -501,9 +552,9 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -501,9 +552,9 @@ class _DefaultProcessUtils implements ProcessUtils {
}) async { }) async {
_traceCommand(cli); _traceCommand(cli);
try { try {
return (await globals.processManager.run(cli, environment: environment)).exitCode == 0; return (await _processManager.run(cli, environment: environment)).exitCode == 0;
} catch (error) { } catch (error) {
globals.printTrace('$cli failed with $error'); _logger.printTrace('$cli failed with $error');
return false; return false;
} }
} }
...@@ -525,9 +576,9 @@ class _DefaultProcessUtils implements ProcessUtils { ...@@ -525,9 +576,9 @@ class _DefaultProcessUtils implements ProcessUtils {
void _traceCommand(List<String> args, { String workingDirectory }) { void _traceCommand(List<String> args, { String workingDirectory }) {
final String argsText = args.join(' '); final String argsText = args.join(' ');
if (workingDirectory == null) { if (workingDirectory == null) {
globals.printTrace('executing: $argsText'); _logger.printTrace('executing: $argsText');
} else { } else {
globals.printTrace('executing: [$workingDirectory${globals.fs.path.separator}] $argsText'); _logger.printTrace('executing: [$workingDirectory/] $argsText');
} }
} }
} }
...@@ -120,8 +120,12 @@ Future<T> runInContext<T>( ...@@ -120,8 +120,12 @@ Future<T> runInContext<T>(
OperatingSystemUtils: () => OperatingSystemUtils(), OperatingSystemUtils: () => OperatingSystemUtils(),
PersistentToolState: () => PersistentToolState(), PersistentToolState: () => PersistentToolState(),
ProcessInfo: () => ProcessInfo(), ProcessInfo: () => ProcessInfo(),
ProcessUtils: () => ProcessUtils(), ProcessUtils: () => ProcessUtils(
processManager: globals.processManager,
logger: globals.logger,
),
Pub: () => const Pub(), Pub: () => const Pub(),
ShutdownHooks: () => ShutdownHooks(logger: globals.logger),
Signals: () => Signals(), Signals: () => Signals(),
SimControl: () => SimControl(), SimControl: () => SimControl(),
Stdio: () => const Stdio(), Stdio: () => const Stdio(),
......
...@@ -6,6 +6,7 @@ import 'dart:async'; ...@@ -6,6 +6,7 @@ import 'dart:async';
import 'package:platform/platform.dart'; import 'package:platform/platform.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/process.dart'; import 'package:flutter_tools/src/base/process.dart';
import 'package:flutter_tools/src/base/terminal.dart'; import 'package:flutter_tools/src/base/terminal.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
...@@ -15,49 +16,59 @@ import '../../src/common.dart'; ...@@ -15,49 +16,59 @@ import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
import '../../src/mocks.dart' show MockProcess, import '../../src/mocks.dart' show MockProcess,
MockProcessManager, MockProcessManager,
MockStdio,
flakyProcessFactory; flakyProcessFactory;
class MockLogger extends Mock implements Logger {}
void main() { void main() {
group('process exceptions', () { group('process exceptions', () {
ProcessManager mockProcessManager; ProcessManager mockProcessManager;
ProcessUtils processUtils;
setUp(() { setUp(() {
mockProcessManager = PlainMockProcessManager(); mockProcessManager = PlainMockProcessManager();
processUtils = ProcessUtils(
processManager: mockProcessManager,
logger: MockLogger(),
);
}); });
testUsingContext('runAsync throwOnError: exceptions should be ProcessException objects', () async { testWithoutContext('runAsync throwOnError: exceptions should be ProcessException objects', () async {
when(mockProcessManager.run(<String>['false'])).thenAnswer( when(mockProcessManager.run(<String>['false'])).thenAnswer(
(Invocation invocation) => Future<ProcessResult>.value(ProcessResult(0, 1, '', ''))); (Invocation invocation) => Future<ProcessResult>.value(ProcessResult(0, 1, '', '')));
expect(() async => await processUtils.run(<String>['false'], throwOnError: true), expect(() async => await processUtils.run(<String>['false'], throwOnError: true),
throwsA(isInstanceOf<ProcessException>())); throwsA(isInstanceOf<ProcessException>()));
}, overrides: <Type, Generator>{ProcessManager: () => mockProcessManager}); });
}); });
group('shutdownHooks', () { group('shutdownHooks', () {
testUsingContext('runInExpectedOrder', () async { testWithoutContext('runInExpectedOrder', () async {
int i = 1; int i = 1;
int serializeRecording1; int serializeRecording1;
int serializeRecording2; int serializeRecording2;
int postProcessRecording; int postProcessRecording;
int cleanup; int cleanup;
addShutdownHook(() async { final ShutdownHooks shutdownHooks = ShutdownHooks(logger: MockLogger());
shutdownHooks.addShutdownHook(() async {
serializeRecording1 = i++; serializeRecording1 = i++;
}, ShutdownStage.SERIALIZE_RECORDING); }, ShutdownStage.SERIALIZE_RECORDING);
addShutdownHook(() async { shutdownHooks.addShutdownHook(() async {
cleanup = i++; cleanup = i++;
}, ShutdownStage.CLEANUP); }, ShutdownStage.CLEANUP);
addShutdownHook(() async { shutdownHooks.addShutdownHook(() async {
postProcessRecording = i++; postProcessRecording = i++;
}, ShutdownStage.POST_PROCESS_RECORDING); }, ShutdownStage.POST_PROCESS_RECORDING);
addShutdownHook(() async { shutdownHooks.addShutdownHook(() async {
serializeRecording2 = i++; serializeRecording2 = i++;
}, ShutdownStage.SERIALIZE_RECORDING); }, ShutdownStage.SERIALIZE_RECORDING);
await runShutdownHooks(); await shutdownHooks.runShutdownHooks();
expect(serializeRecording1, lessThanOrEqualTo(2)); expect(serializeRecording1, lessThanOrEqualTo(2));
expect(serializeRecording2, lessThanOrEqualTo(2)); expect(serializeRecording2, lessThanOrEqualTo(2));
...@@ -68,30 +79,42 @@ void main() { ...@@ -68,30 +79,42 @@ void main() {
group('output formatting', () { group('output formatting', () {
MockProcessManager mockProcessManager; MockProcessManager mockProcessManager;
ProcessUtils processUtils;
BufferLogger mockLogger;
setUp(() { setUp(() {
mockProcessManager = MockProcessManager(); mockProcessManager = MockProcessManager();
mockLogger = BufferLogger(
terminal: AnsiTerminal(
stdio: MockStdio(),
platform: FakePlatform.fromPlatform(const LocalPlatform())..stdoutSupportsAnsi = false,
),
outputPreferences: OutputPreferences(wrapText: true, wrapColumn: 40),
);
processUtils = ProcessUtils(
processManager: mockProcessManager,
logger: mockLogger,
);
}); });
MockProcess Function(List<String>) processMetaFactory(List<String> stdout, { List<String> stderr = const <String>[] }) { MockProcess Function(List<String>) processMetaFactory(List<String> stdout, {
final Stream<List<int>> stdoutStream = List<String> stderr = const <String>[],
Stream<List<int>>.fromIterable(stdout.map<List<int>>((String s) => s.codeUnits)); }) {
final Stream<List<int>> stderrStream = final Stream<List<int>> stdoutStream = Stream<List<int>>.fromIterable(
Stream<List<int>>.fromIterable(stderr.map<List<int>>((String s) => s.codeUnits)); stdout.map<List<int>>((String s) => s.codeUnits,
));
final Stream<List<int>> stderrStream = Stream<List<int>>.fromIterable(
stderr.map<List<int>>((String s) => s.codeUnits,
));
return (List<String> command) => MockProcess(stdout: stdoutStream, stderr: stderrStream); return (List<String> command) => MockProcess(stdout: stdoutStream, stderr: stderrStream);
} }
testUsingContext('Command output is not wrapped.', () async { testWithoutContext('Command output is not wrapped.', () async {
final List<String> testString = <String>['0123456789' * 10]; final List<String> testString = <String>['0123456789' * 10];
mockProcessManager.processFactory = processMetaFactory(testString, stderr: testString); mockProcessManager.processFactory = processMetaFactory(testString, stderr: testString);
await processUtils.stream(<String>['command']); await processUtils.stream(<String>['command']);
expect(mockLogger.statusText, equals('${testString[0]}\n'));
expect(testLogger.statusText, equals('${testString[0]}\n')); expect(mockLogger.errorText, equals('${testString[0]}\n'));
expect(testLogger.errorText, equals('${testString[0]}\n'));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
OutputPreferences: () => OutputPreferences(wrapText: true, wrapColumn: 40),
Platform: () => FakePlatform.fromPlatform(const LocalPlatform())..stdoutSupportsAnsi = false,
}); });
}); });
...@@ -99,43 +122,47 @@ void main() { ...@@ -99,43 +122,47 @@ void main() {
const Duration delay = Duration(seconds: 2); const Duration delay = Duration(seconds: 2);
MockProcessManager flakyProcessManager; MockProcessManager flakyProcessManager;
ProcessManager mockProcessManager; ProcessManager mockProcessManager;
ProcessUtils processUtils;
ProcessUtils flakyProcessUtils;
setUp(() { setUp(() {
// MockProcessManager has an implementation of start() that returns the // MockProcessManager has an implementation of start() that returns the
// result of processFactory. // result of processFactory.
flakyProcessManager = MockProcessManager(); flakyProcessManager = MockProcessManager();
mockProcessManager = MockProcessManager(); mockProcessManager = MockProcessManager();
processUtils = ProcessUtils(
processManager: mockProcessManager,
logger: MockLogger(),
);
flakyProcessUtils = ProcessUtils(
processManager: flakyProcessManager,
logger: MockLogger(),
);
}); });
testUsingContext(' succeeds on success', () async { testWithoutContext(' succeeds on success', () async {
when(mockProcessManager.run(<String>['whoohoo'])).thenAnswer((_) { when(mockProcessManager.run(<String>['whoohoo'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 0, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 0, '', ''));
}); });
expect((await processUtils.run(<String>['whoohoo'])).exitCode, 0); expect((await processUtils.run(<String>['whoohoo'])).exitCode, 0);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' fails on failure', () async { testWithoutContext(' fails on failure', () async {
when(mockProcessManager.run(<String>['boohoo'])).thenAnswer((_) { when(mockProcessManager.run(<String>['boohoo'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 1, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
}); });
expect((await processUtils.run(<String>['boohoo'])).exitCode, 1); expect((await processUtils.run(<String>['boohoo'])).exitCode, 1);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' throws on failure with throwOnError', () async { testWithoutContext(' throws on failure with throwOnError', () async {
when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) { when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 1, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
}); });
expect(() => processUtils.run(<String>['kaboom'], throwOnError: true), expect(() => processUtils.run(<String>['kaboom'], throwOnError: true),
throwsA(isA<ProcessException>())); throwsA(isA<ProcessException>()));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' does not throw on failure with whitelist', () async { testWithoutContext(' does not throw on failure with whitelist', () async {
when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) { when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 1, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
}); });
...@@ -145,12 +172,11 @@ void main() { ...@@ -145,12 +172,11 @@ void main() {
throwOnError: true, throwOnError: true,
whiteListFailures: (int c) => c == 1, whiteListFailures: (int c) => c == 1,
)).exitCode, )).exitCode,
1); 1,
}, overrides: <Type, Generator>{ );
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' throws on failure when not in whitelist', () async { testWithoutContext(' throws on failure when not in whitelist', () async {
when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) { when(mockProcessManager.run(<String>['kaboom'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 2, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 2, '', ''));
}); });
...@@ -160,41 +186,36 @@ void main() { ...@@ -160,41 +186,36 @@ void main() {
throwOnError: true, throwOnError: true,
whiteListFailures: (int c) => c == 1, whiteListFailures: (int c) => c == 1,
), ),
throwsA(isA<ProcessException>())); throwsA(isA<ProcessException>()),
}, overrides: <Type, Generator>{ );
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' flaky process fails without retry', () async { testWithoutContext(' flaky process fails without retry', () async {
flakyProcessManager.processFactory = flakyProcessFactory( flakyProcessManager.processFactory = flakyProcessFactory(
flakes: 1, flakes: 1,
delay: delay, delay: delay,
); );
final RunResult result = await processUtils.run( final RunResult result = await flakyProcessUtils.run(
<String>['dummy'], <String>['dummy'],
timeout: delay + const Duration(seconds: 1), timeout: delay + const Duration(seconds: 1),
); );
expect(result.exitCode, -9); expect(result.exitCode, -9);
}, overrides: <Type, Generator>{
ProcessManager: () => flakyProcessManager,
}); });
testUsingContext(' flaky process succeeds with retry', () async { testWithoutContext(' flaky process succeeds with retry', () async {
flakyProcessManager.processFactory = flakyProcessFactory( flakyProcessManager.processFactory = flakyProcessFactory(
flakes: 1, flakes: 1,
delay: delay, delay: delay,
); );
final RunResult result = await processUtils.run( final RunResult result = await flakyProcessUtils.run(
<String>['dummy'], <String>['dummy'],
timeout: delay - const Duration(milliseconds: 500), timeout: delay - const Duration(milliseconds: 500),
timeoutRetries: 1, timeoutRetries: 1,
); );
expect(result.exitCode, 0); expect(result.exitCode, 0);
}, overrides: <Type, Generator>{
ProcessManager: () => flakyProcessManager,
}); });
testUsingContext(' flaky process generates ProcessException on timeout', () async { testWithoutContext(' flaky process generates ProcessException on timeout', () async {
final Completer<List<int>> flakyStderr = Completer<List<int>>(); final Completer<List<int>> flakyStderr = Completer<List<int>>();
final Completer<List<int>> flakyStdout = Completer<List<int>>(); final Completer<List<int>> flakyStdout = Completer<List<int>>();
flakyProcessManager.processFactory = flakyProcessFactory( flakyProcessManager.processFactory = flakyProcessFactory(
...@@ -211,52 +232,57 @@ void main() { ...@@ -211,52 +232,57 @@ void main() {
flakyStdout.complete(<int>[]); flakyStdout.complete(<int>[]);
return true; return true;
}); });
expect(() => processUtils.run( expect(() => flakyProcessUtils.run(
<String>['dummy'], <String>['dummy'],
timeout: delay - const Duration(milliseconds: 500), timeout: delay - const Duration(milliseconds: 500),
timeoutRetries: 0, timeoutRetries: 0,
), throwsA(isInstanceOf<ProcessException>())); ), throwsA(isInstanceOf<ProcessException>()));
}, overrides: <Type, Generator>{
ProcessManager: () => flakyProcessManager,
}); });
}); });
group('runSync', () { group('runSync', () {
ProcessManager mockProcessManager; ProcessManager mockProcessManager;
ProcessUtils processUtils;
BufferLogger testLogger;
setUp(() { setUp(() {
mockProcessManager = MockProcessManager(); mockProcessManager = MockProcessManager();
testLogger = BufferLogger(
terminal: AnsiTerminal(
stdio: MockStdio(),
platform: FakePlatform.fromPlatform(const LocalPlatform())..stdoutSupportsAnsi = false,
),
outputPreferences: OutputPreferences(wrapText: true, wrapColumn: 40),
);
processUtils = ProcessUtils(
processManager: mockProcessManager,
logger: testLogger,
);
}); });
testUsingContext(' succeeds on success', () async { testWithoutContext(' succeeds on success', () async {
when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn( when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
ProcessResult(0, 0, '', '') ProcessResult(0, 0, '', '')
); );
expect(processUtils.runSync(<String>['whoohoo']).exitCode, 0); expect(processUtils.runSync(<String>['whoohoo']).exitCode, 0);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' fails on failure', () async { testWithoutContext(' fails on failure', () async {
when(mockProcessManager.runSync(<String>['boohoo'])).thenReturn( when(mockProcessManager.runSync(<String>['boohoo'])).thenReturn(
ProcessResult(0, 1, '', '') ProcessResult(0, 1, '', '')
); );
expect(processUtils.runSync(<String>['boohoo']).exitCode, 1); expect(processUtils.runSync(<String>['boohoo']).exitCode, 1);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' throws on failure with throwOnError', () async { testWithoutContext(' throws on failure with throwOnError', () async {
when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn( when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
ProcessResult(0, 1, '', '') ProcessResult(0, 1, '', '')
); );
expect(() => processUtils.runSync(<String>['kaboom'], throwOnError: true), expect(() => processUtils.runSync(<String>['kaboom'], throwOnError: true),
throwsA(isA<ProcessException>())); throwsA(isA<ProcessException>()));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' does not throw on failure with whitelist', () async { testWithoutContext(' does not throw on failure with whitelist', () async {
when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn( when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
ProcessResult(0, 1, '', '') ProcessResult(0, 1, '', '')
); );
...@@ -267,11 +293,9 @@ void main() { ...@@ -267,11 +293,9 @@ void main() {
whiteListFailures: (int c) => c == 1, whiteListFailures: (int c) => c == 1,
).exitCode, ).exitCode,
1); 1);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' throws on failure when not in whitelist', () async { testWithoutContext(' throws on failure when not in whitelist', () async {
when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn( when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
ProcessResult(0, 2, '', '') ProcessResult(0, 2, '', '')
); );
...@@ -282,22 +306,18 @@ void main() { ...@@ -282,22 +306,18 @@ void main() {
whiteListFailures: (int c) => c == 1, whiteListFailures: (int c) => c == 1,
), ),
throwsA(isA<ProcessException>())); throwsA(isA<ProcessException>()));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' prints stdout and stderr to trace on success', () async { testWithoutContext(' prints stdout and stderr to trace on success', () async {
when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn( when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
ProcessResult(0, 0, 'stdout', 'stderr') ProcessResult(0, 0, 'stdout', 'stderr')
); );
expect(processUtils.runSync(<String>['whoohoo']).exitCode, 0); expect(processUtils.runSync(<String>['whoohoo']).exitCode, 0);
expect(testLogger.traceText, contains('stdout')); expect(testLogger.traceText, contains('stdout'));
expect(testLogger.traceText, contains('stderr')); expect(testLogger.traceText, contains('stderr'));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' prints stdout to status and stderr to error on failure with throwOnError', () async { testWithoutContext(' prints stdout to status and stderr to error on failure with throwOnError', () async {
when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn( when(mockProcessManager.runSync(<String>['kaboom'])).thenReturn(
ProcessResult(0, 1, 'stdout', 'stderr') ProcessResult(0, 1, 'stdout', 'stderr')
); );
...@@ -305,71 +325,69 @@ void main() { ...@@ -305,71 +325,69 @@ void main() {
throwsA(isA<ProcessException>())); throwsA(isA<ProcessException>()));
expect(testLogger.statusText, contains('stdout')); expect(testLogger.statusText, contains('stdout'));
expect(testLogger.errorText, contains('stderr')); expect(testLogger.errorText, contains('stderr'));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' does not print stdout with hideStdout', () async { testWithoutContext(' does not print stdout with hideStdout', () async {
when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn( when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
ProcessResult(0, 0, 'stdout', 'stderr') ProcessResult(0, 0, 'stdout', 'stderr')
); );
expect(processUtils.runSync(<String>['whoohoo'], hideStdout: true).exitCode, 0); expect(processUtils.runSync(<String>['whoohoo'], hideStdout: true).exitCode, 0);
expect(testLogger.traceText.contains('stdout'), isFalse); expect(testLogger.traceText.contains('stdout'), isFalse);
expect(testLogger.traceText, contains('stderr')); expect(testLogger.traceText, contains('stderr'));
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
}); });
group('exitsHappySync', () { group('exitsHappySync', () {
ProcessManager mockProcessManager; ProcessManager mockProcessManager;
ProcessUtils processUtils;
setUp(() { setUp(() {
mockProcessManager = MockProcessManager(); mockProcessManager = MockProcessManager();
processUtils = ProcessUtils(
processManager: mockProcessManager,
logger: MockLogger(),
);
}); });
testUsingContext(' succeeds on success', () async { testWithoutContext(' succeeds on success', () async {
when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn( when(mockProcessManager.runSync(<String>['whoohoo'])).thenReturn(
ProcessResult(0, 0, '', '') ProcessResult(0, 0, '', '')
); );
expect(processUtils.exitsHappySync(<String>['whoohoo']), isTrue); expect(processUtils.exitsHappySync(<String>['whoohoo']), isTrue);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' fails on failure', () async { testWithoutContext(' fails on failure', () async {
when(mockProcessManager.runSync(<String>['boohoo'])).thenReturn( when(mockProcessManager.runSync(<String>['boohoo'])).thenReturn(
ProcessResult(0, 1, '', '') ProcessResult(0, 1, '', '')
); );
expect(processUtils.exitsHappySync(<String>['boohoo']), isFalse); expect(processUtils.exitsHappySync(<String>['boohoo']), isFalse);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
}); });
group('exitsHappy', () { group('exitsHappy', () {
ProcessManager mockProcessManager; ProcessManager mockProcessManager;
ProcessUtils processUtils;
setUp(() { setUp(() {
mockProcessManager = MockProcessManager(); mockProcessManager = MockProcessManager();
processUtils = ProcessUtils(
processManager: mockProcessManager,
logger: MockLogger(),
);
}); });
testUsingContext(' succeeds on success', () async { testWithoutContext(' succeeds on success', () async {
when(mockProcessManager.run(<String>['whoohoo'])).thenAnswer((_) { when(mockProcessManager.run(<String>['whoohoo'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 0, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 0, '', ''));
}); });
expect(await processUtils.exitsHappy(<String>['whoohoo']), isTrue); expect(await processUtils.exitsHappy(<String>['whoohoo']), isTrue);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
testUsingContext(' fails on failure', () async { testWithoutContext(' fails on failure', () async {
when(mockProcessManager.run(<String>['boohoo'])).thenAnswer((_) { when(mockProcessManager.run(<String>['boohoo'])).thenAnswer((_) {
return Future<ProcessResult>.value(ProcessResult(0, 1, '', '')); return Future<ProcessResult>.value(ProcessResult(0, 1, '', ''));
}); });
expect(await processUtils.exitsHappy(<String>['boohoo']), isFalse); expect(await processUtils.exitsHappy(<String>['boohoo']), isFalse);
}, overrides: <Type, Generator>{
ProcessManager: () => mockProcessManager,
}); });
}); });
......
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