tests/setup.sh (view raw)
1# This file should be sourced by all test-scripts
2#
3# Main functions:
4# prepare_tests(description) - setup for testing, i.e. create repos+config
5# run_test(description, script) - run one test, i.e. eval script
6#
7# Helper functions
8# cgit_query(querystring) - call cgit with the specified querystring
9# cgit_url(url) - call cgit with the specified virtual url
10#
11# Example script:
12#
13# . setup.sh
14# prepare_tests "html validation"
15# run_test 'repo index' 'cgit_url "/" | tidy -e'
16# run_test 'repo summary' 'cgit_url "/foo" | tidy -e'
17
18unset CDPATH
19
20mkrepo() {
21 name=$1
22 count=$2
23 dir=$PWD
24 test -d "$name" && return
25 printf "Creating testrepo %s\n" "$name"
26 mkdir -p "$name"
27 cd "$name"
28 git init
29 n=1
30 while test $n -le $count
31 do
32 echo $n >file-$n
33 git add file-$n
34 git commit -m "commit $n"
35 n=$(expr $n + 1)
36 done
37 if test "$3" = "testplus"
38 then
39 echo "hello" >a+b
40 git add a+b
41 git commit -m "add a+b"
42 git branch "1+2"
43 fi
44 cd "$dir"
45}
46
47setup_repos()
48{
49 rm -rf trash/cache
50 mkdir -p trash/cache
51 mkrepo trash/repos/foo 5 >/dev/null
52 mkrepo trash/repos/bar 50 >/dev/null
53 mkrepo trash/repos/foo+bar 10 testplus >/dev/null
54 mkrepo "trash/repos/with space" 2 >/dev/null
55 cat >trash/cgitrc <<EOF
56virtual-root=/
57cache-root=$PWD/trash/cache
58
59cache-size=1021
60snapshots=tar.gz tar.bz zip
61enable-log-filecount=1
62enable-log-linecount=1
63summary-log=5
64summary-branches=5
65summary-tags=5
66clone-url=git://example.org/\$CGIT_REPO_URL.git
67
68repo.url=foo
69repo.path=$PWD/trash/repos/foo/.git
70# Do not specify a description for this repo, as it then will be assigned
71# the constant value "[no description]" (which actually used to cause a
72# segfault).
73
74repo.url=bar
75repo.path=$PWD/trash/repos/bar/.git
76repo.desc=the bar repo
77
78repo.url=foo+bar
79repo.path=$PWD/trash/repos/foo+bar/.git
80repo.desc=the foo+bar repo
81
82repo.url=with space
83repo.path=$PWD/trash/repos/with space/.git
84repo.desc=spaced repo
85EOF
86}
87
88prepare_tests()
89{
90 setup_repos
91 rm -f test-output.log 2>/dev/null
92 test_count=0
93 test_failed=0
94 echo "[$0]" "$@" >test-output.log
95 echo "$@" "($0)"
96}
97
98tests_done()
99{
100 printf "\n"
101 if test $test_failed -gt 0
102 then
103 printf "test: *** %s failure(s), logfile=%s\n" \
104 $test_failed "$(pwd)/test-output.log"
105 false
106 fi
107}
108
109run_test()
110{
111 bug=0
112 if test "$1" = "BUG"
113 then
114 bug=1
115 shift
116 fi
117 desc=$1
118 script=$2
119 test_count=$(expr $test_count + 1)
120 printf "\ntest %d: name='%s'\n" $test_count "$desc" >>test-output.log
121 printf "test %d: eval='%s'\n" $test_count "$2" >>test-output.log
122 eval "$2" >>test-output.log 2>>test-output.log
123 res=$?
124 printf "test %d: exitcode=%d\n" $test_count $res >>test-output.log
125 if test $res = 0 -a $bug = 0
126 then
127 printf " %2d) %-60s [ok]\n" $test_count "$desc"
128 elif test $res = 0 -a $bug = 1
129 then
130 printf " %2d) %-60s [BUG FIXED]\n" $test_count "$desc"
131 elif test $bug = 1
132 then
133 printf " %2d) %-60s [KNOWN BUG]\n" $test_count "$desc"
134 else
135 test_failed=$(expr $test_failed + 1)
136 printf " %2d) %-60s [failed]\n" $test_count "$desc"
137 fi
138}
139
140cgit_query()
141{
142 CGIT_CONFIG="$PWD/trash/cgitrc" QUERY_STRING="$1" "$PWD/../cgit"
143}
144
145cgit_url()
146{
147 CGIT_CONFIG="$PWD/trash/cgitrc" QUERY_STRING="url=$1" "$PWD/../cgit"
148}