|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-suite test-ice-9-popen) |
|
#:use-module (test-suite lib) |
|
#:use-module (ice-9 receive) |
|
#:use-module (ice-9 rdelim)) |
|
|
|
|
|
(define mingw? |
|
(string-contains %host-type "-mingw32")) |
|
|
|
|
|
(define (read-string-to-eof port) |
|
(do ((lst '() (cons c lst)) |
|
(c (read-char port) (read-char port))) |
|
((eof-object? c) |
|
(list->string (reverse! lst))))) |
|
|
|
|
|
|
|
(define (with-epipe thunk) |
|
(dynamic-wind |
|
(lambda () |
|
(sigaction SIGPIPE SIG_IGN)) |
|
thunk |
|
restore-signals)) |
|
|
|
(define-syntax-rule (if-supported body ...) |
|
(begin body ...)) |
|
|
|
(if-supported |
|
(use-modules (ice-9 popen)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "open-input-pipe" |
|
|
|
(pass-if-exception "no args" exception:wrong-num-args |
|
(open-input-pipe)) |
|
|
|
(pass-if "port?" |
|
(port? (open-input-pipe "echo hello"))) |
|
|
|
(pass-if "echo hello" |
|
(string=? "hello\n" (read-string-to-eof (open-input-pipe "echo hello")))) |
|
|
|
|
|
(pass-if "stdin==stderr" |
|
(let ((port (open-file "/dev/null" "r+"))) |
|
(with-input-from-port port |
|
(lambda () |
|
(with-error-to-port port |
|
(lambda () |
|
(open-input-pipe "echo hello")))))) |
|
#t) |
|
|
|
|
|
(pass-if "stdout==stderr" |
|
(let ((port (open-file "/dev/null" "r+"))) |
|
(with-output-to-port port |
|
(lambda () |
|
(with-error-to-port port |
|
(lambda () |
|
(open-input-pipe "echo hello")))))) |
|
#t) |
|
|
|
(pass-if "open-input-pipe process gets (current-input-port) as stdin" |
|
(let* ((p2c (pipe)) |
|
(port (with-input-from-port (car p2c) |
|
(lambda () |
|
(open-input-pipe "read line && echo $line"))))) |
|
(display "hello\n" (cdr p2c)) |
|
(force-output (cdr p2c)) |
|
(let ((result (eq? (read port) 'hello))) |
|
(close-port (cdr p2c)) |
|
(close-pipe port) |
|
result))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "no duplicate" |
|
(when mingw? (throw 'unresolved)) |
|
(let* ((c2p (pipe)) |
|
(p2c (pipe)) |
|
(port (with-error-to-port (cdr c2p) |
|
(lambda () |
|
(with-input-from-port (car p2c) |
|
(lambda () |
|
(open-input-pipe |
|
(format #f "exec 1>~a; echo closed 1>&2; \ |
|
exec 2>~a; read REPLY" |
|
%null-device %null-device)))))))) |
|
(close-port (cdr c2p)) |
|
(let ((result (eof-object? (read-char port)))) |
|
(display "hello!\n" (cdr p2c)) |
|
(force-output (cdr p2c)) |
|
(close-pipe port) |
|
result)))) |
|
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "open-output-pipe" |
|
|
|
(pass-if-exception "no args" exception:wrong-num-args |
|
(open-output-pipe)) |
|
|
|
(pass-if "port?" |
|
(port? (open-output-pipe "exit 0"))) |
|
|
|
|
|
(pass-if "stdin==stderr" |
|
(let ((port (open-file "/dev/null" "r+"))) |
|
(with-input-from-port port |
|
(lambda () |
|
(with-error-to-port port |
|
(lambda () |
|
(open-output-pipe "exit 0")))))) |
|
#t) |
|
|
|
|
|
(pass-if "stdout==stderr" |
|
(let ((port (open-file "/dev/null" "r+"))) |
|
(with-output-to-port port |
|
(lambda () |
|
(with-error-to-port port |
|
(lambda () |
|
(open-output-pipe "exit 0")))))) |
|
#t) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "no duplicate" |
|
(let* ((c2p (pipe)) |
|
(port (with-error-to-port (cdr c2p) |
|
(lambda () |
|
(open-output-pipe |
|
(string-append "exec guile --no-auto-compile -s \"" |
|
(getenv "TEST_SUITE_DIR") |
|
"/tests/popen-child.scm\"")))))) |
|
(close-port (cdr c2p)) |
|
(with-epipe |
|
(lambda () |
|
(let ((result |
|
(and (char? (read-char (car c2p))) |
|
(catch 'system-error |
|
(lambda () |
|
(write-char #\x port) |
|
(force-output port) |
|
#f) |
|
(lambda (key name fmt args errno-list) |
|
(= (car errno-list) EPIPE)))))) |
|
|
|
|
|
(close-port (car c2p)) |
|
(close-pipe port) |
|
result)))))) |
|
|
|
|
|
(with-test-prefix "open-pipe*" |
|
|
|
(pass-if-equal "OPEN_BOTH" |
|
'(0 (good!)) |
|
|
|
|
|
|
|
(let ((pipe (open-pipe* OPEN_BOTH "guile" "-c" |
|
(object->string |
|
'(begin |
|
(setvbuf (current-output-port) 'line) |
|
(write '(hello!)) |
|
(newline) |
|
(let ((greeting (read))) |
|
(write '(good!)))))))) |
|
(setvbuf pipe 'line) |
|
(let ((return (read pipe))) |
|
(write '(hi!) pipe) |
|
(newline pipe) |
|
(let ((last (read pipe))) |
|
(list (close-pipe pipe) last)))))) |
|
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "close-pipe" |
|
|
|
(pass-if-exception "no args" exception:wrong-num-args |
|
(close-pipe)) |
|
|
|
(pass-if "exit 0" |
|
(let ((st (close-pipe (open-output-pipe "exit 0")))) |
|
(and (status:exit-val st) |
|
(= 0 (status:exit-val st))))) |
|
|
|
(pass-if "exit 1" |
|
(let ((st (close-pipe (open-output-pipe "exit 1")))) |
|
(and (status:exit-val st) |
|
(= 1 (status:exit-val st))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if-equal "open-process" |
|
'("HELLO WORLD" 0) |
|
(receive (from to pid) |
|
((@@ (ice-9 popen) open-process) OPEN_BOTH |
|
"tr" "[:lower:]" "[:upper:]") |
|
(display "hello world" to) (close to) |
|
(list (read-string from) |
|
(status:exit-val (cdr (waitpid pid)))))) |
|
|
|
(pass-if-equal "piped-process" |
|
42 |
|
(status:exit-val |
|
(cdr (waitpid ((@@ (ice-9 popen) piped-process) |
|
"./meta/guile" '("-c" "(exit 42)")))))) |
|
|
|
(pass-if-equal "piped-process: with output" |
|
'("foo bar\n" 0) |
|
(let* ((p (pipe)) |
|
(pid ((@@ (ice-9 popen) piped-process) "echo" '("foo" "bar") |
|
(cons (port->fdes (car p)) |
|
(port->fdes (cdr p)))))) |
|
(list (read-string (car p)) |
|
(status:exit-val (cdr (waitpid pid)))))) |
|
|
|
(pass-if-equal "pipeline" |
|
'("HELLO WORLD\n" (0 0)) |
|
(receive (from to pids) |
|
(pipeline '(("echo" "hello world") |
|
("tr" "[:lower:]" "[:upper:]"))) |
|
(list (read-string from) |
|
(map (compose status:exit-val cdr waitpid) pids)))) |
|
|