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 ;; Actually there's no real side-effect here. (But see below.) The
17 ;; impurity we're avoiding is the sigchld handler that RUN-PROGRAM
18 ;; sets up, which interfers with the manual unix process control done
19 ;; by the test framework (sometimes the handler will manage to WAIT3 a
20 ;; process before run-tests WAITPIDs it).
22 (let* ((process (sb-ext:run-program "/bin/cat" '() :wait nil
23 :output :stream :input :stream))
24 (out (process-input process))
25 (in (process-output process)))
27 (loop for i from 0 to 255 do
30 (assert (= (read-byte in) i)))
31 (process-close process)))
33 ;;; Test driving an external program (ed) through pipes wrapped in
34 ;;; composite streams.
38 (defvar *tmpfile* "run-program-ed-test.tmp")
40 (with-open-file (f *tmpfile*
42 :if-exists :supersede)
46 (multiple-value-bind (in out) (sb-posix:pipe)
47 (let ((input (sb-sys:make-fd-stream in
49 :external-format :ascii
50 :buffering :none :name "in"))
51 (output (sb-sys:make-fd-stream out
53 :external-format :ascii
54 :buffering :none :name "out")))
55 (make-two-way-stream input output))))
57 (defvar *in-pipe* (make-pipe))
58 (defvar *in* (make-synonym-stream '*in-pipe*))
59 (defvar *out-pipe* (make-pipe))
60 (defvar *out* (make-synonym-stream '*out-pipe*))
63 (run-program "/bin/ed" (list *tmpfile*) :input *in* :output *out* :wait nil))
65 (defun real-input (stream)
66 (two-way-stream-input-stream (symbol-value (synonym-stream-symbol stream))))
68 (defun magic-read-line (stream)
69 ;; KLUDGE 1: The otherwise out :buffering :none is worth nothing,
70 (let ((input (real-input stream)))
71 (with-output-to-string (s)
72 ;; KLUDGE 2: Something funny going on with buffering, as plain
73 ;; READ-CHAR will hang here waiting for the newline, also
74 ;; -NO-HANG will return too early without the sleep.
76 ;; Shoot me now. --NS 2006-06-09
77 (loop for c = (progn (sleep 0.2) (read-char-no-hang input))
78 while (and c (not (eq #\newline c)))
79 do (write-char c s)))))
81 (defun assert-ed (command response)
83 (write-line command *in*)
85 (let ((got (magic-read-line *out*)))
86 (unless (equal response got)
87 (error "wanted ~S from ed, got ~S" response got)))
91 (assert-ed ".s/bar/baz/g" "")
96 (with-open-file (f *tmpfile*)
97 (assert (equal "baz" (read-line f))))