Merge commit 'c8da65a' into new-open
[sbcl/kreuter.git] / tests / run-program.test.sh
blob03778521bce05af1be75da7c6ee2a808ed2f7b3c
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 # This should probably be broken up into separate pieces.
26 run_sbcl --eval "(defvar *exit-ok* $EXIT_LISP_WIN)" <<'EOF'
27 ;; test that $PATH is searched
28 (assert (zerop (sb-ext:process-exit-code
29 (sb-ext:run-program "true" () :search t :wait t))))
30 (assert (not (zerop (sb-ext:process-exit-code
31 (sb-ext:run-program "false" () :search t :wait t)))))
32 (let ((string (with-output-to-string (stream)
33 (sb-ext:run-program "/bin/echo"
34 '("foo" "bar")
35 :output stream))))
36 (assert (string= string "foo bar
37 ")))
38 ;; Unix environment strings are ordinarily passed with SBCL convention
39 ;; (instead of CMU CL alist-of-keywords convention).
40 (let ((string (with-output-to-string (stream)
41 (sb-ext:run-program "/usr/bin/env" ()
42 :output stream
43 :environment '("FEEFIE=foefum")))))
44 (assert (equal string "FEEFIE=foefum
45 ")))
47 ;; Unicode strings
48 (flet ((try (sb-impl::*default-external-format* x y)
49 (let* ((process (run-program
50 "/bin/sh" (list "-c" (format nil "echo ~c, $SB_TEST_FOO." x))
51 :environment (list (format nil "SB_TEST_FOO=~c" y))
52 :output :stream
53 :wait t))
54 (output (read-line (process-output process)))
55 (wanted (format nil "~c, ~c." x y)))
56 (unless (equal output wanted)
57 (error "wanted ~S, got ~S" wanted output))
58 (process-close process))))
59 (try :ascii #\s #\b)
60 (try :latin-1 (code-char 197) (code-char 229))
61 #+sb-unicode
62 (try :utf-8 #\GREEK_CAPITAL_LETTER_OMEGA #\GREEK_SMALL_LETTER_OMEGA))
64 ;; The default Unix environment for the subprocess is the same as
65 ;; for the parent process. (I.e., we behave like perl and lots of
66 ;; other programs, but not like CMU CL.)
67 (let ((string (with-output-to-string (stream)
68 (sb-ext:run-program "/usr/bin/env" ()
69 :output stream)))
70 (expected (apply #'concatenate
71 'string
72 (mapcar (lambda (environ-string)
73 (concatenate 'string
74 environ-string
75 (string #\newline)))
76 (sb-ext:posix-environ)))))
77 (assert (string= string expected)))
78 ;; That's not just because POSIX-ENVIRON is having a bad hair
79 ;; day and returning NIL, is it?
80 (assert (plusp (length (sb-ext:posix-environ))))
81 ;; make sure that a stream input argument is basically reasonable.
82 (let ((string (let ((i (make-string-input-stream "abcdef")))
83 (with-output-to-string (stream)
84 (sb-ext:run-program "/bin/cat" ()
85 :input i :output stream)))))
86 (assert (= (length string) 6))
87 (assert (string= string "abcdef")))
89 ;;; Test the bookkeeping involved in decoding the child's output:
91 ;; repeated short, properly-encoded reads exposed one bug. (But
92 ;; note: this test will be inconclusive if the child's stderr is
93 ;; fully buffered.)
94 (let ((str (with-output-to-string (s)
95 (run-program "/bin/sh"
96 '("-c" "(echo Foo; sleep 2; echo Bar)>&2")
97 :output s :search t :error :output :wait t))))
98 (assert (string= str (format nil "Foo~%Bar~%"))))
100 ;; end of file in the middle of a UTF-8 character
101 (typep (nth-value 1 (ignore-errors
102 (let ((sb-impl::*default-external-format* :utf-8))
103 (with-output-to-string (s)
104 (run-program "printf" '("\\316")
105 :output s :search t :wait t)))))
106 'error)
108 ;; success convention for this Lisp program run as part of a larger script
109 (sb-ext:quit :unix-status *exit-ok*)))
111 check_status_maybe_lose "run program tests" $?
113 exit $EXIT_TEST_WIN