Skip to content

Testing a command

Core\Test::run runs your program as a command and returns how it ended. The program runs the same way as with nvs run. It runs inside the test’s own process, so no new process starts. Use it to test a command-line tool.

You give the words a user types after the program name. Test::run(['deploy', 'staging']) runs the program as deploy staging. Test::run(['rows'], {stdin: $csv}) also gives the program $csv as its input.

The result is a Core\Test\RunResult:

  • exitCode() returns the exit code. exit(3) in the program gives 3, and the test goes on.
  • stdout() returns what the program printed.
  • stderr() returns what the program wrote to standard error.
  • bytes() returns the standard output as bytes.
  • Core\Command::run reads the arguments of a command. When an argument is missing, it prints a usage page on standard error. A usage page lists the arguments the command takes. The exit code is 2.
  • An error that the program does not catch gives the exit code 1. Its message is on standard error.
  • Core\Test::run needs a file that nvs run can run. When the program has no such file, it throws a LogicError.

The program that Core\Test::run starts cannot call Core\Test::run itself. A request that Core\Test::request sends cannot call it either. Both calls throw a LogicError.

A test with #[Test(env: ['API_KEY' => 'test-key'])] sets the variable API_KEY for that test. Core\Env reads it in the test. It also reads it in each program that the test starts with Core\Test::run, and in each request that the test sends with Core\Test::request. Other tests do not see it.

The program still needs the env capability to read the variable. A capability is a permission that the configuration file gives a program.

Read the examples in Reference: Core\Test::run.