Phase 3: $(...), aliases, a start-up file, PROMPT, Done lines, >&2
- $(...) runs in a subshell. A word that is only an unquoted $(...) is one word per line. X=$(cmd) sets $? to its status. - alias, unalias, and command NAME. An alias is one pipe. - ~/.config/bjosh/rc runs before the first prompt. - PROMPT, expanded as in double quotes, with \w \W \u \h \$ \e \n. - "Done" lines are written above the input while the prompt waits. - >&2, for programs and value commands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f54ceb35af
commit
f8c4ad64bc
13 changed files with 680 additions and 128 deletions
|
|
@ -33,7 +33,16 @@
|
|||
((= user "bob") (Some "/home/bob"))
|
||||
(else None)))
|
||||
|
||||
(def ex (Expander (lookup lookup) (home-of home-of)))
|
||||
;; $(...): the words of its first command after the first, as lines,
|
||||
;; without quotes, and two empty lines.
|
||||
(: (-> (Vec Item) string))
|
||||
(defun (substitute items)
|
||||
(def pipe (record-ref (record-ref (vec-ref items 0) and-or) first))
|
||||
(def words (vec-map (fun (w) (string-replace (word->string w) "\"" ""))
|
||||
(record-ref (vec-ref pipe 0) words)))
|
||||
(str (string-join (vec-drop words 1) "\n") "\n\n"))
|
||||
|
||||
(def ex (Expander (lookup lookup) (home-of home-of) (substitute substitute)))
|
||||
|
||||
;; The words of the first command of `text`, joined by "|".
|
||||
(: (-> string string))
|
||||
|
|
@ -56,6 +65,14 @@
|
|||
(expect "~user" (words "cd ~bob/x") "cd|/home/bob/x")
|
||||
(expect "a user that does not exist" (words "cd ~nobody") "cd|~nobody")
|
||||
|
||||
;; $(...). `substitute` returns the words after `echo`, one per line.
|
||||
(expect "a $(...) word is one word per line" (words "x $(echo a b)") "x|a|b")
|
||||
(expect "its empty lines are no words" (words "x $(echo '' a)") "x|a")
|
||||
(expect "nothing is no word" (words "x $(echo) y") "x|y")
|
||||
(expect "in a word it is text, without the last line breaks" (words "x p$(echo a b)q") "x|pa\nbq")
|
||||
(expect "in double quotes it is one word" (words "x \"$(echo a b)\"") "x|a\nb")
|
||||
(expect "it is not a glob" (words "x $(echo '*')") "x|*")
|
||||
|
||||
;; Globs, in a directory of our own.
|
||||
(def dir (path-combine (temp-dir) (str "bjosh-expand-" (int->string process-id))))
|
||||
(ignore (directory-create dir))
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@
|
|||
;; bjo build && bjo test tests/interactive.bjo
|
||||
;;
|
||||
;; The test runs bjosh under `script`, and writes keys to it with pauses
|
||||
;; between them. The history goes to a directory of the test.
|
||||
;; between them. The history and the start-up file are in a directory of
|
||||
;; the test.
|
||||
|
||||
(import (std prelude))
|
||||
(import (std simpletest))
|
||||
|
|
@ -38,6 +39,8 @@
|
|||
((not (file-exists? "src/main.exe")) (println "FAILURE: src/main.exe is missing: run bjo build first"))
|
||||
(else
|
||||
(def state (path-combine (temp-dir) (str "bjosh-interactive-" (int->string process-id))))
|
||||
(ignore (directory-create (path-combine state "bjosh")))
|
||||
(ignore (file-write-text (path-combine state "bjosh/rc") "alias fromrc='echo set in rc'\n"))
|
||||
(def keys (string-join ["sleep 2.5"
|
||||
"printf 'sleep 30\\r'" "sleep 1" "printf '\\032'" "sleep 0.5"
|
||||
"printf 'jobs\\r'" "sleep 0.5"
|
||||
|
|
@ -56,9 +59,13 @@
|
|||
"printf 'stty tostop; printf y | u; stty -tostop\\r'" "sleep 1"
|
||||
"printf 'li < /dev/tty\\r'" "sleep 1" "printf '\\003'" "sleep 0.5"
|
||||
"printf 'echo after $?\\r'" "sleep 0.5"
|
||||
"printf 'fromrc\\r'" "sleep 0.5"
|
||||
"printf 'sleep 0.5 &\\r'" "sleep 0.3" "printf 'ec'" "sleep 1.5" "printf 'ho typed\\r'" "sleep 0.5"
|
||||
"printf 'cd /tmp\\r'" "sleep 0.3"
|
||||
"printf \"PROMPT='<\\\\W \\$(echo s)>\\\\$ '\\r\"" "sleep 0.5"
|
||||
"printf 'exit\\r'" "sleep 1"]
|
||||
"; "))
|
||||
(def command (str "(" keys ") | XDG_STATE_HOME=" state " script -qec 'dotnet src/main.exe' /dev/null"))
|
||||
(def command (str "(" keys ") | XDG_STATE_HOME=" state " XDG_CONFIG_HOME=" state " script -qec 'dotnet src/main.exe' /dev/null"))
|
||||
(def out (match (run/output '(sh "-c" ,command))
|
||||
((Ok (Tuple _ text)) (plain text))
|
||||
((Err e) (run-error-message e))))
|
||||
|
|
@ -76,6 +83,12 @@
|
|||
(expect-true "and with tostop set" (string-contains? out "stty -tostop\ny\n"))
|
||||
(expect-true "Ctrl-C of a read of the terminal, and the next line runs"
|
||||
(string-contains? out "\nafter 130\n"))
|
||||
(expect-true "the start-up file runs" (string-contains? out "fromrc\nset in rc\n"))
|
||||
(expect-true "a job that ends is reported while the prompt waits"
|
||||
(match (string-split-once out "Done sleep 0.5")
|
||||
((Some (Tuple _ after)) (string-contains? after "echo typed\ntyped\n"))
|
||||
(None #f)))
|
||||
(expect-true "PROMPT" (string-contains? out "<tmp s>$ "))
|
||||
(expect-true "the history is saved"
|
||||
(match (file-read-text (path-combine state "bjosh/history"))
|
||||
((Ok text) (string-contains? text "kill %1"))
|
||||
|
|
|
|||
|
|
@ -70,8 +70,40 @@
|
|||
(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 is not supported yet" (c "printf x >&2")
|
||||
"err: bjosh: >&2 is not supported yet\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")
|
||||
|
|
|
|||
|
|
@ -54,6 +54,17 @@
|
|||
(expect "~ at the start" (parsed "cd ~/src ~bob") "cd ~/src ~bob ;")
|
||||
(expect "~ elsewhere is text" (parsed "echo a~b") "echo a~b ;")
|
||||
|
||||
;; $(...).
|
||||
(expect "$(...)" (parsed "echo $(ls -l)") "echo $(ls -l ;) ;")
|
||||
(expect "$(...) in a word" (parsed "echo a$(b)c") "echo a$(b ;)c ;")
|
||||
(expect "$(...) in double quotes" (parsed "echo \"x $(ls)\"") "echo \"x \"\"$(ls ;)\" ;")
|
||||
(expect "a pipe and a list in $(...)" (parsed "echo $(a | b; c && d)") "echo $(a | b ; c && d ;) ;")
|
||||
(expect "$(...) in $(...)" (parsed "echo $(echo $(ls))") "echo $(echo $(ls ;) ;) ;")
|
||||
(expect "quotes in $(...)" (parsed "echo $(echo ')')") "echo $(echo \")\" ;) ;")
|
||||
(expect "line breaks in $(...)" (parsed "echo $(\na\nb\n)") "echo $(a ; b ;) ;")
|
||||
(expect "an empty $(...)" (parsed "echo $()") "echo $() ;")
|
||||
(expect "$(...) in an assignment" (parsed "X=$(ls)") "X=$(ls ;) ;")
|
||||
|
||||
;; Operators.
|
||||
(expect "a pipe" (parsed "ls | wc -l") "ls | wc -l ;")
|
||||
(expect "a pipe without spaces" (parsed "ls|wc") "ls | wc ;")
|
||||
|
|
@ -98,7 +109,9 @@
|
|||
(expect "for" (parsed "for x in a; do ls; done") "error: `for` is not supported")
|
||||
(expect "a quoted if is a word" (parsed "'if' x") "\"if\" x ;")
|
||||
(expect "( )" (parsed "(ls)") "error: subshells ( ) are not supported")
|
||||
(expect "$(...)" (parsed "echo $(ls)") "error: $(...) is not supported yet")
|
||||
(expect "an open $(" (parsed "echo $(ls")
|
||||
"error: Expecting: ')' to close $( / Unexpected: end of input")
|
||||
(expect "a ) without (" (parsed "echo )") "error: ')' without '('")
|
||||
(expect "`...`" (parsed "echo `ls`") "error: `...` is not supported")
|
||||
(expect "here-documents" (parsed "cat <<EOF") "error: here-documents are not supported")
|
||||
(expect "${NAME:-x}" (parsed "echo ${A:-x}")
|
||||
|
|
@ -107,4 +120,15 @@
|
|||
"error: only <, >, >>, 2>, 2>>, 2>&1 and >&2 are supported")
|
||||
(expect "a number before a space is a word" (parsed "echo 3 >x") "echo 3 >x ;")
|
||||
(expect "the column of an open quote" (error-column "echo 'abc") 10)
|
||||
|
||||
;; One pipe, for an alias.
|
||||
(expect "one pipe" (match (parse-pipe "ls -l | wc") ((Ok cs) (vec-length cs)) ((Err _) 0)) 2)
|
||||
(expect "a list is not one pipe" (parse-pipe "a; b") (Err "not one pipe: ;, &&, || and & are not allowed"))
|
||||
(expect "&& is not one pipe" (parse-pipe "a && b") (Err "not one pipe: ;, &&, || and & are not allowed"))
|
||||
(expect "& is not one pipe" (parse-pipe "a &") (Err "not one pipe: ;, &&, || and & are not allowed"))
|
||||
(expect "nothing is no pipe" (parse-pipe " ") (Err "no command"))
|
||||
|
||||
;; Text as in double quotes, for the prompt.
|
||||
(expect "text" (match (parse-text "a \"b\" $X $(c)") ((Ok ps) (word->string ps)) ((Err e) e))
|
||||
"\"a \"\"\"\"\"b\"\"\"\"\" \"\"${X}\"\" \"\"$(c ;)\"")
|
||||
0)
|
||||
|
|
|
|||
|
|
@ -107,9 +107,13 @@
|
|||
(expect "< after another value command" (c "n | so < a.txt")
|
||||
"err: bjosh: so: < on a value command after another one\ncode: 2")
|
||||
(expect "a value command after >" (c "l > f | n")
|
||||
"err: bjosh: n: the value command before it writes to a file, so nothing can be piped into it\ncode: 2")
|
||||
"err: bjosh: n: the value command before it writes to a file or to stderr, so nothing can be piped into it\ncode: 2")
|
||||
(expect "2> of a value command" (c "l 2> e")
|
||||
"err: bjosh: l: 2>, 2>&1 and >&2 of value commands are not supported yet\ncode: 2")
|
||||
"err: bjosh: l: 2> and 2>&1 of value commands are not supported yet\ncode: 2")
|
||||
(expect ">&2 of a value command" (c "l >&2 | n")
|
||||
"err: bjosh: n: the value command before it writes to a file or to stderr, so nothing can be piped into it\ncode: 2")
|
||||
(expect ">&2 at the end writes lines" (c "l | g name | hd 1 >&2") "err: a.txt")
|
||||
(expect ">&2 after a program" (c "printf 'b\\na\\n' | so >&2; echo done") "done\nerr: a\nb")
|
||||
(expect "an assignment of a value command" (c "X=1 l")
|
||||
"err: bjosh: l: assignments of value commands are not supported\ncode: 2")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue