From 614bd38ba1e9d5ea2ffd9145d39b0c94ff49cb4d Mon Sep 17 00:00:00 2001 From: Matthew Darling Date: Thu, 31 Mar 2016 11:17:28 -0400 Subject: [PATCH 1/3] Add test for support of missing files --- test/immuconf/config_test.clj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/immuconf/config_test.clj b/test/immuconf/config_test.clj index 27b3130..95aadff 100644 --- a/test/immuconf/config_test.clj +++ b/test/immuconf/config_test.clj @@ -51,6 +51,8 @@ (System/setProperty "user.home" (str (System/getProperty "user.dir") "/test")) (is (cfg/load "~/fixtures/test-a.edn"))) +(deftest missing-files-are-allowed + (is (= {} (cfg/load "test/fixtures/this-file-does-not-exist.edn")))) From 6e212726d7332f1c7cb868a6c1d7b76e3889dc95 Mon Sep 17 00:00:00 2001 From: Matthew Darling Date: Thu, 31 Mar 2016 11:18:52 -0400 Subject: [PATCH 2/3] Return nil for files that don't exist --- src/immuconf/config.clj | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/immuconf/config.clj b/src/immuconf/config.clj index 998150d..79eea31 100644 --- a/src/immuconf/config.clj +++ b/src/immuconf/config.clj @@ -3,6 +3,7 @@ (:require [clojure.walk :as w] [clojure.edn :as edn] [clojure.string :as s] + [clojure.java.io :as java.io] [clojure.tools.logging :as log])) (ns-unmap *ns* 'Override) @@ -75,7 +76,9 @@ given filename and the resulting config " [file] (try - [file (edn/read-string {:readers *data-readers*} (slurp file))] + (let [file-as-file (java.io/as-file file)] + (when (.exists file-as-file) + [file (edn/read-string {:readers *data-readers*} (slurp file-as-file))])) (catch Throwable t (throw (ex-info (format "Error loading config file: %s" file) {:file file} t))))) From 149229b0e648dbd354df748e276dd8bbf0d4e440 Mon Sep 17 00:00:00 2001 From: Matthew Darling Date: Thu, 31 Mar 2016 11:41:53 -0400 Subject: [PATCH 3/3] New test case for mix of existing/missing files Also clarify the behaviour when all requested files are missing, in case it changes after PR discussion. --- test/immuconf/config_test.clj | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/immuconf/config_test.clj b/test/immuconf/config_test.clj index 95aadff..3f3ecdd 100644 --- a/test/immuconf/config_test.clj +++ b/test/immuconf/config_test.clj @@ -52,7 +52,14 @@ (is (cfg/load "~/fixtures/test-a.edn"))) (deftest missing-files-are-allowed - (is (= {} (cfg/load "test/fixtures/this-file-does-not-exist.edn")))) + (is (= (cfg/load "test/fixtures/test-a.edn" + "test/fixtures/this-file-does-not-exist.edn" + "test/fixtures/test-b.edn") + {:a {:b {:c 1 :d 2}}}) + "Missing files are treated as empty maps") + (is (= {} (cfg/load "test/fixtures/this-file-does-not-exist.edn" + "test/fixtures/another-missing-file.edn")) + "If all files are missing, the config is an empty map"))