More incorrect next_free_page usage.
[sbcl.git] / tests / run-program.test.sh
blobdf49a2843cd064a08ebc6a8c88048cad209f7ee0
1 #!/bin/sh
3 # tests related to SB-EXT:RUN-PROGRAM
5 # This software is part of the SBCL system. See the README file for
6 # more information.
8 # While most of SBCL is derived from the CMU CL system, the test
9 # files (like this one) were written from scratch after the fork
10 # from CMU CL.
12 # This software is in the public domain and is provided with
13 # absolutely no warranty. See the COPYING and CREDITS files for
14 # more information.
16 . ./subr.sh
18 # Make sure that there's at least something in the environment (for
19 # one of the tests below).
20 SOMETHING_IN_THE_ENVIRONMENT='yes there is'
21 export SOMETHING_IN_THE_ENVIRONMENT
22 PATH=/some/path/that/does/not/exist:${PATH}
23 export PATH
25 # Increase potential soft ulimit on file descriptors for file
26 # descriptor test case below.
27 test `ulimit -n` -ge 1050 || ulimit -S -n `ulimit -H -n`
29 # This should probably be broken up into separate pieces.
30 run_sbcl --eval "(defvar *exit-ok* $EXIT_LISP_WIN)" <<'EOF'
31 (defmacro our-run-program (name &rest rest)
32 #+unix `(run-program ,name ,@rest)
33 #-unix `(run-program ,(subseq name (1+ (position #\/ name :from-end t)))
34 ,@rest :search t))
36 ;; test that $PATH is searched
37 (assert (zerop (sb-ext:process-exit-code
38 (sb-ext:run-program "true" () :search t :wait t))))
39 (assert (not (zerop (sb-ext:process-exit-code
40 (sb-ext:run-program "false" () :search t :wait t)))))
41 (let ((string (with-output-to-string (stream)
42 (run-program "echo" '("foo" "bar")
43 :search t :output stream))))
44 (assert (string= string "foo bar
45 ")))
46 (format t ";;; Smoke tests: PASS~%")
48 ;; Unix environment strings are ordinarily passed with SBCL convention
49 ;; (instead of CMU CL alist-of-keywords convention).
50 #+unix ; env works differently for msys2 apparently
51 (let ((string (with-output-to-string (stream)
52 (sb-ext:run-program "/usr/bin/env" ()
53 :output stream
54 :environment '("FEEFIE=foefum")))))
55 (assert (equal string "FEEFIE=foefum
56 ")))
58 ;;; Try to obtain file descriptors numerically greater than FD_SETSIZE
59 ;;; (which is usually 1024) to show that run-program uses poll() rather
60 ;;; than select(), but if we can't do that, then don't.
61 (when (fboundp (find-symbol "UNIX-POLL" "SB-UNIX"))
62 (let ((f (open "/dev/null"))
63 (got-error)
64 (opened))
65 (with-alien ((dup (function int int) :extern))
66 (dotimes (i 1025)
67 (let ((new (alien-funcall dup (sb-impl::fd-stream-fd f))))
68 (when (< new 0)
69 ;; We've no constant for EMFILE, just assume that's the problem
70 (return (setq got-error t)))
71 (push new opened))))
72 (if got-error ; close a bunch
73 (dotimes (i 6) (sb-unix:unix-close (pop opened)))
74 (assert (> (car opened) 1024)))))
76 ;; Unicode strings
77 #+unix
78 (flet ((try (sb-impl::*default-external-format* x y)
79 (let* ((process (run-program
80 "/bin/sh" (list "-c" (format nil "echo ~c, $SB_TEST_FOO." x))
81 :environment (list (format nil "SB_TEST_FOO=~c" y))
82 :output :stream
83 :wait t))
84 (output (read-line (process-output process)))
85 (wanted (format nil "~c, ~c." x y)))
86 (unless (equal output wanted)
87 (error "wanted ~S, got ~S" wanted output))
88 (process-close process))))
89 (try :ascii #\s #\b)
90 (try :latin-1 (code-char 197) (code-char 229))
91 #+sb-unicode
92 (try :utf-8 #\GREEK_CAPITAL_LETTER_OMEGA #\GREEK_SMALL_LETTER_OMEGA))
94 ;; The default Unix environment for the subprocess is the same as
95 ;; for the parent process. (I.e., we behave like perl and lots of
96 ;; other programs, but not like CMU CL.)
97 #+unix
98 (let* ((sb-impl::*default-external-format* :latin-1)
99 (sb-alien::*default-c-string-external-format* :latin-1)
100 (string (with-output-to-string (stream)
101 (sb-ext:run-program "/usr/bin/env" ()
102 :output stream)))
103 (expected (apply #'concatenate
104 'string
105 (mapcar (lambda (environ-string)
106 (concatenate 'string
107 environ-string
108 (string #\newline)))
109 (sb-ext:posix-environ)))))
110 (assert (string= string expected))
111 ;; That's not just because POSIX-ENVIRON is having a bad hair
112 ;; day and returning NIL, is it?
113 (assert (plusp (length (sb-ext:posix-environ)))))
114 ;; make sure that a stream input argument is basically reasonable.
115 (let ((string (let ((i (make-string-input-stream "abcdef")))
116 (with-output-to-string (stream)
117 (run-program "cat" ()
118 :search t :input i :output stream)))))
119 (assert (= (length string) 6))
120 (assert (string= string "abcdef")))
122 ;;; Test the bookkeeping involved in decoding the child's output:
124 ;; repeated short, properly-encoded reads exposed one bug. (But
125 ;; note: this test will be inconclusive if the child's stderr is
126 ;; fully buffered.)
127 (let ((str (with-output-to-string (s)
128 (our-run-program "/bin/sh"
129 '("-c" "(echo Foo; sleep 2; echo Bar)>&2")
130 :output s :search t :error :output :wait t))))
131 (assert (string= str (format nil "Foo~%Bar~%"))))
133 ;; end of file in the middle of a UTF-8 character
134 ;; (FIXME: asserting failure without knowing why is almost as good as no test at all.)
135 (typep (nth-value 1 (ignore-errors
136 (let ((sb-impl::*default-external-format* :utf-8))
137 (with-output-to-string (s)
138 (run-program "printf" '("\\316")
139 :output s :search t :wait t)))))
140 'error)
142 ;; success convention for this Lisp program run as part of a larger script
143 (sb-ext:quit :unix-status *exit-ok*)))
145 check_status_maybe_lose "run program tests" $?
147 exit $EXIT_TEST_WIN