Skip to content

Commit

Permalink
Fix update-file to support paths (#139)
Browse files Browse the repository at this point in the history
* fix update-file to support paths

* rename argument to path

* add changelog
  • Loading branch information
rfhayashi authored Feb 11, 2025
1 parent ed77224 commit 64b1c19
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ For a list of breaking changes, check [here](#breaking-changes).

Babashka [fs](https://github.com/babashka/fs): file system utility library for Clojure

- [#138](https://github.com/babashka/fs/issues/138): Fix `fs/update-file` to support paths ([@rfhayashi](https://github.com/rfhayashi))

## v0.5.24 (2025-01-09)

- [#135](https://github.com/babashka/fs/issues/135): additional fix for preserving protocol when calling `fs/path` on multiple arguments ([@Sohalt](https://github.com/Sohalt))
Expand Down
8 changes: 4 additions & 4 deletions src/babashka/fs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1248,17 +1248,17 @@
Options:
* `:charset` - charset of file, default to \"utf-8\""
{:arglists '([file f & xs] [file opts f & xs])}
([file f & xs]
{:arglists '([path f & xs] [path opts f & xs])}
([path f & xs]
(let [[opts f xs] (if (map? f)
[f (first xs) (rest xs)]
[nil f xs])
{:keys [charset]
:or {charset "utf-8"}} opts
opts [:encoding charset]
old-val (apply slurp file opts)
old-val (apply slurp (as-file path) opts)
new-val (apply f old-val xs)]
(apply spit file new-val opts)
(apply spit (as-file path) new-val opts)
new-val)))

(defn unixify
Expand Down
6 changes: 5 additions & 1 deletion test/babashka/fs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,11 @@

(let [file (fs/file (fs/temp-dir) (str (gensym)))]
(spit file ", ")
(is (= "foo, bar, baz" (fs/update-file file str/join ["foo" "bar" "baz"])))))
(is (= "foo, bar, baz" (fs/update-file file str/join ["foo" "bar" "baz"]))))

(let [path (fs/path (fs/file (fs/temp-dir) (str (gensym))))]
(spit (fs/file path) "foo")
(is (= "foobar" (fs/update-file path str "bar")))))

(deftest unixify-test
(when windows?
Expand Down

0 comments on commit 64b1c19

Please sign in to comment.