# Tests of `build`, `check`, `run`, and `clean`: a program, a program with a # library of its own, building only what changed, a check that fails, and # building for Windows. import std; import std::io; 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; import std::io; def main(args: span): 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")); } @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")); }