121 lines
4.0 KiB
Plaintext
121 lines
4.0 KiB
Plaintext
# Runs the tests of `system`, from the root of the repository, with the
|
|
# standard library's repository beside this one. `blossom test` runs them as
|
|
# `system.tests`, or:
|
|
#
|
|
# sakurac run --lib system=system --lib std=../std tests/*.sakura
|
|
#
|
|
# Add `-- --only TEXT` to run those whose name holds the text. Each test runs
|
|
# in a copy of the program, so what one does with a signal is its own.
|
|
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);
|
|
}
|
|
val status := copy_quietly();
|
|
copies_seen.append(
|
|
$"{status.exited ? 1 : 0},{status.code},{status.signal};".span
|
|
);
|
|
return status;
|
|
}
|
|
|
|
# Starts a copy of the program writing nothing, told how the copies before it
|
|
# stopped, and gives how it stopped.
|
|
def copy_quietly(): system::process_status {
|
|
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);
|
|
}
|
|
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(';') {
|
|
continue if entry.length == 0;
|
|
seen++;
|
|
continue 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;
|
|
}
|
|
}
|