Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.4

0.7.4 / tests / process / process.lucb

## The `process` module against its archive: `run` spawns a child, waits for it, and
## answers its exit status with the captured standard output and error, owned by the
## result and released with it. The child program is `sh`, which resolves through PATH on
## every POSIX host these tests run on, and on Windows to Git's; Windows has no `echo`
## program on PATH outside Git's bash.

test "run captures a child's standard output and a zero exit status":
    # `hello there` reaches the child as one argument, the space and all
    let arguments: c.str[4] = ["-c", "printf %s \"$1\"", "sh", "hello there"]
    let done = try run("sh", arguments)
    defer done.release()
    let status = done.value.exit_code
    let out = done.value.output
    let err = done.value.error_output
    assert(status == 0)
    assert((str)out == "hello there")
    assert(err.length == 0)

test "run reports a non-zero exit status and leaves output empty":
    let arguments: c.str[2] = ["-c", "exit 3"]
    let done = try run("sh", arguments)
    defer done.release()
    let status = done.value.exit_code
    let out = done.value.output
    let err = done.value.error_output
    assert(status == 3)
    assert(out.length == 0)
    assert(err.length == 0)

test "run captures standard error separately from standard output":
    let arguments: c.str[2] = ["-c", "echo boom 1>&2"]
    let done = try run("sh", arguments)
    defer done.release()
    let status = done.value.exit_code
    let out = done.value.output
    let err = done.value.error_output
    assert(status == 0)
    assert(out.length == 0)
    assert((str)err == "boom\n")

test "run reports 127 when the program cannot be executed":
    let arguments: c.str[1] = ["ignored"]
    let done = try run("luce_no_such_program_xyzzy", arguments)
    defer done.release()
    let status = done.value.exit_code
    let out = done.value.output
    let err = done.value.error_output
    assert(status == 127)

test "call lets the child write to our streams and answers its exit status":
    let arguments: c.str[2] = ["-c", "test -d . && exit 5"]
    assert(try call("sh", arguments, directory = "/") == 5)
    let none_at_all: c.str[1] = ["-n"]
    assert(try call("luce-std-no-such-program", none_at_all[..<0]) == 127)

test "environment variables are set, read, listed and removed":
    try set_variable("LUCE_STD_TEST_VARIABLE", "seven")
    assert(variable("LUCE_STD_TEST_VARIABLE") == "seven")
    let all = try variables()
    defer all.release()
    var found = false
    for entry in all.value:
        found = found or entry == "LUCE_STD_TEST_VARIABLE=seven"
    assert(found)
    try unset_variable("LUCE_STD_TEST_VARIABLE")
    assert(variable("LUCE_STD_TEST_VARIABLE") == none)
    try unset_variable("LUCE_STD_TEST_VARIABLE")

test "the working directory is owned text that can change":
    let before = try current_directory()
    defer before.release()
    try change_directory("/")
    let root = try current_directory()
    defer root.release()
    # Windows answers the root of the current drive, `C:\`
    assert(paths.root(root.value) == root.value and paths.root(root.value).length > 0)
    try change_directory((c.str)before.value)
    assert(id() > 0)