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 467 468 469 470 471 472
|
;;; 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-parser)
#:use-module (gash parser)
#:use-module (srfi srfi-64)
#:use-module (tests unit automake))
;;; Commentary:
;;;
;;; Tests for the parser module.
;;;
;;; Code:
(define (parse str)
(call-with-input-string str read-sh))
(test-begin "reader")
;; Commands and lists
(test-equal "Parses simple command"
'(<sh-exec> "echo" "foo")
(parse "echo foo"))
(test-equal "Parses command with keywords as arguments"
'(<sh-exec> "echo" "{" "!" "for" "}")
(parse "echo { ! for }"))
(test-equal "Parses command lists"
'(<sh-begin> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(parse "echo foo; echo bar"))
(test-equal "Parses asynchronous command lists"
'(<sh-begin> (<sh-async> (<sh-exec> "echo" "foo"))
(<sh-async> (<sh-exec> "echo" "bar")))
(parse "echo foo& echo bar&"))
(test-equal "Parses mixed command lists"
'(<sh-begin> (<sh-async> (<sh-exec> "echo" "foo"))
(<sh-exec> "echo" "bar"))
(parse "echo foo& echo bar"))
(test-equal "Parses commands with assignments"
'(<sh-exec-let> (("FOO" "bar"))
"echo" (<sh-ref> "FOO"))
(parse "FOO=bar echo $FOO"))
(test-equal "Parses commands with default redirects"
'(<sh-with-redirects> ((> 1 "bar"))
(<sh-exec> "echo" "foo"))
(parse "echo foo > bar"))
(test-equal "Parses commands with specific redirects"
'(<sh-with-redirects> ((< 5 "bar"))
(<sh-exec> "echo" "foo"))
(parse "echo foo 5< bar"))
(test-equal "Parses commands with dup redirects"
'(<sh-with-redirects> ((>& 1 "3"))
(<sh-exec> "exec"))
(parse "exec >&3"))
(test-equal "Parses commands with close redirects"
'(<sh-with-redirects> ((<& 3 "-"))
(<sh-exec> "exec"))
(parse "exec 3<&-"))
(test-equal "Parses redirects without a command"
'(<sh-with-redirects> ((>& 2 "1")) #f)
(parse "2>&1"))
(test-equal "Parses command with prefix redirect and no arguments"
'(<sh-with-redirects> ((< 0 "bar"))
(<sh-exec> "cat"))
(parse "<bar cat"))
(test-equal "Parses assignments"
'(<sh-set!> ("FOO" "bar"))
(parse "FOO=bar"))
(test-equal "Parses multiple assignments"
'(<sh-set!> ("FOO" "bar") ("BAZ" "quux"))
(parse "FOO=bar BAZ=quux"))
(test-equal "Parses redirects with assignments but no command"
'(<sh-with-redirects> ((> 2 "bar"))
(<sh-set!> ("FOO" "bar")))
(parse "FOO=bar 2>bar"))
;; Boolean expressions
(test-equal "Parses disjunctions"
'(<sh-or> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(parse "echo foo || echo bar"))
(test-equal "Parses conjunctions"
'(<sh-and> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(parse "echo foo && echo bar"))
(test-equal "Parses conjunction than disjunction"
'(<sh-or> (<sh-and> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(<sh-exec> "echo" "baz"))
(parse "echo foo && echo bar || echo baz"))
(test-equal "Parses disjunction than conjunction"
'(<sh-and> (<sh-or> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(<sh-exec> "echo" "baz"))
(parse "echo foo || echo bar && echo baz"))
(test-equal "Parses negations"
'(<sh-not> (<sh-exec> "echo" "foo"))
(parse "! echo foo"))
(test-equal "Parses negated pipelines"
'(<sh-not> (<sh-pipeline> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar")))
(parse "! echo foo | echo bar"))
;; Pipelines
(test-equal "Parses pipelines"
'(<sh-pipeline> (<sh-exec> "cat" "foo.txt")
(<sh-exec> "grep" "bar"))
(parse "cat foo.txt | grep bar"))
;; Brace groups and subshells
(test-equal "Parses brace groups"
'(<sh-begin> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(parse "{ echo foo
echo bar; }"))
(test-equal "Parses subshells"
'(<sh-subshell> (<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(parse "(echo foo; echo bar)"))
;; Here documents
(test-equal "Parses one here-document in a complete command"
'(<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(parse "cat <<eof\nfoo\neof"))
(test-equal "Parses multiple here-documents in a complete command"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-with-redirects> ((<< 0 (<sh-quote> "bar\n")))
(<sh-exec> "cat")))
(parse "cat <<eof1; cat <<eof2\nfoo\neof1\nbar\neof2"))
(test-equal "Parses one here-document in a compound list"
'(<sh-subshell>
(<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat")))
(parse "(cat <<eof\nfoo\neof\n)"))
(test-equal "Parses multiple here-documents in a compound list"
'(<sh-subshell>
(<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-with-redirects> ((<< 0 (<sh-quote> "bar\n")))
(<sh-exec> "cat")))
(parse "(cat <<eof1; cat <<eof2\nfoo\neof1\nbar\neof2\n)"))
(test-equal "Parses here-documents in both simultaneously"
'(<sh-begin> (<sh-subshell>
(<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat")))
(<sh-with-redirects> ((<< 0 (<sh-quote> "bar\n")))
(<sh-exec> "cat")))
(parse "(cat <<eof1); cat <<eof2\nfoo\neof1\nbar\neof2"))
(test-equal "Parses here-document in a conjunction"
'(<sh-and> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-exec> "echo" "bar"))
(parse "cat <<eof &&\nfoo\neof\necho bar"))
(test-equal "Parses here-document in a disjunction"
'(<sh-or> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-exec> "echo" "bar"))
(parse "cat <<eof ||\nfoo\neof\necho bar"))
(test-equal "Parses here-document in a pipeline"
'(<sh-pipeline> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-exec> "echo" "bar"))
(parse "cat <<eof |\nfoo\neof\necho bar"))
(test-equal "Parses here-document before \"in\" in a for loop"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-for> ("x" ("a"))
(<sh-exec> "echo" (<sh-ref> "x"))))
(parse "cat <<eof; for x\nfoo\neof\nin a; do echo $x; done"))
(test-equal "Parses here-document before \"do\" in a for loop"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-for> ("x" ("a"))
(<sh-exec> "echo" (<sh-ref> "x"))))
(parse "cat <<eof; for x in a\nfoo\neof\ndo echo $x; done"))
(test-equal "Parses here-document before \"in\" in a case statement"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-case> (<sh-ref> "x")
(("*") (<sh-exec> "echo" "bar"))))
(parse "cat <<eof; case $x\nfoo\neof\nin *) echo bar; esac"))
(test-equal "Parses here-document after \"in\" in a case statement"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-case> (<sh-ref> "x")
(("*") (<sh-exec> "echo" "bar"))))
(parse "cat <<eof; case $x in\nfoo\neof\n*) echo bar; esac"))
(test-equal "Parses here-document after empty pattern in a case statement"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-case> (<sh-ref> "x")
(("*") #f)))
(parse "cat <<eof; case $x in *)\nfoo\neof\nesac"))
(test-equal "Parses here-document after \";;\" in a case statement"
'(<sh-begin> (<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-case> (<sh-ref> "x")
(("*") (<sh-exec> "echo" "bar"))))
(parse "cat <<eof; case $x in *) echo bar ;;\nfoo\neof\nesac"))
(test-equal "Parses two here-documents split by two newlines"
'(<sh-subshell>
(<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(<sh-with-redirects> ((<< 0 (<sh-quote> "bar\n")))
(<sh-exec> "cat")))
(parse "(\ncat <<eof\nfoo\neof\n\ncat <<eof\nbar\neof\n)"))
(test-equal "Parses tab-trimming here-document"
'(<sh-with-redirects> ((<< 0 (<sh-quote> "foo\n")))
(<sh-exec> "cat"))
(parse "cat <<-eof\n\tfoo\n\teof"))
;; Dash treats backquoted command substitutions in here-documents as
;; being quoted. Bash does not. We follow Bash, as codified in the
;; following test.
(test-equal "Backquoted command substitutions not quoted in here-document"
(let ((cmd '(<sh-exec> "echo" ((<sh-quote> "\"") "foo" (<sh-quote> "\"")))))
`(<sh-with-redirects> ((<< 0 (<sh-quote> ((<sh-cmd-sub> ,cmd) "\n"))))
(<sh-exec> "cat")))
(parse "cat <<eof\n`echo \\\"foo\\\"`\neof"))
;; For loops
(test-equal "Parses for loops over parameters without seperator"
'(<sh-for> ("x" ((<sh-quote> (<sh-ref> "@"))))
(<sh-exec> "echo" (<sh-ref> "x")))
(parse "for x do echo $x; done"))
(test-equal "Parses for loops over parameters with seperator"
'(<sh-for> ("x" ((<sh-quote> (<sh-ref> "@"))))
(<sh-exec> "echo" (<sh-ref> "x")))
(parse "for x; do echo $x; done"))
(test-equal "Parses for loops over parameters with \"in\""
'(<sh-for> ("x" ())
(<sh-exec> "echo" (<sh-ref> "x")))
(parse "for x in; do echo $x; done"))
(test-equal "Parses for loops over word lists"
'(<sh-for> ("x" ("foo" "bar" "baz"))
(<sh-exec> "echo" (<sh-ref> "x")))
(parse "for x in foo bar baz; do echo $x; done"))
;; Case statements
(test-equal "Parses case statements with final seperator"
'(<sh-case> (<sh-ref> "foo")
(("bar") (<sh-exec> "echo" "bar")))
(parse "case $foo in bar) echo bar ;; esac"))
(test-equal "Parses case statements without final seperator"
'(<sh-case> (<sh-ref> "foo")
(("bar") (<sh-exec> "echo" "bar")))
(parse "case $foo in bar) echo bar; esac"))
(test-equal "Parses empty case statements"
'(<sh-case> (<sh-ref> "foo"))
(parse "case $foo in esac"))
(test-equal "Parses case statements with empty case item"
'(<sh-case> (<sh-ref> "foo")
(("bar") #f))
(parse "case $foo in bar) esac"))
(test-equal "Parses case statements with multiple case items"
'(<sh-case> (<sh-ref> "foo")
(("bar") (<sh-exec> "echo" "bar"))
(("baz") (<sh-exec> "echo" "baz")))
(parse "case $foo in bar) echo bar ;; baz) echo baz; esac"))
(test-equal "Parses case statements with compound patterns"
'(<sh-case> (<sh-ref> "foo")
(("bar" "baz") (<sh-exec> "echo" (<sh-quote> "bar or baz"))))
(parse "case $foo in bar | baz) echo 'bar or baz' ;; esac"))
;; If statements
(test-equal "Parses one-branch if statements"
'(<sh-cond>
((<sh-exec> "[" (<sh-ref> "foo") "=" "bar" "]")
(<sh-exec> "echo" "bar")))
(parse "if [ $foo = bar ]; then echo bar; fi"))
(test-equal "Parses two-branch if statements"
'(<sh-cond>
((<sh-exec> "[" (<sh-ref> "foo") "=" "bar" "]")
(<sh-exec> "echo" "bar"))
(<sh-else>
(<sh-exec> "echo" "baz")))
(parse "if [ $foo = bar ]; then echo bar; else echo baz; fi"))
(test-equal "Parses multi-branch if statements"
'(<sh-cond>
((<sh-exec> "[" (<sh-ref> "foo") "=" "bar" "]")
(<sh-exec> "echo" "bar"))
((<sh-exec> "[" (<sh-ref> "foo") "=" "baz" "]")
(<sh-exec> "echo" "baz"))
(<sh-else>
(<sh-exec> "echo" "quux")))
(parse "if [ $foo = bar ]
then
echo bar
elif [ $foo = baz ]
then
echo baz
else
echo quux
fi"))
;; While and until loops
(test-equal "Parses while loops"
'(<sh-while> (<sh-exec> "is-foo-time")
(<sh-exec> "foo"))
(parse "while is-foo-time; do foo; done"))
(test-equal "Parses until loops"
'(<sh-until> (<sh-exec> "is-no-longer-foo-time")
(<sh-exec> "foo"))
(parse "until is-no-longer-foo-time; do foo; done"))
;; Functions
(test-equal "Parses functions"
'(<sh-defun> "foo"
(<sh-exec> "echo" "foo"))
(parse "foo() { echo foo; }"))
;; Nested commands
(test-equal "Parses bracketed command substitions"
'(<sh-exec> "echo"
(<sh-cmd-sub> (<sh-exec> "foo"))
(<sh-cmd-sub> (<sh-exec> "bar")))
(parse "echo $(foo) $(bar)"))
(test-equal "Parses nested bracketed command substitions"
'(<sh-exec> "echo"
(<sh-cmd-sub> (<sh-exec> "foo"
(<sh-cmd-sub> (<sh-exec> "bar")))))
(parse "echo $(foo $(bar))"))
(test-equal "Parses empty bracketed command substitions"
'(<sh-exec> "echo" (<sh-cmd-sub>))
(parse "echo $()"))
(test-equal "Parses multiline bracketed command substitions"
'(<sh-exec> "echo" (<sh-cmd-sub> (<sh-exec> "foo")
(<sh-exec> "bar")))
(parse "echo $(foo
bar)"))
(test-equal "Parses backquoted command substitions"
'(<sh-exec> "echo"
(<sh-cmd-sub> (<sh-exec> "foo"))
(<sh-cmd-sub> (<sh-exec> "bar")))
(parse "echo `foo` `bar`"))
(test-equal "Parses nested backquoted command substitions"
'(<sh-exec> "echo"
(<sh-cmd-sub> (<sh-exec> "foo"
(<sh-cmd-sub> (<sh-exec> "bar")))))
(parse "echo `foo \\`bar\\``"))
(test-equal "Parses empty backquoted command substitions"
'(<sh-exec> "echo" (<sh-cmd-sub>))
(parse "echo ``"))
(test-equal "Parses multiline backquoted command substitions"
'(<sh-exec> "echo" (<sh-cmd-sub> (<sh-exec> "foo")
(<sh-exec> "bar")))
(parse "echo `foo
bar`"))
(test-equal "Parses quotes in unquoted backquoted command substitions"
'(<sh-exec> "echo" (<sh-cmd-sub>
(<sh-exec> "echo" ((<sh-quote> "\"")
"foo"
(<sh-quote> "\"")))))
(parse "echo `echo \\\"foo\\\"`"))
(test-equal "Parses quotes in quoted backquoted command substitions"
'(<sh-exec> "echo" (<sh-quote> (<sh-cmd-sub>
(<sh-exec> "echo" (<sh-quote> "foo")))))
(parse "echo \"`echo \\\"foo\\\"`\""))
;; Other tests
(test-assert "Returns EOF on EOF"
(eof-object? (parse "")))
(test-equal "Parses one statement at a time"
'((<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(call-with-input-string "echo foo
echo bar"
(lambda (port)
(list (read-sh port)
(read-sh port)))))
(test-equal "Reads all commands"
'((<sh-exec> "echo" "foo")
(<sh-exec> "echo" "bar"))
(call-with-input-string "echo foo
echo bar"
read-sh-all))
(test-equal "Reads all commands and returns a list for one command"
'((<sh-exec> "echo" "foo"))
(call-with-input-string "echo foo" read-sh-all))
(test-equal "Reads all commands and returns a list for no commands"
'()
(call-with-input-string "" read-sh-all))
(test-end)
|