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