test-driver.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/bin/sh
  2. exec guile --no-auto-compile -e main -s "$0" "$@"
  3. !#
  4. ;;;; test-driver.scm - Guile test driver for Automake testsuite harness
  5. (define script-version "2021-02-02.05") ;UTC
  6. ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
  7. ;;; Copyright © 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  8. ;;;
  9. ;;; This program is free software; you can redistribute it and/or modify it
  10. ;;; under the terms of the GNU General Public License as published by
  11. ;;; the Free Software Foundation; either version 3 of the License, or (at
  12. ;;; your option) any later version.
  13. ;;;
  14. ;;; This program is distributed in the hope that it will be useful, but
  15. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;; GNU General Public License for more details.
  18. ;;;
  19. ;;; You should have received a copy of the GNU General Public License
  20. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;; Commentary:
  22. ;;;
  23. ;;; This script provides a Guile test driver using the SRFI-64 Scheme API for
  24. ;;; test suites. SRFI-64 is distributed with Guile since version 2.0.9.
  25. ;;;
  26. ;;;; Code:
  27. (use-modules (ice-9 format)
  28. (ice-9 getopt-long)
  29. (ice-9 pretty-print)
  30. (ice-9 regex)
  31. (srfi srfi-1)
  32. (srfi srfi-19)
  33. (srfi srfi-26)
  34. (srfi srfi-64))
  35. (define (show-help)
  36. (display "Usage:
  37. test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
  38. [--expect-failure={yes|no}] [--color-tests={yes|no}]
  39. [--select=REGEXP] [--exclude=REGEXP] [--errors-only={yes|no}]
  40. [--enable-hard-errors={yes|no}] [--brief={yes|no}}]
  41. [--show-duration={yes|no}] [--]
  42. TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
  43. The '--test-name' option is mandatory. The '--select' and '--exclude' options
  44. allow selecting or excluding individual test cases via a regexp, respectively.
  45. The '--errors-only' option can be set to \"yes\" to limit the logged test case
  46. metadata to only those test cases that failed. When set to \"yes\", the
  47. '--brief' option disables printing the individual test case result to the
  48. console. When '--show-duration' is set to \"yes\", the time elapsed per test
  49. case is shown.\n"))
  50. (define %options
  51. '((test-name (value #t))
  52. (log-file (value #t))
  53. (trs-file (value #t))
  54. (select (value #t))
  55. (exclude (value #t))
  56. (errors-only (value #t))
  57. (color-tests (value #t))
  58. (expect-failure (value #t)) ;XXX: not implemented yet
  59. (enable-hard-errors (value #t)) ;not implemented in SRFI-64
  60. (brief (value #t))
  61. (show-duration (value #t))
  62. (help (single-char #\h) (value #f))
  63. (version (single-char #\V) (value #f))))
  64. (define (option->boolean options key)
  65. "Return #t if the value associated with KEY in OPTIONS is \"yes\"."
  66. (and=> (option-ref options key #f) (cut string=? <> "yes")))
  67. (define* (test-display field value #:optional (port (current-output-port))
  68. #:key pretty?)
  69. "Display \"FIELD: VALUE\\n\" on PORT."
  70. (if pretty?
  71. (begin
  72. (format port "~A:~%" field)
  73. (pretty-print value port #:per-line-prefix "+ "))
  74. (format port "~A: ~S~%" field value)))
  75. (define* (result->string symbol #:key colorize?)
  76. "Return SYMBOL as an upper case string. Use colors when COLORIZE is #t."
  77. (let ((result (string-upcase (symbol->string symbol))))
  78. (if colorize?
  79. (string-append (case symbol
  80. ((pass) "") ;green
  81. ((xfail) "") ;light green
  82. ((skip) "") ;blue
  83. ((fail xpass) "") ;red
  84. ((error) "")) ;magenta
  85. result
  86. "") ;no color
  87. result)))
  88. ;;;
  89. ;;; SRFI 64 custom test runner.
  90. ;;;
  91. (define* (test-runner-gnu test-name #:key color? brief? errors-only?
  92. show-duration?
  93. (out-port (current-output-port))
  94. (trs-port (%make-void-port "w"))
  95. select exclude)
  96. "Return an custom SRFI-64 test runner. TEST-NAME is a string specifying the
  97. file name of the current the test. COLOR? specifies whether to use colors.
  98. When BRIEF? is true, the individual test cases results are masked and only the
  99. summary is shown. ERRORS-ONLY? reduces the amount of test case metadata
  100. logged to only that of the failed test cases. OUT-PORT and TRS-PORT must be
  101. output ports. OUT-PORT defaults to the current output port, while TRS-PORT
  102. defaults to a void port, which means no TRS output is logged. SELECT and
  103. EXCLUDE may take a regular expression to select or exclude individual test
  104. cases based on their names."
  105. (define test-cases-start-time (make-hash-table))
  106. (define (test-on-test-begin-gnu runner)
  107. ;; Procedure called at the start of an individual test case, before the
  108. ;; test expression (and expected value) are evaluated.
  109. (let ((test-case-name (test-runner-test-name runner))
  110. (start-time (current-time time-monotonic)))
  111. (hash-set! test-cases-start-time test-case-name start-time)))
  112. (define (test-skipped? runner)
  113. (eq? 'skip (test-result-kind runner)))
  114. (define (test-failed? runner)
  115. (not (or (test-passed? runner)
  116. (test-skipped? runner))))
  117. (define (test-on-test-end-gnu runner)
  118. ;; Procedure called at the end of an individual test case, when the result
  119. ;; of the test is available.
  120. (let* ((results (test-result-alist runner))
  121. (result? (cut assq <> results))
  122. (result (cut assq-ref results <>))
  123. (test-case-name (test-runner-test-name runner))
  124. (start (hash-ref test-cases-start-time test-case-name))
  125. (end (current-time time-monotonic))
  126. (time-elapsed (time-difference end start))
  127. (time-elapsed-seconds (+ (time-second time-elapsed)
  128. (* 1e-9 (time-nanosecond time-elapsed)))))
  129. (unless (or brief? (and errors-only? (test-skipped? runner)))
  130. ;; Display the result of each test case on the console.
  131. (format out-port "~a: ~a - ~a ~@[[~,3fs]~]~%"
  132. (result->string (test-result-kind runner) #:colorize? color?)
  133. test-name test-case-name
  134. (and show-duration? time-elapsed-seconds)))
  135. (unless (and errors-only? (not (test-failed? runner)))
  136. (format #t "test-name: ~A~%" (result 'test-name))
  137. (format #t "location: ~A~%"
  138. (string-append (result 'source-file) ":"
  139. (number->string (result 'source-line))))
  140. (test-display "source" (result 'source-form) #:pretty? #t)
  141. (when (result? 'expected-value)
  142. (test-display "expected-value" (result 'expected-value)))
  143. (when (result? 'expected-error)
  144. (test-display "expected-error" (result 'expected-error) #:pretty? #t))
  145. (when (result? 'actual-value)
  146. (test-display "actual-value" (result 'actual-value)))
  147. (when (result? 'actual-error)
  148. (test-display "actual-error" (result 'actual-error) #:pretty? #t))
  149. (format #t "result: ~a~%" (result->string (result 'result-kind)))
  150. (newline))
  151. (format trs-port ":test-result: ~A ~A [~,3fs]~%"
  152. (result->string (test-result-kind runner))
  153. (test-runner-test-name runner) time-elapsed-seconds)))
  154. (define (test-on-group-end-gnu runner)
  155. ;; Procedure called by a 'test-end', including at the end of a test-group.
  156. (let ((fail (or (positive? (test-runner-fail-count runner))
  157. (positive? (test-runner-xpass-count runner))))
  158. (skip (or (positive? (test-runner-skip-count runner))
  159. (positive? (test-runner-xfail-count runner)))))
  160. ;; XXX: The global results need some refinements for XPASS.
  161. (format trs-port ":global-test-result: ~A~%"
  162. (if fail "FAIL" (if skip "SKIP" "PASS")))
  163. (format trs-port ":recheck: ~A~%"
  164. (if fail "yes" "no"))
  165. (format trs-port ":copy-in-global-log: ~A~%"
  166. (if (or fail skip) "yes" "no"))
  167. (when brief?
  168. ;; Display the global test group result on the console.
  169. (format out-port "~A: ~A~%"
  170. (result->string (if fail 'fail (if skip 'skip 'pass))
  171. #:colorize? color?)
  172. test-name))
  173. #f))
  174. (let ((runner (test-runner-null)))
  175. (test-runner-on-test-begin! runner test-on-test-begin-gnu)
  176. (test-runner-on-test-end! runner test-on-test-end-gnu)
  177. (test-runner-on-group-end! runner test-on-group-end-gnu)
  178. (test-runner-on-bad-end-name! runner test-on-bad-end-name-simple)
  179. runner))
  180. ;;;
  181. ;;; SRFI 64 test specifiers.
  182. ;;;
  183. (define (test-match-name* regexp)
  184. "Return a test specifier that matches a test name against REGEXP."
  185. (lambda (runner)
  186. (string-match regexp (test-runner-test-name runner))))
  187. (define (test-match-name*/negated regexp)
  188. "Return a negated test specifier version of test-match-name*."
  189. (lambda (runner)
  190. (not (string-match regexp (test-runner-test-name runner)))))
  191. ;;; XXX: test-match-all is a syntax, which isn't convenient to use with a list
  192. ;;; of test specifiers computed at run time. Copy this SRFI 64 internal
  193. ;;; definition here, which is the procedural equivalent of 'test-match-all'.
  194. (define (%test-match-all . pred-list)
  195. (lambda (runner)
  196. (let ((result #t))
  197. (let loop ((l pred-list))
  198. (if (null? l)
  199. result
  200. (begin
  201. (if (not ((car l) runner))
  202. (set! result #f))
  203. (loop (cdr l))))))))
  204. ;;;
  205. ;;; Entry point.
  206. ;;;
  207. (define (main . args)
  208. (let* ((opts (getopt-long (command-line) %options))
  209. (option (cut option-ref opts <> <>)))
  210. (cond
  211. ((option 'help #f) (show-help))
  212. ((option 'version #f) (format #t "test-driver.scm ~A~%" script-version))
  213. (else
  214. (let* ((log (and=> (option 'log-file #f) (cut open-file <> "w0")))
  215. (trs (and=> (option 'trs-file #f) (cut open-file <> "wl")))
  216. (out (duplicate-port (current-output-port) "wl"))
  217. (test-name (option 'test-name #f))
  218. (select (option 'select #f))
  219. (exclude (option 'exclude #f))
  220. (test-specifiers (filter-map
  221. identity
  222. (list (and=> select test-match-name*)
  223. (and=> exclude test-match-name*/negated))))
  224. (test-specifier (apply %test-match-all test-specifiers))
  225. (color-tests (if (assoc 'color-tests opts)
  226. (option->boolean opts 'color-tests)
  227. #t)))
  228. (when log
  229. (redirect-port log (current-output-port))
  230. (redirect-port log (current-warning-port))
  231. (redirect-port log (current-error-port)))
  232. (test-with-runner
  233. (test-runner-gnu test-name
  234. #:color? color-tests
  235. #:brief? (option->boolean opts 'brief)
  236. #:errors-only? (option->boolean opts 'errors-only)
  237. #:show-duration? (option->boolean
  238. opts 'show-duration)
  239. #:out-port out #:trs-port trs)
  240. (test-apply test-specifier
  241. (lambda _
  242. (load-from-path test-name))))
  243. (and=> log close-port)
  244. (and=> trs close-port)
  245. (close-port out))))
  246. (exit 0)))
  247. ;;; Local Variables:
  248. ;;; eval: (add-hook 'write-file-functions 'time-stamp)
  249. ;;; time-stamp-start: "(define script-version \""
  250. ;;; time-stamp-format: "%:y-%02m-%02d.%02H"
  251. ;;; time-stamp-time-zone: "UTC"
  252. ;;; time-stamp-end: "\") ;UTC"
  253. ;;; End:
  254. ;;;; test-driver.scm ends here.