~rootmos/lua-hack

9be5257951e394b25c9fa460ff6ddae19cf7c4ae — Gustav Behm 1 year, 1 month ago 49c1510 execute
Add tests for mixed pipe/string for stderr
2 files changed, 34 insertions(+), 14 deletions(-)

M execute.lua
M execute.test.lua
M execute.lua => execute.lua +22 -14
@@ 120,7 120,7 @@ function M.spawn(o)
    if o.stdout == true then
        stdout_fn = tmp .. "/stdout"
        table.insert(call, "1>" .. stdout_fn)
        touch(stdout_fn) -- TODO: add test ensuring this
        touch(stdout_fn)
    elseif o.stdout == false then
        table.insert(call, "1>/dev/null")
    elseif o.stdout == M.PIPE then


@@ 132,19 132,18 @@ function M.spawn(o)
        error("unexpected type of stdout")
    end

    local stderr
    local stderr_fn
    if o.stderr == true then
        local fn = tmp .. "/stderr"
        table.insert(call, "2>" .. fn)
        stderr = function() return io.open(fn):read("a") end
        stderr_fn = tmp .. "/stderr"
        table.insert(call, "2>" .. stderr_fn)
        touch(stderr_fn)
    elseif o.stderr == false then
        table.insert(call, "2>/dev/null")
    elseif o.stderr == M.PIPE then
        do_join = false
        local fn = tmp .. "/stderr"
        table.insert(call, "2>" .. fn)
        mkfifo(fn)
        stderr = function() return io.open(fn) end
        stderr_fn = tmp .. "/stderr"
        table.insert(call, "2>" .. stderr_fn)
        mkfifo(stderr_fn)
    elseif o.stderr ~= nil then
        error("unexpected type of stderr")
    end


@@ 173,8 172,8 @@ function M.spawn(o)
            p.stdout = io.open(stdout_fn):read("a")
        end

        if stderr then
            p.stderr = stderr()
        if stderr_fn then
            p.stderr = io.open(stderr_fn):read("a")
        end

        if o.check then


@@ 185,7 184,7 @@ function M.spawn(o)
    else
        local q = popen("source " .. harness_fn)
        local returncode_f = io.open(returncode_fn)
        local stdout_f
        local stdout_f, stderr_f

        local p = setmetatable({
            join = function(p)


@@ 202,6 201,11 @@ function M.spawn(o)
                    p.stdout = stdout_f:read("a")
                    stdout_f:close()
                    return p.stdout
                elseif t == "stderr" then
                    p:join()
                    p.stderr = stderr_f:read("a")
                    stderr_f:close()
                    return p.stderr
                else
                    return nil
                end


@@ 229,8 233,12 @@ function M.spawn(o)
            end
        end

        if stderr then
            p.stderr = stderr()
        if stderr_fn then
            if o.stderr == M.PIPE then
                p.stderr = io.open(stderr_fn)
            elseif o.stderr == true then
                stderr_f = io.open(stderr_fn)
            end
        end

        return p

M execute.test.lua => execute.test.lua +12 -0
@@ 109,6 109,18 @@ function test_pipe_stdin_string_stdout_foo()
    lu.assertEquals(p.stdout, "foo")
end

function test_string_stdin_pipe_stderr_foo()
    local p = E.spawn{"sh", "-c", "cat 1>&2", stdin="foo", stderr=E.PIPE}
    lu.assertEquals(p.stderr:read(), "foo")
end

function test_pipe_stdin_string_stderr_foo()
    local p = E.spawn{"sh", "-c", "cat 1>&2", stdin=E.PIPE, stderr=true}
    p:write("foo")
    p.stdin:close()
    lu.assertEquals(p.stderr, "foo")
end

function test_sleep()
    lu.assertEquals(E.popen{"sleep", 1}.returncode, 0)
end