From 719d902b61ad27369a90b4bfdf9a31f651e6fd6e Mon Sep 17 00:00:00 2001 From: Bryce Kwon Date: Sat, 7 Nov 2026 10:19:22 -1000 Subject: Decide every unit check skip in the shell Where a standalone Lua had no lpeg, the harness skipped the markdown and man checks itself, behind a note the test library hides, and the test passed on the six checks left. The real luaossl checks were left out the same way. Each now shows as a skip under a prerequisite named for the interpreter, and a misspelt mode fails rather than running the default. --- tests/extensions/harness.lua | 16 ++++++++++++++++ tests/extensions/lib.sh | 18 ++++++++++++++++++ tests/extensions/test-about-render.lua | 23 ++++++----------------- tests/extensions/test-email-avatar.lua | 2 +- tests/extensions/test-link-commits.lua | 8 +++++--- tests/extensions/test-link-trailers.lua | 8 +++++--- tests/extensions/test-syntax-highlight.lua | 2 +- 7 files changed, 52 insertions(+), 25 deletions(-) (limited to 'tests/extensions') diff --git a/tests/extensions/harness.lua b/tests/extensions/harness.lua index 8c1ba6c..b54f727 100644 --- a/tests/extensions/harness.lua +++ b/tests/extensions/harness.lua @@ -82,6 +82,22 @@ function harness.load(script) dofile(script) end +-- The mode a t05xx script names after the script path, which has to be one +-- of those given, so a misspelt mode fails instead of quietly running +-- another one. Lua 5.1 gives a function taking ... a local arg of its own, +-- nil here, so the script's arguments are read from the global table. +function harness.mode(...) + local known = { ... } + local wanted = _G.arg[2] + for _, mode in ipairs(known) do + if wanted == mode then + return mode + end + end + error("unknown mode " .. tostring(wanted) .. ", wanted one of " .. + table.concat(known, ", ")) +end + -- Load a script with some of its own text replaced first, for checks that -- need the commented-out examples of its configuration switched on. Each -- replacement has to match exactly once, so a reworded example fails here diff --git a/tests/extensions/lib.sh b/tests/extensions/lib.sh index 8584446..56098d0 100644 --- a/tests/extensions/lib.sh +++ b/tests/extensions/lib.sh @@ -49,6 +49,24 @@ done # the count, as skipped, rather than vanishing from it. test -z "$interpreters" || test_set_prereq STANDALONE_LUA +# Leaves in lua_prereq the name of a prerequisite that holds when the +# interpreter given first can load every module named after it, so a check +# needing a module shows as skipped under an interpreter that lacks it. +lua_module_prereq() { + lua_prereq=$(printf '%s' "$*" | tr -c 'A-Za-z0-9' '_' | tr 'a-z' 'A-Z') + lua_prereq_bin=$1 + shift + lua_prereq_code= + for lua_prereq_module + do + lua_prereq_code="$lua_prereq_code require('$lua_prereq_module')" + done + if "$lua_prereq_bin" -e "$lua_prereq_code" >/dev/null 2>&1 + then + test_set_prereq "$lua_prereq" + fi +} + # Asks whether cgit's own Lua can require every module named, by pointing a # throwaway commit-filter at the given repository and reading its answer off # the rendered commit page. Filters need a page to run on, which is why a diff --git a/tests/extensions/test-about-render.lua b/tests/extensions/test-about-render.lua index 98b84b8..e6813ef 100644 --- a/tests/extensions/test-about-render.lua +++ b/tests/extensions/test-about-render.lua @@ -1,25 +1,18 @@ -- Unit checks for custom/extensions/about-render.lua, run under a standalone -- Lua by t0501-about-render.sh with the script path as the first argument. --- The second argument may be nolpeg, which makes the lpeg module fail to --- load so the escaped plain text fallback is proven even on a machine where --- lpeg is installed. Without it the markdown and man checks run when lpeg is --- present and are skipped with a note when it is not, the same way the --- filter itself degrades. +-- The second argument is the mode. In lpeg mode, which t0501 asks for only +-- where the interpreter has lpeg, the markdown and man checks run. In nolpeg +-- mode the lpeg module fails to load, so the escaped plain text fallback is +-- proven even on a machine where lpeg is installed. local test_directory = arg[0]:match("^(.*)/") or "." local h = dofile(test_directory .. "/harness.lua") local script = arg[1] -local mode = arg[2] or "" +local mode = h.mode("lpeg", "nolpeg") -local has_lpeg = false if mode == "nolpeg" then h.preload_failure("lpeg") -else - has_lpeg = pcall(require, "lpeg") - if not has_lpeg then - io.write("lpeg not installed, markdown and man checks skipped\n") - end end h.load(script) @@ -27,7 +20,7 @@ h.load(script) -- A missing lpeg is written to the error log as the script loads. if mode == "nolpeg" then h.contains("a missing lpeg is named in the error log", h.error_log(), "Unable to load lpeg") -elseif has_lpeg then +else h.equals("with lpeg the error log stays empty", h.error_log(), "") end @@ -72,10 +65,6 @@ if mode == "nolpeg" then h.finish() end -if not has_lpeg then - h.finish() -end - -- Markdown. out = render("README.md", "# Title") diff --git a/tests/extensions/test-email-avatar.lua b/tests/extensions/test-email-avatar.lua index 65c4ce9..199b4ac 100644 --- a/tests/extensions/test-email-avatar.lua +++ b/tests/extensions/test-email-avatar.lua @@ -11,7 +11,7 @@ local test_directory = arg[0]:match("^(.*)/") or "." local h = dofile(test_directory .. "/harness.lua") local script = arg[1] -local mode = arg[2] or "stub" +local mode = h.mode("stub", "missing", "real") local base_url = "https://gravatar.com/avatar/" if script:find("libravatar", 1, true) then diff --git a/tests/extensions/test-link-commits.lua b/tests/extensions/test-link-commits.lua index ea342bf..45a3f60 100644 --- a/tests/extensions/test-link-commits.lua +++ b/tests/extensions/test-link-commits.lua @@ -9,7 +9,9 @@ local test_directory = arg[0]:match("^(.*)/") or "." local h = dofile(test_directory .. "/harness.lua") -h.load(arg[1]) +local script = arg[1] + +h.load(script) local function render(text) return h.run({}, { text }) @@ -19,7 +21,7 @@ local out, ret = render("see #123 near deadbee") h.equals("an unedited copy leaves issue marks as text", out, "see #123 near deadbee") -h.load_with(arg[1], { { "-- { pattern = \"#(%d+)\"", "{ pattern = \"#(%d+)\"" } }) +h.load_with(script, { { "-- { pattern = \"#(%d+)\"", "{ pattern = \"#(%d+)\"" } }) out, ret = render("see #123 for details") h.contains("issue mark becomes a link", out, "#123") @@ -78,7 +80,7 @@ h.equals("a message with no matches passes through", out, "plain words only") -- A rule broad enough to capture a quote or a percent sign still has to -- leave a url that stays inside its attribute and means what it says. -h.load_with(arg[1], { { "local rules = {", "local rules = { { pattern = \"T:(%S+)\", url = \"https://t/%s\" }," } }) +h.load_with(script, { { "local rules = {", "local rules = { { pattern = \"T:(%S+)\", url = \"https://t/%s\" }," } }) out = render("T:a'b%c") h.equals("a captured value is percent-encoded into the url", out, "T:a'b%c") diff --git a/tests/extensions/test-link-trailers.lua b/tests/extensions/test-link-trailers.lua index 9c3b264..01854a0 100644 --- a/tests/extensions/test-link-trailers.lua +++ b/tests/extensions/test-link-trailers.lua @@ -9,7 +9,9 @@ local test_directory = arg[0]:match("^(.*)/") or "." local h = dofile(test_directory .. "/harness.lua") -h.load(arg[1]) +local script = arg[1] + +h.load(script) local function render(key, text) return h.run({ key, "commit" }, { text }) @@ -17,7 +19,7 @@ end h.equals("an unedited copy leaves a Closes mark as text", render("Closes", "#45"), "#45") -h.load_with(arg[1], { +h.load_with(script, { { "-- { key = \"Closes\"", "{ key = \"Closes\"" }, { "-- { key = \"Bug\"", "{ key = \"Bug\"" }, }) @@ -73,7 +75,7 @@ h.equals("an empty value still answers zero", ret, 0) -- A rule broad enough to capture a quote or a percent sign still has to -- leave a url that stays inside its attribute and means what it says. -h.load_with(arg[1], { { "local rules = {", "local rules = { { key = \"Ref\", pattern = \"^(%S+)$\", url = \"https://t/%s\" }," } }) +h.load_with(script, { { "local rules = {", "local rules = { { key = \"Ref\", pattern = \"^(%S+)$\", url = \"https://t/%s\" }," } }) out = render("Ref", "a'b%c") h.equals("a captured value is percent-encoded into the url", out, "a'b%c") diff --git a/tests/extensions/test-syntax-highlight.lua b/tests/extensions/test-syntax-highlight.lua index b54dc84..08cc72d 100644 --- a/tests/extensions/test-syntax-highlight.lua +++ b/tests/extensions/test-syntax-highlight.lua @@ -11,7 +11,7 @@ local test_directory = arg[0]:match("^(.*)/") or "." local h = dofile(test_directory .. "/harness.lua") local script = arg[1] -local mode = arg[2] or "highlight" +local mode = h.mode("highlight", "nolexers") h.load(script) -- cgit 2.12.1