blob: 26fcbecfd65251a520e98075e225d68384f216b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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, finds 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.

EXTENSIONS_DIRECTORY=$(cd "$TEST_OUTPUT_DIRECTORY/../custom/extensions" && pwd)
EXT_TEST_DIRECTORY=$(cd "$TEST_OUTPUT_DIRECTORY/extensions" && pwd)

# Prints the Lua version a binary reports, like 5.1, which is also what
# LuaJIT reports.
ext_lua_version() {
	"$1" -e 'io.write(string.match(_VERSION, "%d+%.%d+"))' 2>/dev/null
}

# Prints the interpreters found on the path whose version falls between 5.1
# and the given 5.x ceiling, one per line, so a script is never failed on a
# version it never claimed to run on.
ext_lua_interpreters() {
	ext_lua_ceiling=$1
	for ext_lua_bin in luajit lua5.1 lua5.2 lua5.3 lua5.4 lua5.5 lua
	do
		command -v "$ext_lua_bin" >/dev/null 2>&1 || continue
		case "$(ext_lua_version "$ext_lua_bin")" in
		5.[1-9])
			ext_lua_minor=$(ext_lua_version "$ext_lua_bin")
			ext_lua_minor=${ext_lua_minor#5.}
			test "$ext_lua_minor" -le "$ext_lua_ceiling" &&
				echo "$ext_lua_bin"
			;;
		esac
	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.
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 <<EOF
virtual-root=/
cache-size=0
enable-filter-overrides=1
repo.url=lua-probe
repo.path=$cgit_lua_probe_gitdir
repo.commit-filter=lua:$PWD/lua-probe.lua
EOF
	CGIT_CONFIG="$PWD/lua-probe-cgitrc" QUERY_STRING="url=lua-probe/commit/" cgit | grep -q LUA_PROBE_YES
}