#!/bin/sh # Checks syntax-highlight.lua, the shipped source-filter. The real Scintillua # collection is not assumed anywhere. The fake lexer module under # extensions/fake-lexers stands in for a working one, and the fallback # checks keep every lexer the filter could find out of its reach. The unit # checks run twice per interpreter, once with the fake lexers found and once # without any, 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:-lua} do test_expect_success STANDALONE_LUA "highlighting checks under $(lua_version "$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 STANDALONE_LUA "fallback checks without lexers under $(lua_version "$lua")" ' mkdir -p empty-lexers && without_lua_modules env CGIT_SCINTILLUA_PATH="$PWD/empty-lexers" \ "$lua" "$EXT_TEST_DIRECTORY/test-syntax-highlight.lua" \ "$EXTENSIONS_DIRECTORY/syntax-highlight.lua" nolexers ' done # A class the filter writes with no rule in the stylesheet colours nothing. test_expect_success 'every class the filter writes is styled' ' sed -n "s/^.*= \"\(hl-[a-z]*\)\",$/\1/p" "$EXTENSIONS_DIRECTORY/syntax-highlight.lua" | sort -u >classes && test -s classes && while read class do test_grep "^div#cgit \.$class {" "$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" || return 1 done $SQ" >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 enable-blame=1 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_ok_url "fake/tree/hello.fake" >tmp && test_grep "if" tmp && test_grep "42" tmp && test_grep ".<b>." tmp ' test_expect_success CGIT_LUA 'the blame view is highlighted too' ' cgit_ok_url "fake/blame/hello.fake" >tmp && test_grep "if" tmp && test_grep ".<b>." tmp ' test_expect_success CGIT_LUA 'bytes between tokens reach the page escaped' ' cgit_ok_url "fake/tree/hello.fake" >tmp && test_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' ' echo "[cgit] Unable to load the Scintillua lexers: module ${SQ}lexer${SQ} not found:" >expect && without_lua_modules env CGIT_SCINTILLUA_PATH=/nonexistent CGIT_CONFIG="$PWD/cgitrc" \ QUERY_STRING="url=fake/tree/hello.fake" cgit >tmp 2>err && test_cmp expect err && test_grep "^Status: 200" tmp && test_grep "" tmp && test_grep "if 42 & zz" tmp && test_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_ok_url "fake/tree/cr.fake" >tmp && test_grep "if\\\\r" tmp ' test_done