diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index c1d570a..e303de8 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -424,9 +424,8 @@ default socket stops it, waits for it to exit and removes the file, as [the default-socket test](../tests/integration/test_socket_default.lua) does. A program that needs a server of its own starts one with `Runtime:start_server` -and ends it with `OwnedServer:stop`, as every program under -[examples/api](../examples/api/README.md) does, so no shell step starts or stops -tmux. Run one by hand: +and ends it with `OwnedServer:stop`. The [API examples](../examples/api/README.md) +do this by default. Run one by hand: ```console $ lua examples/api/new_session.lua diff --git a/CHANGES.md b/CHANGES.md index 8b42e02..fcf7148 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,13 @@ Notable changes to `libtmux`. `examples/api/run.sh`, which needed `sh` and `perl`, is removed. Run a program with `lua examples/api/connect.lua`. The programs find tmux on `PATH` and no longer read `TMUX_SOCKET`. (#103, #109) +- Examples: by default each program starts and stops its own server, so + `examples/api/run.sh`, which needed `sh` and `perl`, is removed. Run a program + with `lua examples/api/connect.lua`; `TMUX_BIN` still selects another tmux, + and `TMUX_SOCKET` is no longer read. (#103) +- Examples: set `LIBTMUX_SOCKET_PATH` to an existing server's socket to have + `connect.lua` find and print the `work` session there. The example leaves + that server running. (#116) - Docs: the README shows `Pane:resize` and `Window:resize` with a new tested program, `examples/api/resize.lua`, and links the kill, option and hook methods. diff --git a/docs/runtime.md b/docs/runtime.md index 2c54c7b..c24a1ed 100644 --- a/docs/runtime.md +++ b/docs/runtime.md @@ -72,6 +72,9 @@ returns `start_failed` with its cause. `OwnedServer` has `server`, the same handle `connect` returns, and `socket_path`, which a second handle can connect to. `server:close()` releases the handle and leaves the daemon running. +The [connection example](../examples/api/connect.lua) prints the name of the +`work` session on the socket in `LIBTMUX_SOCKET_PATH`. With that variable +unset or empty, it creates `work` on a private server and stops it afterward. `owned:stop():await()` ends the server: diff --git a/examples/api/README.md b/examples/api/README.md index bc3f908..b310d42 100644 --- a/examples/api/README.md +++ b/examples/api/README.md @@ -1,8 +1,9 @@ # Complete API examples -Each Lua file is a standalone program. It starts its own private tmux -server with `Runtime:start_server`, uses it and stops it, even when the program -fails. No existing session is needed. +The programs run on private tmux servers and clean them up on exit. To try +`connect.lua` with an existing server, set `LIBTMUX_SOCKET_PATH` to its +absolute socket path. The example looks for a session named `work` and leaves +the server running. Needs Lua, LuaRocks, Git, tmux 3.3a or newer on `PATH`, a C compiler and CMake on Linux. From the repository root, install the library and its @@ -18,6 +19,7 @@ Run one complete program: ```console $ lua examples/api/connect.lua +work ``` The window examples find tmux on `PATH`. Set `TMUX_BIN` to an absolute path @@ -27,7 +29,7 @@ to select a different build. | --- | --- | --- | | [start_server.lua](start_server.lua) | Start a private server, use it and stop it | `sessions: 0, then 1` | | [with_server.lua](with_server.lua) | Run a body on a server that stops when the body ends | `sessions: 1` | -| [connect.lua](connect.lua) | Connect a second handle to a started server | `connected` | +| [connect.lua](connect.lua) | Find the `work` session on a tmux server | `work` | | [snapshot.lua](snapshot.lua) | List sessions, windows, and panes | Two of each | | [new_session.lua](new_session.lua) | Create a session | `session: demo` | | [new_window.lua](new_window.lua) | Create a window in that session | `window: logs` | diff --git a/examples/api/connect.lua b/examples/api/connect.lua index b4d8a06..bc75b91 100644 --- a/examples/api/connect.lua +++ b/examples/api/connect.lua @@ -8,19 +8,25 @@ local function must(value, err) end must(adapter.run(function(runtime) - local owned = must(runtime:start_server():await()) - local session = { name = "demo", argv = { "/bin/cat" } } - must(owned.server:new_session(session):await()) + local socket = os.getenv("LIBTMUX_SOCKET_PATH") + local owned + if not socket or socket == "" then + owned = must(runtime:start_server():await()) + local session = { name = "work", argv = { "/bin/cat" } } + must(owned.server:new_session(session):await()) + socket = owned.socket_path + end - -- A second handle reaches the same daemon and leaves it running on close. - local target = { socket_path = owned.socket_path } + local target = { socket_path = socket } local server = must(runtime:connect(target):await()) local snapshot = must(server:snapshot({ strict = true }):await()) - local demo = snapshot.sessions:where({ name = "demo" }) - assert(#demo == 1, "demo session is missing") - print("connected") - must(server:close():await()) - must(owned:stop():await()) + local work = snapshot.sessions:where({ name = "work" }) + assert(work[1], "The work session does not exist") + print(work[1].name) + + if owned then + must(owned:stop():await()) + end return true end)) diff --git a/examples/api/manifest.json b/examples/api/manifest.json index f41bd9d..185f878 100644 --- a/examples/api/manifest.json +++ b/examples/api/manifest.json @@ -32,8 +32,8 @@ "libtmux.OwnedServer.socket_path" ], "file": "examples/api/connect.lua", - "description": "Connect a second handle to a running tmux daemon without taking ownership of it.", - "stdout": "connected\n" + "description": "Find the session named work on a supplied tmux socket or try it on a private server.", + "stdout": "work\n" }, { "id": "snapshot", diff --git a/scripts/docs/tests/test_api_examples.lua b/scripts/docs/tests/test_api_examples.lua index fd4469c..b254ba9 100644 --- a/scripts/docs/tests/test_api_examples.lua +++ b/scripts/docs/tests/test_api_examples.lua @@ -28,7 +28,6 @@ end -- Lua program on the library. local OUTSIDE_THE_LIBRARY = { "run.sh", - "TMUX_SOCKET", "os.execute", "io.popen", "perl", @@ -86,6 +85,10 @@ function M.test_complete_programs_cover_their_existing_targets() test.assertStrContains(source, "owned:stop():await()") end test.assertNil(source:find('require("tests.', 1, true)) + test.assertNil( + source:match("%f[%w_]TMUX_SOCKET%f[^%w_]"), + example.id .. " uses TMUX_SOCKET" + ) for _, word in ipairs(OUTSIDE_THE_LIBRARY) do test.assertNil( source:find(word, 1, true), diff --git a/scripts/package/package.lua b/scripts/package/package.lua index 1b22ce2..d0369ad 100644 --- a/scripts/package/package.lua +++ b/scripts/package/package.lua @@ -352,6 +352,7 @@ function M.complete_api_examples(prefix, version, lua, cwd, opts) local ran = 0 with_live_examples(prefix, version, opts, function(child, temporary, binary) child.cwd = directory + child.env.LIBTMUX_SOCKET_PATH = "" -- Run connect.lua on a server the example owns. local major, minor = M.tmux_release(binary) -- Input can reach the terminal before the interactive shell draws its prompt. local startup = directory .. "/capture-startup" @@ -396,8 +397,6 @@ function M.complete_api_examples(prefix, version, lua, cwd, opts) end end end - -- The runtime failure comes after the server started, so it proves the cleanup. - local connect = files.read(directory .. "/connect.lua") local failures = { { "import", @@ -406,10 +405,15 @@ function M.complete_api_examples(prefix, version, lua, cwd, opts) }, { "runtime", - (connect:gsub( - ' print%("connected"%)', - ' error("intentional example failure", 0)' - )), + [[ +local adapter = require("libtmux.runtime.luv") +local ok, err = adapter.run(function(runtime) + local server, cause = runtime:start_server({ binary = os.getenv("TMUX_BIN") }):await() + assert(server, tostring(cause)) + error("intentional example failure", 0) +end) +assert(ok, tostring(err)) +]], "intentional example failure", }, } diff --git a/tests/integration/test_server.lua b/tests/integration/test_server.lua index 983ebe7..3e79540 100644 --- a/tests/integration/test_server.lua +++ b/tests/integration/test_server.lua @@ -1,6 +1,9 @@ -- Public snapshot capture preserves links and sessionless daemon buffers. local test = require("luaunit") +local child = require("tests.support.child") local fixture = require("tests.support.fixture") +local repo = require("support.repo") +local uv = require("support.uv") local M = {} local function run_case(mode, host) @@ -34,4 +37,37 @@ fixture.each_host(M, "retained_pane_keeps_default_snapshots_and_native_pid_types run_case("retained", host) end) +function M.test_connect_example_reads_work_on_the_supplied_socket() + fixture.with(function(f) + assert(uv.fs_symlink(f.binary, f.path .. "/tmux")) + local function connect(socket) + return child.run({ fixture.lua(), "examples/api/connect.lua" }, { + env = f:program_env({ + LIBTMUX_SOCKET_PATH = socket, + PATH = f.path .. ":" .. (f.env.PATH or ""), + }), + cwd = repo.root, + timeout = fixture.DEADLINE, + }) + end + local function sessions() + return f:out("list-sessions", "-F", "#{session_id} #{session_name}") + end + + f:run("rename-session", "-t", "$0", "work-long") + local missing = connect(f.socket) + test.assertNotEquals(missing.code, 0, missing.stdout) + test.assertStrContains(missing.stderr, "The work session does not exist") + test.assertEquals(missing.stdout, "") + test.assertEquals(sessions(), "$0 work-long\n") + + f:run("rename-session", "-t", "$0", "work") + local found = connect(f.socket) + test.assertEquals(found.code, 0, found.stderr) + test.assertEquals(found.stdout, "work\n") + test.assertEquals(sessions(), "$0 work\n") + test.assertEquals(f:pins(), {}) + end) +end + return M