# 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; 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".view(), $""" name: shapes gpid: tests semv: 1.0.0 options: flavor: plain targets: - name: shapes.basic type: library root: ./$\{flavor\} {exports.view()} """.view()); this.write($"{folder}/plain/basic.sakura".view(), "module shapes::basic;\n\npub def side(): strview { return \"plain\"; }\n"); this.write($"{folder}/fancy/basic.sakura".view(), "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".view(), $""" name: app gpid: tests semv: 1.0.0 requires: {requires} targets: - name: app type: program deps: [shapes.basic] srcs: [main.sakura] """.view()); this.write($"{folder}/main.sakura".view(), """ import std; import std::io; import shapes::basic; def main(): void { io::println($"side {basic::side()}".view()); } """); } # 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 made := this.run("sh", from, [ "-c", $"git init -q -b main . && git add -A && git -c user.name=t -c user.email=t@t commit -q -m one && git tag v1.0.0 && git clone -q --bare . {this.at.view()}/{bare}".view() ]); return made.status == 0; } @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.view()); } @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.view()); } @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")); box.app("app", $"- file://{box.at.view()}/shapes.git@v1.0.0".view()); val first := box.blossom("app", ["build"]); t.expects(first.status == 0 && first.says("Cloning file://") && first.says("[1/1] Built app"), first.error.view()); t.expects(box.has($"config/blossom/file{box.at.view()}/shapes@v1.0.0/blossomfile".view())); # 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", $"- file://{box.at.view()}/shapes.git".view()); val main := box.blossom("main", ["run"]); t.expects(main.status == 0 && main.says("side plain"), main.error.view()); t.expects(box.has($"config/blossom/file{box.at.view()}/shapes@main".view())); } @test def a_clone_that_fails_says_why(t: &mut tester, box: sandbox): void { 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.at.view()}/bin:/usr/bin:/bin".view()); 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.view()); t.expects(!box.has("config/blossom/example.com/shapes@v1.0.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.view()); # 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.view()); # 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.view()); # 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.view()}\nrequires:\n- https://example.invalid/never@v1.0.0\n".view()); box.app("app", "- ../shapes"); val ran := box.blossom("app", ["run"]); t.expects(ran.status == 0 && ran.says("side plain") && !ran.says("Cloning"), ran.error.view()); # 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;\nimport std::io;\nimport outer::lib;\n\ndef main(): void { io::println($\"says {lib::says()}\".view()); }\n"); val through := box.blossom("user", ["run"]); t.expects(through.status == 0 && through.says("says plain"), through.error.view()); }