1 ;;;; various RUN-PROGRAM tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absolutely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
14 (cl:in-package :cl-user)
16 ;; In addition to definitions lower down the impurity we're avoiding
17 ;; is the sigchld handler that RUN-PROGRAM sets up, which interfers
18 ;; with the manual unix process control done by the test framework
19 ;; (sometimes the handler will manage to WAIT3 a process before
20 ;; run-tests WAITPIDs it).
22 (with-test (:name :run-program-cat-1)
23 (let* ((process (sb-ext:run-program "/bin/cat" '() :wait nil
24 :output :stream :input :stream))
25 (out (process-input process))
26 (in (process-output process)))
28 (loop for i from 0 to 255 do
31 (assert (= (read-byte in) i)))
32 (process-close process))))
34 ;;; Test driving an external program (cat) through pipes wrapped in
35 ;;; composite streams.
40 (multiple-value-bind (in out) (sb-posix:pipe)
41 (let ((input (sb-sys:make-fd-stream in
43 :external-format :ascii
44 :buffering :none :name "in"))
45 (output (sb-sys:make-fd-stream out
47 :external-format :ascii
48 :buffering :none :name "out")))
49 (make-two-way-stream input output))))
51 (defparameter *cat-in-pipe* (make-pipe))
52 (defparameter *cat-in* (make-synonym-stream '*cat-in-pipe*))
53 (defparameter *cat-out-pipe* (make-pipe))
54 (defparameter *cat-out* (make-synonym-stream '*cat-out-pipe*))
56 (with-test (:name :run-program-cat-2)
57 (let ((cat (run-program "/bin/cat" nil :input *cat-in* :output *cat-out*
59 (dolist (test '("This is a test!"
60 "This is another test!"
61 "This is the last test...."))
62 (write-line test *cat-in*)
63 (assert (equal test (read-line *cat-out*))))
66 ;;; The above test used to use ed, but there were buffering issues: on some platforms
67 ;;; buffering of stdin and stdout depends on their TTYness, and ed isn't sufficiently
68 ;;; agressive about flushing them. So, here's another test using :PTY.
70 (defparameter *tmpfile* "run-program-ed-test.tmp")
72 (with-open-file (f *tmpfile*
74 :if-exists :supersede)
78 (run-program "/bin/ed" (list *tmpfile*) :wait nil :pty t))
80 (defparameter *ed-pipe* (make-two-way-stream (process-pty *ed*) (process-pty *ed*)))
81 (defparameter *ed-in* (make-synonym-stream '*ed-pipe*))
82 (defparameter *ed-out* (make-synonym-stream '*ed-pipe*))
84 (defun read-linish (stream)
85 (with-output-to-string (s)
86 (loop for c = (read-char stream)
87 while (and c (not (eq #\newline c)) (not (eq #\return c)))
88 do (write-char c s))))
90 (defun assert-ed (command response)
92 (write-line command *ed-in*)
93 (force-output *ed-in*))
94 (let ((got (read-linish *ed-out*)))
95 (unless (equal response got)
96 (error "wanted ~S from ed, got ~S" response got)))
100 (with-test (:name :run-program-ed)
102 (assert-ed ".s/bar/baz/g" "")
106 (with-open-file (f *tmpfile*)
107 (assert (equal "baz" (read-line f)))))
108 (delete-file *tmpfile*))
110 ;; Around 1.0.12 there was a regression when :INPUT or :OUTPUT was a
111 ;; pathname designator. Since these use the same code, it should
112 ;; suffice to test just :INPUT.
115 (progn (with-open-file (f "run-program-test.tmp" :direction :output)
116 (setf file (truename f))
117 (write-line "Foo" f))
118 (assert (run-program "cat" ()
119 :input file :output t
122 (delete-file file))))
124 ;;; This used to crash on Darwin and trigger recursive lock errors on
126 (with-test (:name (:run-program :stress))
127 ;; Do it a hundred times in batches of 10 so that with a low limit
128 ;; of the number of processes the test can have a chance to pass.
132 #'sb-ext:process-wait
135 (sb-ext:run-program "/bin/echo" '
136 ("It would be nice if this didn't crash.")
137 :wait nil :output nil)))))