executableArgs function

List<String> executableArgs(
  1. String executable, {
  2. bool node = false,
})

Arguments that can be passed to Process.start and similar APIs along with the executable returned by executableRunner to run executable, which is the basename of an executable in bin (without ".dart").

If node is true, this and executableRunner will run a NodeJS process using the executable compiled by pkg-npm-dev. Otherwise, they'll run a Dart VM process using the executable compiled by pkg-standalone-dev if one exists and running from source otherwise.

This throws a TestFailure if it would run a compiled executable that's out-of-date relative to the pubspec or to source files in lib/ or bin/.

For example:

import 'dart:io';

import 'package:cli_pkg/testing.dart' as pkg;
import 'package:test/test.dart';

void main() {
  test("hello-world prints a message", () async {
    var result = await Process.run(
        pkg.executableRunner("hello-world"),
        pkg.executableArgs("hello-world"));
    expect(result.stdout, equals("Hello, world!\n");
  });
}

Note that in practice it's usually easier to use start.

When using this in multiple tests, consider calling setUpAll with ensureExecutableUpToDate to avoid having many redundant test failures for an out-of-date executable.

Implementation

List<String> executableArgs(String executable, {bool node = false}) {
  ensureExecutableUpToDate(executable, node: node);

  if (node) return [p.absolute("build/npm/$executable.js")];

  var snapshot = p.absolute("build/$executable.snapshot");
  if (File(snapshot).existsSync()) return [snapshot];

  var path = executables.value[executable];
  if (path == null) fail('There is no executable named "$executable".');

  verifyEnvironmentConstants();
  return [
    for (var entry in environmentConstants.value.entries)
      '-D${entry.key}=${entry.value}',
    "--enable-asserts",
    p.absolute(path)
  ];
}