Testing, Running, and Querying
Single-page view · as markdown
Writing and Running Tests
Pekit / Testing, Running, and Querying
A test is a definition made with test, beside your builds. pekit runs
each test's body as a step, so a test that passed doesn't run again until
something it tests changes. A failing test is a result, not a broken
build: the other tests still run.
Before you start #
- A workspace with a target to test. This page uses a C program,
app, that prints a greeting, and a test program,greeting_test, built from the same library.
Write tests #
-
In a build file, declare a test for each check. A test passes if its body succeeds, and fails if its body fails: an
assertthat doesn't hold, a program that exits with an error, or any other failure.def test greeting(prog: File = build greeting_test()) { let ran = exec([prog.path]) } def test output(prog: File = build app(), expected: File = file("test/expected_output.txt")) { let ran = exec([prog.path]) assert ran.stdout == expected.read() }Give a test what it tests through its parameters' defaults, as these do. pekit builds the defaults first, outside the test, and gives the test's step what they built. The step holds nothing else, so a test can't read a file it wasn't given.
-
Run every test below the current directory:
Check the result #
pekit test reports each test as it ended, then a count:
passed example/greeting
passed example/output
2 passed, 0 failed
It exits with status 0 if every test passed, and 1 if any failed.
Read a failure #
A failing test shows its failure's message under its name:
passed (cached) example/greeting
FAILED example/output
assertion failed: `ran.stdout == expected.read()`
1 passed, 1 failed
Each test is reported with one of four results:
| Result | Means |
|---|---|
passed | The test ran and passed. |
passed (cached) | The test passed before with the same inputs, so it didn't run. |
flaky | The test failed, then passed when run again. |
FAILED | The test failed, after any retries. The message follows. |
A test whose program fails shows the program's exit status, then what it wrote to standard output and standard error, in full:
FAILED example/greeting
`/pekit-out/.steps/link/…/greeting_test` exited with status 1
its standard error: greeting() returned "Hi", expected "Hello"
A test whose program fails to build fails too, with the build's error.
Run some tests #
Name tests to run only those, or select them with a glob, as for builds:
Naming Targets describes names and globs.
When tests run again #
A test that passed isn't run again until something it was given changes.
Change main.c, and output runs again with the rebuilt program, while
greeting, which never saw main.c, is passed (cached). A change that
rebuilds a program to the same bytes, such as a new comment, runs no test
at all.
A test that failed runs again every time, in case it was flaky.
Retry flaky tests #
#[retry(3)] runs a failing test up to three more times, each time from
the start in a fresh sandbox. A test that passes only on a retry is
reported flaky:
#[retry(3)]
def test reconnects(prog: File = build client()) {
let ran = exec([prog.path, "--self-test"])
}
#[timeout(5m)] fails a test that runs for longer than five minutes, all
its attempts together.
Tests that can't be sandboxed #
A test that needs something a step can't have, such as the network, can
say #[hermetic(false)]. It runs every time, outside a sandbox, and its
result is never reused:
#[hermetic(false)]
def test mirror_answers() {
let ran = exec(["curl", "--fail", "https://example.com/"])
}
Prefer a hermetic test wherever you can, so that a test that passed costs nothing on the next run.
Running Scripts and Programs
Pekit / Testing, Running, and Querying
Some tasks change things outside the build: deploying a site, booting an
image in a virtual machine, or copying files to a device. Write each as a
script, a definition made with script, beside your builds, and run it
with pekit run. A script can build what it needs first, exactly as
pekit build would.
Write and run a script #
-
In a build file, declare the script:
def script deploy() { let site = build site() let copied = exec(["rsync", "-a", site.path, "web:/srv/site"], interactive = true) }A script's commands run one after another, in the order the script gives them, with your terminal, environment, and network. They aren't shut away as a step is. Pass
interactive = truetoexecto give a program the terminal; otherwise pekit captures its output. -
Run it:
Check the result #
The script's programs run, and pekit run exits with status 0 if the script
succeeded. Whatever a script builds is built as pekit build builds it. The
steps of build site() are those of pekit build site, and are found in the
store if they've been built before.
Give a script arguments #
The words after a script's name are its ^script_session.args:
def script greet() {
let said = exec(["echo", "hello {^script_session.args}"], interactive = true)
}
pekit run greet there prints hello ["there"]. pekit's own options, such
as --jobs, come before the name. Everything after the name belongs to the
script.
Run a program you built #
pekit run also runs the program a build gives, when the build gives one
executable file:
pekit builds app, then runs the file it gives with the words after the
name, in the directory you ran pekit from. It exits with the program's
status. A build that gives anything else, such as a record of files, can't
be run.
Name what to run #
Name a script or a build as you name any target: deploy, or
site/deploy for the one in site. See Naming
Targets.
A build and a script of one name in one directory are an error for every
command, since pekit run couldn't tell which you meant. If two
directories each have one, name the one you mean by its directory.
Scripts and builds stay apart #
A script can run another script, as script greet(). A build, a test, or a
package can't call a script, through any number of functions. The check
fails before anything runs:
error: context `script_session` has no value in this definition
So building never deploys or boots anything. Only pekit run runs a
script.
Paths in scripts #
A script sees the workspace at /w and the build's outputs at
/pekit-out, as steps do, so the paths a build gives work in the script's
commands. The rest of the system is where it always is. pekit gives the
script that view with bubblewrap, so a program that would gain privileges,
such as sudo, can't do so from a script.
Before a script or a program runs, pekit writes every output of the build
to pekit-out/, since the program might read any of them.
Querying What Targets Reach
Pekit / Testing, Running, and Querying
pekit query evaluates targets as a build does, but runs no step. It
answers from the code alone, so it's quick, and it doesn't fill the store.
Use it to see what a target depends on, or to find the targets a change
affects.
List what a target reaches #
Run pekit query deps with the target's name:
pekit lists each step the target reaches, with the workspace files each reads, and the steps whose results each is given:
example/app
code: example/build.gcsl
step .steps/compile/57b56f08130b0576
reads: example/lib/greet.c, example/lib/greet.h, example/lib/internal.h
step .steps/archive/cbefc0a26b666e11
after: .steps/compile/57b56f08130b0576
step .steps/compile/225fcd6be0b8661d
reads: example/lib/greet.h, example/main.c
step .steps/link/7f71dca914d576d2
after: .steps/archive/cbefc0a26b666e11, .steps/compile/225fcd6be0b8661d, .steps/compile/57b56f08130b0576
code lists the build files the target's code is in. Each step is named by
its directory under pekit-out/.
Name targets as pekit build does. --kind test asks about tests instead
of builds, and --kind package about packages.
When a query can't see further #
A target's own code still runs: its fetches, its unpacks, and its reads of the workspace, which it needs to say what it builds. A target that waits for a step's result, a dynamic dependency, can't go further without running that step. The query stops there, and says so:
unknown: it waits at build.gcsl:22 for a step's result, so what it reaches after is known only once that step runs
List the targets a change reaches #
Run pekit query rdeps with a file's path, relative to the current
directory:
pekit lists each target that reads the file, one to a line. It asks about every target below the current directory, or about those you name after the file.
A GCSL file is read by every target whose code is in its directory or
below, since what it declares is visible there, whether or not anything
uses it yet. workspace.gcsl is read by every target.
A target that waits for a step's result might read the file after the wait, so it's listed too, with a note on standard error:
note: `/app` waits at build.gcsl:22 for a step's result, so it might read `main.c` after: it is listed
An unknown is never taken to mean that the file isn't read.
Run only the tests a change reaches #
In continuous integration, you can pass the list to pekit test:
If the list is empty, pekit test given no names runs every test. Check
for an empty list first if you'd rather run none.
Every test waits for the result of its own step, so pekit query rdeps --kind test lists every test, with a note for each one it can't rule out.
For tests, the list is complete, but it holds every test, so this runs
them all.