test-run-program.script 10 KB
Newer Older
1
;; -*- Lisp -*-
2
(declaim (optimize (debug 3) (safety #-gcl 3 #+gcl 0)))
3

4
5
6
7
8
#+(and clisp os-windows)
(ext:without-package-lock ()
  (trace uiop:run-program uiop/run-program::%run-program uiop/run-program::%system
         ext:shell ext:run-shell-command ext:run-program))

9
10
#+(and sbcl os-windows) (trace sb-ext:run-program)

11
12
13
14
;; On Windows, normalize away CRLF into jut the unixy LF.
(defun dewindowize (x)
  (block ()
    (setf x (remove (code-char 13) x))
15
16
    ;; Some buggy implementations output extra space on Windows;
    ;; Implementations that rely on shell redirection on Windows also do, as a limitation.
17
18
19
    (when (eql #\space (last-char x))
      (return (subseq x 0 (1- (length x)))))
    x))
20

21

22
23
24
(DBG "Testing echo ok 1 via run-program as a list")
(assert-equal "ok 1"
              (dewindowize (run-program '("echo" "ok" "1") :output '(:string :stripped t))))
25

26
27
28
29
30
31
32
33
#+(and sbcl os-windows)
(leave-test "SBCL won't let us pass unquoted arguments to cmd.exe" 0)

#-(and sbcl os-windows)
(progn
 (DBG "Testing echo ok 1 via run-program as a string")
 (assert-equal "ok 1"
               (dewindowize (run-program "echo ok 1" :output '(:string :stripped t)))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
34

35
;;; test asdf run-shell-command function
36
(setf *verbose-out* nil)
37
(DBG "Testing true via run-shell-command")
38
(assert-equal 0 (run-shell-command "true"))
39
40
41
(DBG "Testing false via run-shell-command")
(assert-equal 1 (run-shell-command "false"))
(DBG "Testing bad shell command via run-shell-command")
42
43
(unless (< 0 (run-shell-command "./bad-shell-command"))
  (error "Failed to capture exit status indicating shell command failure."))
44

45
#+os-unix
46
(progn
47
  (chdir *test-directory*)
48

49
  (DBG "Testing good shell command in current directory via run-shell-command")
50
  (assert-equal 0 (run-shell-command "./good-shell-command"))
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

  ;; this test checks for a problem there was in allegro -- :output :interactive
  ;; would try to open T as a stream for INPUT.
  (assert-equal '(nil nil 0) (multiple-value-list (run-program "true" :force-shell t :output :interactive)))
  (assert-equal '(nil nil 0) (multiple-value-list (run-program "true" :force-shell nil :output :interactive)))
  (assert-equal '(nil nil 1) (multiple-value-list (run-program "false" :force-shell t :output :interactive :ignore-error-status t)))
  (assert-equal '(nil nil 1) (multiple-value-list (run-program "false" :force-shell nil :output :interactive :ignore-error-status t)))
  (assert-equal
   '("foo" "bar baz" 42)
   (multiple-value-list
    (run-program "cat ; echo bar  baz >&2 ; exit 42"
                 :input '("foo") :output :string :error-output '(:string :stripped t)
                 :ignore-error-status t)))
  (assert-equal
   '(("foo" "bar" "baz" "quux") nil 0)
   (multiple-value-list
    (run-program "echo foo ; echo bar >&2 ; echo baz ; echo quux >& 2"
                 :output :lines :error-output :output))))

70
#-(and sbcl os-windows)
71
(let ((ok1 (format nil "; $ echo ok 1~%ok 1")))
72
  (DBG "Testing awkward legacy output capture via run-shell-command")
73
  (assert-equal
74
75
76
77
78
   (dewindowize
    (stripln
     (with-output-to-string (s)
       (let ((*verbose-out* s))
         (run-shell-command "echo ~A 1" "ok")))))
79
80
   ok1)
  (assert-equal
81
82
83
84
85
86
87
88
   (dewindowize
    (stripln
     (with-output-to-string (s)
       (let ((*verbose-out* t)
             (*standard-output* s))
         (let ((status (run-shell-command "echo ok ~D" 1)))
           (unless (zerop status)
             (error "Didn't get good exit status.")))))))
89
   ok1))
90

91
(assert-equal '(:ok 1) (run-program "echo :ok  1" :output :forms))
92
93
94
(assert-equal "ok" (dewindowize (stripln
                                 (with-output-to-string (*standard-output*)
                                   (run-program "echo ok" :output t :error-output :output)))))
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

;;#+allegro (trace excl:run-shell-command sys:reap-os-subprocess)
;;#+lispworks (trace system:run-shell-command system:pid-exit-status)

#|
Testing run-program
|#

;; We add a newline to the end of a string and return it.
;; We do it in this specific manner so that under unix, windows and macos,
;; format will choose the correct type of newline delimiters
(defun nl (str)
  (format nil "~A~%" str))


;; Convert the input format to a string stream, read it into a string,
;; and see if they match.
(defun slurp-stream-string/check (input-string &key (test #'string=))
  (let ((input-string (format nil input-string)))
    (with-open-stream (s (make-string-input-stream input-string))
      (is (funcall test input-string (slurp-stream-string s))))))

;; Call with a bunch of strings to call the above function upon.
(defun slurp-stream-string/checks (&rest input-string-list)
  (dolist (input-string input-string-list)
    (funcall #'slurp-stream-string/check input-string)))

;; Check to see if the input-string ins converted correctly to the
;; output-form
(defun slurp-stream-lines/check (input-string output-form &key (test #'equal))
  (let ((input-string (format nil input-string)))
    (with-open-stream (s (make-string-input-stream input-string))
      (is (funcall test output-form (slurp-stream-lines s))))))

;; Check to see if the individual input/output lists passed into this
;; function are correct.
(defun slurp-stream-lines/checks (&rest control-forms)
  (dolist (form control-forms)
    (destructuring-bind (input-string output-form) form
      (funcall #'slurp-stream-lines/check input-string output-form))))

(deftest test/slurp-stream-string ()
  ;; Check to make sure the string is exactly what it is when read
  ;; back through a stream. This is a format specifier so we can
  ;; portably test newline processing.
  (slurp-stream-string/checks
   ""
   " "
   "~%"
   "~%~%"
   "~%~%~%"
   "one~%two~%three~%~%four"
   "one two three four"
   "one two~%three four")

  ;; Check some boundary cases on the types passed.
  (signals error (slurp-stream-string nil))
  (signals error (slurp-stream-string 42))
  (signals error (slurp-stream-string "not valid"))
  t)

(deftest test/slurp-stream-lines ()
  (slurp-stream-lines/checks
   ;; input-string first, then expected output-form after its parsing
   '("" nil)
   '(" " (" "))
   '("~%" (""))
   '("~%~%" ("" ""))
   '("~%~%~%" ("" "" ""))
   '("foo" ("foo"))
   '("~%foo" ("" "foo"))
   '("~%foo~%" ("" "foo")) ; consumes last newline!
   '("one~%two~%~%three" ("one" "two" "" "three"))
   '("one~%two~%~%three~%" ("one" "two" "" "three"))
   '("one two three four" ("one two three four"))
   '("one two~%three four~%" ("one two" "three four")))

  ;; Check some boundary cases on the types passed.
  ;; NOTE: NIL is ok since it means read from stdin!
  (signals error (slurp-stream-lines 42))
  (signals error (slurp-stream-lines "not valid"))
  t)

(defun common-test/run-program ()
  ;; Can we echo a simple string?
180
181
  (assert-equal '("abcde") (run-program '("echo" "abcde") :output :lines))
  (assert-equal (nl "fghij") (run-program '("echo" "fghij") :output :string))
182
183

  ;; Are spaces handled properly?
184
185
186
  (assert-equal '("Hello World") (run-program '("echo" "Hello World") :output :lines))
  (assert-equal (nl "Hello World") (run-program '("echo" "Hello World") :output :string))
  (assert-equal (nl "Hello World") (run-program "echo Hello World" :output :string))
187

188
  ;; Test that run-program fails properly with an empty program string
189
190
191
192
  #+(or clozure (and allegro os-unix) cmu (and lispworks os-unix) sbcl scl)
  (signals error (run-program '("") :output :lines))

  ;; An empty string itself is ok since it is passed to the shell.
193
  (assert-equal "" (run-program "" :output :string))
194
195
196
197
198
199
200
201
202
203
204

  ;; Test that run-program fails properly with a
  ;; nil program list
  #+(or clozure (and allegro os-unix) cmu sbcl scl)
  (signals error (run-program nil :output :lines))

  ;; Test that run-program fails properly when the
  ;; executable doesn't exist.
  (signals error (run-program '("does-not-exist") :output :lines))
  (signals error (run-program "does-not-exist" :output :lines))

205
206
  (assert-equal '(nil nil 0) (multiple-value-list (run-program "echo ok" :output nil)))
  (assert-equal '(nil nil 0) (multiple-value-list (run-program '("echo" "ok") :output nil)))
207
208
209
210
211
  t)


(defun unix-only-test/run-program ()

212
  (assert-equal '("ok 1") (run-program "echo ok  1" :output :lines))
213
214
  (assert-equal '("ok  1") (run-program '("echo" "ok  1") :output :lines))
  (assert-equal "ok  1" (run-program '("echo" "ok  1") :output '(:string :stripped t)))
215

216
  (assert-equal '(nil nil 0) (multiple-value-list (run-program "true")))
217
  (signals subprocess-error (run-program "false"))
218
  (assert-equal '(nil nil 1) (multiple-value-list (run-program "false" :ignore-error-status t)))
219
220
221
222

  (let ((tf (native-namestring (test-source "test-file"))))

    ;; a basic smoke test
223
    (assert-equal '("Single")
224
                  (run-program `("grep" "Single" ,tf) :output :lines))
225
226

    ;; Make sure space is handled correctly
227
    (assert-equal '("double entry")
228
                  (run-program `("grep" "double entry" ,tf) :output :lines))
229
230

    ;; Make sure space is handled correctly
231
    (assert-equal '("triple word entry")
232
                  (run-program `("grep" "triple word entry" ,tf) :output :lines))
233
234
235
236

    ;; Testing special characters
    (loop :for char :across "+-_.,%@:/\\!&*(){}"
      :for str = (string char) :do
237
238
239
240
        (assert-equal (list (format nil "escape ~A" str))
                      (run-program
                       `("grep" ,(format nil "[~A]" str) ,tf)
                       :output :lines)))
241
242
243

    ;; Test that run-program signals an error
    ;; with an executable that doesn't return 0
244
    (signals subprocess-error (run-program '("false") :output :lines))
245
246

    ;; Test that we can suppress the error on run-program
247
    (is (null (run-program '("false")
248
249
250
251
252
253
                            :output :lines :ignore-error-status t))))
  t)

(defun windows-only-test/run-program ()

  ;; a basic smoke test
254
  (assert-equal "ok" (dewindowize (run-program '("cmd" "/c" "echo" "ok") :output :line)))
255
  (assert-equal "ok 1" (dewindowize (run-program "echo ok 1" :output :line)))
256
  ;; clozure: beware http://trac.clozure.com/ccl/ticket/1118
257
258

  t)
259
260
261
262
263
264
265
266
267
268

(deftest test/run-program ()
  #+os-unix (common-test/run-program)
  #+os-unix (unix-only-test/run-program)
  #+os-windows (windows-only-test/run-program)
  (terpri)
  t)


(test/run-program)