test-driver.scm 9.3 KB

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