bjosh/tests/interactive.bjo

108 lines
6.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 on a terminal: job control and the prompt.
;;
;; bjo build && bjo test tests/interactive.bjo
;;
;; The test runs bjosh under `script`, and writes keys to it with pauses
;; between them. The history and the start-up file are in a directory of
;; the test.
(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)))
;; The output without escape sequences and carriage returns.
(: (-> string string))
(defun (plain text)
(def chars (string->vec (string-replace text "\r" "")))
(let go ((i 0) (out (stringbuilder-empty)))
(cond
((= i (vec-length chars)) (stringbuilder->string out))
((and (= (char->int (vec-ref chars i)) 27) (< (+ i 1) (vec-length chars)) (= (vec-ref chars (+ i 1)) #\[))
(let skip ((j (+ i 2)))
(if (and (< j (vec-length chars))
(let ((code (char->int (vec-ref chars j)))) (and (>= code 0x20) (<= code 0x3f))))
(skip (+ j 1))
(go (+ j 1) out))))
(else (stringbuilder-add! out (vec-ref chars i)) (go (+ i 1) out)))))
(defun (main args)
(cond
((not (file-exists? "/usr/bin/script")) (println "skipped: no script command"))
((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))))
;; An empty commands directory: no package of user commands is written.
(ignore (directory-create (path-combine state "bjosh/commands")))
(ignore (file-write-text (path-combine state "bjosh/rc") "alias fromrc='echo set in rc'\n"))
(ignore (directory-create (path-combine state "tabs/alpha")))
(ignore (directory-create (path-combine state "tabs/apple")))
(def keys (string-join ["sleep 2.5"
"printf 'sleep 30\\r'" "sleep 1" "printf '\\032'" "sleep 0.5"
"printf 'jobs\\r'" "sleep 0.5"
"printf 'bg\\r'" "sleep 0.5"
"printf 'kill %%1\\r'" "sleep 0.5"
"printf 'jobs\\r'" "sleep 0.5"
"printf 'sleep 30\\r'" "sleep 1" "printf '\\003'" "sleep 0.5"
"printf 'echo status $?\\r'" "sleep 0.5"
"printf 'ech'" "sleep 0.3" "printf '\\032'" "sleep 0.5"
"printf 'o alive\\r'" "sleep 0.5"
"printf 'sleep 30 &\\r'" "sleep 1"
"printf 'jobs | g command\\r'" "sleep 1"
"printf 'history | tl 1 | g line\\r'" "sleep 1"
"printf 'kill %%1\\r'" "sleep 0.5"
"printf 'printf x | u\\r'" "sleep 1"
"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 '/usr/\\r'" "sleep 0.3" "printf 'pwd\\r'" "sleep 0.3"
(str "printf 'ls " state "/tabs/a\\t'") "sleep 0.5" "printf '\\003'" "sleep 0.3"
"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 " 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))))
(expect-true "Ctrl-Z stops the job" (string-contains? out "^Z\n[1]+ Stopped sleep 30"))
(expect-true "jobs lists it" (string-contains? out "jobs\n[1]+ Stopped sleep 30"))
(expect-true "bg continues it" (string-contains? out "bg\n[1]+ sleep 30 &"))
(expect-true "kill ends it" (string-contains? out "[1]+ Terminated sleep 30"))
(expect-true "Ctrl-C ends a job, and the prompt starts a new line" (string-contains? out "^C\n"))
(expect-true "with status 130" (string-contains? out "status 130"))
(expect-true "Ctrl-Z at the prompt does not stop the shell" (string-contains? out "\nalive\n"))
(expect-true "a directory alone changes to it" (string-contains? out "pwd\n/usr\n"))
(expect-true "Tab lists the candidates when it can insert nothing"
(and (string-contains? out "tabs/alpha/") (string-contains? out "tabs/apple/")))
(expect-true "jobs in a pipe" (string-contains? out "jobs | g command\nsleep 30\n"))
(expect-true "history in a pipe, with this line last"
(string-contains? out "history | tl 1 | g line\nhistory | tl 1 | g line\n"))
(expect-true "a value command at the end of a job" (string-contains? out "printf x | u\nx\n"))
(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"))
((Err _) #f)))
(unless (string-contains? out "status 130")
(println out))
(ignore (directory-delete-tree state))))
0)