run-program-test.lisp 7.78 KB
Newer Older
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
;;;; -*- mode:lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE:               run-program-test.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             Common-Lisp
;;;;USER-INTERFACE:     NONE
;;;;DESCRIPTION
;;;;    
;;;;    Tests the run-program function.
;;;;    
;;;;AUTHORS
;;;;    <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;MODIFICATIONS
;;;;    2012-03-25 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;;    AGPL3
;;;;    
;;;;    Copyright Pascal J. Bourguignon 2012 - 2012
;;;;    
;;;;    This program is free software: you can redistribute it and/or modify
;;;;    it under the terms of the GNU Affero General Public License as published by
;;;;    the Free Software Foundation, either version 3 of the License, or
;;;;    (at your option) any later version.
;;;;    
;;;;    This program is distributed in the hope that it will be useful,
;;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;;    GNU Affero General Public License for more details.
;;;;    
;;;;    You should have received a copy of the GNU Affero General Public License
;;;;    along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;;**************************************************************************

(defpackage "COM.INFORMATIMAGO.CLEXT.RUN-PROGRAM.TEST"
  (:use "COMMON-LISP"
        "COM.INFORMATIMAGO.COMMON-LISP.CESARUM.SIMPLE-TEST"
        "COM.INFORMATIMAGO.CLEXT.RUN-PROGRAM")
  (:export "TEST/RUN-PROGRAM")
  (:documentation "
"))
(in-package "COM.INFORMATIMAGO.CLEXT.RUN-PROGRAM.TEST")



(defun dump-stream (stream)
  (loop :for line = (read-line stream nil nil) :while line :do (write-line line)))

(defun dump-file (path)
 (with-open-file (inp path) (dump-stream inp)))

(defun text-file-contents (path)
  (with-open-file (inp path)
    (loop
      :for line = (read-line inp nil nil)
      :while line :collect line)))

(defun text-stream-contents (inp)
  (loop
    :for line = (read-line inp nil nil)
    :while line :collect line))


(defun create-test-input-file ()
  (with-open-file (testinp  "TESTINP.TXT"
                            :direction :output
                            :if-exists :supersede
                            :if-does-not-exist :create)
    (write-string "Hao
Wang
logicien
americain
algorithme
en
question
a
ete
publie
en
1960
dans
IBM
Journal
"
                  testinp)))


(defun check-sorted-output-stream (stream)
  (let ((stream-contents (text-stream-contents stream))
        (sorted-list (sort (copy-seq '("Hao" "Wang" "logicien" "americain"
                                       "algorithme" "en" "question" "a" "ete" "publie"
                                       "en" "1960" "dans" "IBM" "Journal"))
                           (function string<))))
94
    (check equal sorted-list stream-contents
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
           () "~%~20A=~S~%~20A=~S~%"
           "sorted-list" sorted-list
           "stream-contents" stream-contents)))


(defun check-sorted-output-file ()
  (with-open-file (stream "TESTOUT.TXT")
    (check-sorted-output-stream stream)))



(defvar *verbose* nil)


(define-test test/run-program (&key ((:verbose *verbose*) *verbose*))

  (create-test-input-file)
  
  (assert-true (zerop (process-status (run-program "true" '() :wait t))))
  (assert-true (zerop (process-status (prog1 (run-program "true" '() :wait nil)
                                        (sleep 1)))))

  (assert-true (plusp (process-status (run-program "false" '() :wait t))))
  (assert-true (plusp (process-status (prog1 (run-program "false" '() :wait nil)
                                        (sleep 1)))))

  (run-program "true" '() :wait nil :error "TESTERR.TXT")
  (sleep 1)
123
  (check equal '() (text-file-contents "TESTERR.TXT"))
124 125

  (run-program "true" '() :wait t :error "TESTERR.TXT")
126
  (check equal '() (text-file-contents "TESTERR.TXT"))
127 128 129 130 131



  (let ((process (run-program "sh" '("-c" "echo error 1>&2")
                              :wait nil :input nil :output nil :error :stream)))
132
    (check equal '("error")
133 134 135 136 137 138 139 140
          (unwind-protect
               (text-stream-contents (process-error process))
            (close (process-error process)))))

  
  (ignore-errors (delete-file "TESTERR.TXT"))
  (run-program "sh" '("-c" "echo error 1>&2")
               :wait t :input nil :output nil :error "TESTERR.TXT")
141
  (check equal '("error") (text-file-contents "TESTERR.TXT"))
142 143 144 145 146 147 148 149

  
  (with-open-file (err "TESTERR.TXT"
                       :direction :output :if-does-not-exist :create :if-exists :supersede
                       #+ccl :sharing #+ccl :lock
                       )
    (run-program "sh" '("-c" "echo error 1>&2")
                 :wait t :input nil :output nil :error err))
150
  (check equal '("error") (text-file-contents "TESTERR.TXT"))
151 152 153 154 155 156 157


  (run-program "printf" '("Hello\\nWorld\\n") :wait t)

  (run-program "printf" '("Hello\\nWorld\\n") :wait nil)

  (let ((process (run-program "printf" '("Hello\\nWorld\\n") :wait nil :output :stream)))
158
    (check equal '("Hello" "World")
159 160 161 162 163 164 165 166 167 168 169 170
          (unwind-protect
               (loop
                 :for line = (read-line (process-output process) nil nil)
                 :while line :collect line)
            (close (process-output process)))))


  (with-open-file (out  "TESTOUT.TXT"
                        :direction :output :if-does-not-exist :create :if-exists :supersede
                       #+ccl :sharing #+ccl :lock)
     (run-program "printf" '("Hello\\nWorld\\n") :wait nil :output out)
     (sleep 1))
171
  (check equal '("Hello" "World") (text-file-contents "TESTOUT.TXT"))
172 173 174 175 176 177


  (with-open-file (out  "TESTOUT.TXT"
                        :direction :output :if-does-not-exist :create :if-exists :supersede
                       #+ccl :sharing #+ccl :lock)
    (run-program "printf" '("Hello\\nWorld\\n") :wait t :output out))
178
  (check equal '("Hello" "World") (text-file-contents "TESTOUT.TXT"))
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226


  (run-program "sort" '() :environment '(("LC_CTYPE"   . "C") ("LC_COLLATE" . "C"))
               :wait nil :input "TESTINP.TXT" :output "TESTOUT.TXT")
  (sleep 1)
  (check-sorted-output-file)

  (with-open-file (out "TESTOUT.TXT"
                       :direction :output :if-does-not-exist :create :if-exists :supersede
                       #+ccl :sharing #+ccl :lock)
    (run-program "sort" '() :environment '(("LC_CTYPE"   . "C") ("LC_COLLATE" . "C"))
                 :wait t :input "TESTINP.TXT" :output out))
  (check-sorted-output-file)

  (with-open-file (inp  "TESTINP.TXT"
                       #+ccl :sharing #+ccl :lock)
    (run-program "sort" '() :environment '(("LC_CTYPE"   . "C") ("LC_COLLATE" . "C"))
                 :wait t :input inp :output "TESTOUT.TXT"))
  (check-sorted-output-file)

  (with-open-file (out "TESTOUT.TXT"
                       :direction :output :if-does-not-exist :create :if-exists :supersede
                       #+ccl :sharing #+ccl :lock)
    (with-open-file (inp  "TESTINP.TXT"
                       #+ccl :sharing #+ccl :lock)
      (run-program "sort" '() :environment '(("LC_CTYPE"   . "C") ("LC_COLLATE" . "C"))
                   :wait t :input inp :output out)))
  (check-sorted-output-file)


  (let ((process (run-program "sort" '() :environment '(("LC_CTYPE"   . "C") ("LC_COLLATE" . "C"))
                              :wait nil :input :stream :output :stream :error nil)))
    (with-open-file (testinp  "TESTINP.TXT"
                       #+ccl :sharing #+ccl :lock)
      (loop :for line = (read-line testinp nil nil)
        :while line :do (write-line line (process-input process))))
    (close (process-input process))
    (check-sorted-output-stream (process-output process))
    (close (process-output process)))

  (mapc (lambda (file) (ignore-errors (delete-file file)))
        '("TESTINP.TXT"  "TESTOUT.TXT"  "TESTERR.TXT")))


(defun test/all ()
  (test/run-program :verbose nil))

;;;; THE END ;;;;