diff --git a/bin/xspec.sh b/bin/xspec.sh index 541f3d52a..dade08447 100755 --- a/bin/xspec.sh +++ b/bin/xspec.sh @@ -36,12 +36,13 @@ usage() { echo "$1" echo; fi - echo "Usage: xspec [-t|-q|-c|-h] filename [coverage]" + echo "Usage: xspec [-t|-q|-c|-j|-h] filename [coverage]" echo echo " filename the XSpec document" echo " -t test an XSLT stylesheet (the default)" echo " -q test an XQuery module (mutually exclusive with -t)" echo " -c output test coverage report" + echo " -j output JUnit report" echo " -h display this help message" echo " coverage deprecated, use -c instead" } @@ -167,6 +168,13 @@ while echo "$1" | grep -- ^- >/dev/null 2>&1; do # Coverage -c) COVERAGE=1;; + # JUnit report + -j) + if [[ ${SAXON_CP} == *"saxon8"* || ${SAXON_CP} == *"saxon8sa"* ]]; then + echo "Saxon8 detected. JUnit report requires Saxon9." + exit 1 + fi + JUNIT=1;; # Help! -h) usage @@ -219,6 +227,7 @@ COVERAGE_XML=$TEST_DIR/$TARGET_FILE_NAME-coverage.xml COVERAGE_HTML=$TEST_DIR/$TARGET_FILE_NAME-coverage.html RESULT=$TEST_DIR/$TARGET_FILE_NAME-result.xml HTML=$TEST_DIR/$TARGET_FILE_NAME-result.html +JUNIT_RESULT=$TEST_DIR/$TARGET_FILE_NAME-junit.xml COVERAGE_CLASS=com.jenitennison.xslt.tests.XSLTCoverageTraceListener if [ ! -d "$TEST_DIR" ]; then @@ -293,6 +302,12 @@ if test -n "$COVERAGE"; then || die "Error formating the coverage report" echo "Report available at $COVERAGE_HTML" #$OPEN "$COVERAGE_HTML" +elif test -n "$JUNIT"; then + xslt -o:"$JUNIT_RESULT" \ + -s:"$RESULT" \ + -xsl:"$XSPEC_HOME/src/reporter/junit-report.xsl" \ + || die "Error formating the JUnit report" + echo "Report available at $JUNIT_RESULT" else echo "Report available at $HTML" #$OPEN "$HTML" diff --git a/src/reporter/junit-report.xsl b/src/reporter/junit-report.xsl new file mode 100644 index 000000000..6b2ca203c --- /dev/null +++ b/src/reporter/junit-report.xsl @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + skipped + passed + failed + + + + + + + + + + + + + + + Expected: + + + + + + + diff --git a/test/xspec-junit.xspec b/test/xspec-junit.xspec new file mode 100644 index 000000000..9e10616d4 --- /dev/null +++ b/test/xspec-junit.xspec @@ -0,0 +1,78 @@ + + + + + + + Successful test + +

Foo

+
+ +

Foo

+
+
+
+ + + + +
+ + + + + + failing test + +

Foo

+
+ +

Bar

+
+
+
+ + + + + <x:expect xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://www.jenitennison.com/xslt/unit-test" xmlns:x="http://www.jenitennison.com/xslt/xspec"><p>Bar</p></x:expect> + + +
+ + + + + + Successful test + +

Foo

+
+ +

Foo

+
+
+ + Failing test + +

Foo

+
+ +

Bar

+
+
+
+ + + + + <x:expect xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://www.jenitennison.com/xslt/unit-test" xmlns:x="http://www.jenitennison.com/xslt/xspec"><p>Bar</p></x:expect> + + +
+ + +
diff --git a/test/xspec.bats b/test/xspec.bats new file mode 100644 index 000000000..7c4998a5c --- /dev/null +++ b/test/xspec.bats @@ -0,0 +1,72 @@ + +#!/usr/bin/env bats +#=============================================================================== +# +# USAGE: bats xspec.bats +# +# DESCRIPTION: Unit tests for script bin/xspec.sh +# +# INPUT: N/A +# +# OUTPUT: Unit tests results +# +# DEPENDENCIES: This script requires bats (https://github.com/sstephenson/bats) +# +# AUTHOR: Sandro Cirulli, github.com/cirulls +# +# LICENSE: MIT License (https://opensource.org/licenses/MIT) +# +#=============================================================================== + +@test "invoking xspec without arguments prints usage" { + run ../bin/xspec.sh + [ "$status" -eq 1 ] + [ "${lines[2]}" = "Usage: xspec [-t|-q|-c|-j|-h] filename [coverage]" ] +} + + +@test "invoking xspec generates XML report file" { + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + run stat ../tutorial/xspec/escape-for-regex-result.xml + [ "$status" -eq 0 ] +} + +@test "invoking xspec generates HTML report file" { + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + run stat ../tutorial/xspec/escape-for-regex-result.html + [ "$status" -eq 0 ] +} + +@test "invoking xspec with -j option with Saxon8 returns error message" { + export SAXON_CP=/path/to/saxon8.jar + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + [ "$status" -eq 1 ] + [ "${lines[1]}" = "Saxon8 detected. JUnit report requires Saxon9." ] +} + + +@test "invoking xspec with -j option with Saxon8-SA returns error message" { + export SAXON_CP=/path/to/saxon8sa.jar + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + [ "$status" -eq 1 ] + [ "${lines[1]}" = "Saxon8 detected. JUnit report requires Saxon9." ] +} + + +@test "invoking xspec with -j option generates message with JUnit report location" { + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + [ "$status" -eq 0 ] + [ "${lines[18]}" = "Report available at ../tutorial/xspec/escape-for-regex-junit.xml" ] +} + +@test "invoking xspec with -j option generates XML report file" { + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + run stat ../tutorial/xspec/escape-for-regex-result.xml + [ "$status" -eq 0 ] +} + +@test "invoking xspec with -j option generates JUnit report file" { + run ../bin/xspec.sh -j ../tutorial/escape-for-regex.xspec + run stat ../tutorial/xspec/escape-for-regex-junit.xml + [ "$status" -eq 0 ] +}