8000 fix(test): moved fileSync test to tap · xcj-coding/log4js-node@2d7cee2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d7cee2

Browse files
author
Gareth Jones
committed
fix(test): moved fileSync test to tap
1 parent 0dec2db commit 2d7cee2

File tree

2 files changed

+162
-197
lines changed

2 files changed

+162
-197
lines changed

test/tap/fileSyncAppender-test.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
'use strict';
2+
3+
const test = require('tap').test;
4+
const fs = require('fs');
5+
const path = require('path');
6+
const log4js = require('../../lib/log4js');
7+
const EOL = require('os').EOL || '\n';
8+
9+
log4js.clearAppenders();
10+
11+
function remove(filename) {
12+
try {
13+
fs.unlinkSync(filename);
14+
} catch (e) {
15+
// doesn't really matter if it failed
16+
}
17+
}
18+
19+
test('log4js fileSyncAppender', (batch) => {
20+
batch.test('with default fileSyncAppender settings', (t) => {
21+
const testFile = path.join(__dirname, '/fa-default-sync-test.log');
22+
const logger = log4js.getLogger('default-settings');
23+
remove(testFile);
24+
25+
log4js.clearAppenders();
26+
log4js.addAppender(
27+
require('../../lib/appenders/fileSync').appender(testFile),
28+
'default-settings'
29+
);
30+
31+
logger.info('This should be in the file.');
32+
33+
fs.readFile(testFile, 'utf8', (err, fileContents) => {
34+
t.include(fileContents, `This should be in the file.${EOL}`);
35+
t.match(
36+
fileContents,
37+
/\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}] \[INFO] default-settings - /
38+
);
39+
t.end();
40+
});
41+
});
42+
43+
batch.test('with a max file size and no backups', (t) => {
44+
const testFile = path.join(__dirname, '/fa-maxFileSize-sync-test.log');
45+
const logger = log4js.getLogger('max-file-size');
46+
remove(testFile);
47+
remove(`${testFile}.1`);
48+
// log file of 100 bytes maximum, no backups
49+
log4js.clearAppenders();
50+
log4js.addAppender(
51+
require(
52+
'../../lib/appenders/fileSync'
53+
).appender(
54+
testFile,
55+
log4js.layouts.basicLayout,
56+
100,
57+
0
58+
),
59+
'max-file-size'
60+
);
61+
logger.info('This is the first log message.');
62+
logger.info('This is an intermediate log message.');
63+
logger.info('This is the second log message.');
64+
65+
t.test('log file should only contain the second message', (assert) => {
66+
fs.readFile(testFile, 'utf8', (err, fileContents) => {
67+
assert.include(fileContents, `This is the second log message.${EOL}`);
68+
assert.equal(fileContents.indexOf('This is the first log message.'), -1);
69+
assert.end();
70+
});
71+
});
72+
73+
t.test('there should be two test files', (assert) => {
74+
fs.readdir(__dirname, (err, files) => {
75+
const logFiles = files.filter(
76+
file => file.includes('fa-maxFileSize-sync-test.log')
77+
);
78+
assert.equal(logFiles.length, 2);
79+
assert.end();
80+
});
81+
});
82+
t.end();
83+
});
84+
85+
batch.test('with a max file size and 2 backups', (t) => {
86+
const testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-sync-test.log');
87+
const logger = log4js.getLogger('max-file-size-backups');
88+
remove(testFile);
89+
remove(`${testFile}.1`);
90+
remove(`${testFile}.2`);
91+
92+
// log file of 50 bytes maximum, 2 backups
93+
log4js.clearAppenders();
94+
log4js.addAppender(
95+
require('../../lib/appenders/fileSync').appender(
96+
testFile,
97+
log4js.layouts.basicLayout,
98+
50,
99+
2
100+
),
101+
'max-file-size-backups'
102+
);
103+
logger.info('This is the first log message.');
104+
logger.info('This is the second log message.');
105+
logger.info('This is the third log message.');
106+
logger.info('This is the fourth log message.');
107+
108+
t.test('the log files', (assert) => {
109+
assert.plan(5);
110+
fs.readdir(__dirname, (err, files) => {
111+
const logFiles = files.filter(
112+
file => file.includes('fa-maxFileSize-with-backups-sync-test.log')
113+
);
114+
assert.equal(logFiles.length, 3, 'should be 3 files');
115+
assert.same(logFiles, [
116+
'fa-maxFileSize-with-backups-sync-test.log',
117+
'fa-maxFileSize-with-backups-sync-test.log.1',
118+
'fa-maxFileSize-with-backups-sync-test.log.2'
119+
], 'should be named in sequence');
120+
121+
fs.readFile(path.join(__dirname, logFiles[0]), 'utf8', (e, contents) => {
122+
assert.include(contents, 'This is the fourth log message.');
123+
});
124+
fs.readFile(path.join(__dirname, logFiles[1]), 'utf8', (e, contents) => {
125+
assert.include(contents, 'This is the third log message.');
126+
});
127+
fs.readFile(path.join(__dirname, logFiles[2]), 'utf8', (e, contents) => {
128+
assert.include(contents, 'This is the second log message.');
129+
});
130+
});
131+
});
132+
t.end();
133+
});
134+
135+
batch.test('configure with fileSyncAppender', (t) => {
136+
// this config defines one file appender (to ./tmp-sync-tests.log)
137+
// and sets the log level for "tests" to WARN
138+
log4js.configure({
139+
appenders: [
140+
{
141+
category: 'tests',
142+
type: 'file',
143+
filename: 'tmp-sync-tests.log',
144+
layout: { type: 'messagePassThrough' }
145+
}
146+
],
147+
148+
levels: { tests: 'WARN' }
149+
});
150+
const logger = log4js.getLogger('tests');
151+
logger.info('this should not be written to the file');
152+
logger.warn('this should be written to the file');
153+
154+
fs.readFile('tmp-sync-tests.log', 'utf8', (err, contents) => {
155+
t.include(contents, `this should be written to the file${EOL}`);
156+
t.equal(contents.indexOf('this should not be written to the file'), -1);
157+
t.end();
158+
});
159+
});
160+
161+
batch.end();
162+
});

test/vows/fileSyncAppender-test.js

Lines changed: 0 additions & 197 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0