Skip to content

Core\Test\RunResult::exitCode

$runResult->exitCode(): int

exitCode() returns the exit code of one run of your program. In a test, Core\Test::run() runs your own program as a command, with the words you give it. The result is a Core\Test\RunResult, and exitCode() reads the exit code from it.

The exit code is 0 when the program ends normally. When the program calls exit(3), the exit code is 3. When the program throws an error that it does not catch, the exit code is 1. When Core\Command::run prints its usage page, the exit code is 2. A real shell sees the same codes.

An exit code is a number from 0 to 255. A number outside that range is changed to fit, as on Linux and macOS. The exit code is the remainder after division by 256. So exit(-1) gives 255, and exit(300) gives 44. exit with a string prints the string and gives 0.

You can call exitCode() as often as you like. Each call returns the same number.

The examples below show a program that ends normally and a program that calls exit. The last one is a test that checks every way a command-line tool can end.

Return value

Returns int. 0 when the program ended normally. The number given to exit when the program called it. 2 when Core\Command::run printed its usage page. 1 when the program threw an error it did not catch.

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 program that ends normally

<?nvs
// Shows how to read the exit code of a run of your own program.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'greet') {
// This part is the program. It prints a greeting and ends normally.
echo 'Hello, ', Core\Cli::arguments()[1] ?? '', "\n";
} else {
// This part runs the program above with the words `greet` and `Ada`.
var $run = Test::run(['greet', 'Ada']);
// The program ended normally, so the exit code is `0`. This prints `0`.
echo $run->exitCode(), "\n";
}
Output
0

The number given to exit

<?nvs
// Shows that `exitCode()` returns the number the program gave to `exit`.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'check-stock') {
// This part is the program. Nothing is in stock, so it stops with `exit(3)`.
echo "no items left\n";
exit(3);
} else {
var $run = Test::run(['check-stock']);
// The exit code is `3`. The text printed before `exit` is kept.
// This prints `3 no items left`.
echo $run->exitCode(), ' ', $run->stdout();
}
Output
3 no items left

Test every way a tool can end

<?nvs
// Shows how to test the exit code of a command-line tool in each way it can end.
use Core\Test;
class Tool
{
#[Core\Command(name: 'import', about: 'Import orders from a CSV file')]
public static function import(string $file): uint
{
if (!Core\Str::endsWith($file, '.csv')) {
throw new RuntimeError('only CSV files can be imported');
}
echo 'imported ', $file, "\n";
return 0;
}
}
if ((Core\Cli::arguments()[0] ?? '') != '') {
// This part is the tool. `Core\Command::run` returns the exit code.
exit(Core\Command::run() as int);
} else {
// A file that ends in `.csv` is imported, so the exit code is `0`.
Test::assertEquals(Test::run(['import', 'orders.csv'])->exitCode(), 0);
// An error the tool does not catch gives the exit code `1`.
Test::assertEquals(Test::run(['import', 'orders.pdf'])->exitCode(), 1);
// A command name that does not exist prints the usage page and gives `2`.
Test::assertEquals(Test::run(['improt'])->exitCode(), 2);
// All three checks passed, so this prints `the tool ends with the right codes`.
echo "the tool ends with the right codes\n";
}
Output
the tool ends with the right codes