Skip to content

Core\Test::run

Core\Test::run() runs your own program as a command and returns how it ended. Use it to test a command-line tool.

You give it the words a user would type after the program name, such as ['deploy', 'staging', '--dry-run']. You can also give the program its input with stdin. The program runs the same way as with nvs run. It runs inside the test's own process, so no new process starts. The result has the exit code, the standard output and the standard error, each on its own. exit(3) in the program gives the exit code 3, and the test continues.

When the arguments are wrong, Core\Command::run prints a usage page. This is a short text that shows how to call the command. It is on standard error, and the exit code is 2. An error the program does not catch gives the exit code 1, and its message is on standard error.

Good to know: only a test can call run. A call inside a program that run or Core\Test::request started throws a LogicError.

The examples below show a command with a flag, a command that reads its input, and a command called with a missing argument.

Parameters

  • $argumentsarray<string>The words after the program name, such as ['greet', 'Ada', '--loud']. Core\Command::run and Core\Cli::arguments read them.

Options

  • stdinstring|bytesoptionalThe input of the program, as a string or as bytes. Core\IO::stdin reads it. If you give no input, the input is empty.

Return value

Returns Core\Test\RunResult. A Core\Test\RunResult with the exit code and the output. If the program calls exit(3), the exit code is 3. If the program throws an error it does not catch, the exit code is 1 and the error is on standard error.

Errors

  • LogicErrorThe test program has no file that nvs run would run. This is the case when you give nvs test a folder. The call is also an error inside a program that run or Core\Test::request started.

Changelog

VersionChange
0.0.5Core\Test::run runs your program as a command, with the arguments you give and an optional stdin. It returns a Core\Test\RunResult with the exit code, the standard output and the standard error. The program runs inside the test's own process.

Examples

A command with a flag

<?nvs
// Shows how to test a command with a flag, by running the program with the words a user types.
// proof: test
use Core\Test;
class Deploy
{
#[Core\Command(name: 'deploy', about: 'Deploy the site to a server')]
public static function deploy(
string $target,
#[Core\Option(long: 'dry-run', about: 'Print the plan and change nothing')] bool $dryRun = false,
): uint
{
if ($dryRun) {
echo 'would deploy to ', $target, "\n";
} else {
echo 'deploying to ', $target, "\n";
}
return 0;
}
}
class DeployTest
{
#[Test]
public function itDeploysToTheTarget(): void
{
// This runs the program as `deploy staging`.
var $run = Test::run(['deploy', 'staging']);
Test::assertEquals($run->exitCode(), 0);
Test::assertEquals($run->stdout(), "deploying to staging\n");
}
#[Test]
public function theFlagChangesNothing(): void
{
// This runs the program as `deploy staging --dry-run`.
var $run = Test::run(['deploy', 'staging', '--dry-run']);
Test::assertEquals($run->exitCode(), 0);
Test::assertEquals($run->stdout(), "would deploy to staging\n");
}
}
// `Test::run` starts the program here. `Core\Command::run` calls the command
// that the first word names, and its result is the exit code.
uint $status = Core\Command::run();
exit($status as int);
Output
DeployTest
✓ itDeploysToTheTarget
✓ theFlagChangesNothing
0 failed, 2 passed, 0 skipped, 0 flaky

A command that reads its input

<?nvs
// Shows how to test a command that reads its input, by giving the input with `stdin`.
// proof: test
use Core\Test;
class Csv
{
#[Core\Command(name: 'rows', about: 'Count the rows of a CSV file on the input')]
public static function rows(): uint
{
tainted string $input = Core\IO::stdin();
array<tainted string> $lines = Core\Str::lines(Core\Str::trim($input));
// The first line is the header, so it is not a row.
echo 'rows: ', Core\Arr::count($lines) - 1, "\n";
return 0;
}
}
class CsvTest
{
#[Test]
public function itCountsTheRowsBelowTheHeader(): void
{
string $csv = "name,email\nAda,ada@example.com\nBo,bo@example.com\n";
var $run = Test::run(['rows'], { stdin: $csv });
Test::assertEquals($run->stdout(), "rows: 2\n");
}
#[Test]
public function aFileWithOnlyAHeaderHasNoRows(): void
{
var $run = Test::run(['rows'], { stdin: "name,email\n" });
Test::assertEquals($run->stdout(), "rows: 0\n");
}
}
// `Test::run` starts the program here. `Core\Command::run` calls the command
// that the first word names, and its result is the exit code.
uint $status = Core\Command::run();
exit($status as int);
Output
CsvTest
✓ itCountsTheRowsBelowTheHeader
✓ aFileWithOnlyAHeaderHasNoRows
0 failed, 2 passed, 0 skipped, 0 flaky

A missing argument gives exit code 2

<?nvs
// Shows what a test sees when a command is called without an argument it needs.
// proof: test
use Core\Test;
class Backup
{
#[Core\Command(name: 'backup', about: 'Copy a database to a file')]
public static function backup(string $database): uint
{
echo 'saved ', $database, "\n";
return 0;
}
}
class BackupTest
{
#[Test]
public function itNeedsTheDatabaseName(): void
{
var $run = Test::run(['backup']);
// `Core\Command::run` prints a usage page and the exit code is `2`.
Test::assertEquals($run->exitCode(), 2);
// The usage page is on standard error, so standard output is empty.
Test::assertEquals($run->stdout(), '');
Test::assertTrue(Core\Str::contains($run->stderr(), 'usage: backup <database>'));
}
}
// `Test::run` starts the program here. `Core\Command::run` calls the command
// that the first word names, and its result is the exit code.
uint $status = Core\Command::run();
exit($status as int);
Output
BackupTest
✓ itNeedsTheDatabaseName
0 failed, 1 passed, 0 skipped, 0 flaky