Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / tests / generated / main.luc

# web/generated's fragments are what their generators write today, each built and run into
# build/tests/generated and compared byte for byte with the committed files:
# tools/gen_css_values (the value-dependent CSS fragments) and tools/embed_css (the embedded
# style sheets) from data/css, tools/gen_dom_tree (html/media_controls_dom.lucb) from
# data/html and the engine's tag and attribute names, and tools/gen_aria_roles
# (aria/aria_roles.lucb) from data/aria/AriaRoles.json.
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 css = fresh("css")
    must(build_tool("gen_css_values"), ["data/css", css])
    must(build_tool("embed_css"), ["data/css", css])
    let count = compare(css, "src/web/generated")
    print(f"ok    tools/gen_css_values, tools/embed_css: {count} files as committed")
    let html = fresh("html")
    must(build_tool("gen_dom_tree"), ["data/html", "src/web", html])
    print(f"ok    tools/gen_dom_tree: {compare(html, "src/web/generated/html")} file as committed")
    let aria = fresh("aria")
    must(build_tool("gen_aria_roles"), ["data/aria", aria])
    print(f"ok    tools/gen_aria_roles: {compare(aria, "src/web/generated/aria")} file as committed")
    return 0