diff --git a/src/rules_clojure/fs.clj b/src/rules_clojure/fs.clj index ccac760..1a2cd7f 100644 --- a/src/rules_clojure/fs.clj +++ b/src/rules_clojure/fs.clj @@ -139,3 +139,8 @@ (defn new-temp-file [dir prefix suffix] (Files/createTempFile (->path dir) prefix suffix (into-array FileAttribute []))) + +(defn spit-file [f content & opts] + (let [has-newline? (= \newline (last content)) + content (if has-newline? content (str content \newline))] + (apply spit f (str content) opts))) diff --git a/src/rules_clojure/gen_build.clj b/src/rules_clojure/gen_build.clj index 2b275c8..16e23be 100644 --- a/src/rules_clojure/gen_build.clj +++ b/src/rules_clojure/gen_build.clj @@ -729,7 +729,7 @@ (-> dir (fs/->path "BUILD.bazel") fs/path->file - (spit content :encoding "UTF-8")))) + (fs/spit-file content :encoding "UTF-8")))) (s/fdef gen-source-paths- :args (s/cat :a (s/keys :req-un [::deps-edn-dir ::src-ns->label ::dep-ns->label ::jar->lib ::deps-repo-tag ::deps-bazel]) :paths (s/coll-of fs/path?))) (defn gen-source-paths- @@ -808,7 +808,7 @@ "generates the BUILD file for @deps//: with a single target containing all deps.edn-resolved dependencies" [{:keys [repository-dir deps-build-dir dep-ns->label jar->lib lib->jar lib->deps deps-repo-tag deps-bazel] :as args}] (println "writing to" (-> (fs/->path deps-build-dir "BUILD.bazel") fs/path->file)) - (spit (-> (fs/->path deps-build-dir "BUILD.bazel") fs/path->file) + (fs/spit-file (-> (fs/->path deps-build-dir "BUILD.bazel") fs/path->file) (str/join "\n\n" (concat [(emit-bazel (list 'package (kwargs {:default_visibility ["//visibility:public"]}))) (emit-bazel (list 'load "@rules_clojure//:rules.bzl" "clojure_library"))] @@ -987,7 +987,7 @@ (apply disj $ output-ns exclude-nses) (sort $)) conditional-require? (re-matches #".*\.cljc$" output-filename)] - (spit (fs/path->file (fs/->path workspace-root output-filename)) + (fs/spit-file (fs/path->file (fs/->path workspace-root output-filename)) (str ";;; Generated by bazel, do not edit\n\n" (str "(ns " output-ns "\n") (str " " diff --git a/test/rules_clojure/BUILD b/test/rules_clojure/BUILD index ee001ea..21778f5 100644 --- a/test/rules_clojure/BUILD +++ b/test/rules_clojure/BUILD @@ -50,3 +50,7 @@ clojure_test(name="persistent-classloader-test", clojure_test(name="compile-test", deps=[":test-deps"], test_ns = "rules-clojure.compile-test") + +clojure_test(name="fs-test", + deps=[":test-deps"], + test_ns = "rules-clojure.fs-test") diff --git a/test/rules_clojure/fs_test.clj b/test/rules_clojure/fs_test.clj new file mode 100644 index 0000000..6b92b7f --- /dev/null +++ b/test/rules_clojure/fs_test.clj @@ -0,0 +1,20 @@ +(ns rules-clojure.fs-test + (:require [clojure.java.shell :as shell] + [clojure.string :as str] + [clojure.test :refer :all] + [rules-clojure.fs :as fs])) + +(defn count-lines [f] + (-> (shell/sh "wc" "-l" f) + :out + str/trim + first + str + Long/parseLong)) + +(deftest spit-file + (let [tmp-file "/tmp/spit-test.txt"] + (spit tmp-file "test with spit") + (is (= 0 (count-lines tmp-file))) + (fs/spit-file tmp-file "test with fs/spit-file") + (is (= 1 (count-lines tmp-file)))))