2026-12-01 10:38:40 +01:00
|
|
|
;; 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/.
|
|
|
|
|
|
|
|
|
|
;; User commands: a package in $XDG_CONFIG_HOME/bjosh/commands, built and
|
|
|
|
|
;; loaded by bjosh. See plan.org, phase 4.
|
|
|
|
|
;;
|
|
|
|
|
;; - `bjosh --new-commands` writes the package; the test then replaces its
|
|
|
|
|
;; src/commands.bjo.
|
|
|
|
|
;; - Value commands, shell commands, names that are protected, errors, a
|
|
|
|
|
;; rebuild after a change, and `reload`.
|
|
|
|
|
;; - The prompt, under `script`, when /usr/bin/script is there.
|
|
|
|
|
;;
|
|
|
|
|
;; bjo build && bjo test tests/extension.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 config (path-combine (temp-dir) (str "bjosh-extension-" (int->string process-id))))
|
|
|
|
|
|
|
|
|
|
(def source (path-combine config "bjosh/commands/src/commands.bjo"))
|
|
|
|
|
|
|
|
|
|
;; Runs bjosh with `args`, and this test's config directory. Returns "out",
|
|
|
|
|
;; then "err: " and the error text when there is one, then "code: n" when it
|
|
|
|
|
;; is not 0.
|
|
|
|
|
(: (-> (Vec string) string))
|
|
|
|
|
(defun (bjosh args)
|
|
|
|
|
(def err-path (path-combine config "stderr"))
|
|
|
|
|
(match (run/output (FErrInto err-path (FArgv (vec-append ["env" (str "XDG_CONFIG_HOME=" config) "dotnet" shell] args))))
|
|
|
|
|
((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) (bjosh ["-c" line]))
|
|
|
|
|
|
|
|
|
|
;; The test's commands. `help-text` is the help of `twice`, to see a
|
|
|
|
|
;; rebuild.
|
|
|
|
|
(: (-> string string))
|
|
|
|
|
(defun (commands help-text)
|
|
|
|
|
(str "(import (std prelude))
|
|
|
|
|
(import (text json))
|
|
|
|
|
(import (bjosh api))
|
|
|
|
|
|
|
|
|
|
(export bjosh-extension)
|
|
|
|
|
|
|
|
|
|
(: (-> (Vec string) Values Values Unit))
|
|
|
|
|
(defbjo (twice args in out)
|
|
|
|
|
(let go ()
|
|
|
|
|
(match (next! in)
|
|
|
|
|
(None unit)
|
|
|
|
|
((Some v) (emit! out v) (emit! out v) (go)))))
|
|
|
|
|
|
|
|
|
|
(: (-> ShellView (Vec string) int))
|
|
|
|
|
(defun (setx sh args)
|
|
|
|
|
(shell-set-var! sh \"X\" (string-join args \"-\"))
|
|
|
|
|
0)
|
|
|
|
|
|
|
|
|
|
(: (-> ShellView (Vec string) int))
|
|
|
|
|
(defun (both sh args)
|
|
|
|
|
(def a (shell-run sh \"echo one\"))
|
|
|
|
|
(def b (shell-run sh \"false\"))
|
|
|
|
|
(println (str \"statuses \" (int->string a) \" \" (int->string b) \" X=\"
|
|
|
|
|
(match (shell-var sh \"X\") ((Some x) x) (None \"none\"))))
|
|
|
|
|
7)
|
|
|
|
|
|
|
|
|
|
(: (-> ShellView (Vec string) int))
|
|
|
|
|
(defun (boom sh args) (command-error \"boom: it failed\"))
|
|
|
|
|
|
|
|
|
|
(: (-> ShellView (Vec string) int))
|
|
|
|
|
(defun (my-exit sh args) (println \"not this exit\") 0)
|
|
|
|
|
|
|
|
|
|
(: (-> ShellView string))
|
|
|
|
|
(defun (prompt sh)
|
|
|
|
|
(if (= (shell-status sh) 0)
|
|
|
|
|
(str \"[ext \" (int->string (vec-length (shell-jobs sh))) \"]> \")
|
|
|
|
|
(command-error \"the prompt failed\")))
|
|
|
|
|
|
|
|
|
|
(: bjosh-extension Extension)
|
|
|
|
|
(def bjosh-extension
|
|
|
|
|
(extension
|
|
|
|
|
#:commands [(ValueCommand (long \"twice\") (short \"tw\") (usage \"twice\")
|
|
|
|
|
(help \"" help-text "\")
|
|
|
|
|
(body (OnValues (bjoroutine (args in out) (twice args in out)))))]
|
|
|
|
|
#:shell-commands [(ShellCommand (name \"setx\") (usage \"setx WORD...\") (help \"set X\") (run setx))
|
|
|
|
|
(ShellCommand (name \"both\") (usage \"both\") (help \"run two lines\") (run both))
|
|
|
|
|
(ShellCommand (name \"boom\") (usage \"boom\") (help \"raise\") (run boom))
|
|
|
|
|
(ShellCommand (name \"exit\") (usage \"exit\") (help \"not used\") (run my-exit))]
|
|
|
|
|
#:prompt (Some prompt)))
|
|
|
|
|
"))
|
|
|
|
|
|
|
|
|
|
(: (-> string void))
|
|
|
|
|
(defun (write-source text)
|
|
|
|
|
(match (file-write-text source text)
|
|
|
|
|
((Ok _) unit)
|
|
|
|
|
((Err e) (println (str "FAILURE: " (fs-error-message e))))))
|
|
|
|
|
|
|
|
|
|
;; 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)))))
|
|
|
|
|
|
2026-12-01 10:57:19 +01:00
|
|
|
;; Types lines into an interactive bjosh under `script`, with `dir` as
|
|
|
|
|
;; XDG_CONFIG_HOME, after `wait` seconds. Returns what it writes, without
|
|
|
|
|
;; escape sequences.
|
|
|
|
|
(: (-> string string (Vec string) string))
|
|
|
|
|
(defun (interactive dir wait lines)
|
|
|
|
|
(def keys (str "sleep " wait "; "
|
2026-12-01 10:38:40 +01:00
|
|
|
(string-join (vec-map (fun (l) (str "printf '" l "\\r'; sleep 0.7")) lines) "; ")))
|
2026-12-01 10:57:19 +01:00
|
|
|
(def command (str "(" keys ") | XDG_STATE_HOME=" dir " XDG_CONFIG_HOME=" dir
|
2026-12-01 10:38:40 +01:00
|
|
|
" script -qec 'dotnet src/main.exe' /dev/null"))
|
|
|
|
|
(match (run/output '(sh "-c" ,command))
|
|
|
|
|
((Ok (Tuple _ text)) (plain text))
|
|
|
|
|
((Err e) (run-error-message e))))
|
|
|
|
|
|
|
|
|
|
(defun (main args)
|
|
|
|
|
(unless (file-exists? shell)
|
|
|
|
|
(println "FAILURE: src/main.exe is missing: run bjo build first"))
|
|
|
|
|
(ignore (directory-create config))
|
|
|
|
|
|
|
|
|
|
(expect "no package: the shell runs as before" (c "echo hi") "hi")
|
|
|
|
|
(expect-true "--new-commands writes the package"
|
|
|
|
|
(string-starts-with? (bjosh ["--new-commands"]) "Wrote "))
|
|
|
|
|
(expect-true "and not over one that is there"
|
|
|
|
|
(string-ends-with? (bjosh ["--new-commands"]) "there already\ncode: 1"))
|
|
|
|
|
(expect "the example builds and runs" (c "echo a | twice") "a\na")
|
|
|
|
|
|
|
|
|
|
(write-source (commands "each value twice"))
|
|
|
|
|
(expect "a value command, by its long name" (c "echo a | twice | count") "2")
|
|
|
|
|
(expect "and by its short name" (c "echo a | tw | tw | count") "4")
|
|
|
|
|
(expect "type of a value command" (c "type twice") "twice is a value command of yours: twice, tw")
|
|
|
|
|
(expect "type of a shell command" (c "type setx") "setx is a shell command of yours")
|
|
|
|
|
(expect-true "help lists them"
|
|
|
|
|
(string-contains? (c "help") "Your shell commands:\n setx WORD... set X\n"))
|
|
|
|
|
(expect "a shell command sets a variable" (c "setx a b; echo $X") "a-b")
|
|
|
|
|
(expect "it runs lines, and returns a status" (c "X=v; both; echo $?") "one\nstatuses 0 1 X=v\n7")
|
|
|
|
|
(expect "a shell command that raises" (c "boom; echo $?") "1\nerr: bjosh: boom: boom: it failed")
|
|
|
|
|
(expect "exit is not replaced" (c "exit 4") "code: 4")
|
|
|
|
|
(expect "a shell command is not in a pipe" (c "setx a | cat")
|
|
|
|
|
"err: bjosh: setx: a shell command cannot be in a pipe\ncode: 2")
|
|
|
|
|
|
|
|
|
|
(write-source (commands "each value two times"))
|
|
|
|
|
(expect-true "a change is built at the next start"
|
|
|
|
|
(string-contains? (c "help") "each value two times"))
|
|
|
|
|
|
|
|
|
|
(write-source "(oops")
|
|
|
|
|
(def broken (c "echo still runs"))
|
|
|
|
|
(expect-true "a package that does not build: the shell runs"
|
|
|
|
|
(string-starts-with? broken "still runs\nerr: bjosh: the user commands did not build:"))
|
|
|
|
|
(expect-true "with the compiler's message" (string-contains? broken "never closed"))
|
|
|
|
|
(expect-true "and no user commands" (string-contains? (c "type twice") "not found"))
|
|
|
|
|
|
|
|
|
|
(write-source (commands "after reload"))
|
|
|
|
|
(expect-true "reload builds and loads" (string-contains? (c "reload; help") "after reload"))
|
|
|
|
|
|
|
|
|
|
(if (not (file-exists? "/usr/bin/script"))
|
|
|
|
|
(println "skipped: the prompt (no script command)")
|
2026-12-01 10:57:19 +01:00
|
|
|
(let ((out (interactive config "2.5" ["true" "false" "true" "exit"])))
|
2026-12-01 10:38:40 +01:00
|
|
|
(expect-true "the prompt of the commands" (string-contains? out "[ext 0]> true"))
|
|
|
|
|
(expect-true "a prompt that raises writes its message"
|
|
|
|
|
(string-contains? out "bjosh: the prompt of your commands raised: the prompt failed"))
|
|
|
|
|
(expect "once" (vec-length (string-split out "the prompt failed")) 2)
|
|
|
|
|
(expect-true "and the default prompt is used after it" (not (string-contains? out "[ext 0]> exit")))))
|
|
|
|
|
|
2026-12-01 10:57:19 +01:00
|
|
|
(if (file-exists? "/usr/bin/script")
|
|
|
|
|
(let* ((fresh (path-combine config "fresh"))
|
|
|
|
|
(out (interactive fresh "12" ["echo a | twice" "exit"])))
|
|
|
|
|
(expect-true "the first interactive start writes the package"
|
|
|
|
|
(string-contains? out "bjosh: wrote your commands to "))
|
|
|
|
|
(expect-true "and builds and loads it" (string-contains? out "twice\na\na\n")))
|
|
|
|
|
unit)
|
|
|
|
|
|
2026-12-01 10:38:40 +01:00
|
|
|
(ignore (directory-delete-tree config))
|
|
|
|
|
(println "extension ok")
|
|
|
|
|
0)
|