Skip to content

Commit

Permalink
fix: test was failing in unix/mac
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Jones committed Aug 16, 2019
1 parent 5addabd commit fb48f66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 40 additions & 47 deletions test/tap/fileAppender-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require("fs-extra");
const path = require("path");
const sandbox = require("@log4js-node/sandboxed-module");
const zlib = require("zlib");
const util = require('util');
const util = require("util");

const sleep = util.promisify(setTimeout);
const gunzip = util.promisify(zlib.gunzip);
Expand Down Expand Up @@ -43,7 +43,6 @@ test("log4js fileAppender", batch => {
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
);
t.end();

});

batch.test("should flush logs on shutdown", async t => {
Expand All @@ -60,27 +59,25 @@ test("log4js fileAppender", batch => {
logger.info("2");
logger.info("3");

log4js.shutdown(() => {
fs.readFile(testFile, "utf8", (err, fileContents) => {
// 3 lines of output, plus the trailing newline.
t.equal(fileContents.split(EOL).length, 4);
t.match(
fileContents,
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
);
t.end();
});
});
await new Promise(resolve => log4js.shutdown(resolve));
const fileContents = await fs.readFile(testFile, "utf8");
// 3 lines of output, plus the trailing newline.
t.equal(fileContents.split(EOL).length, 4);
t.match(
fileContents,
/\[\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
);
t.end();
});

batch.test("with a max file size and no backups", async t => {
const testFile = path.join(__dirname, "fa-maxFileSize-test.log");
const logger = log4js.getLogger("max-file-size");

t.tearDown(async () => {
await Promise.all([ removeFile(testFile), removeFile(`${testFile}.1`) ]);
await Promise.all([removeFile(testFile), removeFile(`${testFile}.1`)]);
});
await Promise.all([ removeFile(testFile), removeFile(`${testFile}.1`) ]);
await Promise.all([removeFile(testFile), removeFile(`${testFile}.1`)]);

// log file of 100 bytes maximum, no backups
log4js.configure({
Expand Down Expand Up @@ -118,9 +115,9 @@ test("log4js fileAppender", batch => {
const logger = log4js.getLogger("max-file-size-unit");

t.tearDown(async () => {
await Promise.all([ removeFile(testFile), removeFile(`${testFile}.1`) ]);
await Promise.all([removeFile(testFile), removeFile(`${testFile}.1`)]);
});
await Promise.all([ removeFile(testFile), removeFile(`${testFile}.1`) ]);
await Promise.all([removeFile(testFile), removeFile(`${testFile}.1`)]);

// log file of 1K = 1024 bytes maximum, no backups
log4js.configure({
Expand All @@ -137,7 +134,7 @@ test("log4js fileAppender", batch => {
default: { appenders: ["file"], level: "debug" }
}
});
const maxLine = 22; // 1024 max file size / 47 bytes per line
const maxLine = 22; // 1024 max file size / 47 bytes per line
for (let i = 0; i < maxLine; i++) {
logger.info("These are the log messages for the first file."); // 46 bytes per line + '\n'
}
Expand All @@ -163,10 +160,18 @@ test("log4js fileAppender", batch => {
"fa-maxFileSize-with-backups-test.log"
);
const logger = log4js.getLogger("max-file-size-backups");
await Promise.all([ removeFile(testFile), removeFile(`${testFile}.1`), removeFile(`${testFile}.2`) ]);
await Promise.all([
removeFile(testFile),
removeFile(`${testFile}.1`),
removeFile(`${testFile}.2`)
]);

t.tearDown(async () => {
await Promise.all([ removeFile(testFile), removeFile(`${testFile}.1`), removeFile(`${testFile}.2`) ]);
await Promise.all([
removeFile(testFile),
removeFile(`${testFile}.1`),
removeFile(`${testFile}.2`)
]);
});

// log file of 50 bytes maximum, 2 backups
Expand All @@ -191,28 +196,20 @@ test("log4js fileAppender", batch => {
const files = await fs.readdir(__dirname);
const logFiles = files
.sort()
.filter(file =>
file.includes("fa-maxFileSize-with-backups-test.log")
);
.filter(file => file.includes("fa-maxFileSize-with-backups-test.log"));
t.equal(logFiles.length, 3);
t.same(logFiles, [
"fa-maxFileSize-with-backups-test.log",
"fa-maxFileSize-with-backups-test.log.1",
"fa-maxFileSize-with-backups-test.log.2"
]);
let contents = await fs.readFile(
path.join(__dirname, logFiles[0]),
"utf8");
let contents = await fs.readFile(path.join(__dirname, logFiles[0]), "utf8");
t.include(contents, "This is the fourth log message.");
contents = await fs.readFile(
path.join(__dirname, logFiles[1]),
"utf8");
contents = await fs.readFile(path.join(__dirname, logFiles[1]), "utf8");
t.include(contents, "This is the third log message.");
contents = await fs.readFile(
path.join(__dirname, logFiles[2]),
"utf8");
contents = await fs.readFile(path.join(__dirname, logFiles[2]), "utf8");
t.include(contents, "This is the second log message.");

t.end();
});

Expand All @@ -225,14 +222,14 @@ test("log4js fileAppender", batch => {
await Promise.all([
removeFile(testFile),
removeFile(`${testFile}.1.gz`),
removeFile(`${testFile}.2.gz`)
removeFile(`${testFile}.2.gz`)
]);

t.tearDown(async () => {
await Promise.all([
removeFile(testFile),
removeFile(`${testFile}.1.gz`),
removeFile(`${testFile}.2.gz`)
removeFile(`${testFile}.2.gz`)
]);
});

Expand Down Expand Up @@ -267,21 +264,17 @@ test("log4js fileAppender", batch => {
"fa-maxFileSize-with-backups-compressed-test.log.1.gz",
"fa-maxFileSize-with-backups-compressed-test.log.2.gz"
]);
let contents = await fs.readFile(
path.join(__dirname, logFiles[0]),
"utf8");
let contents = await fs.readFile(path.join(__dirname, logFiles[0]), "utf8");
t.include(contents, "This is the fourth log message.");

contents = await gunzip(await fs.readFile(path.join(__dirname, logFiles[1])));
t.include(
contents.toString("utf8"),
"This is the third log message."

contents = await gunzip(
await fs.readFile(path.join(__dirname, logFiles[1]))
);
contents = await gunzip(await fs.readFile(path.join(__dirname, logFiles[2])));
t.include(
contents.toString("utf8"),
"This is the second log message."
t.include(contents.toString("utf8"), "This is the third log message.");
contents = await gunzip(
await fs.readFile(path.join(__dirname, logFiles[2]))
);
t.include(contents.toString("utf8"), "This is the second log message.");
t.end();
});

Expand Down

0 comments on commit fb48f66

Please sign in to comment.