bjosh/tests/run.bjo

98 lines
4.4 KiB
Text
Raw Normal View History

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; bjosh -c LINE, bjosh FILE and bjosh < lines: what lines write, and the
;; exit code.
;;
;; bjo build && bjo test tests/run.bjo
(import (std prelude))
(import (std simpletest))
(import (std run))
(import/extern
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
(process-id (: System.Environment.ProcessId int #:get #:no-raise)))
(def shell "src/main.exe")
(def scratch (path-combine (temp-dir) (str "bjosh-run-" (int->string process-id))))
;; Runs `form` with stderr into a file. Returns "out", then "err: " and
;; the error text when there is one, then "code: n" when it is not 0.
(: (-> Form string))
(defun (outcome form)
(def err-path (path-combine scratch "stderr"))
(match (run/output (FErrInto err-path form))
((Ok (Tuple code out))
(def err (match (file-read-text err-path) ((Ok t) t) ((Err _) "")))
(string-trim-right
(str out
(if (string-empty? err) "" (str "err: " err))
(if (= code 0) "" (str "code: " (int->string code))))))
((Err e) (str "did not run: " (run-error-message e)))))
(: (-> string string))
(defun (c line) (outcome (FArgv ["dotnet" shell "-c" line])))
(defun (main args)
(unless (file-exists? shell)
(println "FAILURE: src/main.exe is missing: run bjo build first"))
(ignore (directory-create scratch))
(def file (path-combine scratch "f.txt"))
;; Pipes, lists and status.
(expect "a pipe" (c "echo a b | tr a-z A-Z") "A B")
(expect "&& and ||" (c "false && echo no || echo yes") "yes")
(expect "; runs both" (c "echo a; echo b") "a\nb")
(expect "$? after a failure" (c "false; echo $?") "1")
(expect "without job control, a pipe's status is its last command's" (c "false | true; echo $?") "0")
(expect "the status of the last line is the exit code" (c "false") "code: 1")
(expect "exit" (c "exit 3; echo no") "code: 3")
;; Words and variables.
(expect "quotes" (c "echo 'a b' \"c d\" e\\ f") "a b c d e f")
(expect "a variable" (c "X=hi; echo $X ${X}x \"$X\"") "hi hix hi")
(expect "a value is not split" (c "X='a b'; printf '[%s]' $X") "[a b]")
(expect "an empty variable is no word" (c "printf '[%s]' $NOPE x") "[x]")
(expect "export" (c "export Y=1; env | grep '^Y='") "Y=1")
(expect "export of a shell variable" (c "Y=2; export Y; env | grep '^Y='") "Y=2")
(expect "unset" (c "Y=1; unset Y; echo [$Y]") "[]")
(expect "NAME=value for one command" (c "Z=2 env | grep '^Z='; echo [$Z]") "Z=2\n[]")
(expect "$0 and $1 of -c" (outcome (FArgv ["dotnet" shell "-c" "echo $0 $1" "one"])) "bjosh one")
;; Redirections.
(expect "> >> <" (c (str "echo x >" file "; echo y >>" file "; cat <" file)) "x\ny")
(expect "2>" (c (str "ls /no/such/dir 2>" file "; echo $?; grep -c . " file)) "2\n1")
(expect "> alone makes an empty file" (c (str ">" file "; wc -c <" file)) "0")
(expect "2>&1 is not supported yet" (c "ls 2>&1")
"err: bjosh: 2>&1 and >&2 are not supported yet\ncode: 2")
;; Shell commands.
(expect "cd and pwd" (c "cd /tmp; pwd") "/tmp")
(expect "cd -" (c "cd /tmp; cd /; cd -") "/tmp")
(expect "cd to nowhere" (string-contains? (c "cd /no/such/dir") "code: 1") #t)
(expect "type" (c "type cd") "cd is a shell command")
(expect "a shell command in a pipe" (c "cd | cat")
"err: bjosh: cd: a shell command cannot be in a pipe\ncode: 2")
;; Errors.
(expect "a program that is not found" (c "nosuchprogram")
"err: bjosh: nosuchprogram: command not found\ncode: 127")
(expect "syntax that is not supported" (string-contains? (c "if true; then ls; fi") "`if` is not supported") #t)
(expect "and its status" (string-ends-with? (c "if true; then ls; fi") "code: 2") #t)
(expect "& needs job control" (c "sleep 1 &")
"err: bjosh: & needs an interactive shell with job control\ncode: 2")
;; A file, and stdin.
(def script-path (path-combine scratch "script.sh"))
(ignore (file-write-text script-path "echo $0 $1\necho second line\n"))
(expect "a file, with its arguments" (outcome (FArgv ["dotnet" shell script-path "arg"]))
(str script-path " arg\nsecond line"))
(expect "lines on stdin" (outcome (FFrom script-path (FArgv ["dotnet" shell])))
"bjosh\nsecond line")
(ignore (directory-delete-tree scratch))
0)