- (bjosh syntax): a part of POSIX sh, parsed with (text parser-combinators). Syntax that is not supported raises an error that names it. - (bjosh expand): variables, ~ and globs. Values are not split. - (bjosh exec): pipes as (std run) jobs, && || ; &, redirections, and NAME=value through env. - (bjosh shell) and (bjosh builtins): the job table and cd, pwd, exit, export, unset, jobs, fg, bg, kill, type, help, history. - (bjosh editor): Enter waits for open quotes and trailing operators; completion of commands and files. - -c LINE, FILE, and stdin. A Native AOT build too. - Tests: the parser, expansion, the editor, lines through -c, and job control under script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
82 lines
3.4 KiB
Text
82 lines
3.4 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 expand): variables, ~ and globs, with a fake environment and a
|
|
;; directory of files made for the test.
|
|
;;
|
|
;; bjo test tests/expand.bjo
|
|
|
|
(import (std prelude))
|
|
(import (std simpletest))
|
|
(import (bjosh syntax))
|
|
(import (bjosh expand))
|
|
|
|
(import/extern
|
|
(temp-dir (: System.IO.Path.GetTempPath (-> string)))
|
|
(process-id (: System.Environment.ProcessId int #:get #:no-raise)))
|
|
|
|
(: (-> string (Option string)))
|
|
(defun (lookup name)
|
|
(cond
|
|
((= name "HOME") (Some "/home/me"))
|
|
((= name "X") (Some "a b"))
|
|
((= name "STAR") (Some "*"))
|
|
((= name "EMPTY") (Some ""))
|
|
((= name "?") (Some "0"))
|
|
(else None)))
|
|
|
|
(: (-> string (Option string)))
|
|
(defun (home-of user)
|
|
(cond
|
|
((string-empty? user) (Some "/home/me"))
|
|
((= user "bob") (Some "/home/bob"))
|
|
(else None)))
|
|
|
|
(def ex (Expander (lookup lookup) (home-of home-of)))
|
|
|
|
;; The words of the first command of `text`, joined by "|".
|
|
(: (-> string string))
|
|
(defun (words text)
|
|
(match (parse-line text)
|
|
((Ok items)
|
|
(def pipe (record-ref (record-ref (vec-ref items 0) and-or) first))
|
|
(string-join (expand-words ex (record-ref (vec-ref pipe 0) words)) "|"))
|
|
((Err _) "parse error")))
|
|
|
|
(defun (main args)
|
|
(expect "plain words" (words "ls -l x") "ls|-l|x")
|
|
(expect "a variable" (words "echo $HOME/src") "echo|/home/me/src")
|
|
(expect "a value with spaces is one word" (words "echo $X") "echo|a b")
|
|
(expect "a variable that is not set is empty" (words "echo a${NOPE}b") "echo|ab")
|
|
(expect "an empty unquoted variable is no word" (words "echo $EMPTY $NOPE x") "echo|x")
|
|
(expect "a quoted empty variable is a word" (words "echo \"$EMPTY\" x") "echo||x")
|
|
(expect "$?" (words "echo $?") "echo|0")
|
|
(expect "~" (words "cd ~ ~/src") "cd|/home/me|/home/me/src")
|
|
(expect "~user" (words "cd ~bob/x") "cd|/home/bob/x")
|
|
(expect "a user that does not exist" (words "cd ~nobody") "cd|~nobody")
|
|
|
|
;; Globs, in a directory of our own.
|
|
(def dir (path-combine (temp-dir) (str "bjosh-expand-" (int->string process-id))))
|
|
(ignore (directory-create dir))
|
|
(for-each (fun (name) (ignore (file-write-text (path-combine dir name) "")))
|
|
["b.txt" "a.txt" "c.log" ".hidden.txt" "*.txt"])
|
|
(def here (current-directory))
|
|
(set-current-directory! dir)
|
|
(expect "a glob, sorted" (words "ls *.txt") "ls|*.txt|a.txt|b.txt")
|
|
(expect "? and [ ]" (words "ls [ab].tx?") "ls|a.txt|b.txt")
|
|
(expect "a glob that matches nothing stays" (words "ls *.md") "ls|*.md")
|
|
(expect "a quoted * is not a glob" (words "ls '*'.txt") "ls|*.txt")
|
|
(expect "a \\* is not a glob" (words "ls \\*.txt") "ls|*.txt")
|
|
(expect "a * from a variable is not a glob" (words "ls $STAR") "ls|*")
|
|
(expect "a quoted part in a glob matches itself" (words "ls '*'*") "ls|*.txt")
|
|
(expect "names with a dot only for a pattern with a dot" (words "ls .h*") "ls|.hidden.txt")
|
|
(set-current-directory! here)
|
|
(for-each (fun (name) (ignore (file-delete (path-combine dir name))))
|
|
["b.txt" "a.txt" "c.log" ".hidden.txt" "*.txt"])
|
|
(ignore (directory-delete dir))
|
|
|
|
;; Redirections.
|
|
(expect "a target" (expand-target ex [(Lit "out" #f)]) (Ok "out"))
|
|
(expect "an empty target" (expand-target ex [(Param "EMPTY" #f)]) (Err "${EMPTY}: no file name"))
|
|
0)
|