1 ;;;; test-driver.scm - Guile test driver for Automake testsuite harness
3 (define script-version "2018-03-11.22") ;UTC
5 ;;; Copyright © 2015-2018 Free Software Foundation, Inc.
7 ;;; This program is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; This program is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22 ;;; This script provides a Guile test driver using the SRFI-64 Scheme API for
23 ;;; test suites. SRFI-64 is distributed with Guile since version 2.0.9.
25 ;;; To use it, you have to manually copy this file in the ‘build-aux’
26 ;;; directory of your package, then adapt the following snippets to your
30 ;;; AC_CONFIG_AUX_DIR([build-aux])
31 ;;; AC_REQUIRE_AUX_FILE([test-driver.scm])
32 ;;; AC_PATH_PROG([GUILE], [guile])
35 ;;; TEST_LOG_DRIVER = $(GUILE) $(top_srcdir)/build-aux/test-driver.scm
36 ;;; AM_TESTS_ENVIRONMENT = env GUILE_AUTO_COMPILE='0'
38 ;;; EXTRA_DIST = $(TESTS)
41 ;;; (use-modules (srfi srfi-64))
42 ;;; (test-begin "foo")
43 ;;; (test-assert "assertion example" #t)
46 ;;; See <https://srfi.schemers.org/srfi-64/srfi-64.html> for general
47 ;;; information about SRFI-64 usage.
51 (use-modules (ice-9 getopt-long)
58 test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
59 [--expect-failure={yes|no}] [--color-tests={yes|no}]
60 [--enable-hard-errors={yes|no}] [--brief={yes|no}}] [--]
61 TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
62 The '--test-name', '--log-file' and '--trs-file' options are mandatory.\n"))
65 '((test-name (value #t))
68 (color-tests (value #t))
69 (expect-failure (value #t)) ;XXX: not implemented yet
70 (enable-hard-errors (value #t)) ;not implemented in SRFI-64
72 (help (single-char #\h) (value #f))
73 (version (single-char #\V) (value #f))))
75 (define (option->boolean options key)
76 "Return #t if the value associated with KEY in OPTIONS is \"yes\"."
77 (and=> (option-ref options key #f) (cut string=? <> "yes")))
79 (define* (test-display field value #:optional (port (current-output-port))
81 "Display \"FIELD: VALUE\\n\" on PORT."
84 (format port "~A:~%" field)
85 (pretty-print value port #:per-line-prefix "+ "))
86 (format port "~A: ~S~%" field value)))
88 (define* (result->string symbol #:key colorize?)
89 "Return SYMBOL as an upper case string. Use colors when COLORIZE is #t."
90 (let ((result (string-upcase (symbol->string symbol))))
92 (string-append (case symbol
93 ((pass) "
\e[0;32m") ;green
94 ((xfail) "
\e[1;32m") ;light green
95 ((skip) "
\e[1;34m") ;blue
96 ((fail xpass) "
\e[0;31m") ;red
97 ((error) "
\e[0;35m")) ;magenta
102 (define* (test-runner-gnu test-name #:key color? brief? out-port trs-port)
103 "Return an custom SRFI-64 test runner. TEST-NAME is a string specifying the
104 file name of the current the test. COLOR? specifies whether to use colors,
105 and BRIEF?, well, you know. OUT-PORT and TRS-PORT must be output ports. The
106 current output port is supposed to be redirected to a '.log' file."
108 (define (test-on-test-begin-gnu runner)
109 ;; Procedure called at the start of an individual test case, before the
110 ;; test expression (and expected value) are evaluated.
111 (let ((result (cute assq-ref (test-result-alist runner) <>)))
112 (format #t "test-name: ~A~%" (result 'test-name))
113 (format #t "location: ~A~%"
114 (string-append (result 'source-file) ":"
115 (number->string (result 'source-line))))
116 (test-display "source" (result 'source-form) #:pretty? #t)))
118 (define (test-on-test-end-gnu runner)
119 ;; Procedure called at the end of an individual test case, when the result
120 ;; of the test is available.
121 (let* ((results (test-result-alist runner))
122 (result? (cut assq <> results))
123 (result (cut assq-ref results <>)))
125 ;; Display the result of each test case on the console.
126 (format out-port "~A: ~A - ~A~%"
127 (result->string (test-result-kind runner) #:colorize? color?)
128 test-name (test-runner-test-name runner)))
129 (when (result? 'expected-value)
130 (test-display "expected-value" (result 'expected-value)))
131 (when (result? 'expected-error)
132 (test-display "expected-error" (result 'expected-error) #:pretty? #t))
133 (when (result? 'actual-value)
134 (test-display "actual-value" (result 'actual-value)))
135 (when (result? 'actual-error)
136 (test-display "actual-error" (result 'actual-error) #:pretty? #t))
137 (format #t "result: ~a~%" (result->string (result 'result-kind)))
139 (format trs-port ":test-result: ~A ~A~%"
140 (result->string (test-result-kind runner))
141 (test-runner-test-name runner))))
143 (define (test-on-group-end-gnu runner)
144 ;; Procedure called by a 'test-end', including at the end of a test-group.
145 (let ((fail (or (positive? (test-runner-fail-count runner))
146 (positive? (test-runner-xpass-count runner))))
147 (skip (or (positive? (test-runner-skip-count runner))
148 (positive? (test-runner-xfail-count runner)))))
149 ;; XXX: The global results need some refinements for XPASS.
150 (format trs-port ":global-test-result: ~A~%"
151 (if fail "FAIL" (if skip "SKIP" "PASS")))
152 (format trs-port ":recheck: ~A~%"
153 (if fail "yes" "no"))
154 (format trs-port ":copy-in-global-log: ~A~%"
155 (if (or fail skip) "yes" "no"))
157 ;; Display the global test group result on the console.
158 (format out-port "~A: ~A~%"
159 (result->string (if fail 'fail (if skip 'skip 'pass))
164 (let ((runner (test-runner-null)))
165 (test-runner-on-test-begin! runner test-on-test-begin-gnu)
166 (test-runner-on-test-end! runner test-on-test-end-gnu)
167 (test-runner-on-group-end! runner test-on-group-end-gnu)
168 (test-runner-on-bad-end-name! runner test-on-bad-end-name-simple)
176 (define (main . args)
177 (let* ((opts (getopt-long (command-line) %options))
178 (option (cut option-ref opts <> <>)))
180 ((option 'help #f) (show-help))
181 ((option 'version #f) (format #t "test-driver.scm ~A" script-version))
183 (let ((log (open-file (option 'log-file "") "w0"))
184 (trs (open-file (option 'trs-file "") "wl"))
185 (out (duplicate-port (current-output-port) "wl")))
186 (redirect-port log (current-output-port))
187 (redirect-port log (current-warning-port))
188 (redirect-port log (current-error-port))
190 (test-runner-gnu (option 'test-name #f)
191 #:color? (option->boolean opts 'color-tests)
192 #:brief? (option->boolean opts 'brief)
193 #:out-port out #:trs-port trs)
194 (load-from-path (option 'test-name #f)))
201 ;;; eval: (add-hook 'before-save-hook 'time-stamp)
202 ;;; time-stamp-start: "(define script-version \""
203 ;;; time-stamp-format: "%:y-%02m-%02d.%02H"
204 ;;; time-stamp-time-zone: "UTC0"
205 ;;; time-stamp-end: "\") ;UTC"
208 ;;;; test-driver.scm ends here.