;; ;; test for system related procedures ;; #!no-fold-case (use gauche.test) (use gauche.config) (use scheme.list) (use srfi.13) (test-start "system") ;;------------------------------------------------------------------- (test-section "system") ;; test this first, so that we can use system commands to verify our results. (test* "system" #t (begin (sys-system ":") #t)) (test* "system" #t (begin (sys-system "") #t)) ;; shorthand of normalizing pathname. this doesn't do anything on ;; unix, but on Windows the separator in PATHNAME is replaced. (define (n pathname) (sys-normalize-pathname pathname)) ;; some common operations via command ;; NB: We do not use sys-* interface yet, for we're going to test them! (define (cmd-rmrf dir) (cond-expand (gauche.os.windows (sys-system #"rmdir /q /s ~(n dir) > NUL 2>&1") (sys-system #"del /q ~(n dir) > NUL 2>&1")) (else (sys-system #"rm -rf ~dir > /dev/null")))) (define (cmd-mkdir dir) (cond-expand (gauche.os.windows (sys-system #"mkdir ~(n dir)")) (else (sys-system #"mkdir ~dir")))) (define (cmd-touch path) (cond-expand (gauche.os.windows (sys-system #"echo \"\" > ~(n path)")) (else (sys-system #"touch ~path")))) (define (get-command-output command) (cmd-rmrf "test.out") (sys-system (format #f "~a > test.out" command)) (call-with-input-file "test.out" (lambda (in) (let loop ((line (read-line in)) (lines '())) (if (eof-object? line) (begin (close-input-port in) (cmd-rmrf "test.out") (string-join (reverse lines) " ")) (loop (read-line in) (cons line lines))))))) (define (get-lsmode file) (string-take (get-command-output (format #f "ls -ld ~a" file)) 10)) (define (get-pwd-via-pwd) ;; use pwd command to get pwd. avoid using shell's built-in pwd, ;; for it may be confused by symlinks. (cond-expand (gauche.os.windows (get-command-output "cd")) (else (cond [(sys-access "/bin/pwd" X_OK) ;; On MacOSX, /bin/pwd returns _logical_ pathname by default, ;; which is IMHO a bad decision (the behavior is /bin/pwd -L in ;; other BSDs). Anyway we have to cope with it. (if (string-contains (gauche-config "--arch") "darwin") (get-command-output "/bin/pwd -P") (get-command-output "/bin/pwd"))] [(sys-access "/usr/bin/pwd" X_OK) (get-command-output "/usr/bin/pwd")] [(sys-access "/sbin/pwd" X_OK) (get-command-output "/sbin/pwd")] [else (get-command-output "pwd")])))) ;;------------------------------------------------------------------- (test-section "environment") (test* "getenv" (string-trim-both (get-command-output (cond-expand (gauche.os.windows "echo %PATH%") (else "echo $PATH")))) (sys-getenv "PATH")) (test* "getcwd" (get-pwd-via-pwd) (sys-getcwd)) ;; putenv (cond-expand [gauche.sys.setenv (test* "sys-putenv" "foo" (begin (sys-putenv "ZZGGGBBB=foo") (sys-getenv "ZZGGGBBB"))) (test* "sys-putenv" "foo" (begin (sys-putenv "ZZGGGBBB" "foo") ;;old API (sys-getenv "ZZGGGBBB")))] [else]) ;; setenv (cond-expand [gauche.sys.setenv (test* "sys-setenv" "foo" (begin (sys-setenv "ZZGGGBBB" "foo" #t) (sys-getenv "ZZGGGBBB"))) (test* "sys-setenv" "foo" (begin (sys-setenv "ZZGGGBBB" "bar" #f) (sys-getenv "ZZGGGBBB")))] [else]) ;; unsetenv (cond-expand [gauche.sys.unsetenv (test* "sys-unsetenv" #f (begin (sys-setenv "ZZGGGBBB" "foo" #t) (sys-unsetenv "ZZGGGBBB") (sys-getenv "ZZGGGBBB")))] [else]) ;; environ (test* "sys-environ->alist" '(("A" . "B") ("A" . "") ("" . "B") ("A" . "B=C")) (sys-environ->alist '("A=B" "A=" "=B" "A=B=C"))) (let ([envs (sys-environ)]) (define (env-test var) (test* #"sys-environ (~var)" #t (cond [(sys-getenv var) => (^[val] (boolean (member #"~|var|=~|val|" envs)))] [else #t]))) (env-test "HOME") (env-test "USER") (env-test "LANG") (env-test "PWD") (env-test "TERM") (env-test "SHELL")) ;;------------------------------------------------------------------- (test-section "pathnames") (test* "basename" "ghi.jkl" (sys-basename "/abc/def/ghi.jkl")) (test* "dirname" "/abc/def" (sys-dirname "/abc/def/ghi.jkl")) (test* "basename" "ghi.jkl" (sys-basename "/abc/def/ghi.jkl/")) (test* "dirname" "/abc/def" (sys-dirname "/abc/def/ghi.jkl/")) (test* "basename" "ghi.jkl" (sys-basename "/abc//def//ghi.jkl//")) (test* "dirname" "/abc//def" (sys-dirname "/abc//def//ghi.jkl//")) (test* "basename" "ghi.jkl" (sys-basename "ghi.jkl")) (test* "dirname" "." (sys-dirname "ghi.jkl")) (test* "basename" "" (sys-basename "")) (test* "dirname" "." (sys-dirname "")) (test* "basename" "" (sys-basename "/")) (test* "dirname" (n "/") (sys-dirname "/")) (test* "basename" "" (sys-basename "//")) (test* "dirname" (n "/") (sys-dirname "//")) (test* "basename" "abc" (sys-basename "/abc")) (test* "dirname" (n "/") (sys-dirname "/abc")) (test* "basename" "abc" (sys-basename "//abc")) (test* "dirname" (n "/") (sys-dirname "//abc")) (test* "basename" ".." (sys-basename "../")) (test* "dirname" "." (sys-dirname "../")) (test* "basename" ".." (sys-basename "../..")) (test* "dirname" ".." (sys-dirname "../..")) (cond-expand (gauche.os.windows ;; test with a drive letter (test* "dirname" "d:\\" (sys-dirname "d:")) (test* "basename" "" (sys-basename "d:")) (test* "dirname" "d:\\" (sys-dirname "d:/")) (test* "basename" "" (sys-basename "d:/")) (test* "dirname" "d:\\" (sys-dirname "d:/z")) (test* "basename" "z" (sys-basename "d:/z")) (test* "dirname" "d:/z" (sys-dirname "d:/z/y")) (test* "basename" "y" (sys-basename "d:/z/y")) (test* "dirname" "d:." (sys-dirname "d:z")) (test* "basename" "z" (sys-basename "d:z")) (test* "dirname" "d:z" (sys-dirname "d:z/y")) (test* "basename" "y" (sys-basename "d:z/y")) ) (else #f)) (test* "normalize" (n (string-append (get-pwd-via-pwd) "/.")) (sys-normalize-pathname "." :absolute #t)) (test* "normalize" (n (string-append (get-pwd-via-pwd) "/")) (sys-normalize-pathname "" :absolute #t)) (test* "normalize" (cond-expand [gauche.os.windows (n (string-append (or (sys-getenv "HOME") ; MSYS (sys-getenv "USERPROFILE") ; cmd.exe "") "/abc"))] [else (n (string-append (slot-ref (sys-getpwuid (sys-getuid)) 'dir) "/abc"))]) (sys-normalize-pathname "~/abc" :expand #t)) (test* "normalize" (n "/a/b/c/d/e") (sys-normalize-pathname "/a/b//.///c//d/./e" :canonicalize #t)) (test* "normalize" (n "/a/b/c/d/e/") (sys-normalize-pathname "/a/b//.///c//d/./e/" :canonicalize #t)) (test* "normalize" (n "/a/b/c/d/e/") (sys-normalize-pathname "/a/B//./../c/d/../../b//c/d/e/f/.." :canonicalize #t)) (test* "normalize" (n "/a/b/") (sys-normalize-pathname "/a/b/c/d/../.." :canonicalize #t)) (test* "normalize" (n "/c/d/") (sys-normalize-pathname "/c/d/e/f/../../" :canonicalize #t)) (test* "normalize" (n "/e/f/") (sys-normalize-pathname "/e/f/g/h/../../." :canonicalize #t)) (test* "normalize" "" (sys-normalize-pathname "" :canonicalize #t)) (test* "normalize" (n "../..") (sys-normalize-pathname "a/b/c/../../../../.." :canonicalize #t)) (test* "normalize" (n "../../x/y") (sys-normalize-pathname "a/b/c/../../../../../x/y" :canonicalize #t)) ;;------------------------------------------------------------------- (test-section "glob") ;; NB: Actual globbing on filesystems is tested in file.scm. ;; Here we test the internal component, glob-component->regexp. (let ((data '("a.c" ".a.c" "...c" ".c" "a?.c" "a*.c"))) (define (t mode pattern expected) (test* (string-append "glob pattern " (x->string mode) " " pattern) expected (filter (glob-component->regexp pattern :mode mode) data))) (define (t-glob pattern expected) (t :glob pattern expected)) (define (t-shell pattern expected) (t :shell pattern expected)) (t-glob "*.c" '("a.c" "a?.c" "a*.c")) (t-glob "??.c" '("a?.c" "a*.c")) (t-glob ".*.c" '(".a.c" "...c")) (t-glob ".*c" '(".a.c" "...c" ".c")) (t-glob "*?.c" '("a.c" "a?.c" "a*.c")) (t-glob "*\\?.c" '("a?.c")) (t-glob "*\\*.c" '("a*.c")) (t-shell "*.c" '("a.c" ".a.c" "...c" ".c" "a?.c" "a*.c")) (t-shell "??.c" '(".a.c" "...c" "a?.c" "a*.c")) (t-shell ".*.c" '(".a.c" "...c" )) (t-shell ".*c" '(".a.c" "...c" ".c")) (t-shell "*?.c" '("a.c" ".a.c" "...c" "a?.c" "a*.c")) (t-shell "*\\?.c" '("a?.c")) (t-shell "*\\*.c" '("a*.c")) ) ;;------------------------------------------------------------------- (test-section "filesystem") (cmd-rmrf "test.dir") (test* "access" '(#f #f #f #f) (map (lambda (flag) (sys-access "test.dir" flag)) (list F_OK R_OK W_OK X_OK))) (cmd-touch "test.dir") (test* "unlink" #f (begin (sys-unlink "test.dir") (sys-access "test.dir" F_OK))) (cond-expand (gauche.os.windows ;; we need entirey different scheme here, but for the time being we ;; just omit the test. (sys-mkdir "test.dir" #o750) ) (else (test* "mkdir" #/drw[sx]r-[sx]---/ (begin (sys-mkdir "test.dir" #o750) (get-lsmode "test.dir")) rxmatch) (test* "chmod" #/drw[sx]r-[sx]r-x/ (begin (sys-chmod "test.dir" #o755) (get-lsmode "test.dir")) rxmatch) (test* "fchmod" #/drw[sx]r-[sx]---/ (begin (call-with-input-file "test.dir" (cut sys-fchmod <> #o750)) (get-lsmode "test.dir")) rxmatch) )) (define *fs-test-str* "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") (with-output-to-file "test.dir/xyzzy" (lambda () (display *fs-test-str*))) (test* "rename" '(#f #t) (begin (sys-rename "test.dir/xyzzy" "test.dir/zzZzz") (list (sys-access "test.dir/xyzzy" F_OK) (sys-access "test.dir/zzZzz" F_OK)))) (test* "readdir" '("." ".." "zzZzz") (sort (sys-readdir "test.dir"))) (test* "link" '("." ".." "xyzzy" "zzZzz") (begin (sys-link "test.dir/zzZzz" "test.dir/xyzzy") (sort (sys-readdir "test.dir")))) (test* "unlink" '("." ".." "xyzzy") (begin (sys-unlink "test.dir/zzZzz") (sort (sys-readdir "test.dir")))) (test* "rename" '("." ".." "zzZzz") (begin (sys-rename "test.dir/xyzzy" "test.dir/zzZzz") (sort (sys-readdir "test.dir")))) (test* "truncate" "abcdefghijklmno" (begin (sys-truncate "test.dir/zzZzz" 15) (call-with-input-file "test.dir/zzZzz" read-line))) (test* "ftruncate" "abcde" (begin (call-with-output-file "test.dir/zzZzz" (cut sys-ftruncate <> 5) :if-exists :append) (call-with-input-file "test.dir/zzZzz" read-line))) (test* "rmdir" #f (begin (sys-unlink "test.dir/zzZzz") (sys-rmdir "test.dir") (sys-access "test.dir" F_OK))) ;; sys-realpath (define (expected-path p) (let1 pp (sys-normalize-pathname p :absolute #t :canonicalize #t) (if (eqv? (string-ref pp (- (string-length pp) 1)) #\/) (substring pp 0 (- (string-length pp) 1)) pp))) (test* "sys-realpath (/)" (sys-normalize-pathname "/") (sys-realpath "/")) (test* "sys-realpath (.)" (expected-path ".") (sys-realpath ".")) (cond-expand [gauche.sys.symlink (sys-unlink "test1.o") (sys-unlink "test2.o") (with-output-to-file "test1.o" (cut print)) (sys-symlink "test1.o" "test2.o") (test* "sys-realpath (symlink)" (expected-path "./test1.o") (sys-realpath "./test2.o")) (sys-unlink "test2.o") (sys-symlink "./test1.o" "test2.o") (test* "sys-realpath (symlink)" (expected-path "./test1.o") (sys-realpath "./test2.o")) (sys-unlink "test2.o") (sys-symlink "../src/test1.o" "test2.o") (test* "sys-realpath (symlink)" (expected-path "./test1.o") (sys-realpath "./test2.o")) (sys-unlink "test1.o") (test* "sys-realpath (dangling)" (test-error) (sys-realpath "./test2.o")) (sys-mkdir "test1.o" #o777) (with-output-to-file "test1.o/test.o" (cut print)) (test* "sys-realpath (symlink to dir)" (expected-path "./test1.o/test.o") (sys-realpath "./test2.o/test.o")) (sys-unlink "test1.o/test.o") (sys-rmdir "test1.o") (sys-unlink "test2.o") (test* "sys-realpath (NOENT)" (test-error ) (sys-realpath "./test2.o/test.o")) ] [else]) ;; sys-mkstemp/sys-mkdtemp (let () ;; Check if multiple temporary files or directories are created. (define (test-mkxtemp name creator tester remover) (test* name '(#t #t) (let1 temps (map creator (iota 3)) (begin0 (list (every tester temps) (let1 sorted (sort temps) (every (complement equal?) sorted (cdr sorted)))) (for-each remover temps))))) (test-mkxtemp "sys-mkstemp" (^_ (receive (p n) (sys-mkstemp "test.o") (close-output-port p) n)) file-is-regular? sys-unlink) (test-mkxtemp "sys-mkdtemp" (^_ (sys-mkdtemp "test.dir")) file-is-directory? sys-rmdir)) ;;------------------------------------------------------------------- (test-section "time") (test* "srfi time" #t (time? (current-time))) (test* "srfi time time->seconds" #t (let1 t (current-time) (= (ref t'second) (floor->exact (time->seconds t))))) (test* "srfi time seconds->time" '(98765432109876 500000000) (let1 t (seconds->time 98765432109876.5) (list (ref t'second) (ref t'nanosecond)))) ;;