bjosh/tests/interactive.bjo
Linus Björnstam f54ceb35af Ctrl-C of a value command that reads a terminal or a FIFO
The file of < is opened with #:cancellable? #t, so Ctrl-C of
li < /dev/tty takes nothing from the terminal, and the next line goes to
the editor. tests/interactive.bjo checks it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 08:13:20 +01:00

86 lines
4.8 KiB
Text

;; 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 goes to 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))))
(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 'exit\\r'" "sleep 1"]
"; "))
(def command (str "(" keys ") | XDG_STATE_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 "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 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)