Files
std/tests/main.sakura
T
2026-11-14 15:32:55 -06:00

112 lines
4.1 KiB
Plaintext

# Runs the tests of the standard library, from the root of its repository, where
# the tests that read files find them. `blossom test` runs each module's as a test
# of its own, as `std.io.tests`, or, for those of `std::io`:
#
# sakurac run --lib std=. tests/main.sakura tests/io/*.sakura tests/io/glob/*.sakura
#
# Add `-- --only TEXT` to run those whose name holds the text, and `-- --here` to
# run them in one program, as a debugger wants. Built with `sakura build`, they run
# as an executable the same way.
import std::io;
import std::process;
import system;
import std::testing;
def main(args: span<strview>): void {
exit(tester::run(args));
}
# Whether running the function stops the program. It runs in a copy of the program
# that writes nothing, so that this one goes on and the report of the stop is not
# mixed into the tests'.
def stops(run: def(): void): bool {
when os == windows {
val stopped := copied(run);
return !(stopped.exited && stopped.code == 0);
}
val child := fork();
if child == 0 {
val out: fd = fd::stdout;
val err: fd = fd::stderr;
out.close();
err.close();
run();
exit(0);
}
var status: process_status;
return wait(child, &mut status) >= 0 && !(status.exited && status.code == 0);
}
# Windows cannot copy a program as it runs, as `system::fork` does, so what the tests
# run in a copy of their own runs there in a copy started again, as `std::testing`
# starts the copy each test runs in.
when os == windows {
# The variable a copy is given, listing how the copies before it stopped.
val copy_variable: strview = "SAKURA_TEST_COPY";
# How many functions `copied` has been asked to run, in this program or a copy,
# and how the copies running each stopped, as `exited,code,signal;` each.
var copies_asked: u64 = 0;
var copies_seen: string = string::new(nothing{});
# Runs a function in a copy of the program, giving how the copy stopped. The
# copy runs the test that asked again, writing nothing, and is told how each
# copy before it stopped, which it gives where it is asked for them, so it goes
# the way the test went here, though a check that fails asks again. It runs the
# function asked for after them, then exits with zero.
def copied(run: def(): void): system::process_status {
copies_asked++;
if process::variable(copy_variable) is some(given) {
if replayed(given, copies_asked) is some(status) {
return status;
}
run();
system::exit(0);
}
var device := path::parse(system::null_device) else (_) { return system::process_status {}; };
var quiet := io::file::open(&device, open_flags::write) else (_) { return system::process_status {}; };
val copy := system::respawn($"{copy_variable}={copies_seen.span}".span, system::fd::stdin, quiet.handle, quiet.handle);
var status := system::process_status { exited = true, code = -1 };
if copy > 0 {
_ = system::wait(copy, &mut status);
}
copies_seen.append($"{status.exited ? 1 : 0},{status.code},{status.signal};".span);
return status;
}
# How the copy running the function asked for at a count stopped, as a copy's
# variable lists it, or nothing for the one after those it lists.
def replayed(given: strview, count: u64): maybe<system::process_status> {
var seen := 0u;
for entry in given.split(';') {
if entry.length == 0 {
continue;
}
seen++;
if seen == count {
val parts := entry.split(',').to_vector();
return system::process_status {
exited = parts[0] == "1",
code = number_in(parts[1]) as i32,
signal = number_in(parts[2]) as i32
};
}
}
return nothing{};
}
# The integer text says, or zero where it says none.
def number_in(text: strview): i64 {
if integer::parse::<i64>(text) is value(number) {
return number;
}
return 0;
}
# A directory files may be made in for a while, as the system's `TEMP` names it.
def temporary(): strview {
return process::variable("TEMP") else { return process::variable("TMP") else { return "."; }; };
}
}