1.0.23.41: fix DX-COMBINATION-P
[sbcl/tcr.git] / tests / run-program.impure.lisp
blob0200e144549a9beba612daacf1e04dcae85eb83d
1 ;;;; various RUN-PROGRAM tests with side effects
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
8 ;;;; from CMU CL.
9 ;;;;
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)))
27 (unwind-protect
28 (loop for i from 0 to 255 do
29 (write-byte i out)
30 (force-output out)
31 (assert (= (read-byte in) i)))
32 (process-close process))))
34 ;;; Test driving an external program (cat) through pipes wrapped in
35 ;;; composite streams.
37 (require :sb-posix)
39 (defun make-pipe ()
40 (multiple-value-bind (in out) (sb-posix:pipe)
41 (let ((input (sb-sys:make-fd-stream in
42 :input t
43 :external-format :ascii
44 :buffering :none :name "in"))
45 (output (sb-sys:make-fd-stream out
46 :output t
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*
58 :wait nil)))
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*))))
64 (process-close cat)))
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*
73 :direction :output
74 :if-exists :supersede)
75 (write-line "bar" f))
77 (defparameter *ed*
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)
91 (when command
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)))
97 *ed*)
99 (unwind-protect
100 (with-test (:name :run-program-ed)
101 (assert-ed nil "4")
102 (assert-ed ".s/bar/baz/g" "")
103 (assert-ed "w" "4")
104 (assert-ed "q" "")
105 (process-wait *ed*)
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.
113 (let ((file))
114 (unwind-protect
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
120 :search t :wait t)))
121 (when file
122 (delete-file file))))