Debian Codegraph @

test-suite/guile-test

raw
1 #!../meta/guile \
2 -e main -s
3 !#
4
5 ;;;; guile-test --- run the Guile test suite
6 ;;;; Jim Blandy <[email protected]> --- May 1999
7 ;;;;
8 ;;;; Copyright (C) 1999, 2001, 2006, 2010, 2014, 2022 Free Software Foundation, Inc.
9 ;;;;
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.
14 ;;;;
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
18 ;;;; GNU Lesser General Public License for more details.
19 ;;;;
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
24
25
26 ;;;; Usage: [guile -L `pwd`/test-suite -e main -s] guile-test [OPTIONS] [TEST ...]
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
31 ;;;; file, `guile.log'. The exit status is #f if any of the tests
32 ;;;; fail or pass unexpectedly.
33 ;;;;
34 ;;;; Normally, guile-test scans the test directory, and executes all
35 ;;;; files whose names end in `.test' and don't begin with `.'. (It
36 ;;;; assumes they contain Scheme code.) However, you can have it
37 ;;;; execute specific tests by listing their filenames on the command
38 ;;;; line.
39 ;;;;
40 ;;;; The option `--test-suite' can be given to specify the test
41 ;;;; directory. If no such option is given, the test directory is
42 ;;;; taken from the environment variable TEST_SUITE_DIR (if defined),
43 ;;;; otherwise a default directory that is hardcoded in this file is
44 ;;;; used (see "Installation" below).
45 ;;;;
46 ;;;; If present, the `--log-file LOG' option tells `guile-test' to put
47 ;;;; the log output in a file named LOG.
48 ;;;;
49 ;;;; If present, the `--debug' option will enable a debugging mode.
50 ;;;;
51 ;;;; If present, the `--flag-unresolved' option will cause guile-test
52 ;;;; to exit with failure status if any tests are UNRESOLVED.
53 ;;;;
54 ;;;;
55 ;;;; Installation:
56 ;;;;
57 ;;;; If you change the #! line at the top of this script to point at
58 ;;;; the Guile interpreter you want to test, you can call this script
59 ;;;; as an executable instead of having to pass it as a parameter to
60 ;;;; guile via "guile -e main -s guile-test". Further, you can edit
61 ;;;; the definition of default-test-suite to point to the parent
62 ;;;; directory of the `tests' tree, which makes it unnecessary to set
63 ;;;; the environment variable `TEST_SUITE_DIR'.
64 ;;;;
65 ;;;;
66 ;;;; Shortcomings:
67 ;;;;
68 ;;;; At the moment, due to a simple-minded implementation, test files
69 ;;;; must live in the test directory, and you must specify their names
70 ;;;; relative to the top of the test directory. If you want to send
71 ;;;; me a patch that fixes this, but still leaves sane test names in
72 ;;;; the log file, that would be great. At the moment, all the tests
73 ;;;; I care about are in the test directory, though.
74 ;;;;
75 ;;;; It would be nice if you could specify the Guile interpreter you
76 ;;;; want to test on the command line. As it stands, if you want to
77 ;;;; change which Guile interpreter you're testing, you need to edit
78 ;;;; the #! line at the top of this file, which is stupid.
79
80 (define (main . args)
81 (let ((module (resolve-module '(test-suite guile-test))))
82 (apply (module-ref module 'main) args)))
83
84 (define-module (test-suite guile-test)
85 :declarative? #f
86 :use-module (test-suite lib)
87 :use-module (ice-9 getopt-long)
88 :use-module (ice-9 and-let-star)
89 :use-module (ice-9 rdelim)
90 :use-module (system vm coverage)
91 :use-module (srfi srfi-11)
92 :use-module (system vm vm)
93 #:declarative? #f
94 :use-module ((test-suite lib automake) :prefix automake/)
95 :export (call-with-temp-dir
96 data-file-name
97 exception-errno
98 main
99 test-file-name))
100
101
102 ;;; User configurable settings:
103 (define (default-test-suite)
104 (let ((argv0 (car (program-arguments))))
105 (if (string=? (basename argv0) "guile-test")
106 (dirname argv0)
107 (error "Cannot find default test suite."))))
108
109
110 ;;; Variables that will receive their actual values later.
111 (define test-suite)
112
113 (define tmp-dir #f)
114
115
116 ;;; General utilities, that probably should be in a library somewhere.
117
118 (define-syntax-rule (call-with-temp-dir template f)
119 "Call (f tempdir) with a temporary directory created by (mkdtemp
120 template) that is always removed on exit from f."
121 (let ((tmpdir (mkdtemp template)))
122 (dynamic-wind
123 (const #f)
124 (λ () (f tmpdir))
125 (λ ()
126 (unless (zero? (status:exit-val (system* "rm" "-rf" tmpdir)))
127 (error "Unable to remove temporary directory: " tmpdir))))))
128
129 (define (exception-errno ex)
130 "Return the errno value from the system-error derived &exception ex."
131 (car (list-ref (exception-args ex) 3)))
132
133 ;;; Enable debugging
134 (define (enable-debug-mode)
135 (write-line %load-path)
136 (set! %load-verbosely #t))
137
138 ;;; Traverse the directory tree at ROOT, applying F to the name of
139 ;;; each file in the tree, including ROOT itself. For a subdirectory
140 ;;; SUB, if (F SUB) is true, we recurse into SUB. Do not follow
141 ;;; symlinks.
142 (define (for-each-file f root)
143
144 ;; A "hard directory" is a path that denotes a directory and is not a
145 ;; symlink.
146 (define (file-is-hard-directory? filename)
147 (eq? (stat:type (lstat filename)) 'directory))
148
149 (let visit ((root root))
150 (let ((should-recur (f root)))
151 (if (and should-recur (file-is-hard-directory? root))
152 (let ((dir (opendir root)))
153 (let loop ()
154 (let ((entry (readdir dir)))
155 (cond
156 ((eof-object? entry) #f)
157 ((or (string=? entry ".")
158 (string=? entry "..")
159 (string=? entry "CVS")
160 (string=? entry "RCS"))
161 (loop))
162 (else
163 (visit (string-append root "/" entry))
164 (loop))))))))))
165
166
167 ;;; The test driver.
168
169
170 ;;; Localizing test files and temporary data files.
171
172 (define (data-file-name filename)
173 (in-vicinity tmp-dir filename))
174
175 (define (test-file-name test)
176 (in-vicinity test-suite test))
177
178 ;;; Return a list of all the test files in the test tree.
179 (define (enumerate-tests test-dir)
180 (let ((root-len (+ 1 (string-length test-dir)))
181 (tests '()))
182 (for-each-file (lambda (file)
183 (when (string-suffix? ".test" file)
184 (let ((short-name (substring file root-len)))
185 (unless (eqv? #\. (string-ref short-name 0))
186 (set! tests (cons short-name tests)))))
187 #t)
188 test-dir)
189
190 ;; for-each-file presents the files in whatever order it finds
191 ;; them in the directory. We sort them here, so they'll always
192 ;; appear in the same order. This makes it easier to compare test
193 ;; log files mechanically.
194 (sort tests string<?)))
195
196 (define (main args)
197 (let ((options (getopt-long args
198 `((test-suite
199 (single-char #\t)
200 (value #t))
201 (flag-unresolved
202 (single-char #\u))
203 (log-file
204 (single-char #\l)
205 (value #t))
206 (coverage
207 (single-char #\c))
208 (debug
209 (single-char #\d))
210 (trs-file
211 (value #t))))))
212 (define (opt tag default)
213 (let ((pair (assq tag options)))
214 (if pair (cdr pair) default)))
215
216 (if (opt 'debug #f)
217 (enable-debug-mode))
218
219 (set! test-suite
220 (or (opt 'test-suite #f)
221 (getenv "TEST_SUITE_DIR")
222 (default-test-suite)))
223
224 ;; directory where temporary files are created.
225 ;; when run from "make check", this must be under the build-dir,
226 ;; not the src-dir.
227 (set! tmp-dir (getcwd))
228
229 (let* ((tests
230 (let ((foo (opt '() '())))
231 (if (null? foo)
232 (enumerate-tests test-suite)
233 foo)))
234 (log-file (opt 'log-file "guile.log"))
235 (trs-file (opt 'trs-file #f)))
236
237 ;; Open the log file.
238 (let ((log-port (open-output-file log-file))
239 (trs-port (and trs-file
240 (let ((p (open-output-file trs-file)))
241 (set-port-encoding! p "UTF-8")
242 (display ":copy-in-global-log: no\n" p)
243 p))))
244
245 ;; Allow for arbitrary Unicode characters in the log file.
246 (set-port-encoding! log-port "UTF-8")
247
248 ;; Don't fail if we can't display a test name to stdout/stderr.
249 (set-port-conversion-strategy! (current-output-port) 'escape)
250 (set-port-conversion-strategy! (current-error-port) 'escape)
251
252 ;; Register some reporters.
253 (let ((global-pass #t)
254 (counter (make-count-reporter)))
255 (when trs-port
256 (register-reporter (automake/reporter trs-port)))
257 (register-reporter (car counter))
258 (register-reporter (make-log-reporter log-port))
259 (register-reporter user-reporter)
260 (register-reporter (lambda results
261 (case (car results)
262 ((unresolved)
263 (and (opt 'flag-unresolved #f)
264 (set! global-pass #f)))
265 ((fail upass error)
266 (set! global-pass #f)))))
267
268 ;; Run the tests.
269 (let ((run-tests
270 (lambda ()
271 (for-each (lambda (test)
272 (display (string-append "Running " test "\n"))
273 (when (defined? 'setlocale)
274 (setlocale LC_ALL "C"))
275 (with-test-prefix test
276 (load (test-file-name test))))
277 tests))))
278 (if (opt 'coverage #f)
279 (let-values (((coverage-data _)
280 (with-code-coverage run-tests)))
281 (let ((out (open-output-file "guile.info")))
282 (coverage-data->lcov coverage-data out)
283 (close out)))
284 (run-tests)))
285
286 ;; Display the final counts, both to the user and in the log
287 ;; file.
288 (let ((counts ((cadr counter))))
289 (unless trs-port
290 (print-counts counts))
291 (print-counts counts log-port)
292
293 (close-port log-port)
294
295 (when trs-port
296 (when global-pass (display ":recheck: no\n" trs-port))
297 (display ":test-global-result: " trs-port)
298 (display (count-summary-line counts) trs-port)
299 (newline trs-port)
300 (close-port trs-port)))
301
302 (quit global-pass))))))
303
304
305 ;;; Local Variables:
306 ;;; mode: scheme
307 ;;; End: