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"));
}
+150
View File
@@ -0,0 +1,150 @@
# Tests of `test`, `install`, and `package`: tests passing, failing, run
# natively, and suites and tests chosen by name; installing into a prefix and
# under a folder; and packages of tar and zip.
import std::io;
import std::testing;
@test def tests_run_and_say_how_many_passed(t: &mut tester, box: sandbox): void {
box.hello("hello");
val tested := box.blossom("hello", ["test"]);
t.expects(tested.status == 0 && tested.says("[3/3] Tested hello.tests (2 passed)") && tested.says("[ OK ] cubes_cube"), tested.error.span);
t.expects(tested.says("Tested tests/hello@1.0.0"));
}
@test def a_test_that_fails_fails_the_command(t: &mut tester, box: sandbox): void {
box.hello("hello");
box.write("hello/tests/wrong.sakura", """
import std::testing;
@test def nothing_is_something(t: &mut tester): void { t.expects(1 == 2); }
""");
val tested := box.blossom("hello", ["test"]);
t.expects(tested.status == 1 && tested.says("Failed hello.tests") && !tested.says("Tested tests/hello"));
}
@test def tests_run_natively_by_name_with_what_theyre_given(t: &mut tester, box: sandbox): void {
box.hello("hello", """
- name: given.tests
type: program
runs: native
srcs: [given/main.sakura]
args: ["${bin}", second]
""");
box.write("hello/given/main.sakura", """
import std::io;
import system;
def main(args: span<strview>): void {
io::println($"given {args[0]} {args[1]}".span);
exit(args[0].ends_with("bin") && args[1] == "second" ? 0 : 1);
}
""");
val tested := box.blossom("hello", ["test", "--suite", "given"]);
t.expects(tested.status == 0 && tested.says("[3/3] Tested given.tests") && !tested.says("hello.tests"), tested.output.span);
t.expects(tested.says($"given {box.path_of("hello/.blossom")}".span));
t.expects(box.blossom("hello", ["test", "--suite", "none"]).status == 2);
}
@test def tests_are_chosen_by_name_within_the_suites_run(t: &mut tester, box: sandbox): void {
box.hello("hello");
# Each suite is given `--only cubes`, so it runs that test alone.
val tested := box.blossom("hello", ["test", "--test", "cubes"]);
t.expects(tested.status == 0 && tested.says("[ OK ] cubes_cube") && !tested.says("areas_multiply"), tested.output.span);
t.expects(tested.says("[3/3] Tested hello.tests (1 passed)"), tested.output.span);
# Within the suites chosen.
val both := box.blossom("hello", ["test", "--suite", "hello", "--test", "areas"]);
t.expects(both.status == 0 && both.says("[ OK ] areas_multiply") && !both.says("cubes_cube"), both.output.span);
# A test named in no suite run is a mistake in the command, as a suite is.
val none := box.blossom("hello", ["test", "--test", "spheres"]);
t.expects(none.status == 2 && none.says("[3/3] Tested hello.tests (none named with `spheres`)"), none.output.span);
t.expects(none.error.span.find("No test of tests/hello@1.0.0 is named with `spheres`.") is some, none.error.span);
}
@test def install_puts_programs_libraries_and_steps_in_the_prefix(t: &mut tester, box: sandbox): void {
box.hello("hello", """
exports:
- hello.geometry
install:
- !include ./share
- !include ./docs:share/doc/hello
- !include ./notes.txt
- !include ./notes.txt:etc/hello.txt
""");
box.write("hello/share/data.txt", "data\n");
box.write("hello/docs/README.md", "# Hello\n");
box.write("hello/notes.txt", "notes\n");
val installed := box.blossom("hello", ["install", "--prefix", $"{box.at.span}/prefix".span]);
t.expects(installed.status == 0 && installed.says("Installed tests/hello@1.0.0 (7 files)"), installed.error.span);
val hello := program("hello");
t.expects(box.has($"prefix/bin/{hello}".span), hello.span);
for name in ["prefix/share/data.txt", "prefix/share/doc/hello/README.md", "prefix/notes.txt", "prefix/etc/hello.txt",
"prefix/share/sakura/hello.geometry/shapes.sakura", "prefix/share/sakura/hello.geometry/solids/solids.sakura"] {
t.expects(box.has(name), name);
}
val ran := box.run(box.path_of($"prefix/bin/{hello}".span).span, ".", span::<strview>::blank());
t.expects(ran.says("area 12"));
# Under a folder, as a package is staged.
val staged := box.blossom("hello", ["install", "--prefix", "/usr/local", "--destdir", box.path_of("stage").span]);
t.expects(staged.status == 0 && box.has($"stage/usr/local/bin/{hello}".span), staged.error.span);
}
# A program for Linux shown as a desktop's program is installed with its icons
# in the icon theme and its desktop entry; one for Windows holds them itself.
@test def install_puts_a_desktop_entry_and_icons_beside_a_program_for_linux(t: &mut tester, box: sandbox): void {
box.shown("shown");
val installed := box.blossom("shown", ["install", "--target", "linux-x86_64", "--prefix", $"{box.at.span}/prefix".span]);
t.expects(installed.status == 0 && installed.says("Installed tests/shown@2.1.0 (4 files)"), installed.error.span);
t.expects(box.has("prefix/bin/shown"));
t.expects(box.has("prefix/share/icons/hicolor/32x32/apps/shown.png"));
t.expects(box.has("prefix/share/icons/hicolor/scalable/apps/shown.svg"));
val entry := box.read("prefix/share/applications/shown.desktop");
t.expects(entry.span.starts_with("[Desktop Entry]\nType=Application\nName=Shown Off\nExec="), entry.span);
t.expects(entry.span.find("/prefix/bin/shown\nIcon=shown\nTerminal=false\nStartupWMClass=shown\n") is some, entry.span);
t.expects(entry.span.find("\\") is none);
# In a console, it runs in a terminal.
val console := box.blossom("shown", ["install", "--target", "linux-x86_64", "-o", "windowed=false", "--prefix", $"{box.at.span}/console".span]);
t.expects(console.status == 0 && box.read("console/share/applications/shown.desktop").span.find("Terminal=true\n") is some, console.error.span);
# A package names it where the package installs it.
val packaged := box.blossom("shown", ["package", "-f", "tar", "--target", "linux-x86_64"]);
t.expects(packaged.status == 0, packaged.error.span);
val dist := box.dist("shown");
val stage := $"{dist.span.substr(0, dist.length - 4)}stage/share/applications/shown.desktop";
val parsed := path::parse(stage.span) else (_) { return; };
val staged := io::read_file(&parsed) else (_) {
t.expects(false, stage.span);
return;
};
t.expects(staged.span.find("Exec=/usr/bin/shown\n") is some, staged.span);
# For Windows, nothing's put beside it.
val windows := box.blossom("shown", ["install", "--target", "windows-x86_64", "--prefix", $"{box.at.span}/windows".span]);
t.expects(windows.status == 0 && box.has("windows/bin/shown.exe") && !box.has("windows/share"), windows.error.span);
}
# A package for Linux, made on any system, compressed by the `gzip` on `PATH`.
@test def packages_hold_what_installing_puts_in_the_prefix(t: &mut tester, box: sandbox): void {
box.hello("hello", "install:\n- !include ./notes.txt\n");
box.write("hello/notes.txt", "notes\n");
val packaged := box.blossom("hello", ["package", "-f", "tar,zip", "--target", "linux-x86_64"]);
t.expects(packaged.status == 0 && packaged.says("hello-1.0.0-linux-x86_64.tar.gz") && packaged.says("hello-1.0.0-linux-x86_64.zip"), packaged.error.span);
val dist := box.dist("hello");
val tar := box.path_of($"{dist}/hello-1.0.0-linux-x86_64.tar.gz".span);
val listed := box.run("tar", "hello", ["-tzf", tar.span]);
t.expects(lines_of(listed.output.span) == "hello-1.0.0-linux-x86_64/\nhello-1.0.0-linux-x86_64/bin/\nhello-1.0.0-linux-x86_64/bin/hello\nhello-1.0.0-linux-x86_64/notes.txt\n", listed.output.span);
val zip := box.read($"{dist}/hello-1.0.0-linux-x86_64.zip".span);
t.expects(zip.span.starts_with("PK"));
# The same stage makes the same bytes.
val first := box.read(tar.span);
_ = box.blossom("hello", ["package", "-f", "tar,zip", "--target", "linux-x86_64"]);
t.expects(first.length != 0 && box.read(tar.span) == first && box.read($"{dist}/hello-1.0.0-linux-x86_64.zip".span) == zip);
# A system's package is refused for Windows before anything's built.
val windows := box.blossom("hello", ["package", "-f", "deb", "--target", "windows-x86_64"]);
t.expects(windows.status == 2 && windows.warns("deb packages are for Linux") && !windows.says("Built"));
}
+125
View File
@@ -0,0 +1,125 @@
# Tests of `libraries`, the `--lib` of each library a project holds or reads,
# its folder absolute, in the order found, which names what the store is missing
# rather than cloning it and refuses what a build refuses; and of `pull`, which
# clones what a project requires, or what's named, or with `--update` clones it
# again.
import std::io;
import std::testing;
@test def libraries_lists_what_a_project_holds_and_reads(t: &mut tester, box: sandbox): void {
# Its own library, then what that reads, from a requirement given options;
# and no `sakura` is needed, since none is run.
box.shapes("shapes");
box.write("app/blossomfile", """
name: app
gpid: tests
semv: 1.0.0
requires:
- remote: ../shapes
options:
flavor: fancy
targets:
- name: app
type: program
deps: [std, app.parts]
srcs: [main.sakura]
- name: app.parts
type: library
deps: [shapes.basic]
tests:
- name: app.tests
type: program
deps: [app.parts, shapes.basic]
""");
box.write("config/blossom/config", "");
val listed := box.blossom("app", ["libraries"], box.search_path().span);
val want := $"--lib app::parts={box.path_of("app/app.parts")}\n--lib shapes::basic={box.path_of("shapes/fancy")}\n";
t.expects(listed.status == 0 && listed.output == want, $"{listed.output.span}{listed.error.span}".span);
# Given another folder, it lists that project's.
val other := box.blossom(".", ["libraries", "shapes"], box.search_path().span);
t.expects(other.status == 0 && other.output == $"--lib shapes::basic={box.path_of("shapes/plain")}\n", other.output.span);
}
@test def libraries_refuses_what_a_build_refuses(t: &mut tester, box: sandbox): void {
box.shapes("hidden", false);
box.app("private", "- ../hidden");
val private := box.blossom("private", ["libraries"]);
t.expects(private.status == 2 && private.output.length == 0, private.output.span);
t.expects(private.warns("`shapes.basic`, which `app` reads, isn't exported by tests/shapes@1.0.0."), private.error.span);
}
# Writes a project holding a library, `colors.basic`, and makes a bare git
# repository of it, `colors.git`, as `remote` does.
def sandbox::colors(&this): bool {
this.write("colors/blossomfile", "name: colors\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: colors.basic\n type: library\nexports:\n- colors.basic\n");
this.write("colors/colors.basic/basic.sakura", "module colors::basic;\n\npub def hue(): strview { return \"red\"; }\n");
return this.remote("colors", "colors.git");
}
@test def libraries_names_what_the_store_is_missing_and_clones_nothing(t: &mut tester, box: sandbox): void {
box.shapes("source");
t.expects(box.remote("source", "shapes.git"));
val shapes := $"{box.url("shapes.git")}@v1.0.0";
box.app("app", $"- {shapes}".span);
val listed := box.blossom("app", ["libraries"]);
t.expects(listed.status == 0 && listed.output == $"--missing {shapes}\n", listed.output.span);
t.expects(listed.warns("1 requirement isn't in the store") && !listed.says("Cloning"), listed.error.span);
t.expects(box.stored("shapes@v1.0.0").length == 0);
}
@test def pull_clones_what_a_project_requires_or_what_is_named(t: &mut tester, box: sandbox): void {
box.shapes("source");
t.expects(box.remote("source", "shapes.git") && box.colors());
val shapes := $"{box.url("shapes.git")}@v1.0.0";
val colors := $"{box.url("colors.git")}@v1.0.0";
box.write("app/blossomfile", $"name: app\ngpid: tests\nsemv: 1.0.0\nrequires:\n- {shapes.span}\n- {colors.span}\ntargets:\n- name: app.parts\n type: library\n deps: [shapes.basic, colors.basic]\n".span);
# One named by the last part of its path.
val one := box.blossom("app", ["pull", "shapes"]);
val stored := box.stored("shapes@v1.0.0");
t.expects(one.status == 0 && stored.length != 0 && one.says($"[1/1] Cloned {shapes.span} ({stored})".span), $"{one.output.span}{one.error.span}".span);
t.expects(box.stored("colors@v1.0.0").length == 0);
val plain := box.path_of($"{stored}/plain".span);
val listed := box.blossom("app", ["libraries"]);
t.expects(listed.output == $"--lib app::parts={box.path_of("app/app.parts")}\n--lib shapes::basic={plain}\n--missing {colors.span}\n", listed.output.span);
# Everything, what's there already said to be.
val all := box.blossom("app", ["pull"]);
t.expects(all.status == 0 && all.says($"[1/2] Present {shapes.span}".span) && all.says($"[2/2] Cloned {colors.span}".span), all.output.span);
t.expects(all.says("Pulled what tests/app@1.0.0 requires"));
t.expects(box.blossom("app", ["libraries"]).output.span.find("--missing") is none);
# A name of nothing it requires.
val none := box.blossom("app", ["pull", "sizes"]);
t.expects(none.status == 2 && none.warns($"`sizes` is no requirement of tests/app@1.0.0: it requires `{shapes.span}`, `{colors.span}`.".span), none.error.span);
}
@test def pull_update_clones_a_branch_that_moved_again(t: &mut tester, box: sandbox): void {
box.shapes("source");
t.expects(box.remote("source", "shapes.git"));
box.app("app", $"- {box.url("shapes.git")}".span);
t.expects(box.blossom("app", ["pull"]).says("Cloned"));
val stored := box.stored("shapes@main");
t.expects(stored.length != 0);
# The branch moves; a pull leaves the store's clone as it is.
box.write("source/plain/basic.sakura", "module shapes::basic;\n\npub def side(): strview { return \"moved\"; }\n");
val bare := box.path_of("shapes.git");
t.expects(box.git("source", ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-am", "two"])
&& box.git("source", ["push", "-q", bare.span, "main"]));
t.expects(box.blossom("app", ["pull"]).says("Present"));
t.expects(box.read($"{stored.span}/plain/basic.sakura".span).span.find("plain") is some);
# With `--update`, it's cloned again, and nothing is left beside it.
val updated := box.blossom("app", ["pull", "--update"]);
t.expects(updated.status == 0 && updated.says("again") && updated.says("[1/1] Updated"), $"{updated.output.span}{updated.error.span}".span);
t.expects(box.read($"{stored.span}/plain/basic.sakura".span).span.find("moved") is some);
t.expects(!box.has($"{stored.span}.old".span) && !box.has($"{stored.span}.partial".span));
val ran := box.blossom("app", ["run"]);
t.expects(ran.status == 0 && ran.says("side moved"), ran.error.span);
}
+45
View File
@@ -0,0 +1,45 @@
# Runs blossom's tests of its commands. Each makes projects in a folder of its
# own, runs the blossom it's given first on them, and compares what it says and
# what it makes with what's expected. blossom's config in that folder names the
# `sakura` that `SAKURA` names, where it's set, as `blossom test` sets it, and
# as its `std` the standard library blossom's own project reads, as `blossom
# libraries` lists it run where the tests are, so they don't depend on a std
# found beside that `sakura`:
#
# blossom test --suite blossom.commands
#
# with `--test TEXT` to run those whose names hold the text; or, from the root
# of the repository, with a blossom built:
#
# SAKURA=SAKURA sakura run tests/commands/*.sakura -- BLOSSOM
#
# Add `--only TEXT` after BLOSSOM to run those whose name holds the text, and
# `--here` to run them in one program, as a debugger wants. Each test's folder
# is in the system's temporary folder, and blossom's config and store are the
# folder's, on Linux and on Windows alike. They run `git` and `tar`, and blossom
# runs `gzip` to package, each found on `PATH`.
import std::io;
import std::process;
import system;
import std::testing;
def main(args: span<strview>): void {
if args.length == 0 || args[0].starts_with("-") {
io::println("blossom's tests are given the blossom they run first, as `-- .blossom/KEY/bin/blossom`.");
exit(2);
}
if process::variable("SAKURA") is none {
io::println("blossom's tests run the `sakura` that `SAKURA` names, as `blossom test` sets it.");
exit(2);
}
blossom_at = args[0];
var listing := process::command::new(blossom_at);
listing.arguments(["libraries"]);
val listed := listing.capture() else (why) {
io::println($"blossom's tests can't run `{blossom_at} libraries`: {why}".span);
exit(2);
return;
};
std_at = std_folder(listed.output.span);
exit(tester::run(args));
}
+203
View File
@@ -0,0 +1,203 @@
# Tests of members: folders of a project holding blossomfiles of their own,
# which say whose members they are, read as one project with it, from its
# folder or a member's; a library's files, but its member's tests'; members
# that say nothing, or another project, or that no project lists; what a member
# requires, from its own folder; and a project requiring one of members.
import std::io;
import std::testing;
# Writes a project, `shop`, of two members: `shapes`, a library, `shop.shapes`,
# whose folder is the member's own, and its tests beside it in `tests`; and
# `app`, a program reading it.
def sandbox::shop(&this, folder: strview): void {
this.write($"{folder}/blossomfile".span, """
name: shop
gpid: tests
semv: 1.0.0
members:
- shapes
- ./app/
exports:
- shop.shapes
""");
this.write($"{folder}/shapes/blossomfile".span, """
member: shop
targets:
- name: shop.shapes
type: library
root: .
tests:
- name: shop.shapes.tests
type: program
deps: [shop.shapes]
""");
this.write($"{folder}/shapes/area.sakura".span, """
module shop::shapes;
pub def area(w: i64, h: i64): i64 { return w * h; }
""");
this.write($"{folder}/shapes/tests/main.sakura".span, """
import system;
import std::testing;
import shop::shapes;
def main(args: span<strview>): void { exit(tester::run(args)); }
@test def areas_multiply(t: &mut tester): void { t.expects(shapes::area(2, 5) == 10); }
""");
this.write($"{folder}/app/blossomfile".span, """
member: shop
targets:
- name: shop
type: program
deps: [std, shop.shapes]
srcs: [main.sakura]
""");
this.write($"{folder}/app/main.sakura".span, """
import std::io;
import shop::shapes;
def main(args: span<strview>): void {
io::println($"area {shapes::area(3, 4)}".span);
}
""");
}
@test def members_are_built_and_tested_as_one_project(t: &mut tester, box: sandbox): void {
box.shop("shop");
val tested := box.blossom("shop", ["test"]);
t.expects(tested.status == 0 && tested.says("[1/3] Checked shop.shapes") && tested.says("Built shop"), tested.output.span);
t.expects(tested.says("[3/3] Tested shop.shapes.tests (1 passed)") && tested.says("Tested tests/shop@1.0.0"), tested.output.span);
# The library's files are its member's, but those its tests list.
val checked := box.blossom("shop", ["check", "-v"]);
t.expects(checked.status == 0 && checked.says("area.sakura"), checked.output.span);
t.expects(!checked.says("tests/main.sakura") && !checked.says("tests\\main.sakura"), checked.output.span);
# Its program, of the member's file, runs as the project's.
val ran := box.blossom("shop", ["run"]);
t.expects(ran.status == 0 && ran.says("area 12"), ran.output.span);
}
@test def a_members_folder_keeps_to_the_member(t: &mut tester, box: sandbox): void {
box.shop("shop");
val tested := box.blossom("shop/shapes", ["test"]);
t.expects(tested.status == 0 && tested.says("[2/2] Tested shop.shapes.tests (1 passed)") && !tested.says("Built shop"), tested.output.span);
# Its outputs are the project's.
t.expects(box.has("shop/.blossom") && !box.has("shop/shapes/.blossom"));
# Its mistakes name it.
val none := box.blossom("shop/app", ["test"]);
t.expects(none.status == 2 && none.error.span.find("app of tests/shop@1.0.0 has no tests.") is some, none.error.span);
val ran := box.blossom("shop/app", ["run"]);
t.expects(ran.status == 0 && ran.says("area 12"), ran.output.span);
}
@test def members_say_whose_they_are(t: &mut tester, box: sandbox): void {
box.shop("shop");
box.write("shop/app/blossomfile", """
targets: []
""");
val unsaid := box.blossom("shop", ["build"]);
t.expects(unsaid.status == 2 && unsaid.error.span.find("`member` is needed.") is some, unsaid.error.span);
box.write("shop/app/blossomfile", """
member: market
""");
val other := box.blossom("shop", ["build"]);
t.expects(other.status == 2 && other.error.span.find("member of `market`, but `shop` lists it") is some, other.error.span);
# A member's blossomfile is never a project's of its own.
box.write("shop/app/blossomfile", """
member: shop
name: app
""");
val named := box.blossom("shop", ["build"]);
t.expects(named.status == 2 && named.error.span.find("`name` is no key of a member's blossomfile.") is some, named.error.span);
# One no project lists is read by no command.
box.shop("shop");
box.write("shop/loose/blossomfile", """
member: shop
""");
val loose := box.blossom("shop/loose", ["build"]);
t.expects(loose.status == 2 && loose.error.span.find("no blossomfile above it lists its folder in `members`") is some, loose.error.span);
# Members name their targets apart from each other.
box.write("shop/app/blossomfile", """
member: shop
targets:
- name: shop.shapes
type: library
""");
val twice := box.blossom("shop", ["build"]);
t.expects(twice.status == 2 && twice.error.span.find("`shop.shapes` names two targets") is some, twice.error.span);
}
@test def a_member_requires_from_its_own_folder(t: &mut tester, box: sandbox): void {
box.shop("shop");
box.write("extra/blossomfile", """
name: extra
gpid: tests
semv: 1.0.0
targets:
- name: extra.numbers
type: library
exports:
- extra.numbers
""");
box.write("extra/extra.numbers/twice.sakura", """
module extra::numbers;
pub def twice(n: i64): i64 { return n * 2; }
""");
box.write("shop/shapes/blossomfile", """
member: shop
requires:
- ../../extra
targets:
- name: shop.shapes
type: library
root: .
deps: [extra.numbers]
""");
box.write("shop/shapes/area.sakura", """
import extra::numbers;
module shop::shapes;
pub def area(w: i64, h: i64): i64 { return numbers::twice(w * h) / 2; }
""");
val built := box.blossom("shop", ["build"]);
t.expects(built.status == 0 && built.says("Built shop"), built.error.span);
}
@test def a_project_requiring_one_of_members_reads_their_libraries(t: &mut tester, box: sandbox): void {
box.shop("shop");
box.write("user/blossomfile", """
name: user
gpid: tests
semv: 1.0.0
requires:
- ../shop
targets:
- name: user
type: program
deps: [std, shop.shapes]
srcs: [main.sakura]
""");
box.write("user/main.sakura", """
import std::io;
import shop::shapes;
def main(args: span<strview>): void {
io::println($"used {shapes::area(2, 3)}".span);
}
""");
val built := box.blossom("user", ["run"]);
t.expects(built.status == 0 && built.says("used 6"), built.error.span);
}
+93
View File
@@ -0,0 +1,93 @@
# Tests of what blossom refuses, each with status 2 and a line saying what's
# wrong and where: a key written twice, keys and options it doesn't know, a
# step of `install` without a tag it knows, and a folder with no blossomfile.
import std::testing;
# Writes a project whose blossomfile is the text given, with a program to build.
def sandbox::project(&this, folder: strview, blossomfile: strview): void {
this.write($"{folder}/blossomfile".span, blossomfile);
this.write($"{folder}/main.sakura".span, "def main(): void {}\n");
}
@test def a_key_written_twice_is_refused_by_its_line(t: &mut tester, box: sandbox): void {
box.project("twice", """
name: twice
gpid: tests
semv: 1.0.0
requires:
- remote: https://example.com/one
branch: v1.0.0
remote: https://example.com/two
branch: v1.0.0
""");
val built := box.blossom("twice", ["build"]);
t.expects(built.status == 2 && built.warns("blossomfile:8: `remote` is written twice in a requirement."), built.error.span);
}
@test def keys_options_and_targets_blossom_doesnt_know_are_refused(t: &mut tester, box: sandbox): void {
box.project("keys", """
name: keys
gpid: tests
semv: 1.0.0
targets:
- name: keys
type: program
srcs: [main.sakura]
optlevel: 2
""");
t.expects(box.blossom("keys", ["build"]).warns("blossomfile:9: `optlevel` is no key of a target."));
box.project("options", """
name: options
gpid: tests
semv: 1.0.0
options:
level: 2
targets:
- name: options
type: program
level: ${nope}
srcs: [main.sakura]
""");
val unknown := box.blossom("options", ["build"]);
t.expects(unknown.status == 2 && unknown.warns("blossomfile:11: `${nope}` names no option declared before it."), unknown.error.span);
val bad := box.blossom("keys", ["build", "-o", "nope=1"]);
t.expects(bad.status == 2 && bad.warns("`nope`, given a value, names no option of tests/keys@1.0.0."));
t.expects(box.blossom("keys", ["build", "--target", "mars"]).status == 2);
t.expects(box.blossom("keys", ["frobnicate"]).status == 2);
t.expects(box.blossom("keys", ["build", "--no-such-option"]).status == 2);
box.project("version", "name: version\ngpid: tests\nsemv: one\n");
t.expects(box.blossom("version", ["build"]).warns("`one` is not a version"));
box.project("level", "name: level\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: level\n type: program\n level: 3\n srcs: [main.sakura]\n");
t.expects(box.blossom("level", ["build"]).warns("blossomfile:7: `level` is 0, 1, or 2, not `3`."));
box.project("missing", "name: missing\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: missing\n type: program\n srcs: [nothing.sakura, '*.c']\n");
val missing := box.blossom("missing", ["build"]);
t.expects(missing.status == 2 && missing.warns("`nothing.sakura`, in `missing`'s `srcs`, names no file."), missing.error.span);
}
@test def a_step_of_install_says_what_it_does_by_a_tag_blossom_knows(t: &mut tester, box: sandbox): void {
box.project("untagged", "name: untagged\ngpid: tests\nsemv: 1.0.0\ninstall:\n- ./share\n");
val untagged := box.blossom("untagged", ["install", "--prefix", "prefix"]);
t.expects(untagged.status == 2 && untagged.warns("blossomfile:5: A step of `install` says what it does with a tag"), untagged.error.span);
box.project("unknown", "name: unknown\ngpid: tests\nsemv: 1.0.0\ninstall:\n- !program ./share\n");
val unknown := box.blossom("unknown", ["install", "--prefix", "prefix"]);
t.expects(unknown.status == 2 && unknown.warns("`!program` is no step blossom knows"), unknown.error.span);
box.project("outside", "name: outside\ngpid: tests\nsemv: 1.0.0\ninstall:\n- !include ../elsewhere\n- !include ./a:/etc\n");
t.expects(box.blossom("outside", ["install", "--prefix", "prefix"]).warns("`../elsewhere` is outside the project"));
}
@test def a_folder_without_a_blossomfile_is_refused(t: &mut tester, box: sandbox): void {
box.write("empty/main.sakura", "def main(): void {}\n");
val built := box.blossom("empty", ["build"]);
t.expects(built.status == 2 && built.warns("holds no blossomfile"), built.error.span);
}
+181
View File
@@ -0,0 +1,181 @@
# Tests of requirements: a folder beside the project, a remote cloned once into
# the store, from a bare git repository in the test's folder, written short with
# a ref and without, and as a block giving options, and what's refused: one
# project with two sets of options, a library not exported, a name two
# projects give, and a clone that fails.
import std::io;
import std::testing;
# Writes a project holding a library, `shapes.basic`, whose `side` gives the
# text the option `flavor` names a folder of, `plain` unless given.
def sandbox::shapes(&this, folder: strview, exported: bool = true): void {
var exports := string::new(nothing {});
if exported {
exports.append("exports:\n- shapes.basic\n");
}
this.write($"{folder}/blossomfile".span, $"""
name: shapes
gpid: tests
semv: 1.0.0
options:
flavor: plain
targets:
- name: shapes.basic
type: library
root: ./$\{flavor\}
{exports.span}
""".span);
this.write($"{folder}/plain/basic.sakura".span, "module shapes::basic;\n\npub def side(): strview { return \"plain\"; }\n");
this.write($"{folder}/fancy/basic.sakura".span, "module shapes::basic;\n\npub def side(): strview { return \"fancy\"; }\n");
}
# Writes a program, `app`, reading `shapes.basic`, which its blossomfile's
# `requires` names.
def sandbox::app(&this, folder: strview, requires: strview): void {
this.write($"{folder}/blossomfile".span, $"""
name: app
gpid: tests
semv: 1.0.0
requires:
{requires}
targets:
- name: app
type: program
deps: [shapes.basic]
srcs: [main.sakura]
""".span);
this.write($"{folder}/main.sakura".span, """
import std::io;
import shapes::basic;
def main(): void {
io::println($"side {basic::side()}".span);
}
""");
}
# Makes a bare git repository of a project, as a remote is, with a commit on
# `main` and a tag, `v1.0.0`.
def sandbox::remote(&this, from: strview, bare: strview): bool {
val into := this.path_of(bare);
return this.git(from, ["init", "-q", "-b", "main", "."])
&& this.git(from, ["add", "-A"])
&& this.git(from, ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", "one"])
&& this.git(from, ["tag", "v1.0.0"])
&& this.git(from, ["clone", "-q", "--bare", ".", into.span]);
}
@test def a_folder_beside_the_project_is_required_by_its_path(t: &mut tester, box: sandbox): void {
box.shapes("shapes");
box.app("app", "- ../shapes");
val ran := box.blossom("app", ["run"]);
t.expects(ran.status == 0 && ran.says("side plain"), ran.error.span);
}
@test def a_block_gives_a_requirement_values_for_its_options(t: &mut tester, box: sandbox): void {
box.shapes("shapes");
box.app("app", "- remote: ../shapes\n options:\n flavor: fancy");
val ran := box.blossom("app", ["run"]);
t.expects(ran.status == 0 && ran.says("side fancy"), ran.error.span);
}
@test def a_remote_is_cloned_once_into_the_store(t: &mut tester, box: sandbox): void {
box.shapes("source");
t.expects(box.remote("source", "shapes.git"));
val shapes := box.url("shapes.git");
box.app("app", $"- {shapes}@v1.0.0".span);
val first := box.blossom("app", ["build"]);
t.expects(first.status == 0 && first.says("Cloning file://") && first.says("[1/1] Built app"), first.error.span);
val stored := box.stored("shapes@v1.0.0");
t.expects(stored.length != 0 && box.has($"{stored}/blossomfile".span));
# Cloned, it's taken from the store.
val second := box.blossom("app", ["build"]);
t.expects(second.status == 0 && !second.says("Cloning"));
# Without a ref, `main` is cloned.
box.app("main", $"- {shapes}".span);
val main := box.blossom("main", ["run"]);
t.expects(main.status == 0 && main.says("side plain"), main.error.span);
t.expects(box.stored("shapes@main").length != 0);
}
@test def a_clone_that_fails_says_why(t: &mut tester, box: sandbox): void {
when os == windows {
box.write("bin/git.cmd", "@echo off\r\necho fatal: the remote hung up 1>&2\r\nexit /b 128\r\n");
}
else {
box.write("bin/git", "#!/bin/sh\necho 'fatal: the remote hung up' >&2\nexit 128\n");
val git := box.at.join("bin/git");
_ = io::set_permissions(&git, 0o755);
}
box.app("app", "- https://example.com/shapes@v1.0.0");
val built := box.blossom("app", ["build"], box.search_path("bin").span);
t.expects(built.status == 1 && built.warns("fatal: the remote hung up") && built.warns("Cloning https://example.com/shapes@v1.0.0 failed."), built.error.span);
t.expects(box.stored("shapes@v1.0.0").length == 0);
}
@test def requirements_that_dont_fit_are_refused(t: &mut tester, box: sandbox): void {
# One project, with two sets of option values.
box.shapes("shapes");
box.write("other/blossomfile", "name: other\ngpid: tests\nsemv: 1.0.0\nrequires:\n- ../shapes\ntargets:\n- name: other.lib\n type: library\n deps: [shapes.basic]\n");
box.app("app", "- ../other\n- remote: ../shapes\n options:\n flavor: fancy");
val twice := box.blossom("app", ["build"]);
t.expects(twice.status == 2 && twice.warns("tests/shapes@1.0.0 is required with two sets of option values"), twice.error.span);
# A library its project doesn't export.
box.shapes("hidden", false);
box.app("private", "- ../hidden");
val private := box.blossom("private", ["build"]);
t.expects(private.status == 2 && private.warns("`shapes.basic`, which `app` reads, isn't exported by tests/shapes@1.0.0."), private.error.span);
# One name, two projects.
box.write("copy/blossomfile", "name: copy\ngpid: tests\nsemv: 1.0.0\ntargets:\n- name: shapes.basic\n type: library\n root: ./plain\nexports:\n- shapes.basic\n");
box.write("copy/plain/basic.sakura", "module shapes::basic;\n\npub def side(): strview { return \"copy\"; }\n");
box.app("both", "- ../shapes\n- ../copy");
val clash := box.blossom("both", ["build"]);
t.expects(clash.status == 2 && clash.warns("`shapes.basic` is a library of both"), clash.error.span);
# A library no project has.
box.app("none", " []");
t.expects(box.blossom("none", ["build"]).warns("`shapes.basic`, which `app` reads, is no library"));
}
@test def a_requirement_reads_what_it_requires_only_where_its_libraries_need_it(t: &mut tester, box: sandbox): void {
# What `shapes` requires for itself alone, which can't be fetched, isn't.
box.shapes("shapes");
val text := box.read("shapes/blossomfile");
box.write("shapes/blossomfile", $"{text.span}\nrequires:\n- https://example.invalid/never@v1.0.0\n".span);
box.app("app", "- ../shapes");
val ran := box.blossom("app", ["run"]);
t.expects(ran.status == 0 && ran.says("side plain") && !ran.says("Cloning"), ran.error.span);
# A library reading one of what its project requires brings that along.
box.shapes("inner");
box.write("outer/blossomfile", """
name: outer
gpid: tests
semv: 1.0.0
requires:
- ../inner
targets:
- name: outer.lib
type: library
deps: [shapes.basic]
exports:
- outer.lib
""");
box.write("outer/outer.lib/lib.sakura", "import shapes::basic;\n\nmodule outer::lib;\n\npub def says(): strview { return basic::side(); }\n");
box.write("user/blossomfile", "name: user\ngpid: tests\nsemv: 1.0.0\nrequires:\n- ../outer\ntargets:\n- name: user\n type: program\n deps: [outer.lib]\n srcs: [main.sakura]\n");
box.write("user/main.sakura", "import std::io;\nimport outer::lib;\n\ndef main(): void { io::println($\"says {lib::says()}\".span); }\n");
val through := box.blossom("user", ["run"]);
t.expects(through.status == 0 && through.says("says plain"), through.error.span);
}
+471
View File
@@ -0,0 +1,471 @@
# What blossom's tests of its commands share: the blossom they run, a folder of
# each test's own, writing projects into it, and running blossom there.
import std::io;
import std::process;
import system;
import std::testing;
#! The blossom the tests run, which `main` is given first.
var blossom_at: strview = "";
#! The standard library the projects the tests make are given, as their
#! config's `std`: the one blossom's own project reads, so the tests don't
#! depend on a `sakura` that finds one of its own. Empty where blossom's store
#! holds none, for the one `sakura` finds.
var std_at: strview = "";
#! The folder of the standard library blossom's own project reads, from what
#! `blossom libraries` lists of it, as `--lib std::io=FOLDER/io`.
#! @param listed What `blossom libraries` printed.
#! @returns The folder, or empty text where it lists no module of std.
def std_folder(listed: strview): strview {
for line in listed.lines() {
continue if !line.starts_with("--lib std::");
val rest := line.substr(11, line.length - 11);
val equals := rest.find("=") else { continue; };
# A module within one, as `std::io::glob`, is a folder deeper.
continue if rest.substr(0, equals).find(":") is some;
val folder := rest.substr(equals + 1, rest.length - equals - 1);
var end := folder.length;
while end > 0 && folder[end - 1] != '/' && folder[end - 1] != '\\' {
end--;
}
return folder.substr(0, end - 1) if end > 1;
}
return strview::blank();
}
#! The line of a sandbox's config naming `std_at` as its `std`.
#! @returns The line, or empty text where there's none.
def std_line(): string {
if std_at.length == 0 {
return string::new(nothing {});
}
return $"std: {std_at}\n";
}
#! The folder the system keeps temporary files in: what `TMPDIR` names, or on
#! Windows `TEMP` or `TMP`, else `/tmp`.
#! @returns The folder.
def temporary_folder(): strview {
for name in ["TMPDIR", "TEMP", "TMP"] {
if process::variable(name) is some(named) {
return named if named.length != 0;
}
}
return "/tmp";
}
#! Removes a file, or a folder and everything in it, as blossom's `remove_tree`
#! does. Links are removed, not followed.
#! @param at What to remove.
def remove_tree(at: &path): void {
val found := io::info(at, false) else (_) { return; };
if found.kind == file_kind::directory {
var names := vector::<path>::new();
var listing := io::directory::open(at) else (_) { return; };
while listing.next() is some(entry) {
val name := path::parse(entry.name) else (_) { return; };
names.append(at.join(&name));
}
for &each in names {
remove_tree(each);
}
}
_ = io::remove(at);
}
#! A folder of a test's own, in the system's temporary folder, holding the
#! projects it makes, and blossom's config, in `config`, naming the `sakura` and
#! std the tests run, and store, in `cache`, removed with all it holds where the
#! test returns.
struct sandbox : fixture<sandbox>, owning {
var at: path;
}
def sandbox::setup(t: &mut tester): sandbox {
val temporary := temporary_folder();
for n in 0..10000 {
var at := path::parse($"{temporary}/blossom-tests-{n}".span) else (_) { break; };
if io::create_directory(&at) is error(_) {
continue;
}
var made := sandbox { at = move(at) };
var config := string::new(nothing {});
if process::variable("SAKURA") is some(sakura) {
config.append($"sakura: {sakura}\n".span);
}
config.append(std_line().span);
made.write("config/blossom/config", config.span);
return made;
}
system::abort();
return sandbox {};
}
def sandbox::close(&mut this): void {
remove_tree(&this.at);
}
#! Writes a file in the sandbox, making the folders above it.
#! @param this The sandbox.
#! @param name The file, relative to the sandbox.
#! @param text What it holds.
def sandbox::write(&this, name: strview, text: strview): void {
val at := this.at.join(name);
if at.parent() is some(&above) {
_ = io::create_directories(above);
}
_ = io::write_file(&at, text);
}
#! Reads a file of the sandbox.
#! @param this The sandbox.
#! @param name The file, relative to the sandbox.
#! @returns What it holds, or empty text where it can't be read.
def sandbox::read(&this, name: strview): string {
val at := this.at.join(name);
return io::read_file(&at) else (_) { return string::new(nothing {}); };
}
#! Whether a file or folder is in the sandbox.
#! @param this The sandbox.
#! @param name Its path, relative to the sandbox.
#! @returns Whether it is.
def sandbox::has(&this, name: strview): bool {
val at := this.at.join(name);
return io::exists(&at);
}
#! The text of a path in the sandbox, as the system writes it and blossom says
#! it: `/tmp/blossom-tests-0/app/app.parts`, and on Windows with `\`.
#! @param this The sandbox.
#! @param name The path, relative to the sandbox.
#! @returns Its text.
def sandbox::path_of(&this, name: strview): string {
val at := this.at.join(name);
return string::from(at.span, nothing {});
}
#! Text a program wrote, its lines ended by `\n` alone, as a program of Windows
#! ends them by `\r\n`.
#! @param text The text.
#! @returns The text without `\r`.
def lines_of(text: strview): string {
var made := string::new(nothing {});
for i in 0..text.length {
if text[i] != '\r' {
made.push(text[i]);
}
}
return made;
}
#! The folder a project's packages are made in, `.blossom/KEY/dist`, of the
#! first output folder holding one.
#! @param this The sandbox.
#! @param folder The project's folder, relative to the sandbox.
#! @returns The folder, as the system writes it, or empty text where there's
#! none.
def sandbox::dist(&this, folder: strview): string {
val outputs := this.at.join(folder).join(".blossom");
val found := folder_named(&outputs, "dist", 0) else { return string::new(nothing {}); };
return string::from(found.span, nothing {});
}
#! The `file://` URL of a folder of the sandbox, as git takes one:
#! `file:///tmp/blossom-tests-0/shapes.git`, and on Windows
#! `file:///C:/Users/me/AppData/Local/Temp/blossom-tests-0/shapes.git`.
#! @param this The sandbox.
#! @param name The folder, relative to the sandbox.
#! @returns The URL.
def sandbox::url(&this, name: strview): string {
val at := this.path_of(name);
var made := string::from("file://", nothing {});
if at.length != 0 && at[0] != '/' {
made.push('/');
}
for i in 0..at.length {
made.push(at[i] == '\\' ? '/' : at[i]);
}
return made;
}
#! The folder of blossom's store in the sandbox that a remote's branch is
#! cloned into, found by its name, as `shapes@v1.0.0`, wherever the remote's
#! path put it, which differs from system to system.
#! @param this The sandbox.
#! @param name The folder's name.
#! @returns Its path, as the system writes it, or empty text where there's none.
def sandbox::stored(&this, name: strview): string {
val store := this.at.join("cache/blossom");
val found := folder_named(&store, name, 0) else { return string::new(nothing {}); };
return string::from(found.span, nothing {});
}
# The first folder of a name within a folder, or within those it holds.
def folder_named(at: &path, name: strview, depth: u32): maybe<path> {
return nothing {} if depth > 16;
var folders := vector::<path>::new();
var listing := io::directory::open(at) else (_) { return nothing {}; };
while listing.next() is some(entry) {
continue if entry.kind != file_kind::directory;
val each := path::parse(entry.name) else (_) { continue; };
if entry.name == name {
return at.join(&each);
}
folders.append(at.join(&each));
}
for &each in folders {
val found := folder_named(each, name, depth + 1);
return found if found is some;
}
return nothing {};
}
#! A program's file, as the system names it: `hello`, and on Windows
#! `hello.exe`.
#! @param name The program.
#! @returns Its file's name.
def program(name: strview): string {
when os == windows {
return $"{name}.exe";
}
return string::from(name, nothing {});
}
when os == windows {
# How `PATH` splits its folders.
val path_split: strview = ";";
# The folder of the system's own programs, which holds no `sakurac`.
def system_programs(): string {
val root := process::variable("SystemRoot") else { return string::from("C:\\Windows\\System32", nothing {}); };
return $"{root}\\System32";
}
}
else {
val path_split: strview = ":";
def system_programs(): string {
return string::from("/usr/bin:/bin", nothing {});
}
}
#! A `PATH` of a folder of the sandbox, where one's named, then the system's own
#! programs, which hold no `sakurac`, split as the system splits it.
#! @param this The sandbox.
#! @param folder The folder, relative to the sandbox, or empty for none.
#! @returns The `PATH`.
def sandbox::search_path(&this, folder: strview = ""): string {
var made := string::new(nothing {});
if folder.length != 0 {
made.append(this.path_of(folder).span);
made.append(path_split);
}
made.append(system_programs().span);
return made;
}
#! Runs git in a folder of the sandbox.
#! @param this The sandbox.
#! @param folder The folder, relative to the sandbox.
#! @param words What git is given.
#! @returns Whether it succeeded.
def sandbox::git(&this, folder: strview, words: span<strview>): bool {
return this.run("git", folder, words).status == 0;
}
#! What a command said, and the status it ended with.
struct said {
var status: i32;
var output: string;
var error: string;
}
#! Whether what a command wrote to its output holds some text.
#! @param this What it said.
#! @param text The text.
#! @returns Whether it does.
def said::says(&this, text: strview): bool {
return this.output.span.find(text) is some;
}
#! Whether what a command wrote to its error holds some text.
#! @param this What it said.
#! @param text The text.
#! @returns Whether it does.
def said::warns(&this, text: strview): bool {
return this.error.span.find(text) is some;
}
#! Runs blossom in a folder of the sandbox, its config and store the sandbox's.
#! @param this The sandbox.
#! @param folder The folder, relative to the sandbox.
#! @param words What blossom is given.
#! @param path What `PATH` is, or empty for the tests' own.
#! @returns What it said.
def sandbox::blossom(&this, folder: strview, words: span<strview>, path: strview = ""): said {
return this.run(blossom_at, folder, words, path);
}
#! Runs a program in a folder of the sandbox, as `blossom` does, where blossom's
#! config and store are the sandbox's: `XDG_CONFIG_HOME` and `XDG_CACHE_HOME`
#! name them, and on Windows `APPDATA` and `LOCALAPPDATA`, which blossom reads
#! there, so no test reaches the user's own.
#! @param this The sandbox.
#! @param program The program.
#! @param folder The folder, relative to the sandbox.
#! @param words What it's given.
#! @param path What `PATH` is, or empty for the tests' own.
#! @returns What it said.
def sandbox::run(&this, program: strview, folder: strview, words: span<strview>, path: strview = ""): said {
var command := process::command::new(program);
command.arguments(words);
val at := this.at.join(folder);
command.directory(&at);
command.set_variable("XDG_CONFIG_HOME", $"{this.at.span}/config".span);
command.set_variable("XDG_CACHE_HOME", $"{this.at.span}/cache".span);
command.set_variable("APPDATA", $"{this.at.span}/config".span);
command.set_variable("LOCALAPPDATA", $"{this.at.span}/cache".span);
command.set_variable("SOURCE_DATE_EPOCH", "1700000000");
# On Windows, the copy of the tests running this one is told its test by
# `SAKURA_TEST_ONE`, which the tests blossom runs would read as theirs.
command.set_variable("SAKURA_TEST_ONE", "");
if path.length != 0 {
command.set_variable("PATH", path);
}
var got := command.capture() else (why) {
return said { status = -1, output = string::new(nothing {}), error = $"`{program}` can't be run: {why}" };
};
var code: i32 = -1;
if got.status is exited(exited) {
code = exited;
}
return said { status = code, output = move(got.output), error = move(got.error) };
}
#! The path a line of blossom's output gives in parentheses, as the program of
#! `[2/2] Built hello (.blossom/KEY/bin/hello)`, relative to the project.
#! @param text What blossom said.
#! @param name The target.
#! @returns The path, or empty text.
def built_path(text: strview, name: strview): strview {
val marker := $"Built {name} (";
val at := text.find(marker.span) else { return strview::blank(); };
val from := at + marker.length;
val rest := text.substr(from, text.length - from);
val end := rest.find(")") else { return strview::blank(); };
return rest.substr(0, end);
}
#! Writes a project, `shown`, in a folder of the sandbox: a program shown as a
#! desktop's program is, by its title, its icon, a PNG of 32 pixels each way
#! and an SVG, and its window, which its option `windowed` says, as an editor
#! building a game would give it.
#! @param this The sandbox.
#! @param folder The project's folder.
def sandbox::shown(&this, folder: strview): void {
this.write($"{folder}/blossomfile".span, """
name: shown
gpid: tests
semv: 2.1.0
options:
windowed: true
targets:
- name: shown
type: program
srcs: [main.sakura]
title: Shown Off
icon: [icons/shown.png, icons/shown.svg]
windowed: ${windowed}
""");
this.write($"{folder}/main.sakura".span, """
import std::io;
def main(): void {
io::println("shown");
}
""");
this.write($"{folder}/icons/shown.png".span, "\x89PNG\r\n\x1A\n\x00\x00\x00\x0DIHDR\x00\x00\x00\x20\x00\x00\x00\x20\x08\x06\x00\x00\x00");
this.write($"{folder}/icons/shown.svg".span, "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\"/>\n");
}
#! A word of a file's bytes, the least significant byte first.
#! @param bytes The bytes.
#! @param at Where it is.
#! @param size How many bytes it is.
#! @returns The word, or 0 where it's past the end.
def word_of(bytes: strview, at: u64, size: u64): u64 {
return 0 if at + size > bytes.length;
var made: u64 = 0;
for k in 0..size {
made = made | (bytes[at + k] as u64 << (8 * k));
}
return made;
}
#! Writes a project, `hello`, in a folder of the sandbox: a program reading a
#! library of its own, `hello.geometry`, which holds a module within it, and
#! tests of the library, as the plan's "Using it" has it.
#! @param this The sandbox.
#! @param folder The project's folder.
#! @param more What's added to its blossomfile.
def sandbox::hello(&this, folder: strview, more: strview = ""): void {
this.write($"{folder}/blossomfile".span, $"""
name: hello
gpid: tests
semv: 1.0.0
options:
level: 2
targets:
- name: hello
type: program
level: $\{level\}
deps: [std, hello.geometry]
srcs: [main.sakura]
- name: hello.geometry
type: library
tests:
- name: hello.tests
type: program
deps: [hello.geometry]
{more}
""".span);
this.write($"{folder}/main.sakura".span, """
import std::io;
import hello::geometry;
import hello::geometry::solids;
def main(args: span<strview>): void {
io::println($"area {geometry::area(3, 4)} cube {solids::cube(2)} given {args.length}".span);
}
""");
this.write($"{folder}/hello.geometry/shapes.sakura".span, """
module hello::geometry;
pub def area(w: i64, h: i64): i64 { return w * h; }
""");
this.write($"{folder}/hello.geometry/solids/solids.sakura".span, """
module hello::geometry::solids;
pub def cube(side: i64): i64 { return side * side * side; }
""");
this.write($"{folder}/tests/main.sakura".span, """
import system;
import std::testing;
import hello::geometry;
import hello::geometry::solids;
def main(args: span<strview>): void { exit(tester::run(args)); }
@test def areas_multiply(t: &mut tester): void { t.expects(geometry::area(2, 5) == 10); }
@test def cubes_cube(t: &mut tester): void { t.expects(solids::cube(3) == 27); }
""");
}
+14
View File
@@ -0,0 +1,14 @@
# Runs blossom's tests of its parts, which read its own files:
#
# blossom test --suite blossom.units
#
# or, from the root of the repository, with the standard library beside it:
#
# sakurac run --lib std=../std
# models/*.sakura management/*.sakura pipeline/*.sakura tests/units/*.sakura
import system;
import std::testing;
def main(args: span<strview>): void {
exit(tester::run(args));
}
+239
View File
@@ -0,0 +1,239 @@
# Tests of blossom's parts: reading blossomfiles, the store's folders, output
# folders' names, versions, the digests packages carry, and commands as `-v`
# prints them.
import std::io;
import std::testing;
import blossom::models;
import blossom::management;
import blossom::pipeline;
# Reads a blossomfile's text, giving what's wrong, or empty text where nothing
# is.
def problem_in(text: strview): string {
val file := path::parse("/projects/tests/blossomfile") else (_) { return string::new(nothing {}); };
var overrides := vector::<models::setting>::new();
var read := models::read_project(text, &file, overrides.span, "");
if read is error(&problem) {
return problem.describe();
}
return string::new(nothing {});
}
# What `problem_in` gives for a problem: the blossomfile's path, as the system
# writes it, as `\projects\tests\blossomfile` on Windows, then the rest.
def problem_at(rest: strview): string {
val file := path::parse("/projects/tests/blossomfile") else (_) { return string::new(nothing {}); };
return $"{file.span}{rest}";
}
@test def digests_are_those_their_standards_give(t: &mut tester): void {
t.expects(pipeline::md5("") == "d41d8cd98f00b204e9800998ecf8427e");
t.expects(pipeline::md5("abc") == "900150983cd24fb0d6963f7d28e17f72");
t.expects(pipeline::md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890") == "57edf4a22be3c955ac49da2e2107b67a");
t.expects(pipeline::sha256("") == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
t.expects(pipeline::sha256("abc") == "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
t.expects(pipeline::sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") == "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
t.expects(pipeline::crc32("123456789") == 0xCBF43926 && pipeline::crc32("") == 0);
}
@test def remotes_are_stored_by_their_host_path_and_ref(t: &mut tester): void {
t.expects(management::store_key("https://git.malunal.studio/malunal/json", "v1.0.0") == "git.malunal.studio/malunal/json@v1.0.0");
t.expects(management::store_key("https://me@host.com/a/b.git", "main") == "host.com/a/b@main");
t.expects(management::store_key("git@github.com:malunal/json.git", "main") == "github.com/malunal/json@main");
t.expects(management::store_key("file:///tmp/x/repo.git", "v2") == "file/tmp/x/repo@v2");
# Nothing leads out of the store.
t.expects(management::store_key("https://host/../../etc", "main") == "host/etc@main");
t.expects(management::store_key("https://host/a", "../../x") == "host/a@.._.._x");
}
@test def output_folders_are_named_by_identity_options_and_target(t: &mut tester): void {
var options := vector::<models::setting>::new();
options.append(models::setting { name = string::from("level", nothing {}), value = string::from("2", nothing {}) });
options.append(models::setting { name = string::from("debug", nothing {}), value = string::from("false", nothing {}) });
val first := models::output_key("malunal/hello@1.0.0", options.span, "");
t.expects(first.length == 16);
# In any order, the same options name the same folder.
var reordered := vector::<models::setting>::new();
reordered.append(models::setting { name = string::from("debug", nothing {}), value = string::from("false", nothing {}) });
reordered.append(models::setting { name = string::from("level", nothing {}), value = string::from("2", nothing {}) });
t.expects(models::output_key("malunal/hello@1.0.0", reordered.span, "") == first);
t.expects(models::output_key("malunal/hello@1.0.0", options.span, "windows-x86_64") != first);
t.expects(models::output_key("malunal/hello@1.0.1", options.span, "") != first);
t.expects(models::hex(0x0123456789abcdef) == "0123456789abcdef");
}
@test def versions_are_semantic(t: &mut tester): void {
for text in ["1.2.0", "0.0.1", "10.20.30", "1.2.0-alpha", "1.2.0-alpha.1", "1.2.0+build.5"] {
t.expects(models::is_version(text), text);
}
for text in ["1", "1.2", "1.2.x", "v1.2.0", "1.2.0-", "1..0", ""] {
t.expects(!models::is_version(text), text);
}
}
@test def blossomfiles_are_read_with_their_mistakes_found(t: &mut tester): void {
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\n").length == 0);
t.expects(problem_in("") == problem_at(": The blossomfile is empty: it needs `name`, `gpid`, and `semv`."));
t.expects(problem_in("name: a\ngpid: b\n") == problem_at(":1: `semv` is needed."));
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\nname: c\n") == problem_at(":4: `name` is written twice in the blossomfile."));
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\noptions:\n bin: x\n").span.find("`bin` is blossom's") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\nexports: [nope]\n").span.find("`nope` is exported, but it's no library") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: x.y\n type: library\n- name: x.y\n type: library\n").span.find("`x.y` names two targets: this, and that of line 5.") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: 9lives\n type: library\n").span.find("names no module") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: p\n type: program\n").span.find("`p` needs `srcs`") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\nrequires:\n- ../x@v1\n- remote: ../y\n branch: v1\n").span.find("A folder takes no `branch`.") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: l\n type: library\n srcs: [a.sakura]\n").span.find("A library takes no `srcs`") is some);
}
@test def requirements_are_read_short_and_as_blocks(t: &mut tester): void {
val file := path::parse("/projects/tests/blossomfile") else (_) { return; };
var overrides := vector::<models::setting>::new();
var read := models::read_project("""
name: a
gpid: b
semv: 1.0.0
requires:
- https://host/json@v1.0.0
- git@host:malunal/yaml
- ../shapes
- remote: https://host/args
options:
strict: true
""", &file, overrides.span, "");
if read is not value(&made) {
t.expects(false);
return;
}
t.expects(made.requires.length == 4);
t.expects(made.requires[0].remote == "https://host/json" && made.requires[0].branch == "v1.0.0" && !made.requires[0].local);
t.expects(made.requires[1].remote == "git@host:malunal/yaml" && made.requires[1].branch == "main");
t.expects(made.requires[2].remote == "../shapes" && made.requires[2].local);
t.expects(made.requires[3].branch == "main" && made.requires[3].options.length == 1 && made.requires[3].options[0].value == "true");
}
# Reads a blossomfile with values given its options, as `-o` gives them.
def read_given(text: strview, given: span<strview>): result<models::project, models::problem> {
val file := path::parse("/projects/tests/blossomfile") else (_) {
return models::problem { message = string::from("no path", nothing {}) };
};
var overrides := vector::<models::setting>::new();
for &each in given {
val split := each.find("=") else { continue; };
overrides.append(models::setting {
name = string::from(each.substr(0, split), nothing {}),
value = string::from(each.substr(split + 1, each.length - split - 1), nothing {})
});
}
return models::read_project(text, &file, overrides.span, "");
}
# A program shown by options, as an editor building a game gives them.
val shown_by_options: strview = """
name: a
gpid: b
semv: 1.0.0
options:
title: ''
icon: ''
windowed: false
targets:
- name: game
type: program
srcs: [main.sakura]
title: ${title}
icon: ${icon}
windowed: ${windowed}
""";
@test def a_program_is_shown_by_its_title_icons_and_window(t: &mut tester): void {
var read := read_given("""
name: a
gpid: b
semv: 1.0.0
targets:
- name: app
type: program
srcs: [main.sakura]
title: The App
icon: [icons/app.png, icons/app.svg, app.ICO]
windowed: true
- name: tool
type: program
srcs: [tool.sakura]
icon: one.png, two.png
- name: plain
type: program
srcs: [plain.sakura]
""", span::<strview>::blank());
if read is not value(&made) {
t.expects(false);
return;
}
val app := &made.targets[0];
t.expects(app.title == "The App" && app.windowed && app.icons.length == 3 && app.icons[0] == "icons/app.png" && app.icons[2] == "app.ICO");
t.expects(app.shown);
t.expects(made.targets[1].icons.length == 2 && made.targets[1].icons[1] == "two.png" && !made.targets[1].windowed);
t.expects(!made.targets[2].shown && made.targets[2].title.length == 0);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: p\n type: program\n srcs: [p.sakura]\n icon: p.bmp\n").span.find("`p.bmp` is no icon blossom knows") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: p\n type: program\n srcs: [p.sakura]\n windowed: yes\n").span.find("`windowed` is `true` or `false`, not `yes`") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntargets:\n- name: l.m\n type: library\n icon: l.png\n").span.find("A library takes no `icon`") is some);
t.expects(problem_in("name: a\ngpid: b\nsemv: 1.0.0\ntests:\n- name: t\n type: program\n title: T\n").span.find("`title` is no key of a test") is some);
}
@test def options_give_a_program_its_title_icons_and_window(t: &mut tester): void {
# Given nothing, an empty title and icon are none.
var plain := read_given(shown_by_options, span::<strview>::blank());
if plain is not value(&made) {
t.expects(false);
return;
}
t.expects(made.targets[0].title.length == 0 && made.targets[0].icons.length == 0 && !made.targets[0].windowed && !made.targets[0].shown);
# Several files are given as one value, between commas.
val given: strview[3] = ["title=Garden", "icon=icon.png, icon-16.png,icon.svg", "windowed=true"];
var shown := read_given(shown_by_options, given[:]);
if shown is not value(&made) {
t.expects(false);
return;
}
val game := &made.targets[0];
t.expects(game.title == "Garden" && game.windowed && game.shown);
t.expects(game.icons.length == 3 && game.icons[0] == "icon.png" && game.icons[1] == "icon-16.png" && game.icons[2] == "icon.svg");
# What's given is checked as what's written is.
val windowed: strview[1] = ["windowed=maybe"];
if read_given(shown_by_options, windowed[:]) is error(&problem) {
t.expects(problem.describe().span.find("`windowed` is `true` or `false`, not `maybe`") is some);
} else {
t.expects(false);
}
val icon: strview[1] = ["icon=icon.gif"];
if read_given(shown_by_options, icon[:]) is error(&problem) {
t.expects(problem.describe().span.find("`icon.gif` is no icon blossom knows") is some);
} else {
t.expects(false);
}
}
@test def a_desktop_entry_runs_its_program_where_the_prefix_holds_it(t: &mut tester): void {
t.expects(pipeline::exec_text("/usr", "game") == "/usr/bin/game");
t.expects(pipeline::exec_text("/opt/games/", "game") == "/opt/games/bin/game");
t.expects(pipeline::exec_text("C:\\Users\\me\\.local", "game") == "C:/Users/me/.local/bin/game");
t.expects(pipeline::exec_text("/home/me/my games", "game") == "\"/home/me/my games/bin/game\"");
t.expects(pipeline::exec_text("/home/me/100%", "game") == "\"/home/me/100%%/bin/game\"");
t.expects(pipeline::for_windows("windows-x86_64") && !pipeline::for_windows("linux-x86_64"));
val size := pipeline::png_size("\x89PNG\r\n\x1A\n\x00\x00\x00\x0DIHDR\x00\x00\x01\x00\x00\x00\x00\x30rest");
t.expects(size[0] == 256 && size[1] == 48);
t.expects(pipeline::png_size("GIF89a, not a PNG at all")[0] == 0);
}
@test def commands_are_printed_as_a_shell_takes_them(t: &mut tester): void {
var words := vector::<string>::new();
for each in ["sakura", "build", "--lib", "a::b=./x", "my file.sakura", "it's"] {
words.append(string::from(each, nothing {}));
}
t.expects(pipeline::spoken(words.span) == "sakura build --lib a::b=./x 'my file.sakura' 'it'\\''s'");
}