Merge pull request #3286 from thelounge/xpaw/webirc-warn

Only warn for top-level unknown config keys
This commit is contained in:
Pavel Djundik 2019-07-08 16:34:17 +03:00 committed by GitHub
commit 2ff8d09a44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 6 deletions

View File

@ -238,11 +238,13 @@ function getDefaultNick() {
} }
function mergeConfig(oldConfig, newConfig) { function mergeConfig(oldConfig, newConfig) {
return _.mergeWith(oldConfig, newConfig, (objValue, srcValue, key, object) => { for (const key in newConfig) {
if (!Object.prototype.hasOwnProperty.call(object, key)) { if (!Object.prototype.hasOwnProperty.call(oldConfig, key)) {
log.warn(`Unknown key "${colors.bold(key)}", please verify your config.`); 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) // 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) { if (typeof objValue !== "undefined" && objValue !== null && typeof objValue !== typeof srcValue) {
log.warn(`Incorrect type for "${colors.bold(key)}", please verify your config.`); 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 expect = require("chai").expect;
const stub = require("sinon").stub; const stub = require("sinon").stub;
const mergeConfig = require("../../src/helper").mergeConfig; const mergeConfig = require("../../src/helper").mergeConfig;
const TestUtil = require("../util");
describe("mergeConfig", function() { describe("mergeConfig", function() {
it("should mutate object", function() { it("should mutate object", function() {
@ -35,9 +36,6 @@ describe("mergeConfig", function() {
}); });
it("should extend objects", function() { it("should extend objects", function() {
let warning = "";
stub(log, "warn").callsFake((str) => warning += str);
expect(mergeConfig({ expect(mergeConfig({
tlsOptions: {}, tlsOptions: {},
}, { }, {
@ -51,9 +49,42 @@ describe("mergeConfig", function() {
thing: 123, 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(); 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() { 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() { it("should keep new properties inside of objects", function() {
expect(mergeConfig({ expect(mergeConfig({
nestedOnce: { nestedOnce: {