| 1 |
(in-package :cl-test) |
| 2 |
|
| 3 |
(suite 'run-program) |
| 4 |
|
| 5 |
;;; I was wondering about the program which we could could use to test |
| 6 |
;;; the interface (i.e both on Linux and Windows). Easy! ECL is a |
| 7 |
;;; perfect program for that. |
| 8 |
|
| 9 |
(eval-when (:compile-toplevel :load-toplevel :execute) |
| 10 |
(defparameter *binary* (si:argv 0)) |
| 11 |
(defparameter *program* (namestring (merge-pathnames "external-process-programs.lisp" *aux-dir*)))) |
| 12 |
|
| 13 |
(defmacro with-run-program ((name args &rest params) &body body) |
| 14 |
`(multiple-value-bind (,name code process) |
| 15 |
(ext:run-program *binary* |
| 16 |
'("--norc" |
| 17 |
"--eval" ,(format nil "(setf *args-number* ~a)" (+ 13 (length args))) |
| 18 |
"--eval" "(setf *load-verbose* nil)" |
| 19 |
"--load" ,*program* |
| 20 |
"--eval" ,(format nil "(~a)" name) |
| 21 |
"--eval" "(quit)" |
| 22 |
"--" ,@args) |
| 23 |
,@params |
| 24 |
:wait nil) |
| 25 |
(declare (ignorable ,name code)) |
| 26 |
(let ((result (progn ,@body))) |
| 27 |
(cons result (multiple-value-list (ext:external-process-wait process t)))))) |
| 28 |
|
| 29 |
(defun slurp (stream) |
| 30 |
(do ((line #1=(read-line stream nil :eof) #1#) |
| 31 |
(last nil line)) |
| 32 |
((eql line :eof) last))) |
| 33 |
|
| 34 |
|
| 35 |
(test arg-test |
| 36 |
(is (equal '(nil :exited 0) |
| 37 |
(with-run-program (arg-test ("a" "b c" "d \\" "e\ 4\\ |
| 38 |
")))) "ext:run-program doesn't escape arguments properly") |
| 39 |
#+windows |
| 40 |
(is (null (equal '(nil :exited 0) |
| 41 |
(with-run-program (arg-test ("a" "b c" "d \\" "e\ 4\\ |
| 42 |
") :escape-arguments nil)))) "ext:run-program :ESCAPE-ARGUMENTS NIL doesn't work")) |
| 43 |
|
| 44 |
(test output-streams |
| 45 |
;; error is a separate stream |
| 46 |
(is-equal '(("Hello stdout" "Hello stderr") :exited 0) |
| 47 |
(with-run-program |
| 48 |
(print-test nil :output :stream :error :stream) |
| 49 |
(let ((print-test-err (ext:external-process-error-stream process))) |
| 50 |
(list (slurp print-test) (slurp print-test-err))))) |
| 51 |
;; :error :output |
| 52 |
(is-equal '(("Hello stderr" nil) :exited 0) |
| 53 |
(with-run-program |
| 54 |
(print-test nil :output :stream :error :output) |
| 55 |
(let ((print-test-err (ext:external-process-error-stream process))) |
| 56 |
;; print-test-err is drained by reading from print-test |
| 57 |
(list (slurp print-test) (slurp print-test-err)))))) |
| 58 |
|
| 59 |
(test interactive-input |
| 60 |
(is-equal '(nil :exited 0) |
| 61 |
(with-run-program (io/err nil) |
| 62 |
(format io/err "42~%"))) |
| 63 |
;; process will have :eof on input and should quit with "1" |
| 64 |
(is-equal '(nil :exited 1) (with-run-program (io/err nil :input nil)))) |
| 65 |
|
| 66 |
(test stream-values () |
| 67 |
(finishes (with-run-program (print-test nil :output nil :error nil :input nil))) |
| 68 |
(finishes (with-run-program (print-test nil :output nil :error nil :input t))) |
| 69 |
(finishes (with-run-program (print-test nil :output nil :error nil :input :stream))) |
| 70 |
(finishes (with-run-program (print-test nil :output nil :error :output :input nil))) |
| 71 |
(finishes (with-run-program (print-test nil :output nil :error :output :input :stream))) |
| 72 |
(finishes (with-run-program (print-test nil :output t :error nil :input nil))) |
| 73 |
(finishes (with-run-program (print-test nil :output t :error :output :input nil))) |
| 74 |
(finishes (with-run-program (print-test nil :output t :error :stream :input nil))) |
| 75 |
(finishes (with-run-program (print-test nil :output t :error nil :input nil))) |
| 76 |
(finishes (with-run-program (print-test nil :output t :error :output :input nil))) |
| 77 |
(finishes (with-run-program (print-test nil :output t :error :stream :input nil))) |
| 78 |
(finishes (with-run-program |
| 79 |
(print-test nil :output :stream :error :output :input :stream))) |
| 80 |
(finishes (with-run-program |
| 81 |
(print-test nil :output :stream :error :stream :input :stream))) |
| 82 |
(signals simple-error |
| 83 |
(with-run-program (print-test nil :output :bam :error :stream :input :stream)))) |
| 84 |
|
| 85 |
|
| 86 |
(test terminate-process |
| 87 |
(is-equal #-windows `(t :signaled ,ext:+sigterm+) |
| 88 |
#+windows `(t :exited -1) |
| 89 |
(with-run-program (terminate nil) |
| 90 |
(is-eql :running (ext:external-process-wait process nil)) |
| 91 |
(finishes (ext:terminate-process process)) |
| 92 |
(sleep 1) |
| 93 |
#-windows(is-eql :signaled (ext:external-process-wait process nil)) |
| 94 |
#+windows(is-eql :exited (ext:external-process-wait process nil)) |
| 95 |
t)) |
| 96 |
(is-equal #-windows `(t :signaled ,ext:+sigkill+) |
| 97 |
#+windows `(t :exited -1) |
| 98 |
(with-run-program (terminate nil) |
| 99 |
(is-eql :running (ext:external-process-wait process nil)) |
| 100 |
(finishes (ext:terminate-process process t)) |
| 101 |
(sleep 1) |
| 102 |
#-windows(is-eql :signaled (ext:external-process-wait process nil)) |
| 103 |
#+windows(is-eql :exited (ext:external-process-wait process nil)) |
| 104 |
(finishes (ext:external-process-status process)) |
| 105 |
t))) |
| 106 |
|
| 107 |
;;; We may want to craft it into an interface. Suspend/Resume *is* possible on Windows: |
| 108 |
;;; http://stackoverflow.com/questions/11010165/how-to-suspend-resume-a-process-in-windows |
| 109 |
#-(or windows cygwin) |
| 110 |
(test suspend-resume |
| 111 |
(is-equal `(t :signaled ,ext:+sigkill+) |
| 112 |
(with-run-program (heartbeat nil) |
| 113 |
(let ((pid (ext:external-process-pid process))) |
| 114 |
(is-eql :running (ext:external-process-wait process nil)) |
| 115 |
(si:killpid pid ext:+sigstop+) |
| 116 |
(sleep 2) |
| 117 |
(is-eql :stopped (ext:external-process-wait process nil)) |
| 118 |
(si:killpid pid ext:+sigcont+) |
| 119 |
(sleep 2) |
| 120 |
(is-eql :resumed (ext:external-process-wait process nil)) |
| 121 |
(finishes (ext:terminate-process process t)) |
| 122 |
t)))) |
| 123 |
|
| 124 |
;;; Cygwin programs seems not to react to signals. We use a stub to |
| 125 |
;;; avoid infintie wait for process termination. |
| 126 |
#+cygwin |
| 127 |
(test suspend-resume |
| 128 |
(is (null "killpid doesn't seem to work on cygwin."))) |
| 129 |
|
| 130 |
#+threads |
| 131 |
(test no-fd-streams |
| 132 |
(with-output-to-string (output-stream) |
| 133 |
(with-output-to-string (error-stream) |
| 134 |
;; note the space – otherwise reader waits for next character |
| 135 |
(with-input-from-string (input-stream "42 ") |
| 136 |
(with-run-program (io/err nil |
| 137 |
:input input-stream |
| 138 |
:output output-stream |
| 139 |
:error error-stream))) |
| 140 |
(is (null (zerop (length (get-output-stream-string output-stream))))) |
| 141 |
(is (null (zerop (length (get-output-stream-string error-stream))))) |
| 142 |
(mapc #'close (list output-stream error-stream))))) |
| 143 |
|
| 144 |
#+threads |
| 145 |
(test empty-string-input-stream |
| 146 |
(with-output-to-string (output-stream) |
| 147 |
(with-output-to-string (error-stream) |
| 148 |
(with-input-from-string (input-stream "") |
| 149 |
(is-equal '(nil :exited 1) |
| 150 |
(with-run-program (io/err nil |
| 151 |
:input input-stream |
| 152 |
:output output-stream |
| 153 |
:error error-stream)))) |
| 154 |
(is (null (zerop (length (get-output-stream-string output-stream))))) |
| 155 |
(is (null (zerop (length (get-output-stream-string error-stream))))) |
| 156 |
(mapc #'close (list output-stream error-stream))))) |
| 157 |
|
| 158 |
#-threads |
| 159 |
(test no-fd-streams |
| 160 |
(with-output-to-string (output-stream) |
| 161 |
(with-output-to-string (error-stream) |
| 162 |
;; note the space – otherwise reader waits for next character |
| 163 |
(with-input-from-string (input-stream "42 ") |
| 164 |
(ext:run-program *binary* `("--norc" |
| 165 |
"--load" ,*program* |
| 166 |
"--eval" ,(format nil "(~a)" 'io/err) |
| 167 |
"--eval" "(quit)") |
| 168 |
:input input-stream |
| 169 |
:output output-stream |
| 170 |
:error error-stream |
| 171 |
:wait t)) |
| 172 |
(is (null (zerop (length (get-output-stream-string output-stream))))) |
| 173 |
(is (null (zerop (length (get-output-stream-string error-stream))))) |
| 174 |
(mapc #'close (list output-stream error-stream))))) |
| 175 |
|
| 176 |
(test process-environ |
| 177 |
(flet ((run-env-program (&rest args) |
| 178 |
;; use :input nil :output nil :error nil as default to stop |
| 179 |
;; pipes from filling up and the process deadlocking |
| 180 |
(setf args (append args '(:input nil :output nil :error nil))) |
| 181 |
#-windows (apply #'ext:run-program "env" nil args) |
| 182 |
#+windows (apply #'ext:run-program "cmd" '("/k" "set" "&" "exit") args)) |
| 183 |
(run-printenv-program (var &rest args) |
| 184 |
#-windows (apply #'ext:run-program "printenv" (list var) args) |
| 185 |
#+windows (apply #'ext:run-program "cmd" |
| 186 |
(list "/k" (concatenate 'string "echo %" var "%") |
| 187 |
"&" "exit") |
| 188 |
args))) |
| 189 |
(is-equal 0 (nth-value 1 (run-env-program))) |
| 190 |
(is-equal 0 (nth-value 1 (run-env-program :environ :default))) |
| 191 |
(is-equal "bar" |
| 192 |
(delete #\Space |
| 193 |
(read-line |
| 194 |
(run-printenv-program |
| 195 |
"foo" |
| 196 |
:environ (list "foo=bar" |
| 197 |
(format nil "PATH=~A" (ext:getenv "PATH")))) |
| 198 |
nil nil))) |
| 199 |
(signals simple-error (run-env-program :environ :bam) nil nil) |
| 200 |
#-cygwin ;; Cygwin always injects `WINDIR=C:\\Windows' variable. |
| 201 |
(is (null (slurp (run-env-program :output :stream :environ nil)))))) |
| 202 |
|
| 203 |
;;; Date: 2022-10-22 |
| 204 |
;;; From: Marius Gerbershagen |
| 205 |
;;; Description: |
| 206 |
;;; |
| 207 |
;;; Check that run-program works correctly with different |
| 208 |
;;; encodings |
| 209 |
;;; |
| 210 |
#-ecl-bytecmp |
| 211 |
(test run-program-encoding |
| 212 |
(let* ((skeleton " |
| 213 |
#include <stdio.h> |
| 214 |
#include <string.h> |
| 215 |
#include <stdlib.h> |
| 216 |
|
| 217 |
#define expected_length ~S |
| 218 |
|
| 219 |
int main (int argc, char **argv) { |
| 220 |
unsigned char expected[expected_length+1] = {~{~S,~}}; |
| 221 |
if (argc != 2) { |
| 222 |
return 1; |
| 223 |
} |
| 224 |
if (strlen(argv[1]) != expected_length) { |
| 225 |
return 2; |
| 226 |
} |
| 227 |
if (strcmp(argv[1], (char*)expected) != 0) { |
| 228 |
return 3; |
| 229 |
} |
| 230 |
if (strcmp(getenv(\"ECLTESTVAR\"), (char*)expected) != 0) { |
| 231 |
return 4; |
| 232 |
} |
| 233 |
printf(\"%s\", argv[1]); |
| 234 |
return 0; |
| 235 |
}")) |
| 236 |
(flet ((test-with-encoding (encoding test-string) |
| 237 |
(let* ((ext:*default-external-format* encoding) |
| 238 |
(encoded-test-string |
| 239 |
(coerce (ext:string-to-octets test-string |
| 240 |
:null-terminate t |
| 241 |
:external-format encoding) |
| 242 |
'list))) |
| 243 |
(multiple-value-bind (return-code output) |
| 244 |
(test-C-program (format nil skeleton |
| 245 |
(1- (length encoded-test-string)) |
| 246 |
encoded-test-string) |
| 247 |
:args (list test-string) |
| 248 |
:environ (list (concatenate 'string "ECLTESTVAR=" test-string)) |
| 249 |
:capture-output :string) |
| 250 |
(is (zerop return-code)) |
| 251 |
(is (string= test-string (delete #\newline output))))))) |
| 252 |
(test-with-encoding ext:*default-external-format* "default-äöüλ🙋") |
| 253 |
#+unicode (progn (test-with-encoding :utf8 "utf8-äöüλ🙋") |
| 254 |
(test-with-encoding :latin-1 "latin-1-äöü") |
| 255 |
(test-with-encoding :greek "greek-λ"))))) |
| 256 |
|