test-driver.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ;;;; test-driver.scm - Guile test driver for Automake testsuite harness
  2. (define script-version "2016-05-09.22") ;UTC
  3. ;;; Copyright (C) 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
  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. ;;;
  18. ;;; As a special exception to the GNU General Public License, if you
  19. ;;; distribute this file as part of a program that contains a configuration
  20. ;;; script generated by Autoconf, you may include it under the same
  21. ;;; distribution terms that you use for the rest of that program.
  22. ;;;; Commentary:
  23. ;;;
  24. ;;; This script provides a Guile test driver using the SRFI-64 Scheme API for
  25. ;;; test suites. SRFI-64 is distributed with Guile since version 2.0.9.
  26. ;;;
  27. ;;;; Code:
  28. (use-modules (ice-9 getopt-long)
  29. (ice-9 pretty-print)
  30. (srfi srfi-26)
  31. (srfi srfi-64))
  32. (define (show-help)
  33. (display "Usage:
  34. test-driver --test-name=NAME --log-file=PATH --trs-file=PATH
  35. [--expect-failure={yes|no}] [--color-tests={yes|no}]
  36. [--enable-hard-errors={yes|no}] [--brief={yes|no}}] [--]
  37. TEST-SCRIPT [TEST-SCRIPT-ARGUMENTS]
  38. The '--test-name', '--log-file' and '--trs-file' options are mandatory.\n"))
  39. (define %options
  40. '((test-name (value #t))
  41. (log-file (value #t))
  42. (trs-file (value #t))
  43. (color-tests (value #t))
  44. (expect-failure (value #t)) ;XXX: not implemented yet
  45. (enable-hard-errors (value #t)) ;not implemented in SRFI-64
  46. (brief (value #t))
  47. (help (single-char #\h) (value #f))
  48. (version (single-char #\V) (value #f))))
  49. (define (option->boolean options key)
  50. "Return #t if the value associated with KEY in OPTIONS is \"yes\"."
  51. (and=> (option-ref options key #f) (cut string=? <> "yes")))
  52. (define* (test-display field value #:optional (port (current-output-port))
  53. #:key pretty?)
  54. "Display \"FIELD: VALUE\\n\" on PORT."
  55. (if pretty?
  56. (begin
  57. (format port "~A:~%" field)
  58. (pretty-print value port #:per-line-prefix "+ "))
  59. (format port "~A: ~A~%" field value)))
  60. (define* (result->string symbol #:key colorize?)
  61. "Return SYMBOL as an upper case string. Use colors when COLORIZE is #t."
  62. (let ((result (string-upcase (symbol->string symbol))))
  63. (if colorize?
  64. (string-append (case symbol
  65. ((pass) "") ;green
  66. ((xfail) "") ;light green
  67. ((skip) "") ;blue
  68. ((fail xpass) "") ;red
  69. ((error) "")) ;magenta
  70. result
  71. "") ;no color
  72. result)))
  73. (define* (test-runner-gnu test-name #:key color? brief? out-port trs-port)
  74. "Return an custom SRFI-64 test runner. TEST-NAME is a string specifying the
  75. file name of the current the test. COLOR? specifies whether to use colors,
  76. and BRIEF?, well, you know. OUT-PORT and TRS-PORT must be output ports. The
  77. current output port is supposed to be redirected to a '.log' file."
  78. (define (test-on-test-begin-gnu runner)
  79. ;; Procedure called at the start of an individual test case, before the
  80. ;; test expression (and expected value) are evaluated.
  81. (let ((result (cute assq-ref (test-result-alist runner) <>)))
  82. (test-display "test-name" (result 'test-name))
  83. (test-display "location"
  84. (string-append (result 'source-file) ":"
  85. (number->string (result 'source-line))))
  86. (test-display "source" (result 'source-form) #:pretty? #t)))
  87. (define (test-on-test-end-gnu runner)
  88. ;; Procedure called at the end of an individual test case, when the result
  89. ;; of the test is available.
  90. (let* ((results (test-result-alist runner))
  91. (result? (cut assq <> results))
  92. (result (cut assq-ref results <>)))
  93. (unless brief?
  94. ;; Display the result of each test case on the console.
  95. (test-display
  96. (result->string (test-result-kind runner) #:colorize? color?)
  97. (string-append test-name " - " (test-runner-test-name runner))
  98. out-port))
  99. (when (result? 'expected-value)
  100. (test-display "expected-value" (result 'expected-value)))
  101. (when (result? 'expected-error)
  102. (test-display "expected-error" (result 'expected-error) #:pretty? #t))
  103. (when (result? 'actual-value)
  104. (test-display "actual-value" (result 'actual-value)))
  105. (when (result? 'actual-error)
  106. (test-display "actual-error" (result 'actual-error) #:pretty? #t))
  107. (test-display "result" (result->string (result 'result-kind)))
  108. (newline)
  109. (test-display ":test-result"
  110. (string-append (result->string (test-result-kind runner))
  111. " " (test-runner-test-name runner))
  112. trs-port)))
  113. (define (test-on-group-end-gnu runner)
  114. ;; Procedure called by a 'test-end', including at the end of a test-group.
  115. (let ((fail (or (positive? (test-runner-fail-count runner))
  116. (positive? (test-runner-xpass-count runner))))
  117. (skip (or (positive? (test-runner-skip-count runner))
  118. (positive? (test-runner-xfail-count runner)))))
  119. ;; XXX: The global results need some refinements for XPASS.
  120. (test-display ":global-test-result"
  121. (if fail "FAIL" (if skip "SKIP" "PASS"))
  122. trs-port)
  123. (test-display ":recheck"
  124. (if fail "yes" "no")
  125. trs-port)
  126. (test-display ":copy-in-global-log"
  127. (if (or fail skip) "yes" "no")
  128. trs-port)
  129. (when brief?
  130. ;; Display the global test group result on the console.
  131. (test-display (result->string (if fail 'fail (if skip 'skip 'pass))
  132. #:colorize? color?)
  133. test-name
  134. out-port))
  135. #f))
  136. (let ((runner (test-runner-null)))
  137. (test-runner-on-test-begin! runner test-on-test-begin-gnu)
  138. (test-runner-on-test-end! runner test-on-test-end-gnu)
  139. (test-runner-on-group-end! runner test-on-group-end-gnu)
  140. (test-runner-on-bad-end-name! runner test-on-bad-end-name-simple)
  141. runner))
  142. ;;;
  143. ;;; Entry point.
  144. ;;;
  145. (let* ((opts (getopt-long (command-line) %options))
  146. (option (cut option-ref opts <> <>)))
  147. (cond
  148. ((option 'help #f) (show-help))
  149. ((option 'version #f) (format #t "test-driver.scm ~A~%" script-version))
  150. (else
  151. (let ((log (open-file (option 'log-file "") "w0"))
  152. (trs (open-file (option 'trs-file "") "wl"))
  153. (out (duplicate-port (current-output-port) "wl")))
  154. (redirect-port log (current-output-port))
  155. (redirect-port log (current-warning-port))
  156. (redirect-port log (current-error-port))
  157. (test-with-runner
  158. (test-runner-gnu (option 'test-name #f)
  159. #:color? (option->boolean opts 'color-tests)
  160. #:brief? (option->boolean opts 'brief)
  161. #:out-port out #:trs-port trs)
  162. (load (string-append (getcwd) "/" (car (option '() '(""))))))
  163. (close-port log)
  164. (close-port trs)
  165. (close-port out))))
  166. (exit 0))
  167. ;;; Local Variables:
  168. ;;; eval: (add-hook 'write-file-functions 'time-stamp)
  169. ;;; time-stamp-start: "(define script-version \""
  170. ;;; time-stamp-format: "%:y-%02m-%02d.%02H"
  171. ;;; time-stamp-time-zone: "UTC"
  172. ;;; time-stamp-end: "\") ;UTC"
  173. ;;; End:
  174. ;;;; test-driver.scm ends here.