Skip to content

Commit

Permalink
[#24] Allow dir as dest in copy
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Apr 23, 2021
1 parent 71bea77 commit c68a386
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
11 changes: 7 additions & 4 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,21 @@
nofollow-links (conj LinkOption/NOFOLLOW_LINKS))))

(defn copy
"Copies src file to dest file.
"Copies src file to dest dir or file.
Options:
- :replace-existing
- :copy-attributes
- :nofollow-links."
- :nofollow-links (used to determine to copy symbolic link itself or not)."
([src dest] (copy src dest nil))
([src dest {:keys [:replace-existing
:copy-attributes
:nofollow-links]}]
(let [copy-options (->copy-opts replace-existing copy-attributes false nofollow-links)]
(Files/copy (as-path src) (as-path dest)
copy-options))))
(if (directory? dest)
(Files/copy (as-path src) (path dest (file-name src))
copy-options)
(Files/copy (as-path src) (as-path dest)
copy-options)))))

(defn posix->str
"Converts a set of PosixFilePermission to a string."
Expand Down
16 changes: 11 additions & 5 deletions test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,17 @@
(is (= "foo/bar/baz" (normalize f)))))

(deftest copy-test
(let [tmp-dir (temp-dir)
tmp-file (fs/create-file (fs/path tmp-dir "tmp-file"))
dest-path (fs/path tmp-dir "tmp-file-dest")]
(fs/copy tmp-file dest-path)
(is (fs/exists? dest-path))))
(let [tmp-dir-1 (temp-dir)
src-file (fs/create-file (fs/path tmp-dir-1 "tmp-file"))
dest-dir (temp-dir)
dest-file (fs/path dest-dir "tmp-file")]
(fs/copy src-file dest-file)
(is (fs/exists? dest-file))
(fs/delete dest-file)
(is (not (fs/exists? dest-file)))
(testing "copying into dir"
(fs/copy src-file dest-dir)
(is (fs/exists? dest-file)))))

(deftest copy-tree-test
(let [tmp-dir (temp-dir)]
Expand Down

0 comments on commit c68a386

Please sign in to comment.