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 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
;;; Gash -- Guile As SHell
;;; Copyright © 2018 Timothy Sample <samplet@ngyro.com>
;;;
;;; This file is part of Gash.
;;;
;;; Gash is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; Gash 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 General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Gash. If not, see <http://www.gnu.org/licenses/>.
(define-module (test-shell)
#:use-module (gash compat)
#:use-module (gash compat textual-ports)
#:use-module (gash environment)
#:use-module (gash shell)
#:use-module (ice-9 ftw)
#:use-module (ice-9 match)
#:use-module (srfi srfi-64)
#:use-module (tests unit automake)
#:use-module (tests unit config))
;;; Commentary:
;;;
;;; Tests for the shell module.
;;;
;;; Code:
(define (make-temporary-directory)
(let loop ((name (tmpnam)))
(catch 'system-error
(lambda ()
(mkdir name #o700)
name)
(lambda args
(unless (= (system-error-errno args) EEXIST)
(apply throw args))
(loop (tmpnam))))))
(define (delete-recursively path)
(define enter? (const #t))
(define (leaf path stat acc) (delete-file path) #f)
(define down (const #f))
(define (up path stat acc) (rmdir path) #f)
(define skip (const #f))
(define (error path stat errno result)
(scm-error 'system-error
"delete-recursively"
"~A" `(,strerror errno)
`(,errno)))
(file-system-fold enter? leaf down up skip error #f path))
(define (call-with-temporary-directory proc)
(let* ((directory (make-temporary-directory)))
(dynamic-wind
(lambda () #t)
(lambda ()
(proc directory))
(lambda ()
(delete-recursively directory)))))
(define (%make-script object . forms)
(define (write-script port)
(chmod port #o755)
(format port "#!~a --no-auto-compile~%!#~%~%" *guile-path*)
(for-each (lambda (form)
(write form port)
(newline port))
forms))
(match object
((? port?) (write-script object))
((? string?) (call-with-output-file object write-script))))
(define-syntax-rule (make-script path form form1 ...)
(%make-script path `form `form1 ...))
(test-begin "shell")
(test-assert "Executes a utility by absolute path"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(sentinal (string-append directory "/sentinal.txt")))
(make-script utility
(with-output-to-file ,sentinal
(lambda ()
(display "x"))))
(sh:exec utility)
(file-exists? sentinal)))))
(test-assert "Executes a utility by searching PATH"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(sentinal (string-append directory "/sentinal.txt")))
(make-script utility
(with-output-to-file ,sentinal
(lambda ()
(display "x"))))
(with-variables `(("PATH" . ,directory))
(lambda () (sh:exec "utility")))
(file-exists? sentinal)))))
(test-equal "Sets status to 127 if a utility cannot be found"
127
(call-with-temporary-directory
(lambda (directory)
(with-variables `(("PATH" . ,directory))
(lambda ()
(sh:exec "utility")
(get-status))))))
(test-equal "Executes regular built-ins"
"foo bar\n"
(with-output-to-string
(lambda ()
(sh:exec "echo" "foo" "bar"))))
;;; Redirects.
;; TODO: Tame this mess with some syntax.
(test-equal "Redirects built-in standard output to file"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt")))
(sh:with-redirects `((> 1 ,foo))
(lambda ()
(display "foo")
(newline)))
(call-with-input-file foo get-string-all)))))
(test-equal "Redirects built-in standard error to file"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt")))
(sh:with-redirects `((> 2 ,foo))
(lambda ()
(display "foo" (current-error-port))
(newline (current-error-port))))
(call-with-input-file foo get-string-all)))))
(test-equal "Redirects external standard output to file"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(foo (string-append directory "/foo.txt")))
(make-script utility
(display "foo")
(newline))
(sh:with-redirects `((> 1 ,foo))
(lambda ()
(sh:exec utility)))
(call-with-input-file foo get-string-all)))))
(test-equal "Redirects external standard error to file"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(foo (string-append directory "/foo.txt")))
(make-script utility
(display "foo" (current-error-port))
(newline (current-error-port)))
(sh:with-redirects `((> 2 ,foo))
(lambda ()
(sh:exec utility)))
(call-with-input-file foo get-string-all)))))
(test-equal "Redirects built-in standard input from file"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt"))
(output (string-append directory "/output.txt")))
(with-output-to-file foo
(lambda ()
(display "foo")
(newline)))
(sh:with-redirects `((< 0 ,foo))
(lambda ()
(with-output-to-file output
(lambda ()
(display (get-string-all (current-input-port)))))))
(call-with-input-file output get-string-all)))))
(test-equal "Redirects external standard input from file"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(foo (string-append directory "/foo.txt"))
(output (string-append directory "/output.txt")))
(with-output-to-file foo
(lambda ()
(display "foo")
(newline)))
(make-script utility
(use-modules (rnrs io ports))
(with-output-to-file ,output
(lambda ()
(display (get-string-all (current-input-port))))))
(sh:with-redirects `((< 0 ,foo))
(lambda ()
(sh:exec utility)))
(call-with-input-file output get-string-all)))))
;; These next two tests are non-deterministic, so we need to allow
;; multiple right answers. (This is preferred to using 'force-output'
;; because we want to be sure that 'sh:with-redirects' handles
;; left-over buffered output.)
(test-assert "Redirects built-in standard error to standard output"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt")))
(sh:with-redirects `((> 1 ,foo) (>& 2 1))
(lambda ()
(display "foo")
(newline)
(display "bar" (current-error-port))
(newline (current-error-port))))
(let ((result (call-with-input-file foo get-string-all)))
(or (string=? result "foo\nbar\n")
(string=? result "bar\nfoo\n")))))))
(test-assert "Redirects external standard error to standard output"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(foo (string-append directory "/foo.txt")))
(make-script utility
(display "foo")
(newline)
(display "bar" (current-error-port))
(newline (current-error-port)))
(sh:with-redirects `((> 1 ,foo) (>& 2 1))
(lambda ()
(sh:exec utility)))
(let ((result (call-with-input-file foo get-string-all)))
(or (string=? result "foo\nbar\n")
(string=? result "bar\nfoo\n")))))))
(test-equal "Appends standard output to file"
"foo\nbar\n"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt")))
(with-output-to-file foo
(lambda ()
(display "foo")
(newline)))
(sh:with-redirects `((>> 1 ,foo))
(lambda ()
(display "bar")
(newline)))
(call-with-input-file foo get-string-all)))))
(test-equal "Redirects here-document to standard input"
"foo\n"
(with-output-to-string
(lambda ()
(sh:with-redirects '((<< 0 "foo\n"))
(lambda ()
(display (get-string-all (current-input-port))))))))
(test-equal "Redirects work with string ports"
"foo\n"
(with-input-from-string "bar\n"
(lambda ()
(with-output-to-string
(lambda ()
(sh:with-redirects '((<< 0 "foo\n"))
(lambda ()
(display (get-string-all (current-input-port))))))))))
(test-equal "Does not use buffered input from current-input-port"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((bar-baz (string-append directory "/bar-baz.txt")))
(with-output-to-file bar-baz
(lambda ()
(display "bar\nbaz\n")))
(with-input-from-file bar-baz
(lambda ()
(setvbuf (current-input-port) 'block 8)
(get-line (current-input-port))
(with-output-to-string
(lambda ()
(sh:with-redirects '((<< 0 "foo\n"))
(lambda ()
(display (get-string-all (current-input-port)))))))))))))
(test-equal "Allows here-document and file redirect"
"foo\n"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt")))
(sh:with-redirects `((> 1 ,foo) (<< 0 "foo\n"))
(lambda ()
(display (get-string-all (current-input-port)))))
(call-with-input-file foo get-string-all)))))
(test-equal "Uses last here-document specified"
"foo\n"
(with-output-to-string
(lambda ()
(sh:with-redirects '((<< 0 "bar\n") (<< 0 "foo\n"))
(lambda ()
(display (get-string-all (current-input-port))))))))
;; TODO: Read-write tests, closing tests, clobbering tests.
;;; Subshells.
(test-equal "Subshells cannot change variables"
"foo"
(with-variables '(("x" . "foo"))
(lambda ()
(sh:subshell
(lambda ()
(setvar! "x" "bar")))
(getvar "x"))))
;; TODO: Test other means of manipulating the environment and exit
;; statuses.
;;; Command substitutions.
(test-equal "Substitutes output from built-in"
"foo"
(sh:substitute-command
(lambda ()
(display "foo"))))
(test-equal "Substitutes output from external utilities"
"foo"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility")))
(make-script utility
(display "foo"))
(sh:substitute-command
(lambda ()
(sh:exec utility)))))))
(test-equal "Trailing newlines are trimmed from substitutions"
"foo"
(sh:substitute-command
(lambda ()
(display "foo")
(newline))))
(test-equal "Non-trailing newlines are preserved in substitutions"
"\nfoo\nbar"
(sh:substitute-command
(lambda ()
(newline)
(display "foo")
(newline)
(display "bar"))))
(test-equal "Empty substitutions produce empty strings"
""
(sh:substitute-command noop))
;; Pipelines.
(test-equal "Built-ins are connected by pipelines"
"foo"
(call-with-temporary-directory
(lambda (directory)
(let ((foo (string-append directory "/foo.txt")))
(sh:pipeline (lambda ()
(display "foo\n"))
(lambda ()
(with-output-to-file foo
(lambda ()
(display (get-line (current-input-port)))))))
(call-with-input-file foo get-string-all)))))
(test-equal "External utilities are connected by pipelines"
"foo"
(call-with-temporary-directory
(lambda (directory)
(let ((utility1 (string-append directory "utility1"))
(utility2 (string-append directory "utility2"))
(foo (string-append directory "/foo.txt")))
(make-script utility1
(display "foo\n"))
(make-script utility2
(use-modules (rnrs io ports))
(with-output-to-file ,foo
(lambda ()
(display (get-line (current-input-port))))))
(sh:pipeline (lambda ()
(sh:exec utility1))
(lambda ()
(sh:exec utility2)))
(call-with-input-file foo get-string-all)))))
(test-equal "Externals and built-ins are connected by pipelines"
"foo"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(foo (string-append directory "/foo.txt")))
(make-script utility
(display "foo\n"))
(sh:pipeline (lambda ()
(sh:exec utility))
(lambda ()
(with-output-to-file foo
(lambda ()
(display (get-line (current-input-port)))))))
(call-with-input-file foo get-string-all)))))
(test-equal "Built-ins and externals are connected by pipelines"
"foo"
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/utility"))
(foo (string-append directory "/foo.txt")))
(make-script utility
(use-modules (rnrs io ports))
(with-output-to-file ,foo
(lambda ()
(display (get-line (current-input-port))))))
(sh:pipeline (lambda ()
(display "foo\n"))
(lambda ()
(sh:exec utility)))
(call-with-input-file foo get-string-all)))))
;;; Errexit.
(test-assert "Exit traps are still called on errexit"
(call-with-prompt default-prompt-tag
(lambda ()
(set-atexit! (lambda () (abort-to-prompt default-prompt-tag)))
(setopt! 'errexit #t)
(call-with-temporary-directory
(lambda (directory)
(let ((utility (string-append directory "/false")))
(make-script utility
(exit EXIT_FAILURE))
(sh:exec utility)))))
(lambda (k) #t)))
(test-end)
|