blob: 757ae043a194ad4390f3f8ccb9046c0457a4188b (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Shared groundwork sourced by every test script. It builds a handful of small
# repositories under repos/ and writes a cgitrc pointing at them, then offers
# the helpers that ask cgit for a page so a test only has to look at what comes
# back. Sourcing it also brings in the test library from the bundled Git tree,
# which is where test_expect_success and the trash directory come from. Set
# CGIT_TEST_NO_CREATE_REPOS to get the helpers without paying for the fixtures.

# The Git test library would run every Git command under Valgrind if it saw
# --valgrind, and only cgit itself is worth watching, so the option is taken out
# here and acted on further down. The arguments are carried across as a newline
# separated list, which keeps any other whitespace in them intact but assumes
# that none holds a newline of its own.
LF='
'
# Trash directories are collected under one trash/ rather than left beside
# the scripts, which keeps the ignore and clean rules to a single plain
# name. The option is seeded ahead of the real arguments so one given on
# the command line still wins.
test_argv="${LF}--root=trash"

while test $# != 0
do
	case "$1" in
	--va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
		cgit_valgrind=t
		test_argv="$test_argv${LF}--verbose"
		;;
	*)
		test_argv="$test_argv$LF$1"
		;;
	esac
	shift
done

OLDIFS=$IFS
IFS=$LF
set -- $test_argv
IFS=$OLDIFS

: ${TEST_DIRECTORY=$(pwd)/../vendor/git/t}
: ${TEST_OUTPUT_DIRECTORY=$(pwd)}
# The library would otherwise leave a repository of its own in the trash
# directory, and every repository these tests want is built by mkrepo below.
TEST_NO_CREATE_REPO=YesPlease
. "$TEST_DIRECTORY"/test-lib.sh

# The library spells its results directory test-results and derives these
# variables before it can be told otherwise, so they are re-pointed here and
# every count lands under the plainer results/ instead. The rarely used
# --tee and --stress options write their raw logs before this line and keep
# the library's own name.
TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/results"
TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
TEST_RESULTS_SAN_DIR="$TEST_RESULTS_BASE.$TEST_RESULTS_SAN_DIR_SFX"

# The library has moved into the trash directory by now, so everything the
# tests reach outside it is anchored to TEST_OUTPUT_DIRECTORY, which still
# names this directory whatever depth the trash sits at.

# The tests run cgit by name, so the binary just built has to come ahead of any
# copy already installed. Under Valgrind the wrappers take that place instead.
if test -n "$cgit_valgrind"
then
	GIT_VALGRIND="$TEST_DIRECTORY/valgrind"
	CGIT_VALGRIND=$(cd "$TEST_OUTPUT_DIRECTORY/valgrind" && pwd)
	PATH="$CGIT_VALGRIND/bin:$PATH"
	export GIT_VALGRIND CGIT_VALGRIND
else
	PATH="$TEST_OUTPUT_DIRECTORY/../build:$PATH"
fi

FILTER_DIRECTORY=$(cd "$TEST_OUTPUT_DIRECTORY/filters" && pwd)

# Lua filters only work when cgit was compiled with Lua, so the tests that use
# them have to ask the binary rather than assume.
if cgit --version | grep -F -q "[+] Lua scripting"
then
	export CGIT_HAS_LUA=1
else
	export CGIT_HAS_LUA=0
fi

# Creates a repository holding the given number of one file commits. A third
# argument asks for a variant, either testplus for a file and a branch whose
# names have to be escaped in a URL, or commit-graph for a written commit graph.
mkrepo() {
	repo=$1
	commits=$2
	variant=$3
	test_create_repo "$repo"
	(
		cd "$repo"
		n=1
		while test $n -le $commits
		do
			echo $n >file-$n
			git add file-$n
			git commit -m "commit $n"
			n=$((n + 1))
		done
		case "$variant" in
		testplus)
			echo "hello" >a+b
			git add a+b
			git commit -m "add a+b"
			git branch "1+2"
			;;
		commit-graph)
			git commit-graph write
			;;
		esac
	)
}

setup_repos()
{
	rm -rf cache
	mkdir -p cache
	mkrepo repos/foo 5 >/dev/null
	mkrepo repos/bar 50 commit-graph >/dev/null
	mkrepo repos/foo+bar 10 testplus >/dev/null
	mkrepo "repos/with space" 2 >/dev/null
	mkrepo repos/filter 5 testplus >/dev/null
	cat >cgitrc <<EOF
virtual-root=/
cache-root=$PWD/cache

cache-size=1021
enable-cache-list=1
snapshots=tar tar.gz tar.bz2 tar.lz tar.xz tar.zst zip
enable-log-filecount=1
enable-log-linecount=1
summary-log=5
summary-branches=5
summary-tags=5
clone-url=git://example.org/\$CGIT_REPO_URL.git
enable-filter-overrides=1

repo.url=foo
repo.path=$PWD/repos/foo/.git
# Do not specify a description for this repo, as it then will be assigned
# the constant value "[no description]" (which actually used to cause a
# segfault).

repo.url=bar
repo.path=$PWD/repos/bar/.git
repo.desc=the bar repo

repo.url=foo+bar
repo.path=$PWD/repos/foo+bar/.git
repo.desc=the foo+bar repo

repo.url=with space
repo.path=$PWD/repos/with space/.git
repo.desc=spaced repo

repo.url=filter-exec
repo.path=$PWD/repos/filter/.git
repo.desc=filtered repo
repo.about-filter=exec:$FILTER_DIRECTORY/dump.sh
repo.commit-filter=exec:$FILTER_DIRECTORY/dump.sh
repo.email-filter=exec:$FILTER_DIRECTORY/dump.sh
repo.source-filter=exec:$FILTER_DIRECTORY/dump.sh
repo.readme=master:a+b
EOF

	if test "$CGIT_HAS_LUA" -eq 1
	then
		cat >>cgitrc <<EOF
repo.url=filter-lua
repo.path=$PWD/repos/filter/.git
repo.desc=filtered repo
repo.about-filter=lua:$FILTER_DIRECTORY/dump.lua
repo.commit-filter=lua:$FILTER_DIRECTORY/dump.lua
repo.email-filter=lua:$FILTER_DIRECTORY/dump.lua
repo.source-filter=lua:$FILTER_DIRECTORY/dump.lua
repo.readme=master:a+b
EOF
	fi
}

cgit_query()
{
	CGIT_CONFIG="$PWD/cgitrc" QUERY_STRING="$1" cgit
}

cgit_url()
{
	CGIT_CONFIG="$PWD/cgitrc" QUERY_STRING="url=$1" cgit
}

# The response headers run down to the first empty line, and a test comparing
# two pages wants to see the bodies alone.
strip_headers() {
	while read -r line
	do
		test -z "$line" && break
	done
	cat
}

test -z "$CGIT_TEST_NO_CREATE_REPOS" && setup_repos