bjosh/tests/values.bjo
Linus Björnstam acd84a2a4c Phase 2: jobs and history as values, redirections of value commands
- jobs and history are value sources in a pipe; alone they write text.
- < on the first value command of a group, > and >> on the last,
  which then writes lines. A value command after > is an error.
- File errors use fs-error-message, without a stack trace.
- plan.org: Ctrl-C does not cancel a blocking read of a terminal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 07:21:04 +01:00

130 lines
6.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/.
;; Value commands with bjosh -c: each command, mixed pipes, errors, and the
;; names of decision 4.
;;
;; bjo build && bjo test tests/values.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 scratch (path-combine (temp-dir) (str "bjosh-values-" (int->string process-id))))
;; Runs `line` in `scratch`. Returns "out", then "err: " and the error
;; text when there is one, then "code: n" when it is not 0.
(: (-> string string))
(defun (c line)
(def err-path (path-combine (temp-dir) (str "bjosh-values-err-" (int->string process-id))))
(def full (str "cd " scratch "; " line))
(match (run/output (FErrInto err-path (FArgv ["dotnet" (path-combine (current-directory) shell) "-c" full])))
((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)))))
(def fruit "printf 'banana\\napple\\nbanana\\ncherry\\n'")
(defun (main args)
(unless (file-exists? shell)
(println "FAILURE: src/main.exe is missing: run bjo build first"))
(ignore (directory-create scratch))
(ignore (directory-create (path-combine scratch "sub")))
(ignore (file-write-text (path-combine scratch "a.txt") "one\ntwo\n"))
(ignore (file-write-text (path-combine scratch "b.json") "[{\"n\": 1}, {\"n\": 2}]"))
(ignore (file-write-text (path-combine scratch "c.csv") "name,size\nx,1\ny,22\n"))
;; Lines from a program, and the filters.
(expect "sort-by" (c (str fruit " | so")) "apple\nbanana\nbanana\ncherry")
(expect "sort-by -r" (c (str fruit " | so -r | hd 1")) "cherry")
(expect "uniq" (c (str fruit " | u")) "banana\napple\ncherry")
(expect "count" (c (str fruit " | n")) "4")
(expect "reverse" (c (str fruit " | rv")) "cherry\nbanana\napple\nbanana")
(expect "first" (c (str fruit " | hd 2")) "banana\napple")
(expect "last" (c (str fruit " | tl 2")) "banana\ncherry")
(expect "lines, said" (c (str fruit " | li | n")) "4")
(expect "where it contains" (c (str fruit " | wh it contains err")) "cherry")
(expect "where it =~" (c (str fruit " | wh it =~ '^a'")) "apple")
(expect "where it ==" (c (str fruit " | wh it == apple")) "apple")
(expect "where with a quoted <" (c "printf '1\\n5\\n10\\n' | wh it '<' 6") "1\n5")
(expect "where gt on numbers" (c "printf '1\\n5\\n10\\n' | fj | wh it gt 2") "5\n10")
;; JSON and CSV.
(expect "from-json: a table, numbers to the right"
(c "echo '[{\"a\":1},{\"a\":30,\"b\":\"x\"}]' | fj")
" a b\n── ─\n 1\n30 x")
(expect "from-json: one value per line" (c "printf '{\"a\":1}\\n{\"a\":2}\\n' | fj | g a") "1\n2")
(expect "to-json keeps the order of fields" (c "echo '{\"b\":null,\"a\":1}' | fj | tj") "{\"b\":null,\"a\":1}")
(expect "select" (c "echo '{\"b\":2,\"a\":1,\"c\":3}' | fj | se c a | tj") "{\"c\":3,\"a\":1}")
(expect "get leaves out a record without the field" (c "echo '[{\"a\":1},{\"b\":2}]' | fj | g a") "1")
(expect "from-csv: numbers are numbers" (c "printf 'n,v\\nx,9\\ny,10\\n' | fcs | so v | g n") "x\ny")
(expect "to-csv" (c "printf 'n,v\\nx,1\\n' | fcs | tcs") "n,v\nx,1")
(expect "a bad document" (c "echo '{' | fj")
"err: bjosh: from-json: line 2, column 1: the document ended inside an object\ncode: 1")
;; Sources.
(expect "files" (c "l | g name") "a.txt\nb.json\nc.csv\nsub")
(expect "files: the type" (c "l | wh type == dir | g name") "sub")
(expect "files: the size" (c "l | wh name == a.txt | g size") "8")
(expect "files of a directory" (c "l sub | n") "0")
(expect "open .json" (c "o b.json | g n") "1\n2")
(expect "open .csv" (c "o c.csv | wh size gt 5 | g name") "y")
(expect "open, lines" (c "o a.txt") "one\ntwo")
(expect "procs" (c "p | wh pid == 1 | n") "1")
;; Values and programs in one pipe.
(expect "values into a program" (c "l | g name | grep json") "b.json")
(expect "a program, values, a program" (c (str fruit " | so | uniq -c | wc -l")) "3")
(expect "a value command between programs" (c (str fruit " | u | tr a-z A-Z")) "BANANA\nAPPLE\nCHERRY")
(expect "to-json into a program" (c "l | wh name == sub | tj | grep -c '\"type\":\"dir\"'") "1")
;; Errors.
(expect "where without its words" (c "l | wh name")
"err: bjosh: where: FIELD OP VALUE, such as: where size gt 1000\ncode: 1")
(expect "a bad count, in a pipe with programs" (c "printf 'a\\n' | hd x | cat")
"err: bjosh: first: not a count: x\ncode: 1")
(expect "a bad regular expression" (c "l | wh name =~ '('")
"err: bjosh: where: not a regular expression: (\ncode: 1")
;; Redirections.
(expect "> writes lines" (c "l > out.txt; wc -l < out.txt") "5")
(expect ">> appends" (c "l | g name > out.txt; l | g name >> out.txt; wc -l < out.txt") "10")
(expect "< reads a file" (c "o a.txt | rv > r.txt; so < r.txt") "one\ntwo")
(expect "< into a pipe with a program" (c "so < a.txt | tr a-z A-Z") "ONE\nTWO")
(expect "> after a program" (c "cat a.txt | rv > r.txt; cat r.txt") "two\none")
(expect "< on a file that is not there" (string-starts-with? (c "so < nosuch") "err: bjosh: not found: nosuch: ") #t)
(expect "< after another value command" (c "n | so < a.txt")
"err: bjosh: so: < on a value command after another one\ncode: 2")
(expect "a value command after >" (c "l > f | n")
"err: bjosh: n: the value command before it writes to a file, so nothing can be piped into it\ncode: 2")
(expect "2> of a value command" (c "l 2> e")
"err: bjosh: l: 2>, 2>&1 and >&2 of value commands are not supported yet\ncode: 2")
(expect "an assignment of a value command" (c "X=1 l")
"err: bjosh: l: assignments of value commands are not supported\ncode: 2")
;; Shell commands as values, in a pipe.
(expect "jobs in a pipe, without jobs" (c "jobs | n") "0")
(expect "history in a pipe, without an editor" (c "history | n") "0")
;; Names (decision 4).
(expect "a long name that is a program runs the program" (c "printf 'a\\na\\n' | uniq -c") " 2 a")
(expect "a short name is always the value command" (c "type u") "u is a value command: uniq, u")
(expect "no short name is a program on this PATH"
(match (run/output '(sh "-c" "for n in l p o wh se g so hd tl n rv u li fj tj fcs tcs; do command -v $n; done; true"))
((Ok (Tuple _ out)) (string-trim-right out))
((Err e) (run-error-message e)))
"")
(ignore (directory-delete-tree scratch))
0)