Skip to content

Commit

Permalink
Make globbing on Windows work similarly to unix (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored May 2, 2022
1 parent dc73460 commit 8563376
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,17 @@
;; we need to escape the file separator on Windows
(when win? "\\")
file-separator
pattern)
(if win?
(str/replace pattern "/" "\\\\")
pattern))
pattern (str prefix ":" pattern)
matcher (.getPathMatcher
(FileSystems/getDefault)
pattern)
match (fn [^Path path]
(if (.matches matcher path)
(swap! results conj! path)
nil))]
(when (.matches matcher path)
(swap! results conj! path))
nil)]
(walk-file-tree
base-path
{:max-depth max-depth
Expand Down Expand Up @@ -323,7 +325,9 @@
([root pattern opts]
(let [recursive (:recursive opts
(or (str/includes? pattern "**")
(str/includes? pattern file-separator)))]
(str/includes? pattern file-separator)
(when win?
(str/includes? pattern "/"))))]
(match root (str "glob:" pattern) (assoc opts :recursive recursive)))))

(defn- ->copy-opts ^"[Ljava.nio.file.CopyOption;"
Expand Down
10 changes: 9 additions & 1 deletion test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@
_ (spit (fs/file nested-dir "dude.txt") "contents")]
(is (= 1 (count (if windows?
(fs/glob tmp-dir1 "foo\\\\bar\\\\baz\\\\*")
(fs/glob tmp-dir1 "foo/bar/baz/*"))))))))
(fs/glob tmp-dir1 "foo/bar/baz/*")))))))
(testing "windows globbing now can be similar to unix"
(when windows?
(let [tmp-dir1 (temp-dir)
nested-dir (fs/file tmp-dir1 "foo" "bar" "baz")
_ (fs/create-dirs nested-dir)
_ (spit (fs/file nested-dir "dude.clj") "contents")
_ (spit (fs/file nested-dir "dude2.clj") "contents")]
(is (= 2 (count (fs/glob tmp-dir1 "foo/bar/baz/*.clj"))))))))

(deftest create-dir-test
(is (fs/create-dir (fs/path (temp-dir) "foo"))))
Expand Down

0 comments on commit 8563376

Please sign in to comment.