#!/bin/sh
# Checks syntax-highlight.lua, the shipped source-filter. The real Scintillua
# collection is not assumed anywhere, because the fake lexer module under
# extensions/fake-lexers drives every path through the filter
# deterministically. The unit checks run twice per interpreter, once with the
# fake lexers found and once with an empty directory so the escaped fallback
# is what everything renders through. The run through cgit itself places the
# fake lexers beside the config file, which is the probe the filter documents
# for a scintillua directory next to cgitrc, and then points the lexer path
# at nothing to see the page fall back to escaped text.
test_description='Check the shipped syntax-highlight extension'
CGIT_TEST_NO_CREATE_REPOS=YesPlease
. ./setup.sh
. "$TEST_OUTPUT_DIRECTORY/extensions/lib.sh"
for lua in $interpreters
do
test_expect_success "highlighting checks under $lua" "
CGIT_SCINTILLUA_PATH='$EXT_TEST_DIRECTORY/fake-lexers' \
'$lua' '$EXT_TEST_DIRECTORY/test-syntax-highlight.lua' \
'$EXTENSIONS_DIRECTORY/syntax-highlight.lua' highlight
"
test_expect_success "fallback checks without lexers under $lua" "
mkdir -p empty-lexers &&
CGIT_SCINTILLUA_PATH=\"\$PWD/empty-lexers\" \
'$lua' '$EXT_TEST_DIRECTORY/test-syntax-highlight.lua' \
'$EXTENSIONS_DIRECTORY/syntax-highlight.lua' nolexers
"
done
test_expect_success 'create a repository holding a lexable file' '
test_create_repo repos/fake &&
(
cd repos/fake &&
echo "if 42 & zz" >hello.fake &&
git add hello.fake &&
git commit -m "add hello"
)
'
test_expect_success 'place the fake lexers beside a config naming the filter' '
mkdir -p scintillua &&
cp -R "$EXT_TEST_DIRECTORY/fake-lexers" scintillua/lexers &&
cat >cgitrc <<-EOF
virtual-root=/
cache-size=0
repo.url=fake
repo.path=$PWD/repos/fake/.git
repo.source-filter=lua:$EXTENSIONS_DIRECTORY/syntax-highlight.lua
EOF
'
test_expect_success CGIT_LUA 'the tree view is highlighted' '
cgit_url "fake/tree/hello.fake" >tmp &&
grep "if" tmp &&
grep "42" tmp
'
test_expect_success CGIT_LUA 'bytes between tokens reach the page escaped' '
grep "42 & " tmp
'
# A missing scintillua leaves the filter with nothing to highlight with, and
# a filter that failed there would take the whole page down with it, so it
# has to hand the source back escaped instead.
test_expect_success CGIT_LUA 'without scintillua the source is served escaped' '
CGIT_SCINTILLUA_PATH=/nonexistent CGIT_CONFIG="$PWD/cgitrc" \
QUERY_STRING="url=fake/tree/hello.fake" cgit >tmp &&
grep "if 42 & zz" tmp &&
! grep "hl-keyword" tmp
'
test_expect_success CGIT_LUA 'a lone carriage return reaches the page as an escape' '
(
cd repos/fake &&
printf "if\\rzz\\n" >cr.fake &&
git add cr.fake &&
git commit -m "add a lone carriage return"
) &&
cgit_url "fake/tree/cr.fake" >tmp &&
grep "if\\\\r" tmp
'
test_done