guile-test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #!../meta/guile \
  2. -e main -s
  3. !#
  4. ;;;; guile-test --- run the Guile test suite
  5. ;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
  6. ;;;;
  7. ;;;; Copyright (C) 1999, 2001, 2006, 2010, 2014 Free Software Foundation, Inc.
  8. ;;;;
  9. ;;;; This program is free software; you can redistribute it and/or
  10. ;;;; modify it under the terms of the GNU Lesser General Public
  11. ;;;; License as published by the Free Software Foundation; either
  12. ;;;; version 3, or (at your option) any later version.
  13. ;;;;
  14. ;;;; This program is distributed in the hope that it will be useful,
  15. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;;; GNU Lesser General Public License for more details.
  18. ;;;;
  19. ;;;; You should have received a copy of the GNU Lesser General Public
  20. ;;;; License along with this software; see the file COPYING.LESSER.
  21. ;;;; If not, write to the Free Software Foundation, Inc., 51 Franklin
  22. ;;;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. ;;;; Usage: [guile -L `pwd`/test-suite -e main -s] guile-test [OPTIONS] [TEST ...]
  24. ;;;;
  25. ;;;; Run tests from the Guile test suite. Report failures and
  26. ;;;; unexpected passes to the standard output, along with a summary of
  27. ;;;; all the results. Record each reported test outcome in the log
  28. ;;;; file, `guile.log'. The exit status is #f if any of the tests
  29. ;;;; fail or pass unexpectedly.
  30. ;;;;
  31. ;;;; Normally, guile-test scans the test directory, and executes all
  32. ;;;; files whose names end in `.test'. (It assumes they contain
  33. ;;;; Scheme code.) However, you can have it execute specific tests by
  34. ;;;; listing their filenames on the command line.
  35. ;;;;
  36. ;;;; The option `--test-suite' can be given to specify the test
  37. ;;;; directory. If no such option is given, the test directory is
  38. ;;;; taken from the environment variable TEST_SUITE_DIR (if defined),
  39. ;;;; otherwise a default directory that is hardcoded in this file is
  40. ;;;; used (see "Installation" below).
  41. ;;;;
  42. ;;;; If present, the `--log-file LOG' option tells `guile-test' to put
  43. ;;;; the log output in a file named LOG.
  44. ;;;;
  45. ;;;; If present, the `--debug' option will enable a debugging mode.
  46. ;;;;
  47. ;;;; If present, the `--flag-unresolved' option will cause guile-test
  48. ;;;; to exit with failure status if any tests are UNRESOLVED.
  49. ;;;;
  50. ;;;;
  51. ;;;; Installation:
  52. ;;;;
  53. ;;;; If you change the #! line at the top of this script to point at
  54. ;;;; the Guile interpreter you want to test, you can call this script
  55. ;;;; as an executable instead of having to pass it as a parameter to
  56. ;;;; guile via "guile -e main -s guile-test". Further, you can edit
  57. ;;;; the definition of default-test-suite to point to the parent
  58. ;;;; directory of the `tests' tree, which makes it unnecessary to set
  59. ;;;; the environment variable `TEST_SUITE_DIR'.
  60. ;;;;
  61. ;;;;
  62. ;;;; Shortcomings:
  63. ;;;;
  64. ;;;; At the moment, due to a simple-minded implementation, test files
  65. ;;;; must live in the test directory, and you must specify their names
  66. ;;;; relative to the top of the test directory. If you want to send
  67. ;;;; me a patch that fixes this, but still leaves sane test names in
  68. ;;;; the log file, that would be great. At the moment, all the tests
  69. ;;;; I care about are in the test directory, though.
  70. ;;;;
  71. ;;;; It would be nice if you could specify the Guile interpreter you
  72. ;;;; want to test on the command line. As it stands, if you want to
  73. ;;;; change which Guile interpreter you're testing, you need to edit
  74. ;;;; the #! line at the top of this file, which is stupid.
  75. (define (main . args)
  76. (let ((module (resolve-module '(test-suite guile-test))))
  77. (apply (module-ref module 'main) args)))
  78. (define-module (test-suite guile-test)
  79. :use-module (test-suite lib)
  80. :use-module (ice-9 getopt-long)
  81. :use-module (ice-9 and-let-star)
  82. :use-module (ice-9 rdelim)
  83. :use-module (system vm coverage)
  84. :use-module (srfi srfi-11)
  85. :use-module (system vm vm)
  86. :export (main data-file-name test-file-name))
  87. ;;; User configurable settings:
  88. (define (default-test-suite)
  89. (let ((argv0 (car (program-arguments))))
  90. (if (string=? (basename argv0) "guile-test")
  91. (dirname argv0)
  92. (error "Cannot find default test suite."))))
  93. ;;; Variables that will receive their actual values later.
  94. (define test-suite)
  95. (define tmp-dir #f)
  96. ;;; General utilities, that probably should be in a library somewhere.
  97. ;;; Enable debugging
  98. (define (enable-debug-mode)
  99. (write-line %load-path)
  100. (set! %load-verbosely #t)
  101. (debug-enable 'backtrace 'debug))
  102. ;;; Traverse the directory tree at ROOT, applying F to the name of
  103. ;;; each file in the tree, including ROOT itself. For a subdirectory
  104. ;;; SUB, if (F SUB) is true, we recurse into SUB. Do not follow
  105. ;;; symlinks.
  106. (define (for-each-file f root)
  107. ;; A "hard directory" is a path that denotes a directory and is not a
  108. ;; symlink.
  109. (define (file-is-hard-directory? filename)
  110. (eq? (stat:type (lstat filename)) 'directory))
  111. (let visit ((root root))
  112. (let ((should-recur (f root)))
  113. (if (and should-recur (file-is-hard-directory? root))
  114. (let ((dir (opendir root)))
  115. (let loop ()
  116. (let ((entry (readdir dir)))
  117. (cond
  118. ((eof-object? entry) #f)
  119. ((or (string=? entry ".")
  120. (string=? entry "..")
  121. (string=? entry "CVS")
  122. (string=? entry "RCS"))
  123. (loop))
  124. (else
  125. (visit (string-append root "/" entry))
  126. (loop))))))))))
  127. ;;; The test driver.
  128. ;;; Localizing test files and temporary data files.
  129. (define (data-file-name filename)
  130. (in-vicinity tmp-dir filename))
  131. (define (test-file-name test)
  132. (in-vicinity test-suite test))
  133. ;;; Return a list of all the test files in the test tree.
  134. (define (enumerate-tests test-dir)
  135. (let ((root-len (+ 1 (string-length test-dir)))
  136. (tests '()))
  137. (for-each-file (lambda (file)
  138. (if (string-suffix? ".test" file)
  139. (let ((short-name
  140. (substring file root-len)))
  141. (set! tests (cons short-name tests))))
  142. #t)
  143. test-dir)
  144. ;; for-each-file presents the files in whatever order it finds
  145. ;; them in the directory. We sort them here, so they'll always
  146. ;; appear in the same order. This makes it easier to compare test
  147. ;; log files mechanically.
  148. (sort tests string<?)))
  149. (define (main args)
  150. (let ((options (getopt-long args
  151. `((test-suite
  152. (single-char #\t)
  153. (value #t))
  154. (flag-unresolved
  155. (single-char #\u))
  156. (log-file
  157. (single-char #\l)
  158. (value #t))
  159. (coverage
  160. (single-char #\c))
  161. (debug
  162. (single-char #\d))))))
  163. (define (opt tag default)
  164. (let ((pair (assq tag options)))
  165. (if pair (cdr pair) default)))
  166. (if (opt 'debug #f)
  167. (enable-debug-mode))
  168. (set! test-suite
  169. (or (opt 'test-suite #f)
  170. (getenv "TEST_SUITE_DIR")
  171. (default-test-suite)))
  172. ;; directory where temporary files are created.
  173. ;; when run from "make check", this must be under the build-dir,
  174. ;; not the src-dir.
  175. (set! tmp-dir (getcwd))
  176. (let* ((tests
  177. (let ((foo (opt '() '())))
  178. (if (null? foo)
  179. (enumerate-tests test-suite)
  180. foo)))
  181. (log-file
  182. (opt 'log-file "guile.log")))
  183. ;; Open the log file.
  184. (let ((log-port (open-output-file log-file)))
  185. ;; Allow for arbitrary Unicode characters in the log file.
  186. (set-port-encoding! log-port "UTF-8")
  187. ;; Don't fail if we can't display a test name to stdout/stderr.
  188. (set-port-conversion-strategy! (current-output-port) 'escape)
  189. (set-port-conversion-strategy! (current-error-port) 'escape)
  190. ;; Register some reporters.
  191. (let ((global-pass #t)
  192. (counter (make-count-reporter)))
  193. (register-reporter (car counter))
  194. (register-reporter (make-log-reporter log-port))
  195. (register-reporter user-reporter)
  196. (register-reporter (lambda results
  197. (case (car results)
  198. ((unresolved)
  199. (and (opt 'flag-unresolved #f)
  200. (set! global-pass #f)))
  201. ((fail upass error)
  202. (set! global-pass #f)))))
  203. ;; Run the tests.
  204. (let ((run-tests
  205. (lambda ()
  206. (for-each (lambda (test)
  207. (display (string-append "Running " test "\n"))
  208. (when (defined? 'setlocale)
  209. (setlocale LC_ALL "C"))
  210. (with-test-prefix test
  211. (load (test-file-name test))))
  212. tests))))
  213. (if (opt 'coverage #f)
  214. (let-values (((coverage-data _)
  215. (with-code-coverage run-tests)))
  216. (let ((out (open-output-file "guile.info")))
  217. (coverage-data->lcov coverage-data out)
  218. (close out)))
  219. (run-tests)))
  220. ;; Display the final counts, both to the user and in the log
  221. ;; file.
  222. (let ((counts ((cadr counter))))
  223. (print-counts counts)
  224. (print-counts counts log-port))
  225. (close-port log-port)
  226. (quit global-pass))))))
  227. ;;; Local Variables:
  228. ;;; mode: scheme
  229. ;;; End: