# Shared groundwork for the t05xx scripts, sourced after setup.sh from inside # the trash directory. It locates the shipped extensions and the unit checks # beside this file, lists the standalone Lua interpreters the checks can run # under, and offers a probe that asks the Lua inside the cgit binary itself # whether a module is available to it, since that interpreter can differ from # every standalone one on the path. LUA_PATH, LUA_CPATH and their versioned # forms come through from the environment on purpose, as they are how a # system points Lua at its modules, so the probes find what the system has. # A fallback that needs a module gone runs through without_lua_modules. EXTENSIONS_DIRECTORY=$(cd "$TEST_OUTPUT_DIRECTORY/../custom/extensions" && pwd) EXT_TEST_DIRECTORY=$(cd "$TEST_OUTPUT_DIRECTORY/extensions" && pwd) # Prints the version an interpreter reports, LuaJIT by its own name rather # than the 5.1 it is compatible with, or the name it was given when it does # not run. lua_version() { "$1" -e 'io.write(jit and jit.version or _VERSION)' 2>/dev/null || printf '%s' "$1" } # The interpreters on the path whose version the extensions claim, 5.1 to # 5.5 with LuaJIT counting as 5.1, so no script is failed on a version it # never claimed to run on. Systems spell a versioned name in one of three # ways, and a plain lua is usually one of those again, so each version runs # once, under the first name found for it. interpreters= ext_lua_versions= for ext_lua_bin in luajit lua5.1 lua51 lua-5.1 lua5.2 lua52 lua-5.2 \ lua5.3 lua53 lua-5.3 lua5.4 lua54 lua-5.4 lua5.5 lua55 lua-5.5 lua do command -v "$ext_lua_bin" >/dev/null 2>&1 || continue ext_lua_version=$(lua_version "$ext_lua_bin") case "$ext_lua_version" in "Lua 5."[1-5]|"LuaJIT 2."*) ;; *) continue ;; esac case "$ext_lua_versions" in *"|$ext_lua_version|"*) continue ;; esac ext_lua_versions="$ext_lua_versions|$ext_lua_version|" interpreters="$interpreters $ext_lua_bin" done # A script registers its unit checks for each of ${interpreters:-lua} under # the STANDALONE_LUA prerequisite, so with none found they still show in # 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 } # Runs the command given with every Lua search path pointed at an empty # directory, so no Lua it starts, cgit's own included, finds a module that # is not built in. A fallback for a missing module then runs on any machine. without_lua_modules() { mkdir -p "$TRASH_DIRECTORY/no-lua-modules" && ( for lua_variable in LUA_PATH LUA_PATH_5_2 LUA_PATH_5_3 \ LUA_PATH_5_4 LUA_PATH_5_5 do eval "$lua_variable=\$TRASH_DIRECTORY/no-lua-modules/?.lua" export $lua_variable done for lua_variable in LUA_CPATH LUA_CPATH_5_2 LUA_CPATH_5_3 \ LUA_CPATH_5_4 LUA_CPATH_5_5 do eval "$lua_variable=\$TRASH_DIRECTORY/no-lua-modules/?.so" export $lua_variable done "$@" ) } # 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 # repository has to be handed in. A probe that crashes or answers neither # way stops the run, since reading it as a missing module would switch the # tests to the wrong expectations. cgit_lua_probe() { test "$CGIT_HAS_LUA" -eq 1 || return 1 cgit_lua_probe_gitdir=$1 shift { echo "function filter_open(...) end" echo "function filter_write(str) end" echo "function filter_close()" echo "local ok = true" for cgit_lua_probe_module in "$@" do echo "if not pcall(require, '$cgit_lua_probe_module') then" echo "ok = false" echo "end" done echo "if ok then html('LUA_PROBE_YES') else html('LUA_PROBE_NO') end" echo "return 0" echo "end" } >lua-probe.lua cat >lua-probe-cgitrc <lua-probe.out 2>lua-probe.err || BAIL_OUT "cgit failed while probing its Lua for $*" if grep LUA_PROBE_YES lua-probe.out >/dev/null then return 0 elif grep LUA_PROBE_NO lua-probe.out >/dev/null then return 1 fi BAIL_OUT "probing cgit's Lua for $* gave no answer" }