Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-14 15:31:15 -06:00
commit ab8208d6c6
28 files changed
+6914

No files matched your search

+181
View File
@@ -0,0 +1,181 @@
# 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}".span);
}
""");
val built := box.blossom("one", ["build"]);
t.expects(built.status == 0, built.error.span);
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.span, "one");
val ran := box.run($"{box.at.span}/one/{output}".span, "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.span);
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. Windows links
# only for those allowed to, and a copy finds no `core` beside it, so there
# `PATH` names the folder of the tests' own, where it's a `sakurac`.
@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().span);
box.hello("hello");
val missing := box.blossom("hello", ["build"], box.search_path().span);
t.expects(missing.status == 2 && missing.warns("No `sakurac` is found"), missing.error.span);
var found := string::new(nothing {});
when os == windows {
val named := path::parse(sakura) else (_) { return; };
return if !named.span.ends_with("sakurac.exe");
val folder := named.parent() else { return; };
found = $"{folder.span};{system_programs()}";
}
else {
box.write("bin/.keep", "");
val linked := box.run("ln", "bin", ["-s", sakura, "sakurac"]);
t.expects(linked.status == 0, linked.error.span);
found = box.search_path("bin");
}
val built := box.blossom("hello", ["build"], found.span);
t.expects(built.status == 0 && built.says("[2/2] Built hello"), built.error.span);
}
@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").span);
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.span, "hello") != built_path(program.output.span, "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.span}\n".span);
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.span.find("main.sakura:2") is some, built.error.span);
# 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.span);
}
# What a program for Windows is shown by is given to `sakura`, which holds it in
# the executable: its version, title, icon, and window, an SVG being Linux's.
@test def a_program_for_windows_is_built_with_its_icon_title_and_window(t: &mut tester, box: sandbox): void {
box.shown("shown");
val built := box.blossom("shown", ["build", "--target", "windows-x86_64", "-v"]);
t.expects(built.status == 0, built.error.span);
t.expects(built.says("--semv 2.1.0 --title 'Shown Off' --icon icons/shown.png --windowed"), built.output.span);
t.expects(!built.says("shown.svg"));
val output := built_path(built.output.span, "shown");
val exe := box.read($"shown/{output}".span);
val header := word_of(exe.span, 0x3C, 4);
t.expects(word_of(exe.span, header + 24 + 68, 2) == 2);
t.expects(word_of(exe.span, header + 24 + 112 + 16, 4) != 0);
# Given a console by its option, it's built again, in one.
val console := box.blossom("shown", ["build", "--target", "windows-x86_64", "-v", "-o", "windowed=false"]);
t.expects(console.status == 0 && !console.says("--windowed"), console.error.span);
val again := box.read($"shown/{built_path(console.output.span, "shown")}".span);
t.expects(word_of(again.span, word_of(again.span, 0x3C, 4) + 24 + 68, 2) == 3);
# Built for Linux, it's given none of it.
val linux := box.blossom("shown", ["build", "--target", "linux-x86_64", "-v"]);
t.expects(linux.status == 0 && !linux.says("--title") && !linux.says("--icon"), linux.error.span);
}
@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.span);
val output := built_path(built.output.span, "hello");
t.expects(output.ends_with("/bin/hello.exe"), output);
t.expects(box.read($"hello/{output}".span).span.starts_with("MZ"));
}