guile-test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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, 2022 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. ;;; Traverse the directory tree at ROOT, applying F to the name of
  102. ;;; each file in the tree, including ROOT itself. For a subdirectory
  103. ;;; SUB, if (F SUB) is true, we recurse into SUB. Do not follow
  104. ;;; symlinks.
  105. (define (for-each-file f root)
  106. ;; A "hard directory" is a path that denotes a directory and is not a
  107. ;; symlink.
  108. (define (file-is-hard-directory? filename)
  109. (eq? (stat:type (lstat filename)) 'directory))
  110. (let visit ((root root))
  111. (let ((should-recur (f root)))
  112. (if (and should-recur (file-is-hard-directory? root))
  113. (let ((dir (opendir root)))
  114. (let loop ()
  115. (let ((entry (readdir dir)))
  116. (cond
  117. ((eof-object? entry) #f)
  118. ((or (string=? entry ".")
  119. (string=? entry "..")
  120. (string=? entry "CVS")
  121. (string=? entry "RCS"))
  122. (loop))
  123. (else
  124. (visit (string-append root "/" entry))
  125. (loop))))))))))
  126. ;;; The test driver.
  127. ;;; Localizing test files and temporary data files.
  128. (define (data-file-name filename)
  129. (in-vicinity tmp-dir filename))
  130. (define (test-file-name test)
  131. (in-vicinity test-suite test))
  132. ;;; Return a list of all the test files in the test tree.
  133. (define (enumerate-tests test-dir)
  134. (let ((root-len (+ 1 (string-length test-dir)))
  135. (tests '()))
  136. (for-each-file (lambda (file)
  137. (if (string-suffix? ".test" file)
  138. (let ((short-name
  139. (substring file root-len)))
  140. (set! tests (cons short-name tests))))
  141. #t)
  142. test-dir)
  143. ;; for-each-file presents the files in whatever order it finds
  144. ;; them in the directory. We sort them here, so they'll always
  145. ;; appear in the same order. This makes it easier to compare test
  146. ;; log files mechanically.
  147. (sort tests string<?)))
  148. (define (main args)
  149. (let ((options (getopt-long args
  150. `((test-suite
  151. (single-char #\t)
  152. (value #t))
  153. (flag-unresolved
  154. (single-char #\u))
  155. (log-file
  156. (single-char #\l)
  157. (value #t))
  158. (coverage
  159. (single-char #\c))
  160. (debug
  161. (single-char #\d))))))
  162. (define (opt tag default)
  163. (let ((pair (assq tag options)))
  164. (if pair (cdr pair) default)))
  165. (if (opt 'debug #f)
  166. (enable-debug-mode))
  167. (set! test-suite
  168. (or (opt 'test-suite #f)
  169. (getenv "TEST_SUITE_DIR")
  170. (default-test-suite)))
  171. ;; directory where temporary files are created.
  172. ;; when run from "make check", this must be under the build-dir,
  173. ;; not the src-dir.
  174. (set! tmp-dir (getcwd))
  175. (let* ((tests
  176. (let ((foo (opt '() '())))
  177. (if (null? foo)
  178. (enumerate-tests test-suite)
  179. foo)))
  180. (log-file
  181. (opt 'log-file "guile.log")))
  182. ;; Open the log file.
  183. (let ((log-port (open-output-file log-file)))
  184. ;; Allow for arbitrary Unicode characters in the log file.
  185. (set-port-encoding! log-port "UTF-8")
  186. ;; Don't fail if we can't display a test name to stdout/stderr.
  187. (set-port-conversion-strategy! (current-output-port) 'escape)
  188. (set-port-conversion-strategy! (current-error-port) 'escape)
  189. ;; Register some reporters.
  190. (let ((global-pass #t)
  191. (counter (make-count-reporter)))
  192. (register-reporter (car counter))
  193. (register-reporter (make-log-reporter log-port))
  194. (register-reporter user-reporter)
  195. (register-reporter (lambda results
  196. (case (car results)
  197. ((unresolved)
  198. (and (opt 'flag-unresolved #f)
  199. (set! global-pass #f)))
  200. ((fail upass error)
  201. (set! global-pass #f)))))
  202. ;; Run the tests.
  203. (let ((run-tests
  204. (lambda ()
  205. (for-each (lambda (test)
  206. (display (string-append "Running " test "\n"))
  207. (when (defined? 'setlocale)
  208. (setlocale LC_ALL "C"))
  209. (with-test-prefix test
  210. (load (test-file-name test))))
  211. tests))))
  212. (if (opt 'coverage #f)
  213. (let-values (((coverage-data _)
  214. (with-code-coverage run-tests)))
  215. (let ((out (open-output-file "guile.info")))
  216. (coverage-data->lcov coverage-data out)
  217. (close out)))
  218. (run-tests)))
  219. ;; Display the final counts, both to the user and in the log
  220. ;; file.
  221. (let ((counts ((cadr counter))))
  222. (print-counts counts)
  223. (print-counts counts log-port))
  224. (close-port log-port)
  225. (quit global-pass))))))
  226. ;;; Local Variables:
  227. ;;; mode: scheme
  228. ;;; End: