Skip to content

Instantly share code, notes, and snippets.

@jkk
Created May 17, 2010 22:51
Show Gist options
  • Select an option

  • Save jkk/404336 to your computer and use it in GitHub Desktop.

Select an option

Save jkk/404336 to your computer and use it in GitHub Desktop.
;; for minimal filesystem mocking (only listFiles and getName methods work)
(defn mock-filesystem
"Takes a tree of vectors and returns a minimal mock file/dir hierarchy"
[file]
(if (vector? file)
(let [[dir & files] file
children (into-array File (map mock-fs files))]
(proxy [File] [dir]
(listFiles [] children)))
(File. file)))
(deftest test-mock-filesystem
(is (= (map #(.getName %)
(.listFiles (mock-filesystem ["/" "foo" "bar" "baz"])))
["foo" "bar" "baz"])))
;; or, with some more proxy methods (listFiles, getName, isDirectory, isFile)
(defn mock-filesystem
"Takes a tree of vectors and returns a mock file/dir hierarchy of
proxied java.io.File instances"
[file]
(if (vector? file)
(let [[dir & files] file
children (into-array java.io.File (map mock-fs files))]
(proxy [java.io.File] [dir]
(isDirectory [] true)
(isFile [] false)
(listFiles [] children)))
(proxy [java.io.File] [file]
(isDirectory [] false)
(isFile [] true))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment