Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.1

0.7.1 / tests / file_walk / main.luc

# A bounded walk (check.lucb) through links, a cycle, a skipped directory and 24 levels,
# moving the tree as it goes.
from luce_std import files, paths, process

let failed = ErrorCode.package(1)

# Run `program`, failing unless it exits 0; what it printed.
func must(program: str, arguments: list[str]) -> str!:
    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}")
    return done.output

# A fresh scratch directory under build/tests/<name>, and check.lucb beside this program
# built into it with LUCE_BASE; the scratch directory and the built check.
func prepare(name: str) -> (str, str)!:
    let here = process.variable("LUC_TEST_DIR") else f"tests/{name}"
    let base = process.variable("LUCE_BASE") else "luce-base"
    let work = paths.join(process.current_directory(), f"build/tests/{name}/work")
    if files.exists(work):
        files.remove_tree(work)
    files.ensure_directory(work)
    let check = paths.join(work, "check")
    _ = must(base, ["build", paths.join(here, "check.lucb"), "-o", check])
    return (work, check)

func expect(condition: bool, what: str) -> unit!:
    if not condition:
        error(failed, what)

pub func main(arguments: list[str]) -> int!:
    let (work, check) = prepare("file_walk")
    let original = paths.join(work, "original")
    let moved = paths.join(work, "moved")
    files.ensure_directory(paths.join(original, "sub"))
    files.write(paths.join(original, "data"), b"data")
    files.write(paths.join(original, "sub/payload"), b"payload")
    files.create_symlink("..", paths.join(original, "sub/back"))
    files.create_symlink("sub", paths.join(original, "link"))
    files.create_symlink("missing", paths.join(original, "dangling"))
    files.ensure_directory(paths.join(original, "skip"))
    files.write(paths.join(original, "skip/hidden"), b"")
    var leaf = original
    for index in 0..<24:
        leaf = paths.join(leaf, f"level-{index:02}")
        files.ensure_directory(leaf)
    files.write(paths.join(leaf, "leaf"), b"leaf")
    _ = must(check, [original, moved])
    expect(files.exists(paths.join(moved, "skip/hidden")), "the skipped directory did not move")
    files.remove_tree(work)
    return 0