Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/montoux/gestalt.clj
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,21 @@ in the configuration file your environment keys should be keywords, e.g. `:devel
*env*)

(defn defined?
"Returns true if and only if key k is defined in the configuration map."
[k]
"Returns true iff the sequence of keys ks is defined in the configuration map."
[& ks]
(when (nil? *cfg*) (reset-gestalt!))
(contains? (clojure.core/get *cfg* *env*) k))
(let [sentinel (Object.)
found (get-in *cfg* (cons *env* ks) sentinel)]
(not (identical? sentinel found))))

(defn get
"Returns the configuration value for keys ks."
"Returns the configuration value for the sequence of keys ks."
[& ks]
(when (nil? *cfg*) (reset-gestalt!))
(or (get-in *cfg* (concat [*env*] ks))
(let [sentinel (Object.)
found (get-in *cfg* (cons *env* ks) sentinel)]
(if (identical? sentinel found)
(throw (IllegalArgumentException.
(str "Configuration \"" (vec ks) "\" not found "
"in environment " *env*)))))

(str "Configuration " (vec ks) " not found in environment " *env*)))
found)))

34 changes: 24 additions & 10 deletions test/test/montoux/gestalt.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(:use clojure.test)
(:require [montoux.gestalt :as gestalt]
[clojure.java.io :as jio])
(:import java.io.StringReader))
(:import (java.io File StringReader)))


(deftest test-with-config-with-environment
Expand Down Expand Up @@ -42,9 +42,9 @@
(is (= 1 (gestalt/get :foo)))))

(defn- tmpfile [contents]
(let [f (doto (java.io.File/createTempFile "cfg-test" ".clj") .deleteOnExit)]
(spit f contents)
f))
(doto (File/createTempFile "cfg-test" ".clj")
(.deleteOnExit)
Comment thread
zmthy marked this conversation as resolved.
(spit contents)))

(defmacro with-system-property
"Temporary sets system property k to value v.
Expand Down Expand Up @@ -77,15 +77,29 @@
(jio/delete-file f)))))

(deftest test-contains?
(gestalt/with-config {:test {:foo 42}}
(gestalt/with-config {:test {:foo 42 :a {:b {:c 1}} :vec [0] :falsey false :null nil}}
(gestalt/with-environment :test
(is (not (gestalt/defined? nil)))
(is (not (gestalt/defined? :bar)))
(is (not (gestalt/defined? :vec 1)))
(is (not (gestalt/defined? "foo")))
(is (gestalt/defined? :foo))
(is (not (gestalt/defined? :bar))))))
(is (gestalt/defined? :a :b :c))
(is (gestalt/defined? :vec 0))
(is (gestalt/defined? :falsey))
(is (gestalt/defined? :null))
)))

(deftest test-get?
(gestalt/with-config {:test {:foo 42 :a {:b {:c 1}}}}
(deftest test-get
(gestalt/with-config {:test {:foo 42 :a {:b {:c 1}} :vec [0] :falsey false :null nil}}
(gestalt/with-environment :test
(is (thrown? IllegalArgumentException (gestalt/get :bar)))
(is (thrown? IllegalArgumentException (gestalt/get nil)))
(is (thrown? IllegalArgumentException (gestalt/get :bar)))
(is (thrown? IllegalArgumentException (gestalt/get :vec 1)))
(is (thrown? IllegalArgumentException (gestalt/get "foo")))
(is (= 42 (gestalt/get :foo)))
(is (= 1 (gestalt/get :a :b :c))))))
(is (= 1 (gestalt/get :a :b :c)))
(is (= 0 (gestalt/get :vec 0)))
(is (= false (gestalt/get :falsey)))
(is (= nil (gestalt/get :null)))
)))