[RFC/PATCH] More robust HTML content tests

John Keeping john at keeping.me.uk
Sun Apr 28 13:43:09 CEST 2013


After introducing a bug recently that I had hoped would be caught by the
test suite, I've been wondering about improving the test suite to check
the generated content more robustly.

It seems to me that there are two strands to this:

	1) grep isn't necessarily the best way to check that the content
	   of an HTML file is correct

	2) We only test with a single hardcoded configuration file at
	   the moment, which hardly triggers any of the configuration
	   variable dependent code

This patch demonstrates a potential approach to (1), using XSL
transforms combined with xsltproc's --html mode to extract structured
content from HTML into text files that can easily be compared with
expected output.

The aim would be to have a library of XSLTs that extract useful content
from CGit HTML pages and that can be reused across tests.

In this patch I introduce an XSLT that extracts repository names from
the index page and a test that demonstrates how this is used, along with
a new framework function to simplify use of the stylesheet.  The
stylesheet has support for sections, although our test suite does not
currently generate any pages that use them.

This patch requires the strip_headers patch that I sent recently [1].

What do you think?

[1] http://article.gmane.org/gmane.comp.version-control.cgit/1349

---
 tests/setup.sh           | 31 +++++++++++++++++++++++++++++++
 tests/t0101-index.sh     | 11 +++++++++++
 tests/xslt/repo_list.xsl | 26 ++++++++++++++++++++++++++
 3 files changed, 68 insertions(+)
 create mode 100644 tests/xslt/repo_list.xsl

diff --git a/tests/setup.sh b/tests/setup.sh
index 1d8677a..653a4d6 100755
--- a/tests/setup.sh
+++ b/tests/setup.sh
@@ -15,6 +15,7 @@
 # run_test 'repo index' 'cgit_url "/" | tidy -e'
 # run_test 'repo summary' 'cgit_url "/foo" | tidy -e'
 
+CGIT_TEST_DIRECTORY=$(pwd)
 : ${TEST_DIRECTORY=$(pwd)/../git/t}
 : ${TEST_OUTPUT_DIRECTORY=$(pwd)}
 TEST_NO_CREATE_REPO=YesPlease
@@ -106,4 +107,16 @@ strip_headers () {
 	cat
 }
 
+test_lazy_prereq XSLTPROC '
+	xsltproc --version >/dev/null 2>&1
+'
+
+xslt_cmp () {
+	local stylesheet=$1 expect=$2 actual=$3
+
+	xsltproc --html "$CGIT_TEST_DIRECTORY/xslt/${stylesheet}.xsl" \
+		"$actual" >"$actual".txt &&
+	test_cmp "$expect" "$actual".txt
+}
+
 test -z "$CGIT_TEST_NO_CREATE_REPOS" && setup_repos
diff --git a/tests/t0101-index.sh b/tests/t0101-index.sh
index 82ef9b0..e29f65c 100755
--- a/tests/t0101-index.sh
+++ b/tests/t0101-index.sh
@@ -14,4 +14,15 @@ test_expect_success 'verify "with%20space" link' 'grep "/with%20space/" tmp'
 test_expect_success 'no tree-link' '! grep "foo/tree" tmp'
 test_expect_success 'no log-link' '! grep "foo/log" tmp'
 
+test_expect_success XSLTPROC 'repo list' '
+	cat <<-\EOF >expect &&
+	bar
+	foo
+	foo+bar
+	with space
+	EOF
+	strip_headers <tmp >actual &&
+	xslt_cmp repo_list expect actual
+'
+
 test_done
diff --git a/tests/xslt/repo_list.xsl b/tests/xslt/repo_list.xsl
new file mode 100644
index 0000000..a497964
--- /dev/null
+++ b/tests/xslt/repo_list.xsl
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+	<xsl:output method="text" indent="yes"/>
+
+	<!-- Don't output text by default. -->
+	<xsl:template match="text()"/>
+
+	<xsl:template match="td[@class='toplevel-repo']">
+		<xsl:value-of select="descendant::text()"/>
+		<xsl:text>
</xsl:text>
+	</xsl:template>
+
+	<xsl:template match="td[@class='reposection']">
+		<xsl:text>* </xsl:text>
+		<xsl:value-of select="descendant::text()"/>
+		<xsl:text>
</xsl:text>
+	</xsl:template>
+
+	<xsl:template match="td[@class='sublevel-repo']">
+		<!-- Indent with TAB. -->
+		<xsl:text>	</xsl:text>
+		<xsl:value-of select="descendant::text()"/>
+		<xsl:text>
</xsl:text>
+	</xsl:template>
+
+</xsl:stylesheet>
-- 
1.8.3.rc0.149.g98a72f2.dirty





More information about the CGit mailing list