;; 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 into a pipe" (c "ls /no/such/dir 2>&1 | grep -c such") "1") (expect ">FILE 2>&1" (c (str "ls /no/such/dir >" file " 2>&1; grep -c such " file)) "1") (expect "2>&1 before >" (c (str "ls 2>&1 >" file)) "err: bjosh: 2>&1 before > or >> is not supported yet: write >FILE 2>&1\ncode: 2") (expect ">&2" (c "printf x >&2; echo y") "y\nerr: x") (expect ">&2 with 2>" (c "printf x >&2 2>f") "err: bjosh: >&2 with >, >>, 2>, 2>> or 2>&1 is not supported\ncode: 2") ;; $(...). (expect "$(...)" (c "echo a$(echo b)c") "abc") (expect "a $(...) word is one word per line" (c "printf '[%s]' $(printf 'a b\\nc\\n\\n')") "[a b][c]") (expect "in double quotes, one word" (c "printf '[%s]' \"$(printf 'a\\nb\\n')\"") "[a\nb]") (expect "nested" (c "echo $(echo $(echo in))") "in") (expect "value commands write lines" (c "echo $(printf 'b\\na\\n' | so)") "a b") (expect "shell commands" (c "cd /tmp; echo $(pwd)") "/tmp") (expect "cd in it is undone" (c "cd /tmp; echo $(cd /; pwd); pwd") "/\n/tmp") (expect "variables in it are its own" (c "X=1; echo $(X=2; echo $X) $X") "2 1") (expect "exit ends only it" (c "echo $(exit 3; echo no)x") "x") (expect "an assignment's status is its $(...)'s" (c "X=$(exit 4) || echo $?") "4") (expect "a redirection in it" (c (str "echo [$(echo hidden >" file ")]; cat " file)) "[]\nhidden") (expect "its stderr is not captured" (c "echo [$(echo e >&2)]") "[]\nerr: e") (expect "2>&1 in it is" (c "echo [$(ls /no/such/dir 2>&1 | grep -c such)]") "[1]") ;; Aliases and command. (expect "an alias" (c "alias say='echo said'; say it") "said it") (expect "an alias with a pipe" (c "alias up='tr a-z A-Z'; echo x | up") "X") (expect "an alias in an alias" (c "alias a1='echo one'; alias a2='a1 two'; a2 three") "one two three") (expect "an alias of its own name" (c "alias ls='ls -d'; ls /") "/") (expect "a quoted name is no alias" (c "alias say='echo said'; 'say' x") "err: bjosh: say: command not found\ncode: 127") (expect "alias lists, sorted and quoted" (c "alias b='x y'; alias a=\"echo \\\"it's\\\"\"; alias") "alias a='echo \"it'\\''s\"'\nalias b='x y'") (expect "and runs that alias" (c "alias a=\"echo \\\"it's\\\"\"; a") "it's") (expect "an alias must be one pipe" (c "alias x='a; b'") "err: bjosh: alias: x: not one pipe: ;, &&, || and & are not allowed\ncode: 1") (expect "unalias" (c "alias x=ls; unalias x; type x") "err: bjosh: type: x: not found\ncode: 1") (expect "type of an alias" (c "alias x='ls -l'; type x") "x is an alias for ls -l") (expect "command runs the program" (c "alias echo='echo E'; command echo x") "x") (expect "command skips the value command" (c "printf 'a\\na\\n' | command uniq | wc -l") "1") ;; 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 "prevd goes back" (c "cd /tmp; cd /; prevd; pwd") "/tmp") (expect "and nextd forward" (c "cd /tmp; cd /; prevd; nextd; pwd") "/") (expect "prevd N" (c "cd /tmp; cd /usr; cd /; prevd 2; pwd") "/tmp") (expect "cd empties the way forward" (string-contains? (c "cd /tmp; cd /; prevd; cd /usr; nextd") "no next directory") #t) (expect "prevd with no history" (string-contains? (c "prevd") "code: 1") #t) (expect "cdh lists newest first" (c "cd /tmp; cd /usr; cd /; cdh") (str " 1 /usr\n 2 /tmp\n 3 " (current-directory))) (expect "cdh N goes there" (c "cd /tmp; cd /usr; cd /; cdh 2; pwd") "/tmp") (expect "the same directory is not history" (c "cd /tmp; cd /tmp; cdh") (str " 1 " (current-directory))) (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)