diff --git a/tests/integration/program_contract.lua b/tests/integration/program_contract.lua index 4c03f32..48cd521 100644 --- a/tests/integration/program_contract.lua +++ b/tests/integration/program_contract.lua @@ -208,6 +208,60 @@ local function poll(check) error("program state did not become ready", 0) end +local function output_witness(server) + package.path = "scripts/?.lua;" .. package.path + local files = require("support.files") + local directory = assert(os.getenv("LIBTMUX_PROGRAM_TEST_DIR")) + local label = assert(os.getenv("LIBTMUX_PROGRAM_JOB")) + local program = must(server + :start_program({ + "/bin/sh", + "-c", + [[stty -echo || exit 99 +: > "$1/ready" +IFS= read -r token || exit 98 +printf '%s' "$token" > "$1/ack" +printf 'progress %s\r\033[Kdone' "$2" +IFS= read -r finish || exit 97 +/bin/sh -c 'test "$1" = A || exit 7' job "$2" +status=$? +printf '%s' "$status" > "$1/exit" +exit "$status"]], + "job", + directory, + label, + }, { width = 40, height = 8 }) + :await()) + local ref = program:reference() + files.write(directory .. "/observing.tmp", ref.session_id .. " " .. ref.pane_id) + assert(uv.fs_rename(directory .. "/observing.tmp", directory .. "/observing")) + must(server:command({ "wait-for", "witness-listening" }):await()) + must(program:send_text("token-" .. label):await()) + must(program:send_keys({ "Enter" }):await()) + local snapshot = must(server:snapshot():await()) + local row = must(snapshot.panes:where({ id = ref.pane_id }):one()) + local pane = must(server:handle(snapshot, row)) + must(pane:wait_for_text("done", { timeout = 3000 }):await()) + must(server:command({ "wait-for", "witness-captured" }):await()) + must(program:send_keys({ "Enter" }):await()) + local finished = must(program:wait({ timeout = 3000 }):await()) + assert(finished.cleanup.pane_closed and finished.output.complete) + local screen = must(finished.screen, finished.screen_error) + assert( + screen.final and screen.bytes == files.read(directory .. "/screen"), + "Program screen differs from capture-pane" + ) + assert( + finished.output.bytes == files.read(directory .. "/terminal"), + "Program output differs from tmux -C" + ) + local expected = tonumber(files.read(directory .. "/exit")) + assert( + finished.kind == "exit" and finished.exit_code == expected, + ("expected exit %s, got %s"):format(expected, finished.exit_code) + ) +end + local function operator_stop(server) local mode = assert(os.getenv("LIBTMUX_PROGRAM_OPERATOR_MODE")) local receipt = assert(os.getenv("LIBTMUX_PROGRAM_TEST_DIR")) .. "/operator-exit" @@ -468,6 +522,7 @@ local cases = { noninteractive = noninteractive, admission = admission, stopping = stopping, + output_witness = output_witness, operator_stop = operator_stop, cancellation = cancellation, replacement = replacement, diff --git a/tests/integration/test_program_contract.lua b/tests/integration/test_program_contract.lua index dd4d1ea..3bf2c9d 100644 --- a/tests/integration/test_program_contract.lua +++ b/tests/integration/test_program_contract.lua @@ -1,4 +1,6 @@ local test = require("luaunit") +local child = require("tests.support.child") +local files = require("support.files") local fixture = require("tests.support.fixture") local repo = require("support.repo") local M = {} @@ -33,6 +35,79 @@ for _, case in ipairs({ end) end +local function terminal_bytes(control, pane) + local bytes = {} + for line in control:output():gmatch("([^\n]+)\n") do + local id, data = line:match("^%%output (%%%d+) (.*)$") + if id == pane then + bytes[#bytes + 1] = data:gsub("\\([0-7][0-7][0-7])", function(octal) + return string.char(tonumber(octal, 8)) + end) + end + end + return table.concat(bytes) +end + +fixture.each_host(M, "public_program_same_screen_different_output_and_exit", function(host) + local first_screen + for _, label in ipairs({ "A", "B" }) do + fixture.with(function(f) + local directory = assert(f.path) + local app = f:start_program(host, "program_contract", { + LIBTMUX_PROGRAM_CASE = "output_witness", + LIBTMUX_PROGRAM_TEST_DIR = directory, + LIBTMUX_PROGRAM_JOB = label, + }) + test.assertTrue( + child.pump(function() + return files.exists(directory .. "/observing") + and files.exists(directory .. "/ready") + end, fixture.DEADLINE), + app:errors() + ) + local session, pane = files.read(directory .. "/observing"):match("^(%$%d+) (%%%d+)$") + test.assertNotNil(pane) + local control = f:spawn( + { "-C", "attach-session", "-f", "ignore-size", "-t", session }, + { stdin = "pipe" } + ) + control:write("display-message -p WITNESS_READY\n") + test.assertTrue( + child.pump(function() + return control:output():find("\nWITNESS_READY\n", 1, true) ~= nil + end, fixture.DEADLINE), + control:errors() + ) + f:run("wait-for", "-S", "witness-listening") + local expected = "progress " .. label .. "\r\027[Kdone" + test.assertTrue( + child.pump(function() + return terminal_bytes(control, pane):find("done", 1, true) ~= nil + end, fixture.DEADLINE), + control:output() + ) + local bytes = terminal_bytes(control, pane) + local screen = f:out("capture-pane", "-p", "-t", pane) + test.assertEquals(bytes, expected) + test.assertStrMatches(screen, "^done\n+$") + if first_screen then + test.assertEquals(screen, first_screen) + end + first_screen = screen + test.assertEquals(files.read(directory .. "/ack"), "token-" .. label) + files.write(directory .. "/terminal", bytes) + files.write(directory .. "/screen", screen) + control:write("detach-client\n") + test.assertTrue(control:wait(fixture.DEADLINE), control:errors()) + f:run("wait-for", "-S", "witness-captured") + local result = app:finish(fixture.DEADLINE) + test.assertEquals(result.code, 0, result.stderr) + test.assertEquals(files.read(directory .. "/exit"), label == "A" and "0" or "7") + test.assertEquals(f:out("list-sessions", "-F", "#{session_name}"), "fixture\n") + end) + end +end) + for _, mode in ipairs({ "session", "server" }) do fixture.each_host(M, "public_program_operator_stop_" .. mode, function(host) fixture.with(function(f)