Skip to content

Commit c2e32fe

Browse files
committed
[#25] Allow dir as dest in move
1 parent fc385ea commit c2e32fe

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/babashka/fs.cljc

+19-7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
(Files/isDirectory (as-path f)
8686
(->link-opts nofollow-links))))
8787

88+
(def ^:private simple-link-opts
89+
(into-array LinkOption []))
90+
91+
(defn- directory-simple?
92+
[^Path f] (Files/isDirectory f simple-link-opts))
93+
8894
(defn hidden?
8995
"Returns true if f is hidden."
9096
[f] (Files/isHidden (as-path f)))
@@ -308,11 +314,12 @@
308314
([src dest {:keys [:replace-existing
309315
:copy-attributes
310316
:nofollow-links]}]
311-
(let [copy-options (->copy-opts replace-existing copy-attributes false nofollow-links)]
312-
(if (directory? dest)
317+
(let [copy-options (->copy-opts replace-existing copy-attributes false nofollow-links)
318+
dest (as-path dest)]
319+
(if (directory-simple? dest)
313320
(Files/copy (as-path src) (path dest (file-name src))
314321
copy-options)
315-
(Files/copy (as-path src) (as-path dest)
322+
(Files/copy (as-path src) dest
316323
copy-options)))))
317324

318325
(defn posix->str
@@ -472,14 +479,19 @@
472479
(Files/createFile (as-path path) attrs))))
473480

474481
(defn move
475-
"Move or rename a file to a target file via Files/move."
482+
"Move or rename a file to a target dir or file via Files/move."
476483
([source target] (move source target nil))
477484
([source target {:keys [:replace-existing
478485
:atomic-move
479486
:nofollow-links]}]
480-
(Files/move (as-path source)
481-
(as-path target)
482-
(->copy-opts replace-existing false atomic-move nofollow-links))))
487+
(let [target (as-path target)]
488+
(if (directory-simple? target)
489+
(Files/move (as-path source)
490+
(path target (file-name source))
491+
(->copy-opts replace-existing false atomic-move nofollow-links))
492+
(Files/move (as-path source)
493+
target
494+
(->copy-opts replace-existing false atomic-move nofollow-links))))))
483495

484496
(defn parent
485497
"Returns parent of f, is it exists."

test/babashka/fs_test.clj

+14-4
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,24 @@
187187
(is (not (fs/exists? tmp-dir1)))))))
188188

189189
(deftest move-test
190-
(let [tmp-dir1 (fs/create-temp-dir)
191-
f (fs/file tmp-dir1 "foo.txt")
190+
(let [src-dir (fs/create-temp-dir)
191+
dest-dir (fs/create-temp-dir)
192+
f (fs/file src-dir "foo.txt")
192193
_ (spit f "foo")
193-
f2 (fs/file tmp-dir1 "bar.txt")]
194+
f2 (fs/file dest-dir "foo.txt")]
194195
(fs/move f f2)
195196
(is (not (fs/exists? f)))
196197
(is (fs/exists? f2))
197-
(is (= "foo" (str/trim (slurp f2))))))
198+
(is (= "foo" (str/trim (slurp f2))))
199+
(fs/delete f2)
200+
(is (not (fs/exists? f2)))
201+
(testing "moving into dir"
202+
(spit f "foo")
203+
(is (fs/exists? f))
204+
(fs/move f dest-dir)
205+
(is (not (fs/exists? f)))
206+
(is (fs/exists? f2))
207+
(is (= "foo" (str/trim (slurp f2)))))))
198208

199209
(deftest set-attribute-test
200210
(let [dir (fs/create-temp-dir)

0 commit comments

Comments
 (0)