Browse Source

Sort the vars in the usage string.

More testing, including when env vars are set.
stephen@centriqhome.com 2 years ago
parent
commit
968f3411c8
5 changed files with 81 additions and 23 deletions
  1. 4
    0
      .env
  2. 7
    2
      project.clj
  3. 2
    10
      src/defenv/core.clj
  4. 25
    0
      test-with-checks.sh
  5. 43
    11
      test/defenv/core_test.clj

+ 4
- 0
.env View File

@@ -0,0 +1,4 @@
1
+FUN=good
2
+APPLES=21
3
+WEIGHT=0.12
4
+EAT=true

+ 7
- 2
project.clj View File

@@ -15,10 +15,15 @@
15 15
 ;     along with defenv.  If not, see <http://www.gnu.org/licenses/>.
16 16
 ;
17 17
 
18
-(defproject coreagile/defenv "0.0.2"
18
+(defproject coreagile/defenv "0.0.3"
19 19
   :description "A simple library for managing environment variables in Clojure"
20 20
   :url "https://github.com/coreagile/defenv"
21 21
   :license {:name "GNU General Public License v3"
22 22
             :url "https://www.gnu.org/licenses/gpl.html"}
23 23
   :min-lein-version "2.0.0"
24
-  :dependencies [[org.clojure/clojure "1.8.0"]])
24
+  :dependencies [[org.clojure/clojure "1.8.0" :upgrade false]]
25
+  :profiles {:dev {:plugins [[com.jakemccrary/lein-test-refresh "0.22.0"]
26
+                             [lein-ancient "0.6.15"]
27
+                             [lein-bikeshed "0.5.1"]
28
+                             [lein-dotenv "1.0.0"]
29
+                             [lein-kibit "0.1.6"]]}})

+ 2
- 10
src/defenv/core.clj View File

@@ -18,7 +18,7 @@
18 18
 (ns defenv.core
19 19
   (:require [clojure.string :as str]))
20 20
 
21
-(def defined-env (atom {}))
21
+(def defined-env (atom (sorted-map)))
22 22
 
23 23
 (defn- env-or [env-key f]
24 24
   (let [val (System/getenv env-key)]
@@ -28,7 +28,7 @@
28 28
 
29 29
 (defn- render-docs [{:keys [e-name doc default]}]
30 30
   (let [e-val (env e-name default)]
31
-    (format "%s = %s%s" e-name (if e-val e-val "*MISSING*")
31
+    (format "%s = %s%s" e-name (or e-val "*MISSING*")
32 32
             (if doc (str " ;; " doc) ""))))
33 33
 
34 34
 (defn make-usage-string
@@ -48,14 +48,6 @@
48 48
   ([env-key] (env-or env-key throw-usage))
49 49
   ([env-key default-value] (env-or env-key #(identity default-value))))
50 50
 
51
-(defn nilsafe-parse-int
52
-  "Given a String, if nonempty, attempt to parse it as an Integer."
53
-  [s] (when s (Integer/parseInt s)))
54
-
55
-(defn nilsafe-parse-double
56
-  "Given a String, if nonempty, attempt to parse it as a Double."
57
-  [s] (when s (Double/parseDouble s)))
58
-
59 51
 (defmacro defenv
60 52
   "Define a binding to an environment variable. Creates a delayed object
61 53
    that, when dereferenced, will load an environment variable of the given

+ 25
- 0
test-with-checks.sh View File

@@ -0,0 +1,25 @@
1
+#!/bin/bash
2
+
3
+# A script for making sure our code stays clean
4
+
5
+check_return_code () {
6
+  if [ $? -ne 0 ]; then
7
+    echo $1
8
+    exit 1
9
+  fi
10
+}
11
+
12
+lein test 
13
+check_return_code "lein test failed"
14
+
15
+lein ancient :all :no-colors
16
+check_return_code "lein ancient failed"
17
+
18
+lein kibit
19
+check_return_code "lein kibit failed"
20
+
21
+lein bikeshed --var-redefs=false
22
+check_return_code "lein bikeshed failed"
23
+
24
+echo "build successful"
25
+exit 0

+ 43
- 11
test/defenv/core_test.clj View File

@@ -20,24 +20,56 @@
20 20
             [defenv.core :refer :all])
21 21
   (:import (clojure.lang ExceptionInfo)))
22 22
 
23
-(defenv log-level "LOG_LEVEL" :doc "Global log level." :tfn keyword
24
-        :default "info")
23
+;;Undefined vars
24
+(defenv log-level "LOG_LEVEL" :doc "Global log level."
25
+        :tfn keyword :default "info")
26
+(defenv should-log "SHOULD_LOG" :doc "Should I log? A boolean."
27
+        :tfn Boolean/parseBoolean :default "false")
28
+
29
+;; Testing we can remove the previous definition from the docstring
25 30
 (defenv thing "THING" :default "go away")
26 31
 (defenv thing "THING")
27
-(defenv amount "AMOUNT" :doc "An amount. A double." :tfn nilsafe-parse-double
28
-        :default "1.5")
32
+
33
+(defenv amount "AMOUNT" :doc "An amount. A double."
34
+        :tfn Double/parseDouble :default "1.5")
35
+(defenv acount "COUNT" :doc "A count. An int."
36
+        :tfn Integer/parseInt :default "5")
29 37
 (defenv another "ANOTHER" :doc "This is really important!")
30 38
 
39
+;;Vars defined in .env
40
+(defenv fun "FUN")
41
+(defenv apples "APPLES" :tfn Integer/parseInt :default "10")
42
+(defenv weight "WEIGHT" :tfn Float/parseFloat)
43
+(defenv eat "EAT" :tfn Boolean/parseBoolean :default "false"
44
+        :doc "Should we eat?")
45
+
31 46
 (def expected-message
32 47
   (str "One or more environment variables is missing:\n"
48
+       "AMOUNT = 1.5 ;; An amount. A double.\n"
49
+       "ANOTHER = *MISSING* ;; This is really important!\n"
50
+       "APPLES = 21\n"
51
+       "COUNT = 5 ;; A count. An int.\n"
52
+       "EAT = true ;; Should we eat?\n"
53
+       "FUN = good\n"
33 54
        "LOG_LEVEL = info ;; Global log level.\n"
55
+       "SHOULD_LOG = false ;; Should I log? A boolean.\n"
34 56
        "THING = *MISSING*\n"
35
-       "AMOUNT = 1.5 ;; An amount. A double.\n"
36
-       "ANOTHER = *MISSING* ;; This is really important!"))
57
+       "WEIGHT = 0.12"))
37 58
 
38 59
 (deftest usage
39
-  (is (= expected-message (make-usage-string)))
40
-  (is (= 1.5 @amount))
41
-  (is (thrown? ExceptionInfo @thing))
42
-  (is (= expected-message
43
-         (try @thing (catch ExceptionInfo e (.getMessage e))))))
60
+  (testing "message construction"
61
+    (is (= expected-message (make-usage-string))))
62
+
63
+  (testing "exception throwing"
64
+    (is (thrown? ExceptionInfo @thing))
65
+    (is (= expected-message
66
+           (try @thing (catch ExceptionInfo e (.getMessage e))))))
67
+
68
+  (testing "value extraction"
69
+    (is (= 1.5 @amount))
70
+    (is (= 5 @acount))
71
+    (is (not @should-log))
72
+    (is (= "good" @fun))
73
+    (is (= 21 @apples))
74
+    (is (= (float 0.12) @weight))
75
+    (is @eat)))