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 GH-746 - Loading config files order with APP_INSTANCE #747

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,15 @@ util.loadFileConfigs = function(configDir, options) {
let resolutionIndex = 1;
const extNames = Parser.getFilesOrder();
baseNames.forEach(function(baseName) {
extNames.forEach(function(extName) {
allowedFiles[baseName + '.' + extName] = resolutionIndex++;
if (APP_INSTANCE) {
allowedFiles[baseName + '-' + APP_INSTANCE + '.' + extName] = resolutionIndex++;
}
});
const fileNames = [baseName];
if (APP_INSTANCE) {
fileNames.push(baseName + '-' + APP_INSTANCE);
}
fileNames.forEach(function(fileName) {
extNames.forEach(function(extName) {
allowedFiles[fileName + '.' + extName] = resolutionIndex++;
});
})
});

const locatedFiles = util.locateMatchingFiles(dir, allowedFiles);
Expand Down
4 changes: 4 additions & 0 deletions test/22-config/default-instance.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"prop2": "prop2FromJsonInstance",
"prop3": "prop3FromJsonInstance"
}
1 change: 1 addition & 0 deletions test/22-config/default-instance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
prop3: prop3FromYmlInstance
3 changes: 3 additions & 0 deletions test/22-config/default.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
prop1: prop1FromDefault
prop2: prop2FromDefault
prop3: prop3FromDefault
38 changes: 38 additions & 0 deletions test/22-files-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const requireUncached = require("./_utils/requireUncached");

// Dependencies
const vows = require("vows");
const assert = require("assert");

/**
* <p>Unit tests for the node-config library. To run type:</p>
* <pre>npm test</pre>
* <p>Or, in a project that uses node-config:</p>
* <pre>npm test config</pre>
*
* @class ConfigTest
*/

vows.describe("Test suite for node-config").addBatch({
"Library initialization": {
topic: function () {
// Change the configuration directory for testing
process.env.NODE_CONFIG_DIR = __dirname + "/22-config";

// Hard-code $NODE_ENV=test for testing
process.env.NODE_ENV = "test";

// Test for multi-instance applications
process.env.NODE_APP_INSTANCE = "instance";

CONFIG = requireUncached(__dirname + "/../lib/config");

return CONFIG;
},
"Config files have been loaded in right order": function (CONFIG) {
assert.equal(CONFIG.get("prop1"), "prop1FromDefault");
assert.equal(CONFIG.get("prop2"), "prop2FromJsonInstance");
assert.equal(CONFIG.get("prop3"), "prop3FromYmlInstance");
},
},
}).export(module);