1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
; Shellpool - Interface from Common Lisp to External Programs
; Copyright (C) 2014-2015 Kookamara LLC
;
; Contact:
;
; Kookamara LLC
; 11410 Windermere Meadows
; Austin, TX 78759, USA
; http://www.kookamara.com/
;
; License: (An MIT/X11-style license)
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@kookamara.com>
; basic.lisp -- tests of basic return/output capture
(defun run-and-gather (cmd)
"Runs command, gathering output, returns (values status stdout stderr)."
(format t "** Running command ~s~%" cmd)
(let* ((stdout nil)
(stderr nil)
(each-line (lambda (line type)
(format t " (~s) ~s~%" type line)
(force-output)
(case type
(:stdout (push line stdout))
(:stderr (push line stderr))
(otherwise (error "Bad type ~s for line ~s~%" type line)))))
(status (shellpool:run cmd :each-line each-line))
(stdout (nreverse stdout))
(stderr (nreverse stderr)))
(values status stdout stderr)))
(defun check (name expected actual)
(if (equal expected actual)
(format t "OK ~a~%" name)
(error "FAIL ~a: Expected ~s, Found ~s~%" name expected actual)))
(defun basic-test (cmd &key
(status '0)
(stdout 'nil)
(stderr 'nil))
(multiple-value-bind
(actual-status actual-stdout actual-stderr)
(run-and-gather cmd)
(check "status" status actual-status)
(check "stdout" stdout actual-stdout)
(check "stderr" stderr actual-stderr)))
; Trivial tests:
(basic-test "echo hello"
:stdout '("hello"))
(basic-test "echo hello 1>&2 "
:stderr '("hello"))
(basic-test "echo -n hello "
:stdout '("hello"))
(basic-test "echo -n hello 1>&2 "
:stderr '("hello"))
(basic-test "exit 1"
:status 1)
(basic-test "exit 15"
:status 15)
; This read test is meant to ensure that the subcommand really doesn't get any
; input stream attached to it. Perhaps we would one day want to change how
; this works to allow hooking up streams to commands, but that seems pretty
; hard and scary. What if they send Ctrl+D and end the stream or something?
; Could we lose our bash shell?
(basic-test "read -p 'Will this work?' yn"
:status 1)
; Test some more complex output interleaving.
(basic-test "./test1.sh"
:status 0
:stdout '("stdout line 1" "stdout line 2" "stdout line 3")
:stderr '("stderr line 1" "stderr line 2"))
; Like the previous test, but the output should be produced (and streamed)
; incrementally. You should notice lag as the messages are printed, but you
; should see each message as it becomes available.
(basic-test "./test2.sh"
:status 2
:stdout '("stdout line 1" "stdout line 2" "stdout line 3")
:stderr '("stderr line 1" "stderr line 2"))
; Now some tests of garbage commands.
(defun check-bad-input (cmd &key
(status '2)
(stdout 'nil))
(multiple-value-bind
(actual-status actual-stdout actual-stderr)
(run-and-gather cmd)
(check "status" status actual-status)
(check "stdout" stdout actual-stdout)
(or (consp actual-stderr)
(error "Expected an error message."))))
(check-bad-input "echo \"oops, forgot ending quote")
; Now try to make sure that didn't screw up the shell somehow.
(basic-test "echo hello"
:stdout '("hello"))
(check-bad-input "echo 'oops, forgot ending single quote")
; And try to make sure THAT didn't screw up the shell, either.
(basic-test "echo hello"
:stdout '("hello"))
(check-bad-input "(echo \"Oops, no closing paren.\" ")
; And again, try to make sure that didn't screw up the shell.
(basic-test "echo hello"
:stdout '("hello"))
;; (let* ((proc (ccl:run-program "./harness2.sh" nil
;; :wait nil
;; :input :stream
;; :output :stream
;; :error :stream))
;; (stdout (ccl::external-process-output-stream proc))
;; (stderr (ccl::external-process-output-stream proc)))
;; (declare (ignorable stdout stderr))
;; ;; (let ((foo (read-line stderr nil)))
;; ;; (format t "Got stderr line~s~%" foo)
;; ;; (force-output))
;; (loop do
;; (let ((line (read-line stdout nil)))
;; (format t "Got line ~s~%" line)
;; (force-output)
;; (when (not line)
;; (loop-finish)))))
(let ((oops nil))
(format t "** Checking that starting too many shells doesn't work.~%")
(handler-case
(progn (shellpool:start 2000)
(setq oops t))
(error (condition)
(declare (ignore condition))
(format t "OK: Got error as expected.~%")
nil))
(when oops
(error "Started 2000 shells?")))
(let ((oops nil))
(format t "** Checking that ensure'ing too many shells doesn't work.~%")
(handler-case
(progn (shellpool:ensure 2000)
(setq oops t))
(error (condition)
(declare (ignore condition))
(format t "OK: Got error as expected.~%")
nil))
(when oops
(error "Started 2000 shells?")))
|