72 lines
3.6 KiB
Text
72 lines
3.6 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 '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 "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)
|