472 lines
16 KiB
Plaintext
472 lines
16 KiB
Plaintext
# 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); }
|
|
""");
|
|
}
|