From d59120b70540dec727120f899c38a2adf6328566 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 12:00:45 -0600 Subject: [PATCH 1/2] Docs(fix): Check the selected release types why: The first alpha predates several current handle types and has its own API reference. what: - Validate foundation types and the public handle classes declared by the selected source. --- scripts/docs/export.lua | 93 ++++++++--- scripts/docs/tests/test_export.lua | 249 ++++++++++++++++++++++------- 2 files changed, 262 insertions(+), 80 deletions(-) diff --git a/scripts/docs/export.lua b/scripts/docs/export.lua index 1752e43..1265c74 100644 --- a/scripts/docs/export.lua +++ b/scripts/docs/export.lua @@ -10,6 +10,7 @@ local shell = require("support.shell") local M = {} +---@type table M.EXPECTED_MODULES = { ["libtmux.query"] = { "NULL", @@ -30,22 +31,6 @@ M.EXPECTED_MODULES = { ["libtmux.runtime.luv"] = { "run" }, ["libtmux.runtime.nvim"] = { "start" }, } -M.EXPECTED_TYPES = { - "libtmux.Buffer", - "libtmux.Client", - "libtmux.Configurable", - "libtmux.Entity", - "libtmux.Pane", - "libtmux.Reference", - "libtmux.Request", - "libtmux.Runtime", - "libtmux.Selection", - "libtmux.Server", - "libtmux.Session", - "libtmux.Snapshot", - "libtmux.Window", - "libtmux.WindowLink", -} -- Public modules whose annotations the export adds to a copy of the source, so the runtime -- modules stay free of export-only tags. M.ANNOTATED = { @@ -140,7 +125,7 @@ function M.public_declarations(raw) local declarations = {} for _, entry in ipairs(raw) do local name = entry.name or "" - if name:sub(1, 8) == "libtmux." then + if name == "libtmux" or name:sub(1, 8) == "libtmux." then local item = M.normalize(entry) local fields = {} for _, field in ipairs(item.fields or {}) do @@ -230,14 +215,76 @@ function M.content_files(source, entries) return rows end +--- Public types declared in this checkout, plus the API shared by every release. +---@param source string +---@return table +function M.required_types(source) + local required = { + ["libtmux.Entity"] = true, + ["libtmux.Reference"] = true, + ["libtmux.Request"] = true, + ["libtmux.Runtime"] = true, + ["libtmux.Selection"] = true, + ["libtmux.Server"] = true, + ["libtmux.Snapshot"] = true, + } + local directory = source .. "/lua" + for _, relative in ipairs(fs.walk(directory)) do + if relative:match("%.lua$") then + local text = "\n" .. files.read(directory .. "/" .. relative) + for kind, name in text:gmatch("\n%-%-%-@(%a+)[ \t]+(libtmux%.[%w_%.]+)") do + if kind == "class" or kind == "alias" then + required[name] = true + end + end + end + end + return required +end + +local function required_modules(source) + local required = {} + for name, fields in pairs(M.EXPECTED_MODULES) do + required[name] = fields + end + -- Root coverage comes from the selected initializer. + required.libtmux = nil + local path = source .. "/lua/libtmux/init.lua" + if not fs.is_file(path) then + return required + end + + local text = "\n" .. files.read(path) + local class = text:find("\n%-%-%-@class[ \t]+libtmux%f[^%w_.]") + local returned = text:match("\nreturn[ \t]+([%a_][%w_]*)%f[^%w_]") + local fields = {} + if returned then + local member = returned .. "%.([%a][%w_]*)" + for line in text:gmatch("\n([^\n]*)") do + local name = line:match("^function[ \t]+" .. member .. "[ \t]*%(") + or line:match("^" .. member .. "[ \t]*=") + if name then + fields[#fields + 1] = name + end + end + end + -- Older initializers return an unannotated table containing only version metadata. + if class or #fields > 0 then + required.libtmux = fields + end + return required +end + ---@param payload table -function M.validate(payload) +---@param source? string +function M.validate(payload, source) + source = source or repo.root local declarations = {} for _, entry in ipairs(payload.declarations) do declarations[entry.name] = entry end local missing = {} - for _, name in ipairs(M.EXPECTED_TYPES) do + for name in pairs(M.required_types(source)) do if not declarations[name] then missing[#missing + 1] = name end @@ -246,7 +293,11 @@ function M.validate(payload) table.sort(missing) fail("missing public types: " .. table.concat(missing, ", ")) end - for module, expected in pairs(M.EXPECTED_MODULES) do + local modules = required_modules(source) + if declarations.libtmux and not modules.libtmux then + fail("unexpected public module: libtmux") + end + for module, expected in pairs(modules) do local entry = declarations[module] if not entry then fail("missing public module: " .. module) @@ -389,7 +440,7 @@ function M.build_payload(source, luals) } payload.examples, payload.api_examples = M.example_bundle(source, api_examples.public_symbols(declarations)) - M.validate(payload) + M.validate(payload, source) return payload end diff --git a/scripts/docs/tests/test_export.lua b/scripts/docs/tests/test_export.lua index c83f986..b068e22 100644 --- a/scripts/docs/tests/test_export.lua +++ b/scripts/docs/tests/test_export.lua @@ -6,6 +6,23 @@ local json = require("support.json") local repo = require("support.repo") local M = {} +local CURRENT_TYPES = { + "libtmux.Buffer", + "libtmux.Client", + "libtmux.Configurable", + "libtmux.Entity", + "libtmux.Pane", + "libtmux.Reference", + "libtmux.Request", + "libtmux.Runtime", + "libtmux.Selection", + "libtmux.Server", + "libtmux.Session", + "libtmux.Snapshot", + "libtmux.Window", + "libtmux.WindowLink", +} + local function read(relative) return files.read(repo.root .. "/" .. relative) end @@ -67,19 +84,10 @@ function M.test_the_export_is_deterministic_json() test.assertEquals(json.encode(value), '{"b":[2,{"z":1,"a":2}],"a":"x"}') end -function M.test_every_handle_class_is_a_required_export() - local expected = {} - for _, name in ipairs(export.EXPECTED_TYPES) do - expected[name] = true - end - local classes = {} - local entity = read("lua/libtmux/_internal/entity.lua") - for name in entity:gmatch("\n%-%-%-@class (libtmux%.[%w_]+)") do - classes[#classes + 1] = name - end - test.assertTrue(expected["libtmux.Session"]) - for _, name in ipairs(classes) do - test.assertTrue(expected[name], name .. " is not a required export") +function M.test_the_checkout_requires_all_current_handle_classes() + local required = export.required_types(repo.root) + for _, name in ipairs(CURRENT_TYPES) do + test.assertTrue(required[name], name) end end @@ -99,59 +107,182 @@ function M.test_declarations_drop_private_fields_and_sort_by_name_and_position() test.assertEquals(declarations[1].defines[1].file, "lua/x.lua") end -function M.test_the_export_refuses_what_it_must_not_publish() - local function payload(declarations) - local all = {} - for _, name in ipairs(export.EXPECTED_TYPES) do - all[#all + 1] = { name = name, fields = {}, defines = {} } - end - for module, names in pairs(export.EXPECTED_MODULES) do - local fields = {} - for _, name in ipairs(names) do - fields[#fields + 1] = { name = name, file = "lua/libtmux/x.lua" } - end - all[#all + 1] = { name = module, fields = fields, defines = {} } +function M.test_root_module_keeps_public_fields() + local raw = json.decode([==[[ + {"name":"libtmux","fields":[{"name":"_VERSION"},{"name":"option_names"}]}]]==]) + local declarations = export.public_declarations(raw) + test.assertEquals(#declarations, 1) + test.assertEquals(declarations[1].name, "libtmux") + test.assertEquals(#declarations[1].fields, 1) + test.assertEquals(declarations[1].fields[1].name, "option_names") +end + +local function historical_source() + local root = fs.mkdtemp("libtmux-docs-source-") + fs.mkdir_p(root .. "/lua/libtmux") + files.write(root .. "/lua/libtmux/init.lua", 'return { _VERSION = "0.1.1" }\n') + return root +end + +local function payload(source, declarations) + local all = {} + for name in pairs(export.required_types(source)) do + all[#all + 1] = { name = name, fields = {}, defines = {} } + end + for module, names in pairs(export.EXPECTED_MODULES) do + local fields = {} + for _, name in ipairs(names) do + fields[#fields + 1] = { name = name, file = "lua/libtmux/x.lua" } end - for _, extra in ipairs(declarations or {}) do - all[#all + 1] = extra + all[#all + 1] = { name = module, fields = fields, defines = {} } + end + for _, extra in ipairs(declarations or {}) do + all[#all + 1] = extra + end + return { declarations = all } +end + +function M.test_root_module_coverage_follows_the_selected_initializer() + local root = historical_source() + local init = root .. "/lua/libtmux/init.lua" + local historical_payload = payload(root) + local previous_root = export.EXPECTED_MODULES.libtmux + export.EXPECTED_MODULES.libtmux = { "catalog_field" } + local historical, historical_error = pcall(export.validate, historical_payload, root) + export.EXPECTED_MODULES.libtmux = previous_root + local extra_module, extra_module_error = + pcall(export.validate, payload(root, { { name = "libtmux" } }), root) + local missing_runtime = payload(root) + for _, entry in ipairs(missing_runtime.declarations) do + if entry.name == "libtmux.runtime.nvim" then + entry.fields = {} end - return { declarations = all } end - export.validate(payload()) - test.assertErrorMsgContains( - "invalid declaration source", - export.validate, - payload({ { name = "libtmux.X", fields = { { name = "x", file = "lua/tests/y.lua" } } } }) - ) - test.assertErrorMsgContains( - "invalid declaration source", - export.validate, - payload({ { name = "libtmux.X", fields = { { name = "x", file = "elsewhere.lua" } } } }) + local runtime, runtime_error = pcall(export.validate, missing_runtime, root) + + files.write( + init, + [[---@class libtmux +local Root = { _VERSION = "0.1.1" } +function Root.option_names() end +function Root.hook_names() end +Root.setting_descriptor = function() end +function Root._private() end +return Root +]] ) - test.assertErrorMsgContains( - "private helper exported", - export.validate, - payload({ { name = "libtmux.X", defines = { { name = "_p", file = "lua/a.lua" } } } }) + local missing_module, module_error = pcall(export.validate, payload(root), root) + local declaration = { + name = "libtmux", + fields = { { name = "option_names" }, { name = "hook_names" } }, + } + local missing_field, field_error = pcall(export.validate, payload(root, { declaration }), root) + declaration.fields[3] = { name = "setting_descriptor" } + local complete, complete_error = pcall(export.validate, payload(root, { declaration }), root) + declaration.fields[4] = { name = "unexpected" } + local extra_field, extra_error = pcall(export.validate, payload(root, { declaration }), root) + fs.remove_tree(root) + + test.assertTrue(historical, tostring(historical_error)) + test.assertFalse(extra_module) + test.assertStrContains(tostring(extra_module_error), "unexpected public module: libtmux") + test.assertFalse(runtime) + test.assertStrContains( + tostring(runtime_error), + "coverage differs for libtmux.runtime.nvim: missing=[start]" ) - test.assertErrorMsgContains( - "machine-specific path", - export.validate, - payload({ - { - name = "libtmux.X", - defines = { { name = "p", file = "lua/a.lua", note = "/home/x/y" } }, - }, - }) + test.assertFalse(missing_module) + test.assertStrContains(tostring(module_error), "missing public module: libtmux") + test.assertFalse(missing_field) + test.assertStrContains( + tostring(field_error), + "coverage differs for libtmux: missing=[setting_descriptor]" ) - local missing = payload() - table.remove(missing.declarations, 1) - test.assertErrorMsgContains("missing public types", export.validate, missing) - local extra = payload() - table.insert( - extra.declarations[#extra.declarations].fields, - { name = "surprise", file = "lua/a.lua" } + test.assertTrue(complete, tostring(complete_error)) + test.assertFalse(extra_field) + test.assertStrContains( + tostring(extra_error), + "coverage differs for libtmux: missing=[] extra=[unexpected]" ) - test.assertErrorMsgContains("public module coverage differs", export.validate, extra) +end + +function M.test_export_checks_the_classes_and_aliases_declared_in_the_selected_source() + local root = historical_source() + fs.mkdir_p(root .. "/lua/libtmux/_internal") + local entities = root .. "/lua/libtmux/_internal/entity.lua" + files.write(entities, "---@class libtmux.Reference\n---@class libtmux.Entity\n") + local archive = payload(root) + local ok, problem = pcall(export.validate, archive, root) + local options = root .. "/lua/libtmux/_internal/options_catalog.lua" + files.write(options, "---@alias libtmux.ShellOption string\n") + local accepted_option, missing_option = pcall(export.validate, archive, root) + assert(os.remove(options)) + files.append(entities, "---@class libtmux.Pane: libtmux.Entity\n") + local accepted, missing = pcall(export.validate, archive, root) + fs.remove_tree(root) + test.assertTrue(ok, tostring(problem)) + test.assertFalse(accepted_option) + test.assertStrContains(tostring(missing_option), "missing public types: libtmux.ShellOption") + test.assertFalse(accepted) + test.assertStrContains(tostring(missing), "missing public types: libtmux.Pane") +end + +function M.test_the_export_refuses_what_it_must_not_publish() + local root = historical_source() + local ok, problem = pcall(function() + export.validate(payload(root), root) + test.assertErrorMsgContains( + "invalid declaration source", + export.validate, + payload( + root, + { { name = "libtmux.X", fields = { { name = "x", file = "lua/tests/y.lua" } } } } + ), + root + ) + test.assertErrorMsgContains( + "invalid declaration source", + export.validate, + payload( + root, + { { name = "libtmux.X", fields = { { name = "x", file = "elsewhere.lua" } } } } + ), + root + ) + test.assertErrorMsgContains( + "private helper exported", + export.validate, + payload( + root, + { { name = "libtmux.X", defines = { { name = "_p", file = "lua/a.lua" } } } } + ), + root + ) + test.assertErrorMsgContains( + "machine-specific path", + export.validate, + payload(root, { + { + name = "libtmux.X", + defines = { { name = "p", file = "lua/a.lua", note = "/home/x/y" } }, + }, + }), + root + ) + local missing = payload(root) + table.remove(missing.declarations, 1) + test.assertErrorMsgContains("missing public types", export.validate, missing, root) + local extra = payload(root) + table.insert( + extra.declarations[#extra.declarations].fields, + { name = "surprise", file = "lua/a.lua" } + ) + test.assertErrorMsgContains("public module coverage differs", export.validate, extra, root) + end) + fs.remove_tree(root) + if not ok then + error(problem, 0) + end end function M.test_the_rock_metadata_and_bundles_come_from_the_checkout() From bf7fcd20d8ddcd29e371db2f4f11ba0b7805c14c Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 5 Dec 2026 12:00:45 -0600 Subject: [PATCH 2/2] Changelog: Rebuild the Lua API reference for early why: The first alpha predates several current handle types and has its own API reference. what: - Record the change under Unreleased. --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 24fb390..41cb381 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ Notable changes to `libtmux`. supplied script. - Docs: The API export identifies the numbered rock named by the selected checkout’s `release.json`, even when older rockspecs are present. +- Docs: The API reference for `v0.1.0alpha1` can be built from its own Lua + types. Each release must export the classes and aliases its source declares. - Runtime: `Runtime:start_server` starts a private tmux server and resolves to an `OwnedServer` with its `server` handle, its `socket_path` and `stop()`. The server lives in a new directory under `root` (the system temporary