Skip to content

Core\Test\RunResult::stderr

$runResult->stderr(): string

stderr() returns the text that one run of your program wrote to standard error. In a test, Core\Test::run() runs your own program as a command. The result is a Core\Test\RunResult, and stderr() reads the standard error from it as a string.

A program writes a message to standard error with Core\Cli::write and { stream: Core\Cli::Stream::Err }. Two other messages also go there. The first is an error the program does not catch: it starts with [error] and the message, and the lines after it show where the error was thrown. The second is the usage page that Core\Command::run prints for a wrong command name. If the program wrote nothing to standard error, the result is an empty string.

The text that the program printed with echo is not part of it. stdout() returns that text. So a test can check the result and the messages one at a time.

stderr() throws a RuntimeError when the text is not valid UTF-8. Each call returns the same text.

The examples below show an error the program does not catch and keep progress notes apart from the result. The last one is a test that checks that a typo prints the usage page.

Return value

Returns string. Everything the program wrote to standard error, in order. A usage page from Core\Command::run is here, and so is an error the program did not catch. If the program wrote nothing there, the result is an empty string.

Errors

  • RuntimeErrorThe text is not valid UTF-8, so it is not a string.

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

An error the program does not catch

<?nvs
// Shows where an error goes when the program does not catch it.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'save') {
// This part is the program. It prints one line and then throws an error.
echo "saving the order\n";
throw new RuntimeError('the disk is full');
} else {
var $run = Test::run(['save']);
// The error is on standard error. Its first line is `[error]` and the message.
var $lines = Core\Str::split($run->stderr(), "\n");
echo 'stderr: ', $lines[0], "\n";
// The text printed before the error is on standard output.
echo 'stdout: ', $run->stdout();
}
Output
stderr: [error] the disk is full
stdout: saving the order

Progress notes and the result

<?nvs
// Shows that `stderr()` and `stdout()` return the two output streams of a run apart.
use Core\Test;
if ((Core\Cli::arguments()[0] ?? '') == 'count-orders') {
// This part is the program. The progress note goes to standard error.
Core\Cli::write('reading orders.csv', { stream: Core\Cli::Stream::Err, newline: true });
// The result goes to standard output.
echo "314 orders\n";
} else {
var $run = Test::run(['count-orders']);
// This prints `notes: reading orders.csv`, and then `result: 314 orders`.
echo 'notes: ', $run->stderr();
echo 'result: ', $run->stdout();
}
Output
notes: reading orders.csv
result: 314 orders

Test that a typo shows the usage page

<?nvs
// Shows how to test that a wrong command name prints the usage page.
use Core\Test;
class Tool
{
#[Core\Command(name: 'backup', about: 'Copy the database to a safe place')]
public static function backup(): uint
{
echo "backup done\n";
return 0;
}
}
if ((Core\Cli::arguments()[0] ?? '') != '') {
// This part is the tool.
exit(Core\Command::run() as int);
} else {
// This part is the test. `bakup` is not a command of this tool.
var $run = Test::run(['bakup']);
// `Core\Command::run` writes the usage page to standard error.
// The page lists each command of the tool.
Test::assertEquals($run->exitCode(), 2);
Test::assertTrue(Core\Str::contains($run->stderr(), 'backup Copy the database to a safe place'));
Test::assertEquals($run->stdout(), '');
// All three checks passed, so this prints `a typo shows the usage page`.
echo "a typo shows the usage page\n";
}
Output
a typo shows the usage page