log4js-node/test/tap/configuration-test.js
Lam Wei Li c226cb7569
test: update fakeFS.read as graceful-fs uses it
`fs-extra@10.0.0` broke it as it removed the check for `fs.realpath.native`.

```diff
+L064 exports.realpath.native = u(fs.realpath.native)

-L126
-L127 // fs.realpath.native only available in Node v9.2+
-L128 if (typeof fs.realpath.native === 'function') {
-L129   exports.realpath.native = u(fs.realpath.native)
-L130 }
```
_(https://github.com/jprichardson/node-fs-extra/pull/887/files)_

When `fs.realpath` is an empty function, fs.realpath.native is `undefined`.

25c17ad980/test/tap/configuration-test.js (L17)
2022-01-14 21:13:13 +08:00

104 lines
2.5 KiB
JavaScript

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: realFS.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();
});