test-driver.scm 7.7 KB

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