Give _runFlutterTest the ability to validate command output

In another change (#37646), I want to test that a test fails and
prints expected output.  I didn't see an existing way to do that, so
I modified `_runFlutterTest` and `runCommand` to allow capturing the
output.  Currently capturing and printing output are mutually
exclusive since we don't need both.

Some awkward bits:
* There already exists a `runAndGetStdout` function that is very
  similar to `runCommand`, and this change makes the conceptual
  distinction more confusing.

* `runFlutterTest` has multiple code paths for different
  configurations.  I don't understand what the different paths are
  for, and I added output checking only along one of them.
This commit is contained in:
James Lin
2019-08-09 15:10:45 -07:00
committed by James D. Lin
parent 01feddbece
commit c02b805cdc
3 changed files with 77 additions and 11 deletions

View File

@@ -83,12 +83,15 @@ Future<void> runCommand(String executable, List<String> arguments, {
bool expectNonZeroExit = false,
int expectedExitCode,
String failureMessage,
bool printOutput = true,
OutputMode outputMode = OutputMode.print,
CapturedOutput output,
bool skip = false,
bool expectFlaky = false,
Duration timeout = _kLongTimeout,
bool Function(String) removeLine,
}) async {
assert((outputMode == OutputMode.capture) == (output != null));
final String commandDescription = '${path.relative(executable, from: workingDirectory)} ${arguments.join(' ')}';
final String relativeWorkingDir = path.relative(workingDirectory);
if (skip) {
@@ -110,7 +113,7 @@ Future<void> runCommand(String executable, List<String> arguments, {
.where((String line) => removeLine == null || !removeLine(line))
.map((String line) => '$line\n')
.transform(const Utf8Encoder());
if (printOutput) {
if (outputMode == OutputMode.print) {
await Future.wait<void>(<Future<void>>[
stdout.addStream(stdoutSource),
stderr.addStream(process.stderr),
@@ -125,6 +128,12 @@ Future<void> runCommand(String executable, List<String> arguments, {
return (expectNonZeroExit || expectFlaky) ? 0 : 1;
});
print('$clock ELAPSED TIME: $bold${elapsedTime(start)}$reset for $commandDescription in $relativeWorkingDir: ');
if (output != null) {
output.stdout = flattenToString(await savedStdout);
output.stderr = flattenToString(await savedStderr);
}
// If the test is flaky we don't care about the actual exit.
if (expectFlaky) {
return;
@@ -133,9 +142,12 @@ Future<void> runCommand(String executable, List<String> arguments, {
if (failureMessage != null) {
print(failureMessage);
}
if (!printOutput) {
stdout.writeln(utf8.decode((await savedStdout).expand<int>((List<int> ints) => ints).toList()));
stderr.writeln(utf8.decode((await savedStderr).expand<int>((List<int> ints) => ints).toList()));
// Print the output when we get unexpected results (unless output was
// printed already).
if (outputMode != OutputMode.print) {
stdout.writeln(flattenToString(await savedStdout));
stderr.writeln(flattenToString(await savedStderr));
}
print(
'$redLine\n'
@@ -147,3 +159,18 @@ Future<void> runCommand(String executable, List<String> arguments, {
exit(1);
}
}
T identity<T>(T x) => x;
/// Flattens a nested list of UTF-8 code units into a single string.
String flattenToString(List<List<int>> chunks) =>
utf8.decode(chunks.expand<int>(identity).toList(growable: false));
/// Specifies what to do with command output from [runCommand].
enum OutputMode { print, capture, discard }
/// Stores command output from [runCommand] when used with [OutputMode.capture].
class CapturedOutput {
String stdout;
String stderr;
}