Skip to content

Commit

Permalink
style: format test code
Browse files Browse the repository at this point in the history
a error occur in `loggin-test.js` - 'invalid configuration'.

It may reveal some internal defects of this library.

Another problem occurs in `configureNoLevels-test.js`. I mark a
**todo** there.
  • Loading branch information
e-cloud committed Dec 12, 2016
1 parent ff18214 commit 3e4f3a6
Show file tree
Hide file tree
Showing 40 changed files with 3,171 additions and 2,933 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
test

1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"root": true,
"extends": "airbnb-base",
"rules": {
"comma-dangle": 0,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"node": ">=4.0"
},
"scripts": {
"clean": "find test -type f ! -name '*.json' ! -name '*.js' -delete && rm *.log",
"clean": "find test -type f ! -name '*.json' ! -name '*.js' ! -name '.eslintrc' -delete && rm *.log",
"lint": "eslint lib/ test/",
"posttest": "npm run clean",
"pretest": "eslint lib/**/*",
"test": "tape 'test/tape/**/*.js' && vows test/vows/*.js"
Expand Down
11 changes: 11 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../.eslintrc",
"rules": {
"no-plusplus": 0,
"global-require": 0,
"no-mixed-operators": 0,
"no-underscore-dangle": 0,
"guard-for-in": 0,
"no-restricted-syntax": ["error", "WithStatement"]
}
}
33 changes: 18 additions & 15 deletions test/tape/default-settings-test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
"use strict";
var test = require('tape')
, sandbox = require('sandboxed-module');
'use strict';

test('default settings', function(t) {
var output = []
, log4js = sandbox.require(
const test = require('tape');
const sandbox = require('sandboxed-module');

test('default settings', (t) => {
const output = [];

const log4js = sandbox.require(
'../../lib/log4js',
{
requires: {
'./appenders/stdout': {
'name': 'stdout',
'appender': function () {
return function(evt) {
name: 'stdout',
appender: function () {
return function (evt) {
output.push(evt);
};
},
'configure': function (config) {
configure: function () {
return this.appender();
}
}
}
}
)
, logger = log4js.getLogger("default-settings");
);

const logger = log4js.getLogger('default-settings');

logger.info("This should go to stdout.");
logger.info('This should go to stdout.');

t.plan(2);
t.equal(output.length, 1, "It should log to stdout.");
t.equal(output[0].data[0], "This should go to stdout.", "It should log the message.");
t.equal(output.length, 1, 'It should log to stdout.');
t.equal(output[0].data[0], 'This should go to stdout.', 'It should log the message.');
t.end();
});
31 changes: 17 additions & 14 deletions test/tape/file-sighup-test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
"use strict";
var test = require('tape')
, sandbox = require('sandboxed-module');
'use strict';

test('file appender SIGHUP', function(t) {
var closeCalled = 0
, openCalled = 0
, appender = sandbox.require(
const test = require('tape');
const sandbox = require('sandboxed-module');

test('file appender SIGHUP', (t) => {
let closeCalled = 0;
let openCalled = 0;

sandbox.require(
'../../lib/appenders/file',
{
'requires': {
'streamroller': {
'RollingFileStream': function() {
this.openTheStream = function() {
requires: {
streamroller: {
RollingFileStream: function () {
this.openTheStream = function () {
openCalled++;
};

this.closeTheStream = function(cb) {
this.closeTheStream = function (cb) {
closeCalled++;
cb();
};

this.on = function() {};
this.on = function () {
};
}
}
}
Expand All @@ -29,7 +32,7 @@ test('file appender SIGHUP', function(t) {

process.kill(process.pid, 'SIGHUP');
t.plan(2);
setTimeout(function() {
setTimeout(() => {
t.equal(openCalled, 1, 'open should be called once');
t.equal(closeCalled, 1, 'close should be called once');
t.end();
Expand Down
27 changes: 14 additions & 13 deletions test/tape/reload-shutdown-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
"use strict";
var test = require('tape')
, path = require('path')
, sandbox = require('sandboxed-module');
'use strict';

test('Reload configuration shutdown hook', function(t) {
var timerId
, log4js = sandbox.require(
const test = require('tape');
const path = require('path');
const sandbox = require('sandboxed-module');

test('Reload configuration shutdown hook', (t) => {
let timerId;

const log4js = sandbox.require(
'../../lib/log4js',
{
globals: {
clearInterval: function(id) {
clearInterval: function (id) {
timerId = id;
},
setInterval: function(fn, time) {
return "1234";
setInterval: function () {
return '1234';
}
}
}
Expand All @@ -25,9 +27,8 @@ test('Reload configuration shutdown hook', function(t) {
);

t.plan(1);
log4js.shutdown(function() {
t.equal(timerId, "1234", "Shutdown should clear the reload timer");
log4js.shutdown(() => {
t.equal(timerId, '1234', 'Shutdown should clear the reload timer');
t.end();
});

});
26 changes: 17 additions & 9 deletions test/tape/stderrAppender-test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
"use strict";
var test = require('tape')
, layouts = require('../../lib/layouts')
, sandbox = require('sandboxed-module');
'use strict';

test('stderr appender', function(t) {
var output = []
, appender = sandbox.require(
const test = require('tape');
const layouts = require('../../lib/layouts');
const sandbox = require('sandboxed-module');

test('stderr appender', (t) => {
const output = [];

const appender = sandbox.require(
'../../lib/appenders/stderr',
{
globals: {
process: { stderr: { write : function(data) { output.push(data); } } }
process: {
stderr: {
write: function (data) {
output.push(data);
}
}
}
}
}
).appender(layouts.messagePassThroughLayout);

appender({ data: ["biscuits"] });
appender({ data: ['biscuits'] });
t.plan(2);
t.equal(output.length, 1, 'There should be one message.');
t.equal(output[0], 'biscuits\n', 'The message should be biscuits.');
Expand Down
26 changes: 17 additions & 9 deletions test/tape/stdoutAppender-test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
"use strict";
var test = require('tape')
, layouts = require('../../lib/layouts')
, sandbox = require('sandboxed-module');
'use strict';

test('stdout appender', function(t) {
var output = []
, appender = sandbox.require(
const test = require('tape');
const layouts = require('../../lib/layouts');
const sandbox = require('sandboxed-module');

test('stdout appender', (t) => {
const output = [];

const appender = sandbox.require(
'../../lib/appenders/stdout',
{
globals: {
process: { stdout: { write : function(data) { output.push(data); } } }
process: {
stdout: {
write: function (data) {
output.push(data);
}
}
}
}
}
).appender(layouts.messagePassThroughLayout);

appender({ data: ["cheese"] });
appender({ data: ['cheese'] });
t.plan(2);
t.equal(output.length, 1, 'There should be one message.');
t.equal(output[0], 'cheese\n', 'The message should be cheese.');
Expand Down
70 changes: 35 additions & 35 deletions test/vows/categoryFilter-test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
'use strict';

var vows = require('vows')
, fs = require('fs')
, assert = require('assert')
, EOL = require('os').EOL || '\n';
const vows = require('vows');
const fs = require('fs');
const assert = require('assert');
const EOL = require('os').EOL || '\n';

function remove(filename) {
try {
fs.unlinkSync(filename);
} catch (e) {
//doesn't really matter if it failed
// doesn't really matter if it failed
}
}

vows.describe('log4js categoryFilter').addBatch({
'appender': {
topic: function() {

var log4js = require('../../lib/log4js'), logEvents = [], webLogger, appLogger;
appender: {
topic: function () {
const log4js = require('../../lib/log4js');
const logEvents = [];
log4js.clearAppenders();
var appender = require('../../lib/appenders/categoryFilter')
const appender = require('../../lib/appenders/categoryFilter')
.appender(
['app'],
function(evt) { logEvents.push(evt); }
(evt) => {
logEvents.push(evt);
}
);
log4js.addAppender(appender, ["app","web"]);
log4js.addAppender(appender, ['app', 'web']);

webLogger = log4js.getLogger("web");
appLogger = log4js.getLogger("app");
const webLogger = log4js.getLogger('web');
const appLogger = log4js.getLogger('app');

webLogger.debug('This should get logged');
appLogger.debug('This should not');
Expand All @@ -36,48 +38,46 @@ vows.describe('log4js categoryFilter').addBatch({

return logEvents;
},
'should only pass matching category' : function(logEvents) {
'should only pass matching category': function (logEvents) {
assert.equal(logEvents.length, 2);
assert.equal(logEvents[0].data[0], 'This should get logged');
assert.equal(logEvents[1].data[0], 'Hello again');
}
},

'configure': {
topic: function() {
var log4js = require('../../lib/log4js')
, logger, weblogger;

remove(__dirname + '/categoryFilter-web.log');
remove(__dirname + '/categoryFilter-noweb.log');
configure: {
topic: function () {
const log4js = require('../../lib/log4js');
remove(`${__dirname}/categoryFilter-web.log`);
remove(`${__dirname}/categoryFilter-noweb.log`);

log4js.configure('test/vows/with-categoryFilter.json');
logger = log4js.getLogger("app");
weblogger = log4js.getLogger("web");
const logger = log4js.getLogger('app');
const weblogger = log4js.getLogger('web');

logger.info('Loading app');
logger.debug('Initialising indexes');
weblogger.info('00:00:00 GET / 200');
weblogger.warn('00:00:00 GET / 500');
//wait for the file system to catch up
// wait for the file system to catch up
setTimeout(this.callback, 500);
},
'tmp-tests.log': {
topic: function() {
fs.readFile(__dirname + '/categoryFilter-noweb.log', 'utf8', this.callback);
topic: function () {
fs.readFile(`${__dirname}/categoryFilter-noweb.log`, 'utf8', this.callback);
},
'should contain all log messages': function(contents) {
var messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['Loading app','Initialising indexes']);
'should contain all log messages': function (contents) {
const messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['Loading app', 'Initialising indexes']);
}
},
'tmp-tests-web.log': {
topic: function() {
fs.readFile(__dirname + '/categoryFilter-web.log','utf8',this.callback);
topic: function () {
fs.readFile(`${__dirname}/categoryFilter-web.log`, 'utf8', this.callback);
},
'should contain only error and warning log messages': function(contents) {
var messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['00:00:00 GET / 200','00:00:00 GET / 500']);
'should contain only error and warning log messages': function (contents) {
const messages = contents.trim().split(EOL);
assert.deepEqual(messages, ['00:00:00 GET / 200', '00:00:00 GET / 500']);
}
}
}
Expand Down
Loading

0 comments on commit 3e4f3a6

Please sign in to comment.