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

4
5
;; On Windows, normalize away CRLF into jut the unixy LF.
(defun dewindowize (x)
6
7
8
  ;; Some buggy implementations output extra space on Windows;
  ;; Implementations that rely on shell redirection on Windows also do, as a limitation.
  (string-trim " " (remove (code-char 13) x)))
9

10
;;#+(and clisp os-windows) (ext:without-package-lock () (trace ext:run-shell-command))
11
12
13
14
15
16
17
(os-cond
 ((os-unix-p)
  (DBG "Testing echo ok 1 via run-program as a list")
  (assert-equal "ok 1"
                (run-program '("echo" "ok" "1") :output '(:string :stripped t))))
 ((os-windows-p)
  (DBG "Testing ipconfig /all via run-progam as a string")
18
19
20
21
  ;; Interestingly, "ipconfig /all" fails on the msys2 bash, but not on the cygwin bash.
  (nest
   #+ecl (errors parameter-error) ;; FIXME: known breakage
   (run-program "ipconfig /all"))
22
23
  (DBG "Testing ipconfig /all via run-progam as a list")
  (run-program '("ipconfig" "/all"))))
24

25
(DBG "Testing echo ok 1 via run-program as a string")
26
#-(and ecl os-windows)
27
28
(assert-equal "ok 1"
              (dewindowize (run-program "echo ok 1" :output '(:string :stripped t))))
29
30
31
#+(and ecl os-windows)
(errors parameter-error
  (dewindowize (run-program "echo ok 1" :output '(:string :stripped t))))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
32

33
;;; test asdf run-shell-command function
34
35
#-(and ecl os-windows)
(progn
36
(setf *verbose-out* nil)
37
(DBG "Testing true via run-shell-command")
38
(assert-equal 0 (run-shell-command "exit 0"))
39
(DBG "Testing false via run-shell-command")
40
(assert-equal 1 (run-shell-command "exit 1"))
41
(DBG "Testing bad shell command via run-shell-command")
42
(unless (< 0 (run-shell-command "./bad-shell-command"))
43
44
45
46
  (error "Failed to capture exit status indicating shell command failure.")))
#+(and ecl os-windows)
(errors not-implemented-error
  (eql 0  (run-shell-command "exit 0")))
47

48
49
(chdir *test-directory*)

50
#+os-unix
51
52
(progn
  (DBG "Testing good shell command in current directory via run-shell-command")
53
  (assert-equal 0 (run-shell-command "./good-shell-command")))
54

Elias Pipping's avatar
Elias Pipping committed
55
(DBG "Testing exit status with :output :interactive")
56
57
;; This test checks for a problem there was in allegro -- :output :interactive
;; would try to open T as a stream for INPUT.
58
#-(and ecl os-windows)
59
60
61
62
63
64
65
66
67
68
69
70
(loop
   :for (program . exit-code) :in
   #+os-unix '(("true" . 0) ("false" . 1))
   #+os-windows '(("cmd /c exit 0" . 0) ("cmd /c exit 1" . 1))
   :do (loop
          :for force-shell :in '(t nil)
          :do (assert-equal `(nil nil ,exit-code)
                            (multiple-value-list
                             (run-program program
                                          :ignore-error-status t
                                          :force-shell force-shell
                                          :output :interactive)))))
71
72
73
74
#+(and ecl os-windows)
(DBG "Test skipped on ECL + Windows")

#-(and ecl os-windows)
75
76
77
78
(assert-equal
 '("foo" "bar baz" 42)
 (multiple-value-bind (o e c)
     (run-program #+os-unix "cat ; echo bar  baz >&2 ; exit 42"
Elias Pipping's avatar
Elias Pipping committed
79
80
81
82
83
84
                  #+os-windows "findstr \"^\" & echo bar baz >&2 & exit 42"
                  ;; On some versions of windows, findstr hangs indefinitely
                  ;; when passed input without a trailing newline
                  :input '("foo" :terpri t)
                  :output '(:string :stripped t) :error-output '(:string :stripped t)
                  :ignore-error-status t)
85
86
   (DBG "aaa" o e c)
   (list (dewindowize o) (dewindowize e) c)))
87
#-(and ecl os-windows)
88
89
90
91
(assert-equal
 '(("foo" "bar" "baz" "quux") nil 0)
 (multiple-value-bind (o e c)
     (run-program #+os-unix "echo foo ; echo bar >&2 ; echo baz ; echo quux >& 2"
92
93
                       #+os-windows "echo foo & echo bar >&2 & echo baz & echo quux >& 2"
                       :output :lines :error-output :output)
94
   (list (mapcar 'dewindowize o) e c)))
95

96
#-(and ecl os-windows)
97
(let ((ok1 (format nil "; $ echo ok 1~%ok 1")))
98
  (DBG "Testing awkward legacy output capture via run-shell-command")
99
  (assert-equal
100
101
102
103
104
   (dewindowize
    (stripln
     (with-output-to-string (s)
       (let ((*verbose-out* s))
         (run-shell-command "echo ~A 1" "ok")))))
105
106
   ok1)
  (assert-equal
107
108
109
110
111
112
113
114
   (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.")))))))
115
   ok1))
116

117
#-(and ecl os-windows)
118
(assert-equal '(:ok 1) (run-program "echo :ok  1" :output :forms))
119
#-(and ecl os-windows)
120
121
122
(assert-equal "ok" (dewindowize (stripln
                                 (with-output-to-string (*standard-output*)
                                   (run-program "echo ok" :output t :error-output :output)))))
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

;; 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 ()
200
  (DBG "Can we echo a simple string?")
201
202
  (assert-equal '("abcde") (run-program '("echo" "abcde") :output :lines))
  (assert-equal (nl "fghij") (run-program '("echo" "fghij") :output :string))
203

204
  (DBG "Are spaces handled properly?")
205
206
207
  (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))
208

209
  (DBG "Test that run-program fails properly with an empty program string")
210
  #+(or clozure (and allegro os-unix) cmucl (and lispworks os-unix) sbcl scl)
211
212
  (signals error (run-program '("") :output :lines))

213
  (DBG "An empty string itself is ok since it is passed to the shell.")
214
  (assert-equal "" (run-program "" :output :string))
215

216
  (DBG "Test that run-program fails properly with a nil program list")
217
  #+(or clozure (and allegro os-unix) cmucl sbcl scl)
218
219
  (signals error (run-program nil :output :lines))

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

224
225
  (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)))
226
227
228
229
230
  t)


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

231
  (DBG "Checking that spaces between arguments are collapsed by shell when and only when unquoted")
232
  (assert-equal '("ok 1") (run-program "echo ok  1" :output :lines))
233
234
  (assert-equal '("ok  1") (run-program '("echo" "ok  1") :output :lines))
  (assert-equal "ok  1" (run-program '("echo" "ok  1") :output '(:string :stripped t)))
235

236
  (DBG "Checking behavior on program success or failure")
237
  (assert-equal '(nil nil 0) (multiple-value-list (run-program "true")))
238
  (signals subprocess-error (run-program "false"))
239
  (assert-equal '(nil nil 1) (multiple-value-list (run-program "false" :ignore-error-status t)))
240

241
  (DBG "Tests with test-file")
242
243
244
  (let ((tf (native-namestring (test-source "test-file"))))

    ;; a basic smoke test
245
    (assert-equal '("Single")
246
                  (run-program `("grep" "Single" ,tf) :output :lines))
247
248

    ;; Make sure space is handled correctly
249
    (assert-equal '("double entry")
250
                  (run-program `("grep" "double entry" ,tf) :output :lines))
251
252

    ;; Make sure space is handled correctly
253
    (assert-equal '("triple word entry")
254
                  (run-program `("grep" "triple word entry" ,tf) :output :lines))
255
256
257
258

    ;; Testing special characters
    (loop :for char :across "+-_.,%@:/\\!&*(){}"
      :for str = (string char) :do
259
260
261
262
        (assert-equal (list (format nil "escape ~A" str))
                      (run-program
                       `("grep" ,(format nil "[~A]" str) ,tf)
                       :output :lines)))
263
264
265

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

    ;; Test that we can suppress the error on run-program
269
    (is (null (run-program '("false")
270
271
272
273
                            :output :lines :ignore-error-status t))))
  t)

(defun windows-only-test/run-program ()
274
  (DBG "WINDOWS-ONLY-TEST/RUN-PROGRAM")
275
  ;; a basic smoke test
276
  (assert-equal "ok" (dewindowize (run-program '("cmd" "/c" "echo" "ok") :output :line)))
277
  #-(and ecl os-windows)
278
  (assert-equal "ok 1" (dewindowize (run-program "echo ok 1" :output :line)))
279
  ;; clozure: beware http://trac.clozure.com/ccl/ticket/1118
280
281

  t)
282
283
284
285
286
287
288
289
290

(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)