guile-test 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!../libguile/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 Free Software Foundation, Inc.
  8. ;;;;
  9. ;;;; This program is free software; you can redistribute it and/or modify
  10. ;;;; it under the terms of the GNU General Public License as published by
  11. ;;;; the Free Software Foundation; either version 2, or (at your option)
  12. ;;;; 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 General Public License for more details.
  18. ;;;;
  19. ;;;; You should have received a copy of the GNU General Public License
  20. ;;;; along with this software; see the file COPYING. If not, write to
  21. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;;;; Boston, MA 02110-1301 USA
  23. ;;;; Usage: [guile -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. :export (main data-file-name test-file-name))
  84. ;;; User configurable settings:
  85. (define default-test-suite
  86. (string-append (getenv "HOME") "/bogus-path/test-suite"))
  87. ;;; Variables that will receive their actual values later.
  88. (define test-suite default-test-suite)
  89. (define tmp-dir #f)
  90. ;;; General utilities, that probably should be in a library somewhere.
  91. ;;; Enable debugging
  92. (define (enable-debug-mode)
  93. (write-line %load-path)
  94. (set! %load-verbosely #t)
  95. (debug-enable 'backtrace 'debug))
  96. ;;; Traverse the directory tree at ROOT, applying F to the name of
  97. ;;; each file in the tree, including ROOT itself. For a subdirectory
  98. ;;; SUB, if (F SUB) is true, we recurse into SUB. Do not follow
  99. ;;; symlinks.
  100. (define (for-each-file f root)
  101. ;; A "hard directory" is a path that denotes a directory and is not a
  102. ;; symlink.
  103. (define (file-is-hard-directory? filename)
  104. (eq? (stat:type (lstat filename)) 'directory))
  105. (let visit ((root root))
  106. (let ((should-recur (f root)))
  107. (if (and should-recur (file-is-hard-directory? root))
  108. (let ((dir (opendir root)))
  109. (let loop ()
  110. (let ((entry (readdir dir)))
  111. (cond
  112. ((eof-object? entry) #f)
  113. ((or (string=? entry ".")
  114. (string=? entry "..")
  115. (string=? entry "CVS")
  116. (string=? entry "RCS"))
  117. (loop))
  118. (else
  119. (visit (string-append root "/" entry))
  120. (loop))))))))))
  121. ;;; The test driver.
  122. ;;; Localizing test files and temporary data files.
  123. (define (data-file-name filename)
  124. (in-vicinity tmp-dir filename))
  125. (define (test-file-name test)
  126. (in-vicinity test-suite test))
  127. ;;; Return a list of all the test files in the test tree.
  128. (define (enumerate-tests test-dir)
  129. (let ((root-len (+ 1 (string-length test-dir)))
  130. (tests '()))
  131. (for-each-file (lambda (file)
  132. (if (has-suffix? file ".test")
  133. (let ((short-name
  134. (substring file root-len)))
  135. (set! tests (cons short-name tests))))
  136. #t)
  137. test-dir)
  138. ;; for-each-file presents the files in whatever order it finds
  139. ;; them in the directory. We sort them here, so they'll always
  140. ;; appear in the same order. This makes it easier to compare test
  141. ;; log files mechanically.
  142. (sort tests string<?)))
  143. (define (main args)
  144. (let ((options (getopt-long args
  145. `((test-suite
  146. (single-char #\t)
  147. (value #t))
  148. (flag-unresolved
  149. (single-char #\u))
  150. (log-file
  151. (single-char #\l)
  152. (value #t))
  153. (debug
  154. (single-char #\d))))))
  155. (define (opt tag default)
  156. (let ((pair (assq tag options)))
  157. (if pair (cdr pair) default)))
  158. (if (opt 'debug #f)
  159. (enable-debug-mode))
  160. (set! test-suite
  161. (or (opt 'test-suite #f)
  162. (getenv "TEST_SUITE_DIR")
  163. default-test-suite))
  164. ;; directory where temporary files are created.
  165. ;; when run from "make check", this must be under the build-dir,
  166. ;; not the src-dir.
  167. (set! tmp-dir (getcwd))
  168. (let* ((tests
  169. (let ((foo (opt '() '())))
  170. (if (null? foo)
  171. (enumerate-tests test-suite)
  172. foo)))
  173. (log-file
  174. (opt 'log-file "guile.log")))
  175. ;; Open the log file.
  176. (let ((log-port (open-output-file log-file)))
  177. ;; Register some reporters.
  178. (let ((global-pass #t)
  179. (counter (make-count-reporter)))
  180. (register-reporter (car counter))
  181. (register-reporter (make-log-reporter log-port))
  182. (register-reporter user-reporter)
  183. (register-reporter (lambda results
  184. (case (car results)
  185. ((unresolved)
  186. (and (opt 'flag-unresolved #f)
  187. (set! global-pass #f)))
  188. ((fail upass error)
  189. (set! global-pass #f)))))
  190. ;; Run the tests.
  191. (for-each (lambda (test)
  192. (display (string-append "Running " test "\n"))
  193. (with-test-prefix test
  194. (load (test-file-name test))))
  195. tests)
  196. ;; Display the final counts, both to the user and in the log
  197. ;; file.
  198. (let ((counts ((cadr counter))))
  199. (print-counts counts)
  200. (print-counts counts log-port))
  201. (close-port log-port)
  202. (quit global-pass))))))
  203. ;;; Local Variables:
  204. ;;; mode: scheme
  205. ;;; End: