From 8563376bfbdfde99ec6e2b3dfd347bd06756fa87 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 2 May 2022 13:29:36 +0200 Subject: [PATCH] Make globbing on Windows work similarly to unix (#59) --- src/babashka/fs.cljc | 14 +++++++++----- test/babashka/fs_test.clj | 10 +++++++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/babashka/fs.cljc b/src/babashka/fs.cljc index 2d9681a..7e0272f 100644 --- a/src/babashka/fs.cljc +++ b/src/babashka/fs.cljc @@ -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 @@ -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;" diff --git a/test/babashka/fs_test.clj b/test/babashka/fs_test.clj index 116633a..8141b4c 100644 --- a/test/babashka/fs_test.clj +++ b/test/babashka/fs_test.clj @@ -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"))))