# 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 write a config of a test's own and 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. LF=' ' # Trash directories are collected under one trash/, which keeps the ignore # and clean rules to a single name. Seeded ahead of the real arguments so # one given on the command line still wins. test_argv="${LF}--root=trash" # The Git test library would run every Git command under Valgrind if it saw # --valgrind, and only cgit is worth watching, so the option is taken out # here and acted on further down. 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 # Globbing is off while the arguments are split apart again, so one that looks # like a pattern reaches the library as it was given. cgit_ifs=$IFS IFS=$LF set -f set -- $test_argv set +f IFS=$cgit_ifs unset test_argv cgit_ifs # The library takes these from the environment when they are set there, so # they are set outright, or a shell set up for work on Git itself would point # the suite at another library and every path below at another tree. The # fixtures and many tests name master, which the library would otherwise take # from the same environment, or from a Git built with its breaking changes. TEST_DIRECTORY=$(pwd)/../vendor/git/t TEST_OUTPUT_DIRECTORY=$(pwd) GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME # The library would otherwise leave a repository of its own in the trash # directory, and the tests build the repositories they want themselves. TEST_NO_CREATE_REPO=YesPlease . "$TEST_DIRECTORY"/test-lib.sh # cgit reads the CGI variables and a few of its own from the environment, and # the library clears only Git's, so a developer's shell could hand any of them # to every request. LUA_INIT runs code in each standalone interpreter. The Lua # search paths are left alone, since they are how a system points Lua at its # modules. for cgit_variable in SCRIPT_NAME PATH_INFO QUERY_STRING HTTP_HOST HTTPS NO_HTTP \ REQUEST_METHOD SERVER_NAME SERVER_PORT HTTP_COOKIE HTTP_REFERER CONTENT_LENGTH \ CONTENT_TYPE REMOTE_ADDR REMOTE_USER CGIT_CONFIG CGIT_SCINTILLUA_PATH \ LUA_INIT LUA_INIT_5_2 LUA_INIT_5_3 LUA_INIT_5_4 LUA_INIT_5_5 do unset $cgit_variable done unset cgit_variable umask 022 # The library has moved into the trash directory by now, so paths outside it # are anchored to TEST_OUTPUT_DIRECTORY. 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. A cgit that cannot print its # version stops the run rather than reading as one built without Lua, and the # answer has to agree with the flags the binary was built with. cgit_version=$(cgit --version) || BAIL_OUT "cgit --version failed, so the binary under test is broken" case "$cgit_version" in *"[+] Lua scripting"*) CGIT_HAS_LUA=1 test_set_prereq CGIT_LUA ;; *) CGIT_HAS_LUA=0 ;; esac cgit_flags="$TEST_OUTPUT_DIRECTORY/../build/CGIT-CFLAGS" if test -z "$cgit_valgrind" && test -f "$cgit_flags" then if grep -e "-DNO_LUA" "$cgit_flags" >/dev/null then test "$CGIT_HAS_LUA" = 0 || BAIL_OUT "cgit reports Lua but was built with NO_LUA" else test "$CGIT_HAS_LUA" = 1 || BAIL_OUT "cgit was built with Lua but its version does not say so" fi fi unset cgit_version cgit_flags # 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() { mkrepo_commits=$2 mkrepo_variant=$3 test_create_repo "$1" && ( cd "$1" && n=1 && while test $n -le $mkrepo_commits do echo $n >file-$n && git add file-$n && git commit -m "commit $n" && n=$((n + 1)) || exit 1 done && case "$mkrepo_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() { mkdir 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 <>cgitrc < under the # url named second, with the cache off and links rooted at /, and takes the # rest as settings. A repository copies its defaults from the global settings # read before its repo.url, so whatever order they come in, the repo.* ones # go after the repository and all others ahead of it. A repo.path setting # names a repository kept somewhere else. cgitrc_repo() { cgitrc_file=$1 cgitrc_url=$2 shift 2 cgitrc_path="repo.path=$PWD/repos/$cgitrc_url/.git" cgitrc_before= cgitrc_after= for cgitrc_setting do case "$cgitrc_setting" in repo.path=*) cgitrc_path=$cgitrc_setting ;; repo.*) cgitrc_after="$cgitrc_after$cgitrc_setting$LF" ;; *) cgitrc_before="$cgitrc_before$cgitrc_setting$LF" ;; esac done printf '%s' "virtual-root=/$LF" "cache-size=0$LF" "$cgitrc_before" \ "repo.url=$cgitrc_url$LF" "$cgitrc_path$LF" "$cgitrc_after" >"$cgitrc_file" } # Writes the global settings named after the file ahead of a copy of the # shared cgitrc. The copy turns the cache off, because a page is cached under # its request alone and would otherwise come back as the shared config made # it. cgitrc_copy() { cgitrc_file=$1 shift { test $# = 0 || printf '%s\n' "$@" sed "s/^cache-size=.*/cache-size=0/" cgitrc } >"$cgitrc_file" } # Runs cgit on the config file named first for the query string named second, # passing on its output and its exit status, which is 0 for an error page too. cgit_rc() { CGIT_CONFIG="$PWD/$1" QUERY_STRING="$2" cgit } # These two ask the shared config, which keeps each page in a cache keyed on # the request alone. A test that changes a repository and asks again for a page # fetched before gets the old copy back, unless it uses cgit_ok_url. cgit_query() { cgit_rc cgitrc "$1" } cgit_url() { cgit_rc cgitrc "url=$1" } # Asks for a page as cgit_rc does but fails unless the request really worked, # with a 200 status, nothing logged and, for an HTML page, the closing tag. # cgit exits 0 whatever goes wrong, and a death halfway through a page leaves # it cut short behind a 200 status, so the exit status alone proves little. # The page still goes to stdout and the log to stderr. Under -x a shell # without BASH_XTRACEFD would trace cgit_rc into the log kept here, so the # trace stops for that one call. cgit_ok() { cgit_ok_flags=$- set +x cgit_rc "$@" >cgit-ok.out 2>cgit-ok.err cgit_ok_status=$? case $cgit_ok_flags in *x*) set -x ;; esac cat cgit-ok.out cat cgit-ok.err >&2 if test "$cgit_ok_status" != 0 then echo "cgit exited with status $cgit_ok_status" >&2 return 1 elif test -s cgit-ok.err then echo "cgit logged an error" >&2 return 1 elif ! sed -n 1p cgit-ok.out | grep "^Status: 200" >/dev/null then echo "cgit answered $(sed -n 1p cgit-ok.out)" >&2 return 1 elif sed "/^$/q" cgit-ok.out | grep -i "^Content-Type: text/html" >/dev/null && ! grep "" cgit-ok.out >/dev/null then echo "the page stops short of " >&2 return 1 fi } # The same against the shared repositories, through a copy of the shared # config with the page cache off, so the page is rendered afresh rather than # replayed from a slot an earlier test filled. cgit_ok_url() { sed "s/^cache-size=.*/cache-size=0/" cgitrc >cgitrc-ok && cgit_ok cgitrc-ok "url=$1" } # 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 strip_headers_line do test -z "$strip_headers_line" && break done cat } # The fixtures are built here rather than in a test so that --run can still # pick out any test, and a fixture that cannot be built stops the run where # it broke rather than failing every test after it. if test -z "$CGIT_TEST_NO_CREATE_REPOS" then setup_repos || BAIL_OUT "the shared repositories could not be built" fi