gnulib-tool --import environ; rely on gnulib for environ definitions
[bpt/guile.git] / test-suite / tests / popen.test
1 ;;;; popen.test --- exercise ice-9/popen.scm -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2003, 2006 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-suite test-ice-9-popen)
20 #:use-module (test-suite lib)
21 #:use-module (ice-9 popen))
22
23
24 ;; read from PORT until eof is reached, return what's read as a string
25 (define (read-string-to-eof port)
26 (do ((lst '() (cons c lst))
27 (c (read-char port) (read-char port)))
28 ((eof-object? c)
29 (list->string (reverse! lst)))))
30
31 ;; call (THUNK), with SIGPIPE set to SIG_IGN so that an EPIPE error is
32 ;; generated rather than a SIGPIPE signal
33 (define (with-epipe thunk)
34 (dynamic-wind
35 (lambda ()
36 (sigaction SIGPIPE SIG_IGN))
37 thunk
38 restore-signals))
39
40
41 ;;
42 ;; open-input-pipe
43 ;;
44
45 (with-test-prefix "open-input-pipe"
46
47 (pass-if-exception "no args" exception:wrong-num-args
48 (open-input-pipe))
49
50 (pass-if "port?"
51 (port? (open-input-pipe "echo hello")))
52
53 (pass-if "echo hello"
54 (string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello"))))
55
56 ;; exercise file descriptor setups when stdin is the same as stderr
57 (pass-if "stdin==stderr"
58 (let ((port (open-file "/dev/null" "r+")))
59 (with-input-from-port port
60 (lambda ()
61 (with-error-to-port port
62 (lambda ()
63 (open-input-pipe "echo hello"))))))
64 #t)
65
66 ;; exercise file descriptor setups when stdout is the same as stderr
67 (pass-if "stdout==stderr"
68 (let ((port (open-file "/dev/null" "r+")))
69 (with-output-to-port port
70 (lambda ()
71 (with-error-to-port port
72 (lambda ()
73 (open-input-pipe "echo hello"))))))
74 #t)
75
76 (pass-if "open-input-pipe process gets (current-input-port) as stdin"
77 (let* ((p2c (pipe))
78 (port (with-input-from-port (car p2c)
79 (lambda ()
80 (open-input-pipe "read && echo $REPLY")))))
81 (display "hello\n" (cdr p2c))
82 (force-output (cdr p2c))
83 (let ((result (eq? (read port) 'hello)))
84 (close-port (cdr p2c))
85 (close-pipe port)
86 result)))
87
88 ;; After the child closes stdout (which it indicates here by writing
89 ;; "closed" to stderr), the parent should see eof. In Guile 1.6.4
90 ;; and earlier a duplicate of stdout existed in the child, meaning
91 ;; eof was not seen.
92 ;;
93 ;; Note that the objective here is to test that the parent sees EOF
94 ;; while the child is still alive. (It is obvious that the parent
95 ;; must see EOF once the child has died.) The use of the `p2c'
96 ;; pipe, and `echo closed' and `read' in the child, allows us to be
97 ;; sure that we are testing what the parent sees at a point where
98 ;; the child has closed stdout but is still alive.
99 (pass-if "no duplicate"
100 (let* ((c2p (pipe))
101 (p2c (pipe))
102 (port (with-error-to-port (cdr c2p)
103 (lambda ()
104 (with-input-from-port (car p2c)
105 (lambda ()
106 (open-input-pipe
107 "exec 1>/dev/null; echo closed 1>&2; exec 2>/dev/null; read")))))))
108 (close-port (cdr c2p)) ;; write side
109 (let ((result (eof-object? (read-char port))))
110 (display "hello!\n" (cdr p2c))
111 (force-output (cdr p2c))
112 (close-pipe port)
113 result)))
114
115 )
116
117 ;;
118 ;; open-output-pipe
119 ;;
120
121 (with-test-prefix "open-output-pipe"
122
123 (pass-if-exception "no args" exception:wrong-num-args
124 (open-output-pipe))
125
126 (pass-if "port?"
127 (port? (open-output-pipe "exit 0")))
128
129 ;; exercise file descriptor setups when stdin is the same as stderr
130 (pass-if "stdin==stderr"
131 (let ((port (open-file "/dev/null" "r+")))
132 (with-input-from-port port
133 (lambda ()
134 (with-error-to-port port
135 (lambda ()
136 (open-output-pipe "exit 0"))))))
137 #t)
138
139 ;; exercise file descriptor setups when stdout is the same as stderr
140 (pass-if "stdout==stderr"
141 (let ((port (open-file "/dev/null" "r+")))
142 (with-output-to-port port
143 (lambda ()
144 (with-error-to-port port
145 (lambda ()
146 (open-output-pipe "exit 0"))))))
147 #t)
148
149 ;; After the child closes stdin (which it indicates here by writing
150 ;; "closed" to stderr), the parent should see a broken pipe. We
151 ;; setup to see this as EPIPE (rather than SIGPIPE). In Guile 1.6.4
152 ;; and earlier a duplicate of stdin existed in the child, preventing
153 ;; the broken pipe occurring.
154 ;;
155 ;; Note that the objective here is to test that the parent sees a
156 ;; broken pipe while the child is still alive. (It is obvious that
157 ;; the parent will see a broken pipe once the child has died.) The
158 ;; use of the `c2p' pipe, and the repeated `echo closed' in the
159 ;; child, allows us to be sure that we are testing what the parent
160 ;; sees at a point where the child has closed stdin but is still
161 ;; alive.
162 ;;
163 ;; Note that `with-epipe' must apply only to the parent and not to
164 ;; the child process; we rely on the child getting SIGPIPE, to
165 ;; terminate it (and avoid leaving a zombie).
166 (pass-if "no duplicate"
167 (let* ((c2p (pipe))
168 (port (with-error-to-port (cdr c2p)
169 (lambda ()
170 (open-output-pipe
171 "exec 0</dev/null; while true; do echo closed 1>&2; done")))))
172 (close-port (cdr c2p)) ;; write side
173 (with-epipe
174 (lambda ()
175 (let ((result
176 (and (char? (read-char (car c2p))) ;; wait for child to do its thing
177 (catch 'system-error
178 (lambda ()
179 (write-char #\x port)
180 (force-output port)
181 #f)
182 (lambda (key name fmt args errno-list)
183 (= (car errno-list) EPIPE))))))
184 ;; Now close our reading end of the pipe. This should give
185 ;; the child a broken pipe and so allow it to exit.
186 (close-port (car c2p))
187 (close-pipe port)
188 result)))))
189
190 )
191
192 ;;
193 ;; close-pipe
194 ;;
195
196 (with-test-prefix "close-pipe"
197
198 (pass-if-exception "no args" exception:wrong-num-args
199 (close-pipe))
200
201 (pass-if "exit 0"
202 (let ((st (close-pipe (open-output-pipe "exit 0"))))
203 (and (status:exit-val st)
204 (= 0 (status:exit-val st)))))
205
206 (pass-if "exit 1"
207 (let ((st (close-pipe (open-output-pipe "exit 1"))))
208 (and (status:exit-val st)
209 (= 1 (status:exit-val st))))))
210