From ecf55c5601fb5768e41a7a144f9f8808205ec9c8 Mon Sep 17 00:00:00 2001 From: Dan Dillinger Date: Tue, 22 Nov 2016 10:09:42 -0500 Subject: [PATCH 1/2] Unit test: missing envvar returns configured default. --- test/carica/test/middleware.clj | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/test/carica/test/middleware.clj b/test/carica/test/middleware.clj index 13f74e7..4d36420 100644 --- a/test/carica/test/middleware.clj +++ b/test/carica/test/middleware.clj @@ -82,8 +82,8 @@ (is (nil? (env-config :extra)))))))) (deftest test-env-substitute-config - (with-redefs [getenv (constantly "Now.")] - (testing "the envvar value is in the location" + (testing "the envvar value is in the location" + (with-redefs [getenv (constantly "Now.")] (let [env-config (configurer (resources "config.clj") [(env-substitute-config "NOOP" :magic-word) @@ -92,4 +92,11 @@ (is (= "Now." (env-config :magic-word)) "Should see our overridden value.") (is (= "Now." (env-config :i-totally :dont-exist)) - "Nested key paths should work, even if they aren't defined."))))) + "Nested key paths should work, even if they aren't defined.")))) + (testing "a missing envvar returns the configured default" + (with-redefs [getenv (constantly nil)] + (let [env-config (configurer + (resources "config.clj") + [(env-substitute-config "NOOP" :magic-word)])] + (is (= "mellon" (env-config :magic-word)) + "Should see our configured default value."))))) From 95a391c2fef64c444598338b81e4a5c4df32079d Mon Sep 17 00:00:00 2001 From: Dan Dillinger Date: Tue, 22 Nov 2016 10:10:07 -0500 Subject: [PATCH 2/2] Missing envvar returns configured default. If the envvar is unset, System/getenv returns a nil, which was being assoc'd in place. So you couldn't actually have a default in config, that you *optionally* override with an envvar. It'd just always be nil. This changes that so that if the envvar is unset, it won't assoc to the location; if a key did exist there it will now return. --- src/carica/middleware.clj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/carica/middleware.clj b/src/carica/middleware.clj index 36933d4..8b552ac 100644 --- a/src/carica/middleware.clj +++ b/src/carica/middleware.clj @@ -150,4 +150,6 @@ (fn [f] (fn [resources] (let [cfg-map (f resources)] - (assoc-in cfg-map keyseq env-val)))))) + (if-not (nil? env-val) + (assoc-in cfg-map keyseq env-val) + cfg-map))))))