test-driver.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ;;;; test-driver.scm - Guile test driver for Automake testsuite harness
  2. (define script-version "2019-01-15.13") ;UTC
  3. ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
  4. ;;; Copyright © 2019 Alex Sassmannshausen <alex@pompo.co>
  5. ;;;
  6. ;;; This program is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; This program is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. ;;;; Commentary:
  19. ;;;
  20. ;;; This script provides a Guile test driver using the SRFI-64 Scheme API for
  21. ;;; test suites. SRFI-64 is distributed with Guile since version 2.0.9.
  22. ;;;
  23. ;;; This script is a lightly modified version of the orignal written by
  24. ;;; Matthieu Lirzin. The changes make it suitable for use as part of the
  25. ;;; guile-hall infrastructure.
  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.
  39. "))
  40. (define %options
  41. '((test-name (value #t))
  42. (log-file (value #t))
  43. (trs-file (value #t))
  44. (color-tests (value #t))
  45. (expect-failure (value #t)) ;XXX: not implemented yet
  46. (enable-hard-errors (value #t)) ;not implemented in SRFI-64
  47. (brief (value #t))
  48. (help (single-char #\h) (value #f))
  49. (version (single-char #\V) (value #f))))
  50. (define (option->boolean options key)
  51. "Return #t if the value associated with KEY in OPTIONS is 'yes'."
  52. (and=> (option-ref options key #f) (cut string=? <> "yes")))
  53. (define* (test-display field value #:optional (port (current-output-port))
  54. #:key pretty?)
  55. "Display 'FIELD: VALUE\n' on PORT."
  56. (if pretty?
  57. (begin
  58. (format port "~A:~%" field)
  59. (pretty-print value port #:per-line-prefix "+ "))
  60. (format port "~A: ~S~%" field value)))
  61. (define* (result->string symbol #:key colorize?)
  62. "Return SYMBOL as an upper case string. Use colors when COLORIZE is #t."
  63. (let ((result (string-upcase (symbol->string symbol))))
  64. (if colorize?
  65. (string-append (case symbol
  66. ((pass) "") ;green
  67. ((xfail) "") ;light green
  68. ((skip) "") ;blue
  69. ((fail xpass) "") ;red
  70. ((error) "")) ;magenta
  71. result
  72. "") ;no color
  73. result)))
  74. (define* (test-runner-gnu test-name #:key color? brief? out-port trs-port)
  75. "Return an custom SRFI-64 test runner. TEST-NAME is a string specifying the
  76. file name of the current the test. COLOR? specifies whether to use colors,
  77. and BRIEF?, well, you know. OUT-PORT and TRS-PORT must be output ports. The
  78. current output port is supposed to be redirected to a '.log' file."
  79. (define (test-on-test-begin-gnu runner)
  80. ;; Procedure called at the start of an individual test case, before the
  81. ;; test expression (and expected value) are evaluated.
  82. (let ((result (cute assq-ref (test-result-alist runner) <>)))
  83. (format #t "test-name: ~A~%" (result 'test-name))
  84. (format #t "location: ~A~%"
  85. (string-append (result 'source-file) ":"
  86. (number->string (result 'source-line))))
  87. (test-display "source" (result 'source-form) #:pretty? #t)))
  88. (define (test-on-test-end-gnu runner)
  89. ;; Procedure called at the end of an individual test case, when the result
  90. ;; of the test is available.
  91. (let* ((results (test-result-alist runner))
  92. (result? (cut assq <> results))
  93. (result (cut assq-ref results <>)))
  94. (unless brief?
  95. ;; Display the result of each test case on the console.
  96. (format out-port "~A: ~A - ~A~%"
  97. (result->string (test-result-kind runner) #:colorize? color?)
  98. test-name (test-runner-test-name runner)))
  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. (format #t "result: ~a~%" (result->string (result 'result-kind)))
  108. (newline)
  109. (format trs-port ":test-result: ~A ~A~%"
  110. (result->string (test-result-kind runner))
  111. (test-runner-test-name runner))))
  112. (define (test-on-group-end-gnu runner)
  113. ;; Procedure called by a 'test-end', including at the end of a test-group.
  114. (let ((fail (or (positive? (test-runner-fail-count runner))
  115. (positive? (test-runner-xpass-count runner))))
  116. (skip (or (positive? (test-runner-skip-count runner))
  117. (positive? (test-runner-xfail-count runner)))))
  118. ;; XXX: The global results need some refinements for XPASS.
  119. (format trs-port ":global-test-result: ~A~%"
  120. (if fail "FAIL" (if skip "SKIP" "PASS")))
  121. (format trs-port ":recheck: ~A~%"
  122. (if fail "yes" "no"))
  123. (format trs-port ":copy-in-global-log: ~A~%"
  124. (if (or fail skip) "yes" "no"))
  125. (when brief?
  126. ;; Display the global test group result on the console.
  127. (format out-port "~A: ~A~%"
  128. (result->string (if fail 'fail (if skip 'skip 'pass))
  129. #:colorize? color?)
  130. test-name))
  131. #f))
  132. (let ((runner (test-runner-null)))
  133. (test-runner-on-test-begin! runner test-on-test-begin-gnu)
  134. (test-runner-on-test-end! runner test-on-test-end-gnu)
  135. (test-runner-on-group-end! runner test-on-group-end-gnu)
  136. (test-runner-on-bad-end-name! runner test-on-bad-end-name-simple)
  137. runner))
  138. ;;;
  139. ;;; Entry point.
  140. ;;;
  141. (define (main . args)
  142. (let* ((opts (getopt-long (command-line) %options))
  143. (option (cut option-ref opts <> <>)))
  144. (cond
  145. ((option 'help #f) (show-help))
  146. ((option 'version #f) (format #t "test-driver.scm ~A" script-version))
  147. (else
  148. (let ((log (open-file (option 'log-file "") "w0"))
  149. (trs (open-file (option 'trs-file "") "wl"))
  150. (out (duplicate-port (current-output-port) "wl")))
  151. (redirect-port log (current-output-port))
  152. (redirect-port log (current-warning-port))
  153. (redirect-port log (current-error-port))
  154. (test-with-runner
  155. (test-runner-gnu (option 'test-name #f)
  156. #:color? (option->boolean opts 'color-tests)
  157. #:brief? (option->boolean opts 'brief)
  158. #:out-port out #:trs-port trs)
  159. (load-from-path (option 'test-name #f)))
  160. (close-port log)
  161. (close-port trs)
  162. (close-port out))))
  163. (exit 0)))