;; 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))) ;; $(...): the words of its first command after the first, as lines, ;; without quotes, and two empty lines. (: (-> (Vec Item) string)) (defun (substitute items) (def pipe (record-ref (record-ref (vec-ref items 0) and-or) first)) (def words (vec-map (fun (w) (string-replace (word->string w) "\"" "")) (record-ref (vec-ref pipe 0) words))) (str (string-join (vec-drop words 1) "\n") "\n\n")) (def ex (Expander (lookup lookup) (home-of home-of) (substitute substitute))) ;; 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") ;; $(...). `substitute` returns the words after `echo`, one per line. (expect "a $(...) word is one word per line" (words "x $(echo a b)") "x|a|b") (expect "its empty lines are no words" (words "x $(echo '' a)") "x|a") (expect "nothing is no word" (words "x $(echo) y") "x|y") (expect "in a word it is text, without the last line breaks" (words "x p$(echo a b)q") "x|pa\nbq") (expect "in double quotes it is one word" (words "x \"$(echo a b)\"") "x|a\nb") (expect "it is not a glob" (words "x $(echo '*')") "x|*") ;; 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)