Build the user commands with $BJO, after the start-up file

- The build runs $BJO, else bjo on PATH. A bjo that is not there gives a
  message that says what to set, and where.
- An interactive bjosh runs the start-up file first, and loads the user
  commands after it, so the file can set PATH or BJO.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-12-01 11:22:07 +01:00
parent bf961b12f9
commit 817de694b0
4 changed files with 75 additions and 27 deletions

View file

@ -27,13 +27,14 @@
(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)
;; Runs bjosh with `args`, the environment `env` (NAME=value), 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) (Vec string) string))
(defun (bjosh-env env 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))))
(match (run/output (FErrInto err-path (FArgv (vec-append (vec-append ["env" (str "XDG_CONFIG_HOME=" config)] env)
(vec-append ["dotnet" shell] args)))))
((Ok (Tuple code out))
(def err (match (file-read-text err-path) ((Ok t) t) ((Err _) "")))
(string-trim-right
@ -42,6 +43,9 @@
(if (= code 0) "" (str "code: " (int->string code))))))
((Err e) (str "did not run: " (run-error-message e)))))
(: (-> (Vec string) string))
(defun (bjosh args) (bjosh-env [] args))
(: (-> string string))
(defun (c line) (bjosh ["-c" line]))
@ -122,13 +126,13 @@
(else (stringbuilder-add! out (vec-ref chars i)) (go (+ i 1) out)))))
;; 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)
;; XDG_CONFIG_HOME and `env` (NAME=value ...) before it, after `wait`
;; seconds. Returns what it writes, without escape sequences.
(: (-> string string string (Vec string) string))
(defun (interactive dir env wait lines)
(def keys (str "sleep " wait "; "
(string-join (vec-map (fun (l) (str "printf '" l "\\r'; sleep 0.7")) lines) "; ")))
(def command (str "(" keys ") | XDG_STATE_HOME=" dir " XDG_CONFIG_HOME=" dir
(def command (str "(" keys ") | " env " XDG_STATE_HOME=" dir " XDG_CONFIG_HOME=" dir
" script -qec 'dotnet src/main.exe' /dev/null"))
(match (run/output '(sh "-c" ,command))
((Ok (Tuple _ text)) (plain text))
@ -176,7 +180,7 @@
(if (not (file-exists? "/usr/bin/script"))
(println "skipped: the prompt (no script command)")
(let ((out (interactive config "2.5" ["true" "false" "true" "exit"])))
(let ((out (interactive config "" "2.5" ["true" "false" "true" "exit"])))
(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"))
@ -185,12 +189,39 @@
(if (file-exists? "/usr/bin/script")
(let* ((fresh (path-combine config "fresh"))
(out (interactive fresh "12" ["echo a | twice" "exit"])))
(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)
;; The bjo program: $BJO, else bjo on PATH.
(def real-bjo (match (run/output '(sh "-c" "command -v bjo"))
((Ok (Tuple 0 out)) (string-trim-right out))
(_ "bjo")))
(def narrow "PATH=/usr/bin:/bin")
(write-source (commands "with BJO"))
(expect-true "no bjo on PATH: the message says what to set"
(string-contains? (bjosh-env [narrow] ["-c" "true"])
"bjo was not found. Set PATH, or BJO to the bjo program, in "))
(expect-true "BJO names the program"
(string-contains? (bjosh-env [narrow (str "BJO=" real-bjo)] ["-c" "help"]) "with BJO"))
(write-source (commands "with a BJO that is not there"))
(expect-true "a BJO that is not there"
(string-contains? (bjosh-env [(str "BJO=" config "/no-bjo")] ["-c" "true"])
"/no-bjo was not found"))
(if (file-exists? "/usr/bin/script")
(let ((rc (path-combine config "bjosh/rc")))
(write-source (commands "set in rc"))
(ignore (file-write-text rc (str "export BJO=" real-bjo "\n")))
(def out (interactive config narrow "10" ["echo a | twice" "exit"]))
(expect-true "the start-up file runs before the commands are built"
(string-contains? out "twice\na\na\n"))
(expect-true "and they build with its BJO" (not (string-contains? out "was not found")))
(ignore (file-delete rc)))
unit)
(ignore (directory-delete-tree config))
(println "extension ok")
0)