/
configuration-test.js
103 lines (88 loc) · 2.45 KB
/
configuration-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
const { test } = require("tap");
const sandbox = require("@log4js-node/sandboxed-module");
const realFS = require("fs");
const modulePath = "some/path/to/mylog4js.json";
const pathsChecked = [];
let fakeFS = {};
let dependencies;
let fileRead;
test("log4js configure", batch => {
batch.beforeEach(done => {
fileRead = 0;
fakeFS = {
realpath: () => {}, // fs-extra looks for this
ReadStream: realFS.ReadStream, // need to define these, because graceful-fs uses them
WriteStream: realFS.WriteStream,
read: realFS.read,
closeSync: () => {},
config: {
appenders: {
console: {
type: "console",
layout: { type: "messagePassThrough" }
}
},
categories: {
default: {
appenders: ["console"],
level: "INFO"
}
}
},
readdirSync: dir => require("fs").readdirSync(dir),
readFileSync: (file, encoding) => {
fileRead += 1;
batch.type(file, "string");
batch.equal(file, modulePath);
batch.equal(encoding, "utf8");
return JSON.stringify(fakeFS.config);
},
statSync: path => {
pathsChecked.push(path);
if (path === modulePath) {
return { mtime: new Date() };
}
throw new Error("no such file");
}
};
dependencies = {
requires: {
fs: fakeFS
}
};
done();
});
batch.test(
"when configuration file loaded via LOG4JS_CONFIG env variable",
t => {
process.env.LOG4JS_CONFIG = "some/path/to/mylog4js.json";
const log4js = sandbox.require("../../lib/log4js", dependencies);
log4js.getLogger("test-logger");
t.equal(fileRead, 1, "should load the specified local config file");
delete process.env.LOG4JS_CONFIG;
t.end();
}
);
batch.test(
"when configuration is set via configure() method call, return the log4js object",
t => {
const log4js = sandbox
.require("../../lib/log4js", dependencies)
.configure(fakeFS.config);
t.type(
log4js,
"object",
"Configure method call should return the log4js object!"
);
const log = log4js.getLogger("daemon");
t.type(
log,
"object",
"log4js object, returned by configure(...) method should be able to create log object."
);
t.type(log.info, "function");
t.end();
}
);
batch.end();
});