Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions docs/runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
10 changes: 6 additions & 4 deletions examples/api/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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` |
Expand Down
26 changes: 16 additions & 10 deletions examples/api/connect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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))
4 changes: 2 additions & 2 deletions examples/api/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion scripts/docs/tests/test_api_examples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ end
-- Lua program on the library.
local OUTSIDE_THE_LIBRARY = {
"run.sh",
"TMUX_SOCKET",
"os.execute",
"io.popen",
"perl",
Expand Down Expand Up @@ -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),
Expand Down
16 changes: 10 additions & 6 deletions scripts/package/package.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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",
},
}
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/test_server.lua
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Loading