6300c3b80a05a5b758e74df16992f795d157b8f8
[bpt/guile.git] / test-suite / tests / popen.test
1 ;;;; popen.test --- exercise ice-9/popen.scm -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2003, 2006, 2010, 2011 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 3 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 line && echo $line")))))
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 (string-append "guile --no-auto-compile -s \""
172 (getenv "TEST_SUITE_DIR")
173 "/tests/popen-child.scm\""))))))
174 (close-port (cdr c2p)) ;; write side
175 (with-epipe
176 (lambda ()
177 (let ((result
178 (and (char? (read-char (car c2p))) ;; wait for child to do its thing
179 (catch 'system-error
180 (lambda ()
181 (write-char #\x port)
182 (force-output port)
183 #f)
184 (lambda (key name fmt args errno-list)
185 (= (car errno-list) EPIPE))))))
186 ;; Now close our reading end of the pipe. This should give
187 ;; the child a broken pipe and so allow it to exit.
188 (close-port (car c2p))
189 (close-pipe port)
190 result)))))
191
192 )
193
194 ;;
195 ;; close-pipe
196 ;;
197
198 (with-test-prefix "close-pipe"
199
200 (pass-if-exception "no args" exception:wrong-num-args
201 (close-pipe))
202
203 (pass-if "exit 0"
204 (let ((st (close-pipe (open-output-pipe "exit 0"))))
205 (and (status:exit-val st)
206 (= 0 (status:exit-val st)))))
207
208 (pass-if "exit 1"
209 (let ((st (close-pipe (open-output-pipe "exit 1"))))
210 (and (status:exit-val st)
211 (= 1 (status:exit-val st))))))
212