From 9d9e272e70f634fcc1794ad1b9e4106e3c84a560 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 17 May 2022 16:54:08 -0700 Subject: [PATCH] [tool] Add tests for FakeProcess (#104013) Because this class has some subtle behaviour with regards to control of exit timing and when and how it streams data to stderr and stdout, it's worth adding unit tests for this class directly, as well as (in a followup patch) for FakeProcessManager. This is additional testing relating to refactoring landed in: https://github.com/flutter/flutter/pull/103947 Issue: https://github.com/flutter/flutter/issues/102451 --- .../fake_process_manager_test.dart | 175 ++++++++++++++++++ .../test/src/fake_process_manager.dart | 91 +++++---- 2 files changed, 229 insertions(+), 37 deletions(-) create mode 100644 packages/flutter_tools/test/general.shard/fake_process_manager_test.dart diff --git a/packages/flutter_tools/test/general.shard/fake_process_manager_test.dart b/packages/flutter_tools/test/general.shard/fake_process_manager_test.dart new file mode 100644 index 0000000000..f2d5154848 --- /dev/null +++ b/packages/flutter_tools/test/general.shard/fake_process_manager_test.dart @@ -0,0 +1,175 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; +import 'package:fake_async/fake_async.dart'; + +import '../src/common.dart'; +import '../src/fake_process_manager.dart'; + +void main() { + group(FakeProcess, () { + testWithoutContext('exits with specified exit code', () async { + final FakeProcess process = FakeProcess(exitCode: 42); + expect(await process.exitCode, 42); + }); + + testWithoutContext('exits with specified stderr, stdout', () async { + final FakeProcess process = FakeProcess( + stderr: 'stderr\u{FFFD}'.codeUnits, + stdout: 'stdout\u{FFFD}'.codeUnits, + ); + await process.exitCode; + + // Verify that no encoding changes have been applied to output. + // + // In the past, we had hardcoded UTF-8 encoding for these streams in + // FakeProcess. When a specific encoding is desired, it can be specified + // on FakeCommand or in the encoding parameter of FakeProcessManager.run + // or FakeProcessManager.runAsync. + expect((await process.stderr.toList()).expand((List x) => x), 'stderr\u{FFFD}'.codeUnits); + expect((await process.stdout.toList()).expand((List x) => x), 'stdout\u{FFFD}'.codeUnits); + }); + + testWithoutContext('exits after specified delay (if no completer specified)', () { + final bool done = FakeAsync().run((FakeAsync time) { + final FakeProcess process = FakeProcess( + duration: const Duration(seconds: 30), + ); + + bool hasExited = false; + unawaited(process.exitCode.then((int _) { hasExited = true; })); + + // Verify process hasn't exited before specified delay. + time.elapse(const Duration(seconds: 15)); + expect(hasExited, isFalse); + + // Verify process has exited after specified delay. + time.elapse(const Duration(seconds: 20)); + expect(hasExited, isTrue); + + return true; + }); + expect(done, isTrue); + }); + + testWithoutContext('exits when completer completes (if no duration specified)', () { + final bool done = FakeAsync().run((FakeAsync time) { + final Completer completer = Completer(); + final FakeProcess process = FakeProcess( + completer: completer, + ); + + bool hasExited = false; + unawaited(process.exitCode.then((int _) { hasExited = true; })); + + // Verify process hasn't exited when all async tasks flushed. + time.elapse(Duration.zero); + expect(hasExited, isFalse); + + // Verify process has exited after completer completes. + completer.complete(); + time.flushMicrotasks(); + expect(hasExited, isTrue); + + return true; + }); + expect(done, isTrue); + }); + + testWithoutContext('when completer and duration are specified, does not exit until completer is completed', () { + final bool done = FakeAsync().run((FakeAsync time) { + final Completer completer = Completer(); + final FakeProcess process = FakeProcess( + duration: const Duration(seconds: 30), + completer: completer, + ); + + bool hasExited = false; + unawaited(process.exitCode.then((int _) { hasExited = true; })); + + // Verify process hasn't exited before specified delay. + time.elapse(const Duration(seconds: 15)); + expect(hasExited, isFalse); + + // Verify process hasn't exited until the completer completes. + time.elapse(const Duration(seconds: 20)); + expect(hasExited, isFalse); + + // Verify process exits after the completer completes. + completer.complete(); + time.flushMicrotasks(); + expect(hasExited, isTrue); + + return true; + }); + expect(done, isTrue); + }); + + testWithoutContext('when completer and duration are specified, does not exit until duration has elapsed', () { + final bool done = FakeAsync().run((FakeAsync time) { + final Completer completer = Completer(); + final FakeProcess process = FakeProcess( + duration: const Duration(seconds: 30), + completer: completer, + ); + + bool hasExited = false; + unawaited(process.exitCode.then((int _) { hasExited = true; })); + + // Verify process hasn't exited before specified delay. + time.elapse(const Duration(seconds: 15)); + expect(hasExited, isFalse); + + // Verify process does not exit until duration has elapsed. + completer.complete(); + expect(hasExited, isFalse); + + // Verify process exits after the duration elapses. + time.elapse(const Duration(seconds: 20)); + expect(hasExited, isTrue); + + return true; + }); + expect(done, isTrue); + }); + + testWithoutContext('process exit is asynchronous', () async { + final FakeProcess process = FakeProcess(); + + bool hasExited = false; + unawaited(process.exitCode.then((int _) { hasExited = true; })); + + // Verify process hasn't completed. + expect(hasExited, isFalse); + + // Flush async tasks. Verify process completes. + await Future.delayed(Duration.zero); + expect(hasExited, isTrue); + }); + + testWithoutContext('stderr, stdout stream data after exit when outputFollowsExit is true', () async { + final FakeProcess process = FakeProcess( + stderr: 'stderr'.codeUnits, + stdout: 'stdout'.codeUnits, + outputFollowsExit: true, + ); + + final List stderr = []; + final List stdout = []; + process.stderr.listen(stderr.addAll); + process.stdout.listen(stdout.addAll); + + // Ensure that no bytes have been received at process exit. + await process.exitCode; + expect(stderr, isEmpty); + expect(stdout, isEmpty); + + // Flush all remaining async work. Ensure stderr, stdout is received. + await Future.delayed(Duration.zero); + expect(stderr, 'stderr'.codeUnits); + expect(stdout, 'stdout'.codeUnits); + }); + }); +} diff --git a/packages/flutter_tools/test/src/fake_process_manager.dart b/packages/flutter_tools/test/src/fake_process_manager.dart index 760249ec5d..db77d428f6 100644 --- a/packages/flutter_tools/test/src/fake_process_manager.dart +++ b/packages/flutter_tools/test/src/fake_process_manager.dart @@ -123,48 +123,63 @@ class FakeCommand { } } -class _FakeProcess implements io.Process { - _FakeProcess( - this._exitCode, - Duration duration, - this.pid, - this._stderr, +/// A fake process for use with [FakeProcessManager]. +/// +/// The process delays exit until both [duration] (if specified) has elapsed +/// and [completer] (if specified) has completed. +/// +/// When [outputFollowsExit] is specified, bytes are streamed to [stderr] and +/// [stdout] after the process exits. +@visibleForTesting +class FakeProcess implements io.Process { + FakeProcess({ + int exitCode = 0, + Duration duration = Duration.zero, + this.pid = 1234, + List stderr = const [], IOSink? stdin, - this._stdout, - this._completer, - bool outputFollowsExit, - ) : exitCode = Future.delayed(duration).then((void value) { - if (_completer != null) { - return _completer.future.then((void _) => _exitCode); - } - return _exitCode; - }), - stdin = stdin ?? IOSink(StreamController>().sink) + List stdout = const [], + Completer? completer, + bool outputFollowsExit = false, + }) : _exitCode = exitCode, + exitCode = Future.delayed(duration).then((void value) { + if (completer != null) { + return completer.future.then((void _) => exitCode); + } + return exitCode; + }), + _stderr = stderr, + stdin = stdin ?? IOSink(StreamController>().sink), + _stdout = stdout, + _completer = completer { if (_stderr.isEmpty) { - stderr = const Stream>.empty(); + this.stderr = const Stream>.empty(); } else if (outputFollowsExit) { // Wait for the process to exit before emitting stderr. - stderr = Stream>.fromFuture(exitCode.then((_) { + this.stderr = Stream>.fromFuture(this.exitCode.then((_) { return Future>(() => _stderr); })); } else { - stderr = Stream>.value(_stderr); + this.stderr = Stream>.value(_stderr); } if (_stdout.isEmpty) { - stdout = const Stream>.empty(); + this.stdout = const Stream>.empty(); } else if (outputFollowsExit) { // Wait for the process to exit before emitting stdout. - stdout = Stream>.fromFuture(exitCode.then((_) { + this.stdout = Stream>.fromFuture(this.exitCode.then((_) { return Future>(() => _stdout); })); } else { - stdout = Stream>.value(_stdout); + this.stdout = Stream>.value(_stdout); } } + /// The process exit code. final int _exitCode; + + /// When specified, blocks process exit until completed. final Completer? _completer; @override @@ -173,6 +188,7 @@ class _FakeProcess implements io.Process { @override final int pid; + /// The raw byte content of stderr. final List _stderr; @override @@ -184,6 +200,7 @@ class _FakeProcess implements io.Process { @override late final Stream> stdout; + /// The raw byte content of stdout. final List _stdout; @override @@ -231,7 +248,7 @@ abstract class FakeProcessManager implements ProcessManager { commands.forEach(addCommand); } - final Map _fakeRunningProcesses = {}; + final Map _fakeRunningProcesses = {}; /// Whether this fake has more [FakeCommand]s that are expected to run. /// @@ -251,7 +268,7 @@ abstract class FakeProcessManager implements ProcessManager { int _pid = 9999; - _FakeProcess _runCommand( + FakeProcess _runCommand( List command, String? workingDirectory, Map? environment, @@ -266,15 +283,15 @@ abstract class FakeProcessManager implements ProcessManager { if (fakeCommand.onRun != null) { fakeCommand.onRun!(); } - return _FakeProcess( - fakeCommand.exitCode, - fakeCommand.duration, - _pid, - encoding?.encode(fakeCommand.stderr) ?? fakeCommand.stderr.codeUnits, - fakeCommand.stdin, - encoding?.encode(fakeCommand.stdout) ?? fakeCommand.stdout.codeUnits, - fakeCommand.completer, - fakeCommand.outputFollowsExit, + return FakeProcess( + duration: fakeCommand.duration, + exitCode: fakeCommand.exitCode, + pid: _pid, + stderr: encoding?.encode(fakeCommand.stderr) ?? fakeCommand.stderr.codeUnits, + stdin: fakeCommand.stdin, + stdout: encoding?.encode(fakeCommand.stdout) ?? fakeCommand.stdout.codeUnits, + completer: fakeCommand.completer, + outputFollowsExit: fakeCommand.outputFollowsExit, ); } @@ -287,7 +304,7 @@ abstract class FakeProcessManager implements ProcessManager { bool runInShell = false, // ignored io.ProcessStartMode mode = io.ProcessStartMode.normal, // ignored }) { - final _FakeProcess process = _runCommand(command.cast(), workingDirectory, environment, io.systemEncoding); + final FakeProcess process = _runCommand(command.cast(), workingDirectory, environment, io.systemEncoding); if (process._completer != null) { _fakeRunningProcesses[process.pid] = process; process.exitCode.whenComplete(() { @@ -307,7 +324,7 @@ abstract class FakeProcessManager implements ProcessManager { Encoding? stdoutEncoding = io.systemEncoding, Encoding? stderrEncoding = io.systemEncoding, }) async { - final _FakeProcess process = _runCommand(command.cast(), workingDirectory, environment, stdoutEncoding); + final FakeProcess process = _runCommand(command.cast(), workingDirectory, environment, stdoutEncoding); await process.exitCode; return io.ProcessResult( process.pid, @@ -327,7 +344,7 @@ abstract class FakeProcessManager implements ProcessManager { Encoding? stdoutEncoding = io.systemEncoding, Encoding? stderrEncoding = io.systemEncoding, }) { - final _FakeProcess process = _runCommand(command.cast(), workingDirectory, environment, stdoutEncoding); + final FakeProcess process = _runCommand(command.cast(), workingDirectory, environment, stdoutEncoding); return io.ProcessResult( process.pid, process._exitCode, @@ -345,7 +362,7 @@ abstract class FakeProcessManager implements ProcessManager { @override bool killPid(int pid, [io.ProcessSignal signal = io.ProcessSignal.sigterm]) { // Killing a fake process has no effect unless it has an attached completer. - final _FakeProcess? fakeProcess = _fakeRunningProcesses[pid]; + final FakeProcess? fakeProcess = _fakeRunningProcesses[pid]; if (fakeProcess == null) { return false; }