forked from log4js-node/log4js-node
/
configuration-validation-test.js
444 lines (398 loc) · 12.4 KB
/
configuration-validation-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
const { test } = require("tap");
const { unlinkSync } = require("fs");
const util = require("util");
const path = require("path");
const sandbox = require("@log4js-node/sandboxed-module");
const debug = require("debug")("log4js:test.configuration-validation");
const deepFreeze = require("deep-freeze");
const log4js = require("../../lib/log4js");
const configuration = require("../../lib/configuration");
const testAppender = (label, result) => ({
configure(config, layouts, findAppender) {
debug(
`testAppender(${label}).configure called, with config: ${util.inspect(
config
)}`
);
result.configureCalled = true;
result.type = config.type;
result.label = label;
result.config = config;
result.layouts = layouts;
result.findAppender = findAppender;
return {};
}
});
test("log4js configuration validation", batch => {
batch.test("should give error if config is just plain silly", t => {
[null, undefined, "", " ", []].forEach(config => {
const expectedError = new Error(
`Problem with log4js configuration: (${util.inspect(
config
)}) - must be an object.`
);
t.throws(() => configuration.configure(config), expectedError);
});
t.end();
});
batch.test("should give error if config is an empty object", t => {
t.throws(
() => log4js.configure({}),
'- must have a property "appenders" of type object.'
);
t.end();
});
batch.test("should give error if config has no appenders", t => {
t.throws(
() => log4js.configure({ categories: {} }),
'- must have a property "appenders" of type object.'
);
t.end();
});
batch.test("should give error if config has no categories", t => {
t.throws(
() => log4js.configure({ appenders: { out: { type: "stdout" } } }),
'- must have a property "categories" of type object.'
);
t.end();
});
batch.test("should give error if appenders is not an object", t => {
t.throws(
() => log4js.configure({ appenders: [], categories: [] }),
'- must have a property "appenders" of type object.'
);
t.end();
});
batch.test("should give error if appenders are not all valid", t => {
t.throws(
() =>
log4js.configure({ appenders: { thing: "cheese" }, categories: {} }),
'- appender "thing" is not valid (must be an object with property "type")'
);
t.end();
});
batch.test("should require at least one appender", t => {
t.throws(
() => log4js.configure({ appenders: {}, categories: {} }),
"- must define at least one appender."
);
t.end();
});
batch.test("should give error if categories are not all valid", t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: { thing: "cheese" }
}),
'- category "thing" is not valid (must be an object with properties "appenders" and "level")'
);
t.end();
});
batch.test("should give error if default category not defined", t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: { thing: { appenders: ["stdout"], level: "ERROR" } }
}),
'- must define a "default" category.'
);
t.end();
});
batch.test("should require at least one category", t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: {}
}),
"- must define at least one category."
);
t.end();
});
batch.test("should give error if category.appenders is not an array", t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: { thing: { appenders: {}, level: "ERROR" } }
}),
'- category "thing" is not valid (appenders must be an array of appender names)'
);
t.end();
});
batch.test("should give error if category.appenders is empty", t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: { thing: { appenders: [], level: "ERROR" } }
}),
'- category "thing" is not valid (appenders must contain at least one appender name)'
);
t.end();
});
batch.test(
"should give error if categories do not refer to valid appenders",
t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: { thing: { appenders: ["cheese"], level: "ERROR" } }
}),
'- category "thing" is not valid (appender "cheese" is not defined)'
);
t.end();
}
);
batch.test("should give error if category level is not valid", t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: { default: { appenders: ["stdout"], level: "Biscuits" } }
}),
'- category "default" is not valid (level "Biscuits" not recognised; valid levels are ALL, TRACE'
);
t.end();
});
batch.test(
"should give error if category enableCallStack is not valid",
t => {
t.throws(
() =>
log4js.configure({
appenders: { stdout: { type: "stdout" } },
categories: {
default: {
appenders: ["stdout"],
level: "Debug",
enableCallStack: "123"
}
}
}),
'- category "default" is not valid (enableCallStack must be boolean type)'
);
t.end();
}
);
batch.test("should give error if appender type cannot be found", t => {
t.throws(
() =>
log4js.configure({
appenders: { thing: { type: "cheese" } },
categories: { default: { appenders: ["thing"], level: "ERROR" } }
}),
'- appender "thing" is not valid (type "cheese" could not be found)'
);
t.end();
});
batch.test("should create appender instances", t => {
const thing = {};
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
requires: {
cheese: testAppender("cheesy", thing)
},
ignoreMissing: true
});
sandboxedLog4js.configure({
appenders: { thing: { type: "cheese" } },
categories: { default: { appenders: ["thing"], level: "ERROR" } }
});
t.ok(thing.configureCalled);
t.equal(thing.type, "cheese");
t.end();
});
batch.test(
"should use provided appender instance if instance provided",
t => {
const thing = {};
const cheese = testAppender("cheesy", thing);
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
ignoreMissing: true
});
sandboxedLog4js.configure({
appenders: { thing: { type: cheese } },
categories: { default: { appenders: ["thing"], level: "ERROR" } }
});
t.ok(thing.configureCalled);
t.same(thing.type, cheese);
t.end();
}
);
batch.test("should not throw error if configure object is freezed", t => {
const filename = "test/tap/freeze-date-file-test"
t.tearDown(() => {
try {
unlinkSync(filename);
} catch (_) {
// doesn't really matter if it failed
}
});
t.doesNotThrow(() =>
log4js.configure(
deepFreeze({
appenders: {
dateFile: {
filename,
type: "dateFile",
alwaysIncludePattern: false
}
},
categories: {
default: { appenders: ["dateFile"], level: log4js.levels.ERROR }
}
})
)
);
t.end();
});
batch.test("should load appenders from core first", t => {
const result = {};
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
requires: {
"./cheese": testAppender("correct", result),
cheese: testAppender("wrong", result)
},
ignoreMissing: true
});
sandboxedLog4js.configure({
appenders: { thing: { type: "cheese" } },
categories: { default: { appenders: ["thing"], level: "ERROR" } }
});
t.ok(result.configureCalled);
t.equal(result.type, "cheese");
t.equal(result.label, "correct");
t.end();
});
batch.test(
"should load appenders relative to main file if not in core, or node_modules",
t => {
const result = {};
const mainPath = path.dirname(require.main.filename);
const sandboxConfig = {
ignoreMissing: true,
requires: {}
};
sandboxConfig.requires[`${mainPath}/cheese`] = testAppender(
"correct",
result
);
// add this one, because when we're running coverage the main path is a bit different
sandboxConfig.requires[
`${path.join(mainPath, "../../node_modules/nyc/bin/cheese")}`
] = testAppender("correct", result);
// in node v6, there's an extra layer of node modules for some reason, so add this one to work around it
sandboxConfig.requires[
`${path.join(
mainPath,
"../../node_modules/tap/node_modules/nyc/bin/cheese"
)}`
] = testAppender("correct", result);
const sandboxedLog4js = sandbox.require(
"../../lib/log4js",
sandboxConfig
);
sandboxedLog4js.configure({
appenders: { thing: { type: "cheese" } },
categories: { default: { appenders: ["thing"], level: "ERROR" } }
});
t.ok(result.configureCalled);
t.equal(result.type, "cheese");
t.equal(result.label, "correct");
t.end();
}
);
batch.test(
"should load appenders relative to process.cwd if not found in core, node_modules",
t => {
const result = {};
const fakeProcess = new Proxy(process, {
get(target, key) {
if (key === "cwd") {
return () => "/var/lib/cheese";
}
return target[key];
}
});
// windows file paths are different to unix, so let's make this work for both.
const requires = {};
requires[path.join("/var", "lib", "cheese", "cheese")] = testAppender("correct", result);
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
ignoreMissing: true,
requires,
globals: {
process: fakeProcess
}
});
sandboxedLog4js.configure({
appenders: { thing: { type: "cheese" } },
categories: { default: { appenders: ["thing"], level: "ERROR" } }
});
t.ok(result.configureCalled);
t.equal(result.type, "cheese");
t.equal(result.label, "correct");
t.end();
}
);
batch.test("should pass config, layout, findAppender to appenders", t => {
const result = {};
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
ignoreMissing: true,
requires: {
cheese: testAppender("cheesy", result),
notCheese: testAppender("notCheesy", {})
}
});
sandboxedLog4js.configure({
appenders: {
thing: { type: "cheese", foo: "bar" },
thing2: { type: "notCheese" }
},
categories: { default: { appenders: ["thing"], level: "ERROR" } }
});
t.ok(result.configureCalled);
t.equal(result.type, "cheese");
t.equal(result.config.foo, "bar");
t.type(result.layouts, "object");
t.type(result.layouts.basicLayout, "function");
t.type(result.findAppender, "function");
t.type(result.findAppender("thing2"), "object");
t.end();
});
batch.test(
"should not give error if level object is used instead of string",
t => {
t.doesNotThrow(() =>
log4js.configure({
appenders: { thing: { type: "stdout" } },
categories: {
default: { appenders: ["thing"], level: log4js.levels.ERROR }
}
})
);
t.end();
}
);
batch.test("should not create appender instance if not used in categories", t => {
const used = {};
const notUsed = {};
const sandboxedLog4js = sandbox.require("../../lib/log4js", {
requires: {
cat: testAppender("meow", used),
dog: testAppender("woof", notUsed)
},
ignoreMissing: true
});
sandboxedLog4js.configure({
appenders: { used: { type: "cat" }, notUsed: { type: "dog" } },
categories: { default: { appenders: ["used"], level: "ERROR" } }
});
t.ok(used.configureCalled);
t.notOk(notUsed.configureCalled);
t.end();
});
batch.end();
});