From 61e8ee6eeddce27229c303a844e839950d0b6a22 Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Thu, 22 May 2014 23:35:48 -0400 Subject: [PATCH] Document the test framework and improve the test output This adds the possibility to configure tests via TEST_OPTS env variable which is used to control the indentation depending on whether showing verbose output or not. --- Makefile | 4 +++- test/README.adoc | 30 ++++++++++++++++++++++++++++++ test/tools/libtest.sh | 49 +++++++++++++++++++++++++++---------------------- 3 files changed, 60 insertions(+), 23 deletions(-) create mode 100644 test/README.adoc diff --git a/Makefile b/Makefile index e4f1b30..4b964c5 100644 --- a/Makefile +++ b/Makefile @@ -161,6 +161,8 @@ clean-test: test: clean-test $(TESTS) $(QUIET_SUMMARY)test/tools/show-results.sh +export TEST_OPTS ?= $(V:1=no-indent) + $(TESTS): PATH := $(CURDIR)/test/tools:$(CURDIR)/src:$(PATH) $(TESTS): $(EXE) test/tools/test-graph $(QUIET_TEST)$@ @@ -325,7 +327,7 @@ QUIET_DB2PDF = $(Q:@=@echo ' DB2PDF '$@;) # tools/install.sh will print 'file -> $install_dir/file' QUIET_INSTALL = $(Q:@=@printf ' INSTALL ';) QUIET_INSTALL_EACH = $(Q:@=printf ' INSTALL ';) -QUIET_TEST = $(Q:@=@printf ' TEST '$@;) +QUIET_TEST = $(Q:@=@echo ' TEST '$@;) QUIET_SUMMARY = $(Q:@=@printf ' SUMMARY ';) export V diff --git a/test/README.adoc b/test/README.adoc new file mode 100644 index 0000000..8785ab5 --- /dev/null +++ b/test/README.adoc @@ -0,0 +1,30 @@ +Test Overview +============= + +All tests can be run with `make test`. This will run all scripts that +end with `-test` in the `test` folder and summarize the test results +using the script `test/tools/show-results.sh`. + +To run individual tests, use `make ` e.g. `make +test/tigrc/parse-test`. Alternatively, tests can be run directly via the +test scripts as long as `PATH` is set to include the directories `src/` +and `test/tools`. The latter directory is where the test helper +libraries are located, the most important of which is `libtest.sh`. + +Options +------- + +Tests can be configured by setting the `TEST_OPTS` environment variable. +The variable should contain a space-separated list of options. The +following options are supported: + +verbose:: + + Whether to print individual test results even when all + assertions passed. The default is to not results for passed + tests. + +no-indent:: + + Do not indent test output. This is automatically set depending + on whether `V=1` was passed to `make` to show verbose output. diff --git a/test/tools/libtest.sh b/test/tools/libtest.sh index b0366a0..0b6291d 100644 --- a/test/tools/libtest.sh +++ b/test/tools/libtest.sh @@ -26,6 +26,19 @@ output_dir="$tmp_dir/$prefix_dir/$test" [ -t 1 ] && diff_color_arg=--color +indent=' ' +verbose= + +set -- $TEST_OPTS + +while [ $# -gt 0 ]; do + arg="$1"; shift + case "$arg" in + verbose) verbose=yes ;; + no-indent) indent= ;; + esac +done + [ -e "$output_dir" ] && rm -rf "$output_dir" mkdir -p "$output_dir" @@ -135,36 +148,28 @@ assert_equals() show_test_results() { - indent=' ' - if [ ! -e .test-result ]; then - echo - { - [ -e stderr ] && - sed "s/^/[stderr] /" < stderr - [ -e stderr.orig ] && - sed "s/^/[stderr] /" < stderr.orig - echo "No test results found" - } | sed "s/^/$indent| /" + [ -e stderr ] && + sed "s/^/[stderr] /" < stderr + [ -e stderr.orig ] && + sed "s/^/[stderr] /" < stderr.orig + echo "No test results found" elif grep FAIL -q < .test-result; then failed="$(grep FAIL < .test-result | wc -l)" count="$(sed -n '/\(FAIL\|OK\)/p' < .test-result | wc -l)" - echo - { - printf "Failed %d out of %d test(s)%s\n" $failed $count + printf "Failed %d out of %d test(s)%s\n" $failed $count - # Show output from stderr if no output is expected - [ -e expected/stderr ] || - sed "s/^/[stderr] /" < stderr + # Show output from stderr if no output is expected + [ -e expected/stderr ] || + sed "s/^/[stderr] /" < stderr - [ -e .test-result ] && - cat .test-result - } | sed "s/^/$indent| /" - else + [ -e .test-result ] && + cat .test-result + elif [ "$verbose" ]; then count="$(sed -n '/\(OK\)/p' < .test-result | wc -l)" - printf " (passed %d)\n" $count - fi + printf "Passed %d assertions\n" $count + fi | sed "s/^/$indent| /" } trap 'show_test_results' EXIT -- 2.11.4.GIT