Initial Commit
This commit is contained in:
27 files changed
+5400
No files matched your search
@@ -0,0 +1,208 @@
|
||||
# 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 = "";
|
||||
|
||||
#! A folder of a test's own, holding the projects it makes, and blossom's
|
||||
#! config, in `config`, 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 {
|
||||
for n in 0..10000 {
|
||||
var at := path::parse($"/tmp/blossom-tests-{n}".view()) 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".view());
|
||||
}
|
||||
made.write("config/blossom/config", config.view());
|
||||
return made;
|
||||
}
|
||||
system::abort();
|
||||
return sandbox {};
|
||||
}
|
||||
|
||||
def sandbox::close(&mut this): void {
|
||||
var remove := process::command::new("rm");
|
||||
remove.arguments(["-rf", this.at.view()]);
|
||||
_ = remove.run();
|
||||
}
|
||||
|
||||
#! 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);
|
||||
}
|
||||
|
||||
#! 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.view().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.view().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.
|
||||
#! @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.view()}/config".view());
|
||||
command.set_variable("XDG_CACHE_HOME", $"{this.at.view()}/cache".view());
|
||||
command.set_variable("SOURCE_DATE_EPOCH", "1700000000");
|
||||
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.view()) else { return strview::empty(); };
|
||||
val from := at + marker.length;
|
||||
val rest := text.substr(from, text.length - from);
|
||||
val end := rest.find(")") else { return strview::empty(); };
|
||||
return rest.substr(0, end);
|
||||
}
|
||||
|
||||
#! 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".view(), $"""
|
||||
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}
|
||||
""".view());
|
||||
this.write($"{folder}/main.sakura".view(), """
|
||||
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}".view());
|
||||
}
|
||||
""");
|
||||
this.write($"{folder}/hello.geometry/shapes.sakura".view(), """
|
||||
module hello::geometry;
|
||||
|
||||
pub def area(w: i64, h: i64): i64 { return w * h; }
|
||||
""");
|
||||
this.write($"{folder}/hello.geometry/solids/solids.sakura".view(), """
|
||||
module hello::geometry::solids;
|
||||
|
||||
pub def cube(side: i64): i64 { return side * side * side; }
|
||||
""");
|
||||
this.write($"{folder}/tests/main.sakura".view(), """
|
||||
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); }
|
||||
""");
|
||||
}
|
||||
Reference in new issue
Block a user