Skip to content

Commit

Permalink
Read boot-env using io/resource and io/reader
Browse files Browse the repository at this point in the history
Previously, classpath resources were assumed to be files and passed to
io/file. This failed when the resource was within a jar. The fix is to
use io/reader instead, which works on resources within or without
jars.

Fixes: weavejester#80
  • Loading branch information
larkery committed Apr 20, 2018
1 parent a20fac7 commit 8167510
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
4 changes: 3 additions & 1 deletion environ/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
:scm {:dir ".."}
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]])
:dependencies [[org.clojure/clojure "1.5.1"]]
:profiles {:test {:resource-paths ["test-resources"]}}
)
15 changes: 12 additions & 3 deletions environ/src/environ/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@
(map (fn [[k v]] [(keywordize k) v]))
(into {})))

(defn- read-env-reader [r]
(when-let [reader (io/reader r)]
(into {}
(for [[k v] (edn/read-string (slurp r))]
[(sanitize-key k) (sanitize-val k v)]))))

(defn- read-env-file [f]
(if-let [env-file (io/file f)]
(if (.exists env-file)
(into {} (for [[k v] (edn/read-string (slurp env-file))]
[(sanitize-key k) (sanitize-val k v)])))))
(read-env-reader env-file))))

(defn- read-env-resource [r]
(when-let [resource (io/resource r)]
(read-env-reader resource)))

(defn- warn-on-overwrite [ms]
(doseq [[k kvs] (group-by key (apply concat ms))
:let [vs (map val kvs)]
Expand All @@ -51,6 +60,6 @@
env
(merge-env
(read-env-file ".lein-env")
(read-env-file (io/resource ".boot-env"))
(read-env-resource ".boot-env")
(read-system-env)
(read-system-props)))
1 change: 1 addition & 0 deletions environ/test-resources/.boot-env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:boot "hobnail"}
7 changes: 6 additions & 1 deletion environ/test/environ/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@
(spit ".lein-env" (prn-str {:foo 1 :bar :baz}))
(let [env (refresh-env)]
(is (= (:foo env) "1"))
(is (= (:bar env) ":baz")))))
(is (= (:bar env) ":baz"))))
(testing "boot-env file in classpath (from test-resources)"
(spit ".boot-env" (prn-str {:boot-env "1"}))
(let [env (refresh-env)]
(is (= (:boot env) "hobnail"))))
)

0 comments on commit 8167510

Please sign in to comment.