~peter-pearse/ubuntu/natty/guile-1.8/prop001

« back to all changes in this revision

Viewing changes to test-suite/tests/popen.test

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Schepler
  • Date: 2006-11-09 03:11:16 UTC
  • Revision ID: james.westby@ubuntu.com-20061109031116-hu0q1jxqg12y6yeg
Tags: upstream-1.8.1+1
ImportĀ upstreamĀ versionĀ 1.8.1+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
  ;; After the child closes stdout (which it indicates here by writing
 
77
  ;; "closed" to stderr), the parent should see eof.  In Guile 1.6.4 and
 
78
  ;; earlier a duplicate of stdout existed in the child, meaning eof was not
 
79
  ;; seen.
 
80
  (pass-if "no duplicate"
 
81
    (let* ((pair (pipe))
 
82
           (port (with-error-to-port (cdr pair)
 
83
                   (lambda ()
 
84
                     (open-input-pipe
 
85
                      "exec 1>/dev/null; echo closed 1>&2; exec 2>/dev/null; sleep 999")))))
 
86
      (close-port (cdr pair))   ;; write side
 
87
      (and (char? (read-char (car pair))) ;; wait for child to do its thing
 
88
           (char-ready? port)
 
89
           (eof-object? (read-char port))))))
 
90
 
 
91
;;
 
92
;; open-output-pipe
 
93
;;
 
94
 
 
95
(with-test-prefix "open-output-pipe"
 
96
  
 
97
  (pass-if-exception "no args" exception:wrong-num-args
 
98
    (open-output-pipe))
 
99
  
 
100
  (pass-if "port?"
 
101
    (port? (open-output-pipe "exit 0")))
 
102
  
 
103
  ;; exercise file descriptor setups when stdin is the same as stderr  
 
104
  (pass-if "stdin==stderr"
 
105
    (let ((port (open-file "/dev/null" "r+")))
 
106
      (with-input-from-port port
 
107
        (lambda ()
 
108
          (with-error-to-port port
 
109
            (lambda ()
 
110
              (open-output-pipe "exit 0"))))))
 
111
    #t)
 
112
  
 
113
  ;; exercise file descriptor setups when stdout is the same as stderr
 
114
  (pass-if "stdout==stderr"
 
115
    (let ((port (open-file "/dev/null" "r+")))
 
116
      (with-output-to-port port
 
117
        (lambda ()
 
118
          (with-error-to-port port
 
119
            (lambda ()
 
120
              (open-output-pipe "exit 0"))))))
 
121
    #t)
 
122
  
 
123
  ;; After the child closes stdin (which it indicates here by writing
 
124
  ;; "closed" to stderr), the parent should see a broken pipe.  We setup to
 
125
  ;; see this as EPIPE (rather than SIGPIPE).  In Guile 1.6.4 and earlier a
 
126
  ;; duplicate of stdin existed in the child, preventing the broken pipe
 
127
  ;; occurring.
 
128
  (pass-if "no duplicate"
 
129
    (with-epipe
 
130
     (lambda ()
 
131
       (let* ((pair (pipe))
 
132
              (port (with-error-to-port (cdr pair)
 
133
                      (lambda ()
 
134
                        (open-output-pipe
 
135
                         "exec 0</dev/null; echo closed 1>&2; exec 2>/dev/null; sleep 999")))))
 
136
         (close-port (cdr pair))   ;; write side
 
137
         (and (char? (read-char (car pair))) ;; wait for child to do its thing
 
138
              (catch 'system-error
 
139
                (lambda ()
 
140
                  (write-char #\x port)
 
141
                  (force-output port)
 
142
                  #f)
 
143
                (lambda (key name fmt args errno-list)
 
144
                  (= (car errno-list) EPIPE)))))))))
 
145
 
 
146
;;
 
147
;; close-pipe
 
148
;;
 
149
 
 
150
(with-test-prefix "close-pipe"
 
151
  
 
152
  (pass-if-exception "no args" exception:wrong-num-args
 
153
    (close-pipe))
 
154
  
 
155
  (pass-if "exit 0"
 
156
    (let ((st (close-pipe (open-output-pipe "exit 0"))))
 
157
      (and (status:exit-val st)
 
158
           (= 0 (status:exit-val st)))))
 
159
  
 
160
  (pass-if "exit 1"
 
161
    (let ((st (close-pipe (open-output-pipe "exit 1"))))
 
162
      (and (status:exit-val st)
 
163
           (= 1 (status:exit-val st))))))
 
164