Permalink
Browse files

Added optional flags (default off) to throw Errors on missing env files.

  • Loading branch information...
1 parent 8837f40 commit 2c7a3792c2ccbbeaa1185f929790869c1413bdfc @coreyjewett committed Oct 25, 2011
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
@@ -0,0 +1,6 @@
+{
+ "a": 100
+ , "b": {
+ "c": 120
+ }
+}
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
@@ -0,0 +1,7 @@
+{
+ "a": 10
+ , "b": {
+ "c": 12
+ , "d": 13
+ }
+}
View
@@ -3,12 +3,17 @@ var path = require('path')
, merge = require('./merge');
-module.exports = function(basePath) {
+module.exports = function(basePath, alwaysRequireEnv) {
basePath = path.normalize(basePath);
domains = {};
- return function(domain) {
+ return function(domain, requireEnv) {
var config;
-
+
+ // default to global expectation unless specifically overridden.
+ if (requireEnv !== false && alwaysRequireEnv === true) {
+ requireEnv = true;
+ }
+
if (domains.hasOwnProperty(domain)) {
return domains[domain];
}
@@ -17,7 +22,7 @@ module.exports = function(basePath) {
config = merge(
load(path.join(basePath, domain + '.json'), domain, true)
- , load(path.join(basePath, domain + '.' + env + '.json'), domain, false)
+ , load(path.join(basePath, domain + '.' + env + '.json'), domain + "." + env, !!(requireEnv === true))
);
domains[domain] = config;
View
@@ -15,9 +15,93 @@ test("non-existing base configuration file throws error", function(t) {
var config = konphyg('test2');
} catch(err) {
threw = true
- t.equal(err.message, 'Could not find configuration file for test2 domain')
+ t.ok(!!(/^Could not find configuration file for test2 domain/.test(err.message)), "Error message: " + err.message);
}
t.ok(threw, "should throw when base file is not found");
t.end();
-});
+});
+
+test("non-existing env configuration file doesn't throw error if requireEnv not specified", function(t) {
+ var didnt_throw = true;
+
+ try {
+ var config = konphyg('test3', false);
+ } catch(err) {
+ didnt_throw = false;
+ }
+
+ t.ok(didnt_throw, "should not throw, requireEnv not set.");
+ t.end();
+});
+
+test("non-existing env configuration file doesn't throw error if requireEnv !== true", function(t) {
+ var didnt_throw = true;
+
+ try {
+ var config = konphyg('test4', false);
+ } catch(err) {
+ didnt_throw = false;
+ }
+
+ t.ok(didnt_throw, "should not throw, requireEnv not set.");
+ t.end();
+});
+
+test("non-existing env configuration file throws error if requireEnv", function(t) {
+ var threw = false;
+
+ try {
+ var config = konphyg('test5', true);
+ } catch(err) {
+ threw = true
+ t.ok(!!(/^Could not find configuration file for test5.development domain/.test(err.message)), "Error message: " + err.message);
+ }
+
+ t.ok(threw, "should throw when env file is not found");
+ t.end();
+});
+
+test("non-existing env configuration file doesn't throw error if alwaysRequireEnv === false", function(t) {
+ var my_konphyg = require('../.')(__dirname + '/../assets/config', false);
+ var didnt_throw = true;
+
+ try {
+ var config = my_konphyg('test6', false);
+ } catch(err) {
+ didnt_throw = false;
+ }
+
+ t.ok(didnt_throw, "should not throw, alwaysRequireEnv not set.");
+ t.end();
+});
+
+test("non-existing env configuration file throws error if alwaysRequireEnv === true", function(t) {
+ var my_konphyg = require('../.')(__dirname + '/../assets/config', true);
+ var threw = false;
+
+ try {
+ var config = my_konphyg('test7');
+ } catch(err) {
+ threw = true
+ t.ok(!!(/^Could not find configuration file for test7.development domain/.test(err.message)), "Error message: " + err.message);
+ }
+
+ t.ok(threw, "should throw when env file is not found (alwaysRequireEnv)");
+ t.end();
+});
+
+test("non-existing env configuration file doesn't throw error if alwaysRequireEnv === true && requireEnv === false", function(t) {
+ var my_konphyg = require('../.')(__dirname + '/../assets/config', true);
+ var didnt_throw = true;
+
+ try {
+ var config = my_konphyg('test8', false);
+ } catch(err) {
+ didnt_throw = false;
+ }
+
+ t.ok(didnt_throw, "should not throw, requireEnv === false.");
+ t.end();
+});
+

0 comments on commit 2c7a379

Please sign in to comment.