Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-std

Source of release 0.7.0

0.7.0 / tests / unit / process_test.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 programs -- `echo` and `sh` -- resolve through
## PATH on every POSIX host these tests run on.
import c
import process

test "run captures a child's standard output and a zero exit status":
    let arguments: c.str[2] = ["-n", "hello there"]
    let done = try process.run("echo", 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 process.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 process.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 process.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 process.call("sh", arguments, directory = "/") == 5)
    let none_at_all: c.str[1] = ["-n"]
    assert(try process.call("luce-std-no-such-program", none_at_all[..<0]) == 127)

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

test "the working directory is owned text that can change":
    let before = try process.current_directory()
    defer before.release()
    try process.change_directory("/")
    let root = try process.current_directory()
    defer root.release()
    assert(root.value == "/")
    try process.change_directory((c.str)before.value)
    assert(process.id() > 0)