Only warn for top-level unknown config keys

This commit is contained in:
Pavel Djundik 2019-07-08 14:12:31 +03:00
parent 85ffaa1ec7
commit 48ae5a4cdd
2 changed files with 71 additions and 6 deletions

View File

@ -238,11 +238,13 @@ function getDefaultNick() {
}
function mergeConfig(oldConfig, newConfig) {
return _.mergeWith(oldConfig, newConfig, (objValue, srcValue, key, object) => {
if (!Object.prototype.hasOwnProperty.call(object, key)) {
for (const key in newConfig) {
if (!Object.prototype.hasOwnProperty.call(oldConfig, key)) {
log.warn(`Unknown key "${colors.bold(key)}", please verify your config.`);
}
}
return _.mergeWith(oldConfig, newConfig, (objValue, srcValue, key) => {
// Do not override config variables if the type is incorrect (e.g. object changed into a string)
if (typeof objValue !== "undefined" && objValue !== null && typeof objValue !== typeof srcValue) {
log.warn(`Incorrect type for "${colors.bold(key)}", please verify your config.`);

View File

@ -4,6 +4,7 @@ const log = require("../../src/log");
const expect = require("chai").expect;
const stub = require("sinon").stub;
const mergeConfig = require("../../src/helper").mergeConfig;
const TestUtil = require("../util");
describe("mergeConfig", function() {
it("should mutate object", function() {
@ -35,9 +36,6 @@ describe("mergeConfig", function() {
});
it("should extend objects", function() {
let warning = "";
stub(log, "warn").callsFake((str) => warning += str);
expect(mergeConfig({
tlsOptions: {},
}, {
@ -51,9 +49,42 @@ describe("mergeConfig", function() {
thing: 123,
},
});
});
it("should warn for unknown top level keys", function() {
let warning = "";
stub(log, "warn").callsFake(TestUtil.sanitizeLog((str) => warning += str));
expect(mergeConfig({
optionOne: 123,
}, {
optionOne: 456,
optionTwo: 789,
})).to.deep.equal({
optionOne: 456,
optionTwo: 789,
});
log.warn.restore();
expect(warning).to.contain("Unknown key");
expect(warning).to.equal('Unknown key "optionTwo", please verify your config.\n');
});
it("should not warn for unknown second level keys", function() {
expect(mergeConfig({
optionOne: {
subOne: 123,
},
}, {
optionOne: {
subOne: 123,
subTwo: 123,
},
})).to.deep.equal({
optionOne: {
subOne: 123,
subTwo: 123,
},
});
});
it("should allow changing nulls", function() {
@ -66,6 +97,38 @@ describe("mergeConfig", function() {
});
});
it("should allow changing nulls with objects", function() {
expect(mergeConfig({
webirc: null,
}, {
webirc: {
serverone: "password",
servertwo: "password2",
},
})).to.deep.equal({
webirc: {
serverone: "password",
servertwo: "password2",
},
});
});
it("should allow changing nulls with objects that has function", function() {
const callbackFunction = () => ({});
expect(mergeConfig({
webirc: null,
}, {
webirc: {
servercb: callbackFunction,
},
})).to.deep.equal({
webirc: {
servercb: callbackFunction,
},
});
});
it("should keep new properties inside of objects", function() {
expect(mergeConfig({
nestedOnce: {