Skip to content

nvs ext test

nvs ext test runs the tests of an extension project with the built .nvsx file loaded.

The command runs every method that has #[Test] in the .nvs files of the project's tests folder, as nvs test does. The tests can call the class of the extension. Run nvs ext build first: if a file of the project changed after the last build, the command stops with an error.

The command does not read ./nvs.toml or the nvs.toml in the data folder. So in a test the extension may not read files or connect to hosts. To test with grants, name a configuration with --config.

Good to know: if the configuration you name lists the .nvsx file with another sha256, the command stops with an error that shows both values.

Examples

The examples below run with this configuration file.

nvs.toml
# `nvs ext test` loads the built `.nvsx` file with an entry like this one. It has no `grants`,
# so the extension may not read files or connect to hosts.
[[extension]]
path = "../../config/extension-grants-what-a-component-may-reach/atlas.nvsx"
sha256 = "8bb2081b5d76340eb1cdc2cf22b0eaa2144bb7851b83abb5662c1b369ce9694b"

Test an extension with no grants

<?nvs
// Shows a test file for an extension, as you put it in the `tests` folder of the project.
// `nvs ext test` runs every method that has `#[Test]` and prints one line for each test.
// `nvs run` runs only the code outside the class.
use Core\Test;
use Geo\Atlas;
final class AtlasTest
{
// The extension has no grants in a test, so reading a file throws a `RuntimeError`.
#[Test]
public function readsNoFileWithoutAGrant(): void
{
Test::assertThrows(fn() => Atlas::read('cities.txt'), RuntimeError::class);
}
// Connecting to a host throws a `RuntimeError` too.
#[Test]
public function reachesNoHostWithoutAGrant(): void
{
Test::assertThrows(fn() => Atlas::status('tiles.example.com'), RuntimeError::class);
}
}
// This prints `no folder is granted`.
try {
echo Atlas::read('cities.txt');
} catch (RuntimeError $denied) {
echo $denied->message, "\n";
}
Output
no folder is granted