test-driver.scm: Don't guess script name from "--test-name"
[automake.git] / contrib / test-driver.scm
blob5292c0a0e3f334920beca6f9572ec01a6a45aef5
1 ;;;; test-driver.scm - Guile test driver for Automake testsuite harness
3 (define script-version "2018-03-24.22") ;UTC
5 ;;; Copyright © 2015-2018 Free Software Foundation, Inc.
6 ;;;
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.
11 ;;;
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.
16 ;;;
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/>.
20 ;;;; Commentary:
21 ;;;
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.
24 ;;;
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
27 ;;; actual needs:
28 ;;;
29 ;;; configure.ac:
30 ;;;   AC_CONFIG_AUX_DIR([build-aux])
31 ;;;   AC_REQUIRE_AUX_FILE([test-driver.scm])
32 ;;;   AC_PATH_PROG([GUILE], [guile])
33 ;;;
34 ;;; Makefile.am
35 ;;;   TEST_LOG_DRIVER = $(GUILE) $(top_srcdir)/build-aux/test-driver.scm
36 ;;;   AM_TESTS_ENVIRONMENT = env GUILE_AUTO_COMPILE='0'
37 ;;;   TESTS = foo.test
38 ;;;   EXTRA_DIST = $(TESTS)
39 ;;;
40 ;;; foo.test
41 ;;;   (use-modules (srfi srfi-64))
42 ;;;   (test-begin "foo")
43 ;;;   (test-assert "assertion example" #t)
44 ;;;   (test-end "foo")
45 ;;;
46 ;;;  See <https://srfi.schemers.org/srfi-64/srfi-64.html> for general
47 ;;;  information about SRFI-64 usage.
48 ;;;
49 ;;;; Code:
51 (use-modules (ice-9 getopt-long)
52              (ice-9 match)
53              (ice-9 pretty-print)
54              (srfi srfi-26)
55              (srfi srfi-64))
57 (define (show-help)
58   (display "Usage:
59    test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
60                [--expect-failure={yes|no}] [--color-tests={yes|no}]
61                [--enable-hard-errors={yes|no}] [--brief={yes|no}}] [--]
62                TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
63 The '--test-name', '--log-file' and '--trs-file' options are mandatory.\n"))
65 (define %options
66   '((test-name                 (value #t))
67     (log-file                  (value #t))
68     (trs-file                  (value #t))
69     (color-tests               (value #t))
70     (expect-failure            (value #t)) ;XXX: not implemented yet
71     (enable-hard-errors        (value #t)) ;not implemented in SRFI-64
72     (brief                     (value #t))
73     (help    (single-char #\h) (value #f))
74     (version (single-char #\V) (value #f))))
76 (define (option->boolean options key)
77   "Return #t if the value associated with KEY in OPTIONS is \"yes\"."
78   (and=> (option-ref options key #f) (cut string=? <> "yes")))
80 (define* (test-display field value  #:optional (port (current-output-port))
81                        #:key pretty?)
82   "Display \"FIELD: VALUE\\n\" on PORT."
83   (if pretty?
84       (begin
85         (format port "~A:~%" field)
86         (pretty-print value port #:per-line-prefix "+ "))
87       (format port "~A: ~S~%" field value)))
89 (define* (result->string symbol #:key colorize?)
90   "Return SYMBOL as an upper case string.  Use colors when COLORIZE is #t."
91   (let ((result (string-upcase (symbol->string symbol))))
92     (if colorize?
93         (string-append (case symbol
94                          ((pass)       "\e[0;32m")  ;green
95                          ((xfail)      "\e[1;32m")  ;light green
96                          ((skip)       "\e[1;34m")  ;blue
97                          ((fail xpass) "\e[0;31m")  ;red
98                          ((error)      "\e[0;35m")) ;magenta
99                        result
100                        "\e[m")          ;no color
101         result)))
103 (define* (test-runner-gnu test-name #:key color? brief? out-port trs-port)
104   "Return an custom SRFI-64 test runner.  TEST-NAME is a string specifying the
105 file name of the current the test.  COLOR? specifies whether to use colors,
106 and BRIEF?, well, you know.  OUT-PORT and TRS-PORT must be output ports.  The
107 current output port is supposed to be redirected to a '.log' file."
109   (define (test-on-test-begin-gnu runner)
110     ;; Procedure called at the start of an individual test case, before the
111     ;; test expression (and expected value) are evaluated.
112     (let ((result (cute assq-ref (test-result-alist runner) <>)))
113       (format #t "test-name: ~A~%" (result 'test-name))
114       (format #t "location: ~A~%"
115               (string-append (result 'source-file) ":"
116                              (number->string (result 'source-line))))
117       (test-display "source" (result 'source-form) #:pretty? #t)))
119   (define (test-on-test-end-gnu runner)
120     ;; Procedure called at the end of an individual test case, when the result
121     ;; of the test is available.
122     (let* ((results (test-result-alist runner))
123            (result? (cut assq <> results))
124            (result  (cut assq-ref results <>)))
125       (unless brief?
126         ;; Display the result of each test case on the console.
127         (format out-port "~A: ~A - ~A~%"
128                 (result->string (test-result-kind runner) #:colorize? color?)
129                 test-name (test-runner-test-name runner)))
130       (when (result? 'expected-value)
131         (test-display "expected-value" (result 'expected-value)))
132       (when (result? 'expected-error)
133         (test-display "expected-error" (result 'expected-error) #:pretty? #t))
134       (when (result? 'actual-value)
135         (test-display "actual-value" (result 'actual-value)))
136       (when (result? 'actual-error)
137         (test-display "actual-error" (result 'actual-error) #:pretty? #t))
138       (format #t "result: ~a~%" (result->string (result 'result-kind)))
139       (newline)
140       (format trs-port ":test-result: ~A ~A~%"
141               (result->string (test-result-kind runner))
142               (test-runner-test-name runner))))
144   (define (test-on-group-end-gnu runner)
145     ;; Procedure called by a 'test-end', including at the end of a test-group.
146     (let ((fail (or (positive? (test-runner-fail-count runner))
147                     (positive? (test-runner-xpass-count runner))))
148           (skip (or (positive? (test-runner-skip-count runner))
149                     (positive? (test-runner-xfail-count runner)))))
150       ;; XXX: The global results need some refinements for XPASS.
151       (format trs-port ":global-test-result: ~A~%"
152               (if fail "FAIL" (if skip "SKIP" "PASS")))
153       (format trs-port ":recheck: ~A~%"
154               (if fail "yes" "no"))
155       (format trs-port ":copy-in-global-log: ~A~%"
156               (if (or fail skip) "yes" "no"))
157       (when brief?
158         ;; Display the global test group result on the console.
159         (format out-port "~A: ~A~%"
160                 (result->string (if fail 'fail (if skip 'skip 'pass))
161                                 #:colorize? color?)
162                 test-name))
163       #f))
165   (let ((runner (test-runner-null)))
166     (test-runner-on-test-begin! runner test-on-test-begin-gnu)
167     (test-runner-on-test-end! runner test-on-test-end-gnu)
168     (test-runner-on-group-end! runner test-on-group-end-gnu)
169     (test-runner-on-bad-end-name! runner test-on-bad-end-name-simple)
170     runner))
174 ;;; Entry point.
177 (let* ((opts   (getopt-long (command-line) %options))
178        (option (cut option-ref opts <> <>)))
179   (cond
180    ((option 'help #f)    (show-help))
181    ((option 'version #f) (format #t "test-driver.scm ~A" script-version))
182    (else
183     (match (option '() '())
184       (()
185        (display "missing test script argument\n" (current-error-port))
186        (exit 1))
187       ((script . args)
188        (let ((log (open-file (option 'log-file "") "w0"))
189              (trs (open-file (option 'trs-file "") "wl"))
190              (out (duplicate-port (current-output-port) "wl")))
191          (redirect-port log (current-output-port))
192          (redirect-port log (current-warning-port))
193          (redirect-port log (current-error-port))
194          (test-with-runner
195              (test-runner-gnu (option 'test-name #f)
196                               #:color? (option->boolean opts 'color-tests)
197                               #:brief? (option->boolean opts 'brief)
198                               #:out-port out #:trs-port trs)
199            (primitive-load script))
200          (close-port log)
201          (close-port trs)
202          (close-port out))))))
203   (exit 0))
205 ;;; Local Variables:
206 ;;; eval: (add-hook 'before-save-hook 'time-stamp)
207 ;;; time-stamp-start: "(define script-version \""
208 ;;; time-stamp-format: "%:y-%02m-%02d.%02H"
209 ;;; time-stamp-time-zone: "UTC0"
210 ;;; time-stamp-end: "\") ;UTC"
211 ;;; End:
213 ;;;; test-driver.scm ends here.