GOOPS cosmetics
[bpt/guile.git] / test-suite / tests / popen.test
1 ;;;; popen.test --- exercise ice-9/popen.scm -*- scheme -*-
2 ;;;;
3 ;;;; Copyright 2003, 2006, 2010, 2011, 2013, 2014 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
22 ;; read from PORT until eof is reached, return what's read as a string
23 (define (read-string-to-eof port)
24 (do ((lst '() (cons c lst))
25 (c (read-char port) (read-char port)))
26 ((eof-object? c)
27 (list->string (reverse! lst)))))
28
29 ;; call (THUNK), with SIGPIPE set to SIG_IGN so that an EPIPE error is
30 ;; generated rather than a SIGPIPE signal
31 (define (with-epipe thunk)
32 (dynamic-wind
33 (lambda ()
34 (sigaction SIGPIPE SIG_IGN))
35 thunk
36 restore-signals))
37
38 (define-syntax-rule (if-supported body ...)
39 (begin body ...))
40
41 (if-supported
42 (use-modules (ice-9 popen))
43
44
45 ;;
46 ;; open-input-pipe
47 ;;
48
49 (with-test-prefix "open-input-pipe"
50
51 (pass-if-exception "no args" exception:wrong-num-args
52 (open-input-pipe))
53
54 (pass-if "port?"
55 (port? (open-input-pipe "echo hello")))
56
57 (pass-if "echo hello"
58 (string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello"))))
59
60 ;; exercise file descriptor setups when stdin is the same as stderr
61 (pass-if "stdin==stderr"
62 (let ((port (open-file "/dev/null" "r+")))
63 (with-input-from-port port
64 (lambda ()
65 (with-error-to-port port
66 (lambda ()
67 (open-input-pipe "echo hello"))))))
68 #t)
69
70 ;; exercise file descriptor setups when stdout is the same as stderr
71 (pass-if "stdout==stderr"
72 (let ((port (open-file "/dev/null" "r+")))
73 (with-output-to-port port
74 (lambda ()
75 (with-error-to-port port
76 (lambda ()
77 (open-input-pipe "echo hello"))))))
78 #t)
79
80 (pass-if "open-input-pipe process gets (current-input-port) as stdin"
81 (let* ((p2c (pipe))
82 (port (with-input-from-port (car p2c)
83 (lambda ()
84 (open-input-pipe "read line && echo $line")))))
85 (display "hello\n" (cdr p2c))
86 (force-output (cdr p2c))
87 (let ((result (eq? (read port) 'hello)))
88 (close-port (cdr p2c))
89 (close-pipe port)
90 result)))
91
92 ;; After the child closes stdout (which it indicates here by writing
93 ;; "closed" to stderr), the parent should see eof. In Guile 1.6.4
94 ;; and earlier a duplicate of stdout existed in the child, meaning
95 ;; eof was not seen.
96 ;;
97 ;; Note that the objective here is to test that the parent sees EOF
98 ;; while the child is still alive. (It is obvious that the parent
99 ;; must see EOF once the child has died.) The use of the `p2c'
100 ;; pipe, and `echo closed' and `read' in the child, allows us to be
101 ;; sure that we are testing what the parent sees at a point where
102 ;; the child has closed stdout but is still alive.
103 (pass-if "no duplicate"
104 (let* ((c2p (pipe))
105 (p2c (pipe))
106 (port (with-error-to-port (cdr c2p)
107 (lambda ()
108 (with-input-from-port (car p2c)
109 (lambda ()
110 (open-input-pipe
111 (format #f "exec 1>~a; echo closed 1>&2; \
112 exec 2>~a; read REPLY"
113 %null-device %null-device))))))))
114 (close-port (cdr c2p)) ;; write side
115 (let ((result (eof-object? (read-char port))))
116 (display "hello!\n" (cdr p2c))
117 (force-output (cdr p2c))
118 (close-pipe port)
119 result))))
120
121 ;;
122 ;; open-output-pipe
123 ;;
124
125 (with-test-prefix "open-output-pipe"
126
127 (pass-if-exception "no args" exception:wrong-num-args
128 (open-output-pipe))
129
130 (pass-if "port?"
131 (port? (open-output-pipe "exit 0")))
132
133 ;; exercise file descriptor setups when stdin is the same as stderr
134 (pass-if "stdin==stderr"
135 (let ((port (open-file "/dev/null" "r+")))
136 (with-input-from-port port
137 (lambda ()
138 (with-error-to-port port
139 (lambda ()
140 (open-output-pipe "exit 0"))))))
141 #t)
142
143 ;; exercise file descriptor setups when stdout is the same as stderr
144 (pass-if "stdout==stderr"
145 (let ((port (open-file "/dev/null" "r+")))
146 (with-output-to-port port
147 (lambda ()
148 (with-error-to-port port
149 (lambda ()
150 (open-output-pipe "exit 0"))))))
151 #t)
152
153 ;; After the child closes stdin (which it indicates here by writing
154 ;; "closed" to stderr), the parent should see a broken pipe. We
155 ;; setup to see this as EPIPE (rather than SIGPIPE). In Guile 1.6.4
156 ;; and earlier a duplicate of stdin existed in the child, preventing
157 ;; the broken pipe occurring.
158 ;;
159 ;; Note that the objective here is to test that the parent sees a
160 ;; broken pipe while the child is still alive. (It is obvious that
161 ;; the parent will see a broken pipe once the child has died.) The
162 ;; use of the `c2p' pipe, and the repeated `echo closed' in the
163 ;; child, allows us to be sure that we are testing what the parent
164 ;; sees at a point where the child has closed stdin but is still
165 ;; alive.
166 ;;
167 ;; Note that `with-epipe' must apply only to the parent and not to
168 ;; the child process; we rely on the child getting SIGPIPE, to
169 ;; terminate it (and avoid leaving a zombie).
170 (pass-if "no duplicate"
171 (let* ((c2p (pipe))
172 (port (with-error-to-port (cdr c2p)
173 (lambda ()
174 (open-output-pipe
175 (string-append "exec guile --no-auto-compile -s \""
176 (getenv "TEST_SUITE_DIR")
177 "/tests/popen-child.scm\""))))))
178 (close-port (cdr c2p)) ;; write side
179 (with-epipe
180 (lambda ()
181 (let ((result
182 (and (char? (read-char (car c2p))) ;; wait for child to do its thing
183 (catch 'system-error
184 (lambda ()
185 (write-char #\x port)
186 (force-output port)
187 #f)
188 (lambda (key name fmt args errno-list)
189 (= (car errno-list) EPIPE))))))
190 ;; Now close our reading end of the pipe. This should give
191 ;; the child a broken pipe and so allow it to exit.
192 (close-port (car c2p))
193 (close-pipe port)
194 result))))))
195
196 ;;
197 ;; close-pipe
198 ;;
199
200 (with-test-prefix "close-pipe"
201
202 (pass-if-exception "no args" exception:wrong-num-args
203 (close-pipe))
204
205 (pass-if "exit 0"
206 (let ((st (close-pipe (open-output-pipe "exit 0"))))
207 (and (status:exit-val st)
208 (= 0 (status:exit-val st)))))
209
210 (pass-if "exit 1"
211 (let ((st (close-pipe (open-output-pipe "exit 1"))))
212 (and (status:exit-val st)
213 (= 1 (status:exit-val st)))))))