2018-03-10 11:54:51 +00:00
|
|
|
"use strict";
|
|
|
|
|
2018-06-15 20:31:06 +00:00
|
|
|
const log = require("../../src/log");
|
2018-03-10 11:54:51 +00:00
|
|
|
const expect = require("chai").expect;
|
2019-03-20 10:58:30 +00:00
|
|
|
const stub = require("sinon").stub;
|
2018-03-10 11:54:51 +00:00
|
|
|
const mergeConfig = require("../../src/helper").mergeConfig;
|
2019-07-08 11:12:31 +00:00
|
|
|
const TestUtil = require("../util");
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
describe("mergeConfig", function () {
|
|
|
|
it("should mutate object", function () {
|
2018-03-10 11:54:51 +00:00
|
|
|
const config = {
|
|
|
|
ip: "default",
|
|
|
|
};
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(config, {
|
|
|
|
ip: "overridden",
|
|
|
|
})
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
ip: "overridden",
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(config).to.deep.equal({
|
|
|
|
ip: "overridden",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should merge new properties", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
ip: "default",
|
|
|
|
newProp: "this should appear too",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ip: "overridden",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
ip: "overridden",
|
|
|
|
newProp: "this should appear too",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should extend objects", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
tlsOptions: {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tlsOptions: {
|
|
|
|
user: "test",
|
|
|
|
thing: 123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
tlsOptions: {
|
|
|
|
user: "test",
|
|
|
|
thing: 123,
|
|
|
|
},
|
|
|
|
});
|
2019-07-08 11:12:31 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should warn for unknown top level keys", function () {
|
2019-07-08 11:12:31 +00:00
|
|
|
let warning = "";
|
2019-07-17 09:33:59 +00:00
|
|
|
stub(log, "warn").callsFake(TestUtil.sanitizeLog((str) => (warning += str)));
|
2019-07-08 11:12:31 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
optionOne: 123,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
optionOne: 456,
|
|
|
|
optionTwo: 789,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
optionOne: 456,
|
|
|
|
optionTwo: 789,
|
|
|
|
});
|
2019-03-20 10:58:30 +00:00
|
|
|
|
|
|
|
log.warn.restore();
|
2019-07-08 11:12:31 +00:00
|
|
|
expect(warning).to.equal('Unknown key "optionTwo", please verify your config.\n');
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should not warn for unknown second level keys", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
optionOne: {
|
|
|
|
subOne: 123,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
optionOne: {
|
|
|
|
subOne: 123,
|
|
|
|
subTwo: 123,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
optionOne: {
|
|
|
|
subOne: 123,
|
|
|
|
subTwo: 123,
|
|
|
|
},
|
|
|
|
});
|
2018-03-10 11:54:51 +00:00
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should allow changing nulls", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
oidentd: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
oidentd: "some path",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
oidentd: "some path",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should allow changing nulls with objects", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
webirc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webirc: {
|
|
|
|
serverone: "password",
|
|
|
|
servertwo: "password2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
webirc: {
|
|
|
|
serverone: "password",
|
|
|
|
servertwo: "password2",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should allow changing nulls with objects that has function", function () {
|
2019-07-08 11:12:31 +00:00
|
|
|
const callbackFunction = () => ({});
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
webirc: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
webirc: {
|
|
|
|
servercb: callbackFunction,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2019-07-08 11:12:31 +00:00
|
|
|
webirc: {
|
|
|
|
servercb: callbackFunction,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should keep new properties inside of objects", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
nestedOnce: {
|
|
|
|
ip: "default",
|
|
|
|
},
|
|
|
|
nestedTwice: {
|
|
|
|
thing: "default",
|
|
|
|
nested: {
|
|
|
|
otherThing: "also default",
|
|
|
|
newThing: "but also this",
|
|
|
|
},
|
|
|
|
},
|
2018-03-10 11:54:51 +00:00
|
|
|
},
|
2019-07-17 09:33:59 +00:00
|
|
|
{
|
|
|
|
nestedOnce: {},
|
|
|
|
nestedTwice: {
|
|
|
|
nested: {
|
|
|
|
otherThing: "overridden",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
nestedOnce: {
|
|
|
|
ip: "default",
|
|
|
|
},
|
|
|
|
nestedTwice: {
|
|
|
|
thing: "default",
|
|
|
|
nested: {
|
|
|
|
otherThing: "overridden",
|
|
|
|
newThing: "but also this",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should not merge arrays", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
test: ["sqlite", "text"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: ["sqlite"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
test: ["sqlite"],
|
|
|
|
});
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
test: ["sqlite", "text"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: [],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
test: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should change order in arrays", function () {
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
test: ["sqlite", "text"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: ["text", "sqlite"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
test: ["text", "sqlite"],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-21 20:55:36 +00:00
|
|
|
it("should only merge same type", function () {
|
2020-01-18 23:14:52 +00:00
|
|
|
stub(log, "warn");
|
2018-03-10 11:54:51 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
shouldBeObject: {
|
|
|
|
thing: "yes",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
shouldBeObject: "bad type",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
shouldBeObject: {
|
|
|
|
thing: "yes",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
expect(
|
|
|
|
mergeConfig(
|
|
|
|
{
|
|
|
|
shouldBeString: "string",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
shouldBeString: 1234567,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
).to.deep.equal({
|
2018-03-10 11:54:51 +00:00
|
|
|
shouldBeString: "string",
|
|
|
|
});
|
|
|
|
|
2020-01-18 23:14:52 +00:00
|
|
|
log.warn.restore();
|
2018-03-10 11:54:51 +00:00
|
|
|
});
|
|
|
|
});
|