Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX #460 - Strict mode warning refer to appropriate env variable #576

Merged
merged 1 commit into from Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/config.js
Expand Up @@ -15,7 +15,7 @@ var deferConfig = require('../defer').deferConfig,
// Static members
var DEFAULT_CLONE_DEPTH = 20,
NODE_CONFIG, CONFIG_DIR, RUNTIME_JSON_FILENAME, NODE_ENV, APP_INSTANCE,
HOST, HOSTNAME, ALLOW_CONFIG_MUTATIONS, CONFIG_SKIP_GITCRYPT,
HOST, HOSTNAME, ALLOW_CONFIG_MUTATIONS, CONFIG_SKIP_GITCRYPT, NODE_ENV_VAR_NAME,
NODE_CONFIG_PARSER,
env = {},
privateUtil = {},
Expand Down Expand Up @@ -510,11 +510,26 @@ util.loadFileConfigs = function(configDir) {
var t = this,
config = {};

// Initialize parameters from command line, environment, or default
NODE_ENV = util.initParam('NODE_ENV', 'development');
// Specify variables that can be used to define the environment
var node_env_var_names = ['NODE_CONFIG_ENV', 'NODE_ENV'];

// Override, NODE_ENV if NODE_CONFIG_ENV is specified.
NODE_ENV = util.initParam('NODE_CONFIG_ENV', NODE_ENV);
// Loop through the variables to try and set environment
for (node_env_var_name of node_env_var_names) {
NODE_ENV = util.initParam(node_env_var_name);
if (!!NODE_ENV) {
NODE_ENV_VAR_NAME = node_env_var_name;
break;
}
}

// If we haven't successfully set the environment using the variables, we'll default it
if (!NODE_ENV) {
NODE_ENV = 'development';
}

node_env_var_names.forEach(node_env_var_name => {
env[node_env_var_name] = NODE_ENV;
});

// Split files name, for loading multiple files.
NODE_ENV = NODE_ENV.split(',');
Expand Down Expand Up @@ -1395,11 +1410,11 @@ util.runStrictnessChecks = function (config) {
});
// development is special-cased because it's the default value
if (env && (env !== 'development') && !anyFilesMatchEnv) {
_warnOrThrow("NODE_ENV value of '"+env+"' did not match any deployment config file names.");
_warnOrThrow(NODE_ENV_VAR_NAME+" value of '"+env+"' did not match any deployment config file names.");
}
// Throw if NODE_ENV matches' default' or 'local'
if ((env === 'default') || (env === 'local')) {
_warnOrThrow("NODE_ENV value of '"+env+"' is ambiguous.");
_warnOrThrow(NODE_ENV_VAR_NAME+" value of '"+env+"' is ambiguous.");
}
});

Expand Down
26 changes: 24 additions & 2 deletions test/6-strict-mode.js
Expand Up @@ -54,6 +54,20 @@ vows.describe('Tests for strict mode').addBatch({
exceptionMessage :"FATAL: NODE_ENV value of 'local' is ambiguous. "
+"See https://github.com/lorenwest/node-config/wiki/Strict-Mode",
}),

"Specifying reserved word for NODE_CONFIG_ENV throws reserved word exception with appropriate wording": _expectException({
NODE_CONFIG_ENV : 'local',
APP_INSTANCE : 'valid-instance',
exceptionMessage :"FATAL: NODE_CONFIG_ENV value of 'local' is ambiguous. "
+"See https://github.com/lorenwest/node-config/wiki/Strict-Mode",
}),

"Specifying NODE_CONFIG_ENV=production,cloud with no cloud file throws an exception with appropriate wording": _expectException({
NODE_CONFIG_ENV : 'cloud',
APP_INSTANCE : 'valid-instance',
exceptionMessage :"FATAL: NODE_CONFIG_ENV value of 'cloud' did not match any deployment config file names. "
+"See https://github.com/lorenwest/node-config/wiki/Strict-Mode",
}),
})
.export(module);

Expand All @@ -67,9 +81,17 @@ function _expectException (opts) {
process.env.NODE_CONFIG_DIR = __dirname + '/6-config';
process.env.NODE_CONFIG_STRICT_MODE = 1;
process.env.NODE_APP_INSTANCE = opts.APP_INSTANCE;
process.env.NODE_ENV = opts.NODE_ENV;

if (!!opts.NODE_ENV) {
process.env.NODE_ENV = opts.NODE_ENV;
}

if (!!opts.NODE_CONFIG_ENV) {
process.env.NODE_CONFIG_ENV = opts.NODE_CONFIG_ENV;
}

delete process.env.NODE_CONFIG;
try {
try {
var config = requireUncached(__dirname + '/../lib/config');
}
catch (e) {
Expand Down