1. Miki Tebeka
  2. fs

Commits

joeatwork  committed f31a370

Minor changes to accomodate lumps in windows filesystem and naming. In particular
- working around a clash between Java's regex syntax and a single "\" file separator
- Delegating determination of absoluteness to File.isAbsolute in fs/join (to accomodate drive letters)
- Massaging test suite to pass or just skip tests on windows

  • Participants
  • Parent commits b9ca3df
  • Branches default

Comments (0)

Files changed (2)

File src/fs.clj

View file
 (def ^:dynamic *extension-separator* ".")
 (declare abspath)
 ; Current working directory (you can't change directory in java)
-(def ^:dynamic *cwd* (.getAbsolutePath (io/as-file ".")))
+(def ^:dynamic *cwd* (.getCanonicalPath (io/as-file ".")))
 
-(declare join)
 (defn- as-file [path]
+  "The challenge is to work nicely with *cwd*"
   (cond
-    (instance? File path) path
-    (= path "") (io/as-file "")
-    (= path ".") (io/as-file *cwd*)
-    (= (.substring path 0 1) *separator*) (io/as-file path)
-    :else (io/as-file (join *cwd* path))))
+   (instance? File path) path
+   (= path "") (io/as-file "")
+   (= path ".") (io/as-file *cwd*)
+   :else
+   (let [ try (new File path) ]
+         (if (.isAbsolute try)
+           try
+           (new File *cwd* path)))))
 
 (defn listdir
   "List files under path."
 (defn split
   "Split path to componenets.\n\t(split \"a/b/c\") -> (\"a\" \"b\" \"c\")."
   [path]
-  (into [] (.split path *separator*)))
+  (into [] (.split path (str "\\Q" *separator* "\\E"))))
 
 (defn rename
   "Rename old-path to new-path."

File test/fs_test.clj

View file
   (:use [fs] :reload-all)
   (:use [clojure.test]))
 
+(def system-tempdir (. System getProperty "java.io.tmpdir"))
+
 (deftest listdir-test
   (is (not (empty? (listdir ".")))))
 
 (def walk-atom (atom #{}))
 
 (defn walk-fn [root dirs files]
-  (swap! walk-atom conj [root dirs files]))
+  (swap! walk-atom conj [(normpath root) dirs files]))
 
 (deftest walk-test
  (let [root (create-walk-dir)]
 
 (deftest test-chmod
   (let [f (tempfile)]
-    (chmod "-x" f)
-    (is (not (executable? f)))
+    ; Skip "-x" on windows, since it'll never work.
+    ; (see http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/enhancements/ )
+    (when (.setExecutable (new File f) false)
+      (chmod "-x" f)
+      (is (not (executable? f))))
     (chmod "+x" f)
     (is (executable? f))
     (delete f)))
 
 (deftest test-copy-tree
   (let [from (create-walk-dir)
-        to (tempdir)]
+        to (normpath (tempdir))]
     (swap! walk-atom (fn [_] #{}))
     (let [path (copy-tree from to)
           dest (join to (basename from))]
-      (is (= path dest))
+      (is (= (normpath path) (normpath dest)))
       (walk to walk-fn)
       (let [result @walk-atom]
         (is (= result
     (is (not (exists? root)))
     (is (= root result))))
 
-(deftest test-home
-  (is (= (home) (System/getenv "HOME"))))
+; Skipping on windows, which has somewhat wacky
+; and complicated ideas about what HOME means.
+(when (System/getenv "HOME")
+  (deftest test-home
+    (is (= (home) (System/getenv "HOME")))))
 
 (deftest text-extension
   (is (= (extension "fs.clj") ".clj"))
   (is (= (extension "") "")))
 
 (deftest test-chdir
-  (let [path "/tmp"]
+  (let [path system-tempdir]
     (chdir path)
-    (is (= *cwd* path))
-    (is (= (cwd) path))))
+    (is (= (normpath *cwd*) (normpath path)))
+    (is (= (normpath (cwd)) (normpath path)))))
 
 (deftest test-with-cwd
-  (with-cwd "/tmp"
-    (is (= (cwd) "/tmp")))
+  (with-cwd system-tempdir
+    (is (= (normpath (cwd)) (normpath system-tempdir))))
   (is (= (cwd) *cwd*)))
 
 (deftest test-file
   ; Test that we work with java.io.File objects as well
-  (is (directory? (File. "/tmp"))))
+  (is (directory? (File. system-tempdir))))