aboutsummaryrefslogtreecommitdiffstats
path: root/tests/openpgp/run-tests.scm
blob: 90879a697d7d5ed82b2c8b9efcf5b78e7f869dd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
;; Test-suite runner.
;;
;; Copyright (C) 2016 g10 Code GmbH
;;
;; This file is part of GnuPG.
;;
;; GnuPG is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.
;;
;; GnuPG is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, see <http://www.gnu.org/licenses/>.

(if (string=? "" (getenv "srcdir"))
    (begin
      (echo "Environment variable 'srcdir' not set.  Please point it to"
	    "tests/openpgp.")
      (exit 2)))

;; Set objdir so that the tests can locate built programs.
(setenv "objdir" (getcwd) #f)

(define test-pool
  (package
   (define (new procs)
     (package
      (define (add test)
	(new (cons test procs)))
      (define (wait)
	(let ((unfinished (filter (lambda (t) (not t::retcode)) procs)))
	  (if (null? unfinished)
	      (package)
	      (let* ((commands (map (lambda (t) t::command) unfinished))
		     (pids (map (lambda (t) t::pid) unfinished))
		     (results
		      (map (lambda (pid retcode) (list pid retcode))
			   pids
			   (wait-processes (map stringify commands) pids #t))))
		(new
		 (map (lambda (t)
			(if t::retcode
			    t
			    (t::set-retcode (cadr (assoc t::pid results)))))
		      procs))))))
      (define (passed)
	(filter (lambda (p) (= 0 p::retcode)) procs))
      (define (skipped)
	(filter (lambda (p) (= 77 p::retcode)) procs))
      (define (hard-errored)
	(filter (lambda (p) (= 99 p::retcode)) procs))
      (define (failed)
	(filter (lambda (p)
		  (not (or (= 0 p::retcode) (= 77 p::retcode)
			   (= 99 p::retcode))))
		procs))
      (define (report)
	(echo (length procs) "tests run,"
	      (length (passed)) "succeeded,"
	      (length (failed)) "failed,"
	      (length (skipped)) "skipped.")
	(length (failed)))))))

(define (verbosity n)
  (if (= 0 n) '() (cons '--verbose (verbosity (- n 1)))))

(define test
  (package
   (define (scm name . args)
     (new name #f `(,*argv0* ,@(verbosity (*verbose*)) ,@args
			     ,(in-srcdir name)) #f #f CLOSED_FD))
   (define (new name directory command pid retcode logfd)
     (package
      (define (set-directory x)
	(new name x command pid retcode logfd))
      (define (set-retcode x)
	(new name directory command pid x logfd))
      (define (set-pid x)
	(new name directory command x retcode logfd))
      (define (set-logfd x)
	(new name directory command pid retcode x))
      (define (open-log-file)
	(let ((filename (string-append name ".log")))
	  (catch '() (unlink filename))
	  (open filename (logior O_RDWR O_BINARY O_CREAT) #o600)))
      (define (run-sync . args)
	(letfd ((log (open-log-file)))
	  (with-working-directory directory
	    (let* ((p (inbound-pipe))
		   (pid (spawn-process-fd (append command args) 0
					  (:write-end p) (:write-end p))))
	      (close (:write-end p))
	      (splice (:read-end p) STDERR_FILENO log)
	      (close (:read-end p))
	      (let ((t' (set-retcode (wait-process name pid #t))))
		(t'::report)
		t')))))
      (define (run-sync-quiet . args)
	(with-working-directory directory
	  (set-retcode
	   (wait-process
	    name (spawn-process-fd (append command args)
				   CLOSED_FD CLOSED_FD CLOSED_FD) #t))))
      (define (run-async . args)
	(let ((log (open-log-file)))
	  (with-working-directory directory
	    (new name directory command
		 (spawn-process-fd (append command args) CLOSED_FD log log)
		 retcode log))))
      (define (status)
	(let ((t (assoc retcode '((0 "PASS") (77 "SKIP") (99 "ERROR")))))
	  (if (not t) "FAIL" (cadr t))))
      (define (report)
	(unless (= logfd CLOSED_FD)
		(seek logfd 0 SEEK_SET)
		(splice logfd STDERR_FILENO)
		(close logfd))
	(echo (string-append (status retcode) ":") name))))))

(define (run-tests-parallel setup tests)
  (lettmp (gpghome-tar)
    (setup::run-sync '--create-tarball gpghome-tar)
    (let loop ((pool (test-pool::new '())) (tests' tests))
      (if (null? tests')
	  (let ((results (pool::wait)))
	    (for-each (lambda (t)
			(catch (echo "Removing" t::directory "failed:" *error*)
			       (unlink-recursively t::directory))
			(t::report)) (reverse results::procs))
	    (exit (results::report)))
	  (let* ((wd (mkdtemp))
		 (test (car tests'))
		 (test' (test::set-directory wd)))
	    (loop (pool::add (test'::run-async '--unpack-tarball gpghome-tar))
		  (cdr tests')))))))

(define (run-tests-sequential setup tests)
  (lettmp (gpghome-tar)
    (setup::run-sync '--create-tarball gpghome-tar)
    (let loop ((pool (test-pool::new '())) (tests' tests))
      (if (null? tests')
	  (let ((results (pool::wait)))
	    (for-each (lambda (t)
			(catch (echo "Removing" t::directory "failed:" *error*)
			       (unlink-recursively t::directory)))
		      results::procs)
	    (exit (results::report)))
	  (let* ((wd (mkdtemp))
		 (test (car tests'))
		 (test' (test::set-directory wd)))
	    (loop (pool::add (test'::run-sync '--unpack-tarball gpghome-tar))
		  (cdr tests')))))))

(let* ((runner (if (member "--parallel" *args*)
		   run-tests-parallel
		   run-tests-sequential))
       (tests (filter (lambda (arg) (not (string-prefix? arg "--"))) *args*)))
  (runner (test::scm "setup.scm") (map test::scm tests)))