Files
blossom/tests/commands/building.sakura
T

144 lines
6.2 KiB
Plaintext
Raw Normal View History

2026-10-24 12:13:52 -05:00
# Tests of `build`, `check`, `run`, and `clean`: a program, a program with a
# library of its own, the `sakurac` run where the config names none, building
# only what changed, a check that fails, and building for Windows.
import std::io;
import std::process;
import std::testing;
@test def a_program_is_built_then_run(t: &mut tester, box: sandbox): void {
box.write("one/blossomfile", """
name: one
gpid: tests
semv: 0.1.0
targets:
- name: one
type: program
srcs: [main.sakura]
""");
box.write("one/main.sakura", """
import std::io;
def main(args: span<strview>): void {
io::println($"given {args.length}".view());
}
""");
val built := box.blossom("one", ["build"]);
t.expects(built.status == 0, built.error.view());
t.expects(built.says("[1/1] Built one (.blossom/") && built.says("Built tests/one@0.1.0"));
# The executable is where blossom said.
val output := built_path(built.output.view(), "one");
val ran := box.run($"{box.at.view()}/one/{output}".view(), "one", ["a", "b"]);
t.expects(ran.says("given 2"));
val again := box.blossom("one", ["run", "--", "x", "y", "z"]);
t.expects(again.status == 0 && again.says("given 3"));
}
@test def a_program_reads_a_library_of_its_project(t: &mut tester, box: sandbox): void {
box.hello("hello");
val built := box.blossom("hello", ["build"]);
t.expects(built.status == 0, built.error.view());
t.expects(built.says("[1/2] Checked hello.geometry") && built.says("[2/2] Built hello"));
val ran := box.blossom("hello", ["run", "--", "1"]);
t.expects(ran.says("area 12 cube 8 given 1"));
# `check` checks each target and writes nothing.
box.hello("other");
val checked := box.blossom("other", ["check"]);
t.expects(checked.status == 0 && checked.says("[1/2] Checked hello") && checked.says("[2/2] Checked hello.geometry"));
t.expects(!box.has("other/.blossom"));
}
# Where the config names no `sakura`, blossom runs the first `sakurac` on
# `PATH`, here a link to the tests' own, as none is beside it.
@test def a_config_naming_no_sakura_runs_sakurac(t: &mut tester, box: sandbox): void {
val sakura := process::variable("SAKURA") else { return; };
box.write("config/blossom/config", std_line().view());
box.hello("hello");
val missing := box.blossom("hello", ["build"], "/usr/bin:/bin");
t.expects(missing.status == 2 && missing.warns("No `sakurac` is found"), missing.error.view());
box.write("bin/.keep", "");
val linked := box.run("ln", "bin", ["-s", sakura, "sakurac"]);
t.expects(linked.status == 0, linked.error.view());
val built := box.blossom("hello", ["build"], $"{box.at.view()}/bin:/usr/bin:/bin".view());
t.expects(built.status == 0 && built.says("[2/2] Built hello"), built.error.view());
}
@test def only_what_changed_is_built_again(t: &mut tester, box: sandbox): void {
box.hello("hello");
t.expects(box.blossom("hello", ["build"]).status == 0);
# Nothing changed: `sakura` isn't run.
val again := box.blossom("hello", ["build", "-v"]);
t.expects(again.says("[1/2] Up to date hello.geometry") && again.says("[2/2] Up to date hello"));
t.expects(!again.says("sakura"));
# A file of the library: it's checked again, and the program built again.
box.write("hello/hello.geometry/solids/solids.sakura", """
module hello::geometry::solids;
pub def cube(side: i64): i64 { return side * side * side + 0; }
""");
val touched := box.blossom("hello", ["build"]);
t.expects(touched.says("[1/2] Checked hello.geometry") && touched.says("[2/2] Built hello"));
# The program's own file: only it's built again.
box.write("hello/main.sakura", box.read("hello/main.sakura").view());
val program := box.blossom("hello", ["build"]);
t.expects(program.says("[1/2] Up to date hello.geometry") && program.says("[2/2] Built hello"));
# Another option value builds in a folder of its own, and the first stays up
# to date.
val other := box.blossom("hello", ["build", "-o", "level=0"]);
t.expects(other.says("[2/2] Built hello") && built_path(other.output.view(), "hello") != built_path(program.output.view(), "hello"));
t.expects(box.blossom("hello", ["build"]).says("[2/2] Up to date hello"));
# Changing the blossomfile builds again.
val text := box.read("hello/blossomfile");
box.write("hello/blossomfile", $"{text.view()}\n".view());
t.expects(box.blossom("hello", ["build"]).says("[2/2] Built hello"));
# `clean` removes this option set's outputs, and `--all` every set's.
val cleaned := box.blossom("hello", ["clean"]);
t.expects(cleaned.status == 0 && cleaned.says("Cleaned tests/hello@1.0.0"));
t.expects(box.has("hello/.blossom") && box.blossom("hello", ["clean"]).says("Nothing to clean"));
t.expects(box.blossom("hello", ["clean", "--all"]).status == 0 && !box.has("hello/.blossom"));
}
@test def a_program_that_fails_to_check_fails_the_build(t: &mut tester, box: sandbox): void {
box.hello("hello");
box.write("hello/main.sakura", """
def main(): void {
val wrong: i64 = "text";
}
""");
val built := box.blossom("hello", ["build"]);
t.expects(built.status == 1 && built.says("Failed hello") && !built.says("Built tests/hello"));
t.expects(built.error.view().find("main.sakura:2") is some, built.error.view());
# A library that fails to check skips the programs reading it.
box.hello("broken");
box.write("broken/hello.geometry/shapes.sakura", """
module hello::geometry;
pub def area(w: i64, h: i64): i64 { return "no"; }
""");
val skipped := box.blossom("broken", ["build"]);
t.expects(skipped.status == 1 && skipped.says("Failed hello.geometry") && skipped.says("Skipped hello"));
# `check` checks every target, so the program fails there too.
val checked := box.blossom("broken", ["check"]);
t.expects(checked.status == 1 && checked.says("Failed hello\n") && checked.says("Failed hello.geometry\n"), checked.output.view());
}
@test def a_target_builds_an_executable_for_windows(t: &mut tester, box: sandbox): void {
box.hello("hello");
val built := box.blossom("hello", ["build", "--target", "windows-x86_64"]);
t.expects(built.status == 0, built.error.view());
val output := built_path(built.output.view(), "hello");
t.expects(output.ends_with("/bin/hello.exe"), output);
t.expects(box.read($"hello/{output}".view()).view().starts_with("MZ"));
}