Something went wrong. Try again.
A rewrite of the Plan 9 rc shell in Goose for UNIX-like systems and with structured CLI args
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091(require '[babashka.process :as p] '[babashka.fs :as fs] '[clojure.string :as str])
(def shell (str (fs/absolutize (or (first *command-line-args*) "./rc2"))))(def failures (atom []))(defn check [name value] (when-not value (swap! failures conj name) (println "FAIL" name)))(defn run [args dir] @(p/process (into [shell] args) {:dir (str dir) :out :string :err :string}))
(let [dir (fs/create-temp-dir {:prefix "rc2-declarations-"}) script (str (fs/path dir "demo.rc"))] (try (spit script (str "/usr/bin/touch before\nargs {\n name demo-tool\n about 'Copy files'\n version '1.2'\n" " flag verbose -v --verbose 'Show details'\n" " option mode -m --mode (fast safe 'space value' '$(touch injected)' 'quote''value') default safe 'Select a mode'\n" " positional files multiple required 'Input files'\n}\n/usr/bin/touch after\necho $mode $files\n")) (let [result (run [script "--help"] dir)] (check "help exit" (zero? (:exit result))) (check "help schema" (and (str/includes? (:out result) "Usage: demo-tool") (str/includes? (:out result) "Copy files") (str/includes? (:out result) "[default: safe]")))) (let [result (run [script "--version"] dir)] (check "version metadata" (= [0 "demo-tool 1.2\n"] [(:exit result) (:out result)]))) (doseq [target ["fish" "bash" "zsh" "rc"]] (let [direct (run ["--completions" target script] dir) invoked (run [script "--completions" target] dir) text (:out direct)] (check (str target " extraction") (and (zero? (:exit direct)) (str/includes? text "demo-tool"))) (check (str target " script invocation") (= [0 text] [(:exit invoked) (:out invoked)])) (when (and (not= target "rc") (fs/which target)) (let [result @(p/process [target "-n"] {:in text :out :string :err :string})] (check (str target " generated syntax") (zero? (:exit result))))) (when (= target "bash") (let [helper (str "__rc2_" (apply str (map #(format "%02x" (int %)) "demo-tool")) "_complete") command (str text "\nCOMP_WORDS=(demo-tool --mode=s); COMP_CWORD=1; " helper "; printf '%s\\n' \"${COMPREPLY[@]}\"") result @(p/process [target "-c" command] {:dir (str dir) :out :string :err :string})] (check "bash equals completion" (= "--mode=safe\n--mode=space value\n" (:out result))))) (when (= target "rc") (let [result (run ["-c" (str text "\ncomplete_demo-tool s --mode")] dir)] (check "rc schema choices" (= "fast\nsafe\nspace value\n$(touch injected)\nquote'value\n" (:out result))))))) (check "metadata never executes script" (every? #(not (fs/exists? (fs/path dir %))) ["before" "after" "injected"])) (let [result (run [script "--mode=fast" "one" "two"] dir)] (check "script values" (= [0 "fast one two\n"] [(:exit result) (:out result)]))) (doseq [[body line character message] [["position name required" 4 3 "unknown argument declaration"] ["wat" 4 3 "unknown argument declaration"] ["option remotes -r multiple" 4 21 "unknown argument modifier"] ["positional files repeat" 4 20 "unknown argument modifier"] ["flag verbose -v int" 4 19 "unknown argument modifier"] ["option mode -m typo" 4 18 "unknown argument modifier"] ["option mode -m foo^bar" 4 18 "argument declarations require literal words"] ["option output" 4 10 "option requires an alias"] ["about one two" 4 13 "metadata requires one literal word"] ["option mode -m default $bad" 4 26 "default requires literal values"] ["command bad/name {}" 4 3 "command requires one literal portable name"] ["command remote {position name}" 4 19 "unknown argument declaration"] ["option mode -m 'First\nsecond'; wat" 5 10 "unknown argument declaration"] ["option mode -m 'é'; wat" 4 23 "unknown argument declaration"]]] (let [source (str "# Header\n\nargs {\n\t " body "\n}\necho WRONG\n") source-line (nth (str/split-lines source) (dec line)) prefix (take (dec character) source-line) caret (str (apply str (map #(if (= % \tab) \tab \space) prefix)) "^") expected [(str script ":" line ": " message) (str "\n" source-line "\n" caret "\n")]] (spit script source) (doseq [args [[script "--help"] ["--completions" "bash" script] [script]]] (let [result (run args dir)] (check (str "declaration diagnostic: " body " " args) (and (= 2 (:exit result)) (empty? (:out result)) (every? #(str/includes? (:err result) %) expected))))))) (let [result (run ["-c" "args {wat}"] dir)] (check "inline declaration excerpt" (= "rc2: <input>:1: unknown argument declaration\nargs {wat}\n ^\n" (:err result)))) (let [source "fn bad {args {wat}}" padding (apply str (repeat (.indexOf source "wat") " "))] (spit script (str source "\n" (apply str (repeat 5000 "x=value\n")) "bad\n")) (let [result (run [script] dir)] (check "declaration excerpt survives collection" (and (= 2 (:exit result)) (= (str "rc2: " script ":1: unknown argument declaration\n" source "\n" padding "^\n") (:err result)))))) (spit script "args {wat}\n") (let [result (run ["-c" (str ". '" script "'")] dir)] (check "sourced declaration path" (= (str "rc2: " script ":1: unknown argument declaration\nargs {wat}\n ^\n") (:err result)))) (finally (fs/delete-tree dir))))
(println "Declaration tests:" (count @failures) "failures")(System/exit (if (empty? @failures) 0 1))