~ubuntu-branches/ubuntu/trusty/cl-asdf/trusty-proposed

« back to all changes in this revision

Viewing changes to test/test-run-program.script

  • Committer: Package Import Robot
  • Author(s): Francois-Rene Rideau
  • Date: 2013-05-27 22:44:50 UTC
  • mfrom: (1.1.28)
  • Revision ID: package-import@ubuntu.com-20130527224450-4bddztgqi7q1uzn7
Tags: 2:3.0.1.2-1
ASDF 3.0.1.2 fixes issues with the debian package itself.
It also includes fixes to run-program and run-shell-command.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
;; -*- Lisp -*-
 
2
(declaim (optimize (debug 3) (safety 3)))
 
3
 
 
4
(assert-equal '("ok 1") (run-program "echo ok 1" :output :lines))
 
5
 
 
6
(unless (os-unix-p)
 
7
  (leave-test "The rest of this test is only supposed to work on Unix"))
 
8
 
 
9
;;; test asdf run-shell-command function
 
10
(setf *verbose-out* nil)
 
11
(assert-equal 1 (run-shell-command "false"))
 
12
(assert-equal 0 (run-shell-command "true"))
 
13
(unless (< 0 (run-shell-command "./bad-shell-command"))
 
14
  (error "Failed to capture exit status indicating shell command failure."))
 
15
(unless (equal 0 (run-shell-command "./good-shell-command"))
 
16
  (error "Failed to capture exit status indicating shell command failure."))
 
17
(let ((ok1 (format nil "; $ echo ok 1~%ok 1~%")))
 
18
  (assert-equal
 
19
   (with-output-to-string (s)
 
20
     (let ((*verbose-out* s))
 
21
       (run-shell-command "echo ~A 1" "ok")))
 
22
   ok1)
 
23
  #-ecl
 
24
  (assert-equal
 
25
   (with-output-to-string (s)
 
26
     (let ((*verbose-out* t)
 
27
           (*standard-output* s))
 
28
       (run-shell-command "echo ok ~D" 1)))
 
29
   ok1))
 
30
;; NB1: run-shell-command is deprecated. Use run-program instead.
 
31
;; NB2: we do NOT support stderr capture to *verbose-out* anymore in run-shell-command.
 
32
;; If you want 2>&1 redirection, you know where to find it.
 
33
(assert-equal '("ok 1") (run-program "echo ok  1" :output :lines))
 
34
(assert-equal "ok  1" (run-program '("echo" "ok  1") :output :line))
 
35
(assert-equal '(:ok 1) (run-program '("echo" ":ok  1") :output :forms))
 
36
(assert-equal (format nil "ok  1~%") (run-program '("echo" "ok  1") :output :string))
 
37
;; this test checks for a problem there was in allegro -- :output :interactive
 
38
;; would try to open T as a stream for INPUT.
 
39
(assert-equal 0 (run-program "true" :force-shell t :output :interactive))
 
40
(assert-equal 0 (run-program "true" :force-shell nil :output :interactive))
 
41
(assert-equal 1 (run-program "false" :force-shell t :output :interactive :ignore-error-status t))
 
42
(assert-equal 1 (run-program "false" :force-shell nil :output :interactive :ignore-error-status t))
 
43
 
 
44
 
 
45
;;#+allegro (trace excl:run-shell-command sys:reap-os-subprocess)
 
46
;;#+lispworks (trace system:run-shell-command system:pid-exit-status)
 
47
 
 
48
;; Poor man's test suite, lacking stefil.
 
49
(defmacro deftest (name formals &body body)
 
50
  `(defun ,name ,formals ,@body))
 
51
(defmacro is (x)
 
52
  `(progn
 
53
     (format! *error-output* "~&Checking whether ~S~%" ',x)
 
54
     (assert ,x)))
 
55
(defmacro signals (condition sexp)
 
56
  `(progn
 
57
     (format! *error-output* "~&Checking whether ~S signals ~S~%" ',sexp ',condition)
 
58
     (handler-case
 
59
         ,sexp
 
60
       (,condition () t)
 
61
       (t (c)
 
62
         (error "Expression ~S raises signal ~S, not ~S" ',sexp c ',condition))
 
63
       (:no-error ()
 
64
         (error "Expression ~S fails to raise condition ~S" ',sexp ',condition)))))
 
65
 
 
66
#|
 
67
Testing run-program
 
68
|#
 
69
 
 
70
;; We add a newline to the end of a string and return it.
 
71
;; We do it in this specific manner so that under unix, windows and macos,
 
72
;; format will choose the correct type of newline delimiters
 
73
(defun nl (str)
 
74
  (format nil "~A~%" str))
 
75
 
 
76
 
 
77
;; Convert the input format to a string stream, read it into a string,
 
78
;; and see if they match.
 
79
(defun slurp-stream-string/check (input-string &key (test #'string=))
 
80
  (let ((input-string (format nil input-string)))
 
81
    (with-open-stream (s (make-string-input-stream input-string))
 
82
      (is (funcall test input-string (slurp-stream-string s))))))
 
83
 
 
84
;; Call with a bunch of strings to call the above function upon.
 
85
(defun slurp-stream-string/checks (&rest input-string-list)
 
86
  (dolist (input-string input-string-list)
 
87
    (funcall #'slurp-stream-string/check input-string)))
 
88
 
 
89
;; Check to see if the input-string ins converted correctly to the
 
90
;; output-form
 
91
(defun slurp-stream-lines/check (input-string output-form &key (test #'equal))
 
92
  (let ((input-string (format nil input-string)))
 
93
    (with-open-stream (s (make-string-input-stream input-string))
 
94
      (is (funcall test output-form (slurp-stream-lines s))))))
 
95
 
 
96
;; Check to see if the individual input/output lists passed into this
 
97
;; function are correct.
 
98
(defun slurp-stream-lines/checks (&rest control-forms)
 
99
  (dolist (form control-forms)
 
100
    (destructuring-bind (input-string output-form) form
 
101
      (funcall #'slurp-stream-lines/check input-string output-form))))
 
102
 
 
103
(deftest test/slurp-stream-string ()
 
104
  ;; Check to make sure the string is exactly what it is when read
 
105
  ;; back through a stream. This is a format specifier so we can
 
106
  ;; portably test newline processing.
 
107
  (slurp-stream-string/checks
 
108
   ""
 
109
   " "
 
110
   "~%"
 
111
   "~%~%"
 
112
   "~%~%~%"
 
113
   "one~%two~%three~%~%four"
 
114
   "one two three four"
 
115
   "one two~%three four")
 
116
 
 
117
  ;; Check some boundary cases on the types passed.
 
118
  (signals error (slurp-stream-string nil))
 
119
  (signals error (slurp-stream-string 42))
 
120
  (signals error (slurp-stream-string "not valid"))
 
121
  t)
 
122
 
 
123
(deftest test/slurp-stream-lines ()
 
124
  (slurp-stream-lines/checks
 
125
   ;; input-string first, then expected output-form after its parsing
 
126
   '("" nil)
 
127
   '(" " (" "))
 
128
   '("~%" (""))
 
129
   '("~%~%" ("" ""))
 
130
   '("~%~%~%" ("" "" ""))
 
131
   '("foo" ("foo"))
 
132
   '("~%foo" ("" "foo"))
 
133
   '("~%foo~%" ("" "foo")) ; consumes last newline!
 
134
   '("one~%two~%~%three" ("one" "two" "" "three"))
 
135
   '("one~%two~%~%three~%" ("one" "two" "" "three"))
 
136
   '("one two three four" ("one two three four"))
 
137
   '("one two~%three four~%" ("one two" "three four")))
 
138
 
 
139
  ;; Check some boundary cases on the types passed.
 
140
  ;; NOTE: NIL is ok since it means read from stdin!
 
141
  (signals error (slurp-stream-lines 42))
 
142
  (signals error (slurp-stream-lines "not valid"))
 
143
  t)
 
144
 
 
145
(defun common-test/run-program ()
 
146
  ;; Can we echo a simple string?
 
147
  (is (equal '("abcde")
 
148
             (run-program '("echo" "abcde") :output :lines)))
 
149
  (is (equal (nl "fghij")
 
150
             (run-program '("echo" "fghij") :output :string)))
 
151
 
 
152
  ;; Are spaces handled properly?
 
153
  (is (equal '("Hello World")
 
154
             (run-program '("echo" "Hello World") :output :lines)))
 
155
  (is (equal (nl "Hello World")
 
156
             (run-program '("echo" "Hello World") :output :string)))
 
157
  (is (equal (nl "Hello World")
 
158
             (run-program "echo Hello World" :output :string)))
 
159
 
 
160
  ;; Test that run-program fails properly with an
 
161
  ;; empty program string
 
162
  #+(or clozure (and allegro os-unix) cmu (and lispworks os-unix) sbcl scl)
 
163
  (signals error (run-program '("") :output :lines))
 
164
 
 
165
  ;; An empty string itself is ok since it is passed to the shell.
 
166
  (is (equal "" (run-program "" :output :string)))
 
167
 
 
168
  ;; Test that run-program fails properly with a
 
169
  ;; nil program list
 
170
  #+(or clozure (and allegro os-unix) cmu sbcl scl)
 
171
  (signals error (run-program nil :output :lines))
 
172
 
 
173
  ;; Test that run-program fails properly when the
 
174
  ;; executable doesn't exist.
 
175
  (signals error (run-program '("does-not-exist") :output :lines))
 
176
  (signals error (run-program "does-not-exist" :output :lines))
 
177
 
 
178
  (is (equal 0 (run-program "echo ok" :output nil)))
 
179
  (is (equal 0 (run-program '("echo" "ok") :output nil)))
 
180
  t)
 
181
 
 
182
 
 
183
(defun unix-only-test/run-program ()
 
184
 
 
185
  (is (equal 0 (run-program "true")))
 
186
  (signals subprocess-error (run-program "false"))
 
187
  (is (equal 1 (run-program "false" :ignore-error-status t)))
 
188
 
 
189
  (let ((tf (native-namestring (test-source "test-file"))))
 
190
 
 
191
    ;; a basic smoke test
 
192
    (is (equal '("Single")
 
193
               (run-program `("grep" "Single" ,tf) :output :lines)))
 
194
 
 
195
    ;; Make sure space is handled correctly
 
196
    (is (equal '("double entry")
 
197
               (run-program `("grep" "double entry" ,tf) :output :lines)))
 
198
 
 
199
    ;; Make sure space is handled correctly
 
200
    (is (equal '("triple word entry")
 
201
               (run-program `("grep" "triple word entry" ,tf) :output :lines)))
 
202
 
 
203
    ;; Testing special characters
 
204
    (loop :for char :across "+-_.,%@:/\\!&*(){}"
 
205
      :for str = (string char) :do
 
206
      (is (equal (list (format nil "escape ~A" str))
 
207
                 (run-program
 
208
                  `("grep" ,(format nil "[~A]" str) ,tf)
 
209
                  :output :lines))))
 
210
 
 
211
    ;; Test that run-program signals an error
 
212
    ;; with an executable that doesn't return 0
 
213
    (signals subprocess-error (run-program '("false") :output :lines))
 
214
 
 
215
    ;; Test that we can suppress the error on run-program
 
216
    (is (null (run-program '("false")
 
217
                            :output :lines :ignore-error-status t))))
 
218
  t)
 
219
 
 
220
(defun windows-only-test/run-program ()
 
221
 
 
222
  ;; a basic smoke test
 
223
  (is (equal (run-program '("cmd" "/c" "echo" "ok") :output :lines)
 
224
             '(("ok"))))
 
225
 
 
226
  t)
 
227
 
 
228
(deftest test/run-program ()
 
229
  #+os-unix (common-test/run-program)
 
230
  #+os-unix (unix-only-test/run-program)
 
231
  #+os-windows (windows-only-test/run-program)
 
232
  (terpri)
 
233
  t)
 
234
 
 
235
 
 
236
(test/run-program)
 
237