Skip to content

Commit

Permalink
[#40] which should return executable when extension is already included
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Nov 29, 2021
1 parent 9cab3da commit 30896ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
28 changes: 15 additions & 13 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@
"Create a hard link from path to target."
[path target]
(Files/createLink
(as-path path)
(as-path target)))
(as-path path)
(as-path target)))

(defn delete
"Deletes f. Returns nil if the delete was successful,
Expand Down Expand Up @@ -707,20 +707,22 @@
;; this, let me know, it may break.
(let [exts (if windows?
(let [exts (or (:win-exts opts)
[".com" ".exe" ".bat" ".cmd"])
["com" "exe" "bat" "cmd"])
ext (extension program)]
(if (and ext (contains? (set exts) ext))
;; this program name already contains the expected extension on Windows
[""]
;; this program name already contains the expected extension on
;; so we search with that
[nil]
exts))
[""])]
[nil])]
(loop [paths (babashka.fs/exec-paths)
results []]
(if-let [p (first paths)]
(let [fs (loop [exts exts
candidates []]
(if-let [ext (first exts)]
(let [f (babashka.fs/path p (str program ext))]
(if (seq exts)
(let [ext (first exts)
f (babashka.fs/path p (str program (when ext (str "." ext))))]
(if (executable? f)
(recur (rest exts)
(conj candidates f))
Expand Down Expand Up @@ -810,8 +812,8 @@
_ (create-dirs dest)
cp-opts (->copy-opts replace-existing nil nil nil)]
(with-open
[fis (Files/newInputStream zip-file (into-array java.nio.file.OpenOption []))
zis (ZipInputStream. fis)]
[fis (Files/newInputStream zip-file (into-array java.nio.file.OpenOption []))
zis (ZipInputStream. fis)]
(loop []
(let [entry (.getNextEntry zis)]
(when entry
Expand All @@ -822,8 +824,8 @@
(do
(create-dirs (parent new-path))
(Files/copy ^java.io.InputStream zis
new-path
cp-opts))))
new-path
cp-opts))))
(recur))))))))

(defmacro with-temp-dir
Expand All @@ -835,7 +837,7 @@
`options` is a map with the keys as for create-temp-dir."
{:arglists '[[[binding-name options] & body]]}
[[binding-name options & more] & body]
{:pre [(empty? more)(symbol? binding-name)]}
{:pre [(empty? more) (symbol? binding-name)]}
`(let [~binding-name (create-temp-dir ~options)]
(try
~@body
Expand Down
13 changes: 8 additions & 5 deletions test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@
(spit "echo hello")
(fs/set-posix-file-permissions "r-xr-x---")))
(is (fs/which "foo.foo"))
(when windows?
(testing "can find executable when including extension"
(is (= (fs/which "foo.foo") (fs/which "foo.foo.bat")))))
(fs/delete-tree "on-path")))

(deftest predicate-test
Expand Down Expand Up @@ -400,9 +403,9 @@
(defn zip
[zip-file src]
(with-open
[fos (FileOutputStream. (str zip-file))
zos (ZipOutputStream. fos)
fis (FileInputStream. (fs/file src))]
[fos (FileOutputStream. (str zip-file))
zos (ZipOutputStream. fos)
fis (FileInputStream. (fs/file src))]
(let [src (fs/path src)
^String src-name (fs/file-name src)
entry (ZipEntry. src-name)]
Expand Down Expand Up @@ -435,7 +438,7 @@
(vreset! capture-dir dir)
(testing "creates a directory with the given options"
(is (fs/exists? dir))
(is (str/starts-with? (fs/file-name(str dir)) "with-temp-dir-test")))
(is (str/starts-with? (fs/file-name (str dir)) "with-temp-dir-test")))
(fs/create-file (fs/path dir "xx"))
(is (fs/exists? (fs/path dir "xx"))))
(testing "deletes its directory and contents on exit from the scope"
Expand Down Expand Up @@ -499,4 +502,4 @@
(is (= (apply fs/path input)
(fs/expand-home (str/join fs/file-separator input))))))
(is (= (fs/path (fs/home) "abc" "~" "def")
(fs/expand-home (fs/path "~" "abc" "~" "def")))))
(fs/expand-home (fs/path "~" "abc" "~" "def")))))

0 comments on commit 30896ac

Please sign in to comment.