Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.1

0.6.1 / tests / generated / main.luc

# web_unicode's generated tables (src/web_unicode/generated_*.lucb) are what tools/gen_ucd
# writes from data today: built and run into build/tests/generated, every file it writes is
# the committed one byte for byte.
from luce_std import files, paths, process
import platform

let failed = ErrorCode.package(1)

## Run `program` with `arguments`, failing unless it exits 0.
func must(program: str, arguments: list[str]) -> unit!:
    let done = process.run(program, arguments)
    if done.exit_code != 0:
        error(failed, f"{program} {arguments}: exit status {done.exit_code}\n{done.output}{done.error_output}")

## A fresh directory build/tests/generated/<name>.
func fresh(name: str) -> str!:
    let directory = paths.join(process.current_directory(), f"build/tests/generated/{name}")
    if files.exists(directory):
        files.remove_tree(directory)
    files.ensure_directory(directory)
    return directory

## Build the generator tools/<tool> with the compiler `luc test` names; its executable.
func build_tool(tool: str) -> str!:
    let base = process.variable("LUCE_BASE") else "luce-base"
    files.ensure_directory(paths.join(process.current_directory(), "build/tests/generated/tools"))
    let executable = paths.join(process.current_directory(), f"build/tests/generated/tools/{tool}" + (".exe" if platform.windows else ""))
    must(base, ["build", f"tools/{tool}", "-o", executable])
    return executable

## Every .lucb file written into `generated` is the committed file of its name in
## `committed`, byte for byte; the number compared.
func compare(generated: str, committed: str) -> int!:
    var count = 0
    for name in files.list(generated):
        if not name.ends_with(".lucb"):
            continue
        let ours = paths.join(committed, name)
        if not files.exists(ours):
            error(failed, f"{ours} is generated but not committed")
        if files.read_text(paths.join(generated, name)) != files.read_text(ours):
            error(failed, f"{ours} is not what its generator writes now (regenerate it and review the change)")
        count += 1
    return count

pub func main(arguments: list[str]) -> int!:
    let out = fresh("web_unicode")
    must(build_tool("gen_ucd"), ["data", out])
    let count = compare(out, "src/web_unicode")
    print(f"ok    tools/gen_ucd: {count} files as committed")
    return 0