# 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::::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::::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::::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::::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): result { val file := path::parse("/projects/tests/blossomfile") else (_) { return models::problem { message = string::from("no path", nothing {}) }; }; var overrides := vector::::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::::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::::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::::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'"); }