Viewing version 1.0.4. Open the latest package page.

test_process

1.0.4

A package for testing subprocesses.

Download archive

Readme

A package for testing subprocesses.

This exposes a TestProcess class that wraps dart:io's Process class and makes it easy to read standard output line-by-line. TestProcess works the same as Process in many ways, but there are a few major differences.

Standard Output

Process.stdout and Process.stderr are binary streams, which is the most general API but isn't the most helpful when working with a program that produces plain text. Instead, TestProcess.stdout and TestProcess.stderr emit a string for each line of output the process produces. What's more, they're StreamQueues, which means they provide a pull-based API. For example:

import 'package:test/test.dart';
import 'package:test_process/test_process.dart';

void main() {
  test("pub get gets dependencies", () async {
    // TestProcess.start() works just like Process.start() from dart:io.
    var process = await TestProcess.start("pub", ["get"]);

    // StreamQueue.next returns the next line emitted on standard out.
    var firstLine = await process.stdout.next;
    expect(firstLine, equals("Resolving dependencies..."));

    // Each call to StreamQueue.next moves one line further.
    String next;
    do {
      next = await process.stdout.next;
    } while (next != "Got dependencies!");

    // Assert that the process exits with code 0.
    await process.shouldExit(0);
  });
}

The test package's stream matchers have built-in support for StreamQueues, which makes them perfect for making assertions about a process's output. We can use this to clean up the previous example:

import 'package:test/test.dart';
import 'package:test_process/test_process.dart';

void main() {
  test("pub get gets dependencies", () async {
    var process = await TestProcess.start("pub", ["get"]);

    // Each stream matcher will consume as many lines as it matches from a
    // StreamQueue, and no more, so it's safe to use them in sequence.
    await expectLater(process.stdout, emits("Resolving dependencies..."));

    // The emitsThrough matcher matches and consumes any number of lines, as
    // long as they end with one matching the argument.
    await expectLater(process.stdout, emitsThrough("Got dependencies!"));

    await process.shouldExit(0);
  });
}

If you want to access the standard output streams without consuming any values from the queues, you can use the stdoutStream() and stderrStream() methods. Each time you call one of these, it produces an entirely new stream that replays the corresponding output stream from the beginning, regardless of what's already been produced by stdout, stderr, or other calls to the stream method.

Signals and Termination

The way signaling works is different from dart:io as well. TestProcess still has a kill() method, but it defaults to SIGKILL on Mac OS and Linux to ensure (as best as possible) that processes die without leaving behind zombies. If you want to send a particular signal (which is unsupported on Windows), you can do so by explicitly calling signal().

In addition to exitCode, which works the same as in dart:io, TestProcess also adds a new method named shouldExit(). This lets tests wait for a process to exit, and (if desired) assert what particular exit code it produced.

Debugging Output

When a test using TestProcess fails, it will print all the output produced by that process. This makes it much easier to figure out what went wrong and why. The debugging output uses a header based on the process's invocation by default, but you can pass in custom description parameters to TestProcess.start() to control the headers.

TestProcess will also produce debugging output as the test runs if you pass forwardStdio: true to TestProcess.start(). This can be particularly useful when you're using an interactive debugger and you want to figure out what a process is doing before the test finishes and the normal debugging output is printed.

Changelog

1.0.4

  • Set max SDK version to <3.0.0, and adjust other dependencies.

1.0.3

  • Support test 1.x.x.

1.0.2

  • Update SDK version to 2.0.0-dev.17.0

1.0.1

  • Declare support for async 2.0.0.

1.0.0

  • Added pid and exitCode getters to TestProcess.

1.0.0-rc.2

  • Subclassed TestProcesses now emit log output based on the superclass's standard IO streams rather than the subclass's. This matches the documented behavior.

1.0.0-rc.1

  • Initial release candidate.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'test_process:1.0.4'
pubspec.yaml
name: test_process
version: 1.0.4

description: A package for testing subprocesses.
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/test_process

environment:
  sdk: '>=2.0.0-dev.55.0 <3.0.0'

dependencies:
  async: '>=1.12.0 <3.0.0'
  meta: '>=0.9.0 <2.0.0'
  path: ^1.0.0
  test: '>=0.12.42 <2.0.0'

dev_dependencies:
  test_descriptor: ^1.0.0

Version history

VersionPublishedArchiveSHA-256
1.0.0-rc.1Cached · Upstream SHA-256 matchedDownload7767ddb7…ce028f
1.0.0-rc.2Cached · Upstream SHA-256 matchedDownloadd0f8adbc…5a4c42
1.0.0Cached · Upstream SHA-256 matchedDownloadb6cfd009…00f3a0
1.0.1Cached · Upstream SHA-256 matchedDownload7dc4304f…d85795
1.0.2Cached · Upstream SHA-256 matchedDownloadda76b236…3dccda
1.0.3Cached · Upstream SHA-256 matchedDownload7c50790b…86d3f5
1.0.4Cached · Upstream SHA-256 matchedDownloadcc5b06f5…630e36
1.0.5Cached · Upstream SHA-256 matchedDownload34c19058…b61b0c
2.0.0Cached · Upstream SHA-256 matchedDownload2c7b57b7…ae57a7
2.0.1Cached · Upstream SHA-256 matchedDownloadce194ebd…7f97e4
2.0.2Cached · Upstream SHA-256 matchedDownloadf5666a50…a268f5
2.0.3Cached · Upstream SHA-256 matchedDownloadb0e6702f…5d005e
2.1.0Cached · Upstream SHA-256 matchedDownload217f19b5…07bbd6
2.1.1Cached · Upstream SHA-256 matchedDownloadea79c090…7e138e

Dependencies

async "\u003e=1.12.0 \u003c3.0.0"

meta "\u003e=0.9.0 \u003c2.0.0"

path "^1.0.0"

test "\u003e=0.12.42 \u003c2.0.0"