|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('tap').test; |
| 4 | +const sandbox = require('sandboxed-module'); |
| 5 | + |
| 6 | +function makeTestAppender() { |
| 7 | + return { |
| 8 | + configure: function (config, options) { |
| 9 | + this.configureCalled = true; |
| 10 | + this.config = config; |
| 11 | + this.options = options; |
| 12 | + return this.appender(); |
| 13 | + }, |
| 14 | + appender: function () { |
| 15 | + const self = this; |
| 16 | + return function (logEvt) { |
| 17 | + self.logEvt = logEvt; |
| 18 | + }; |
| 19 | + } |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +test('log4js configure', (batch) => { |
| 24 | + batch.test('when appenders specified by type', (t) => { |
| 25 | + const testAppender = makeTestAppender(); |
| 26 | + const log4js = sandbox.require( |
| 27 | + '../../lib/log4js', |
| 28 | + { |
| 29 | + singleOnly: true, |
| 30 | + requires: { |
| 31 | + './appenders/cheese': testAppender |
| 32 | + } |
| 33 | + } |
| 34 | + ); |
| 35 | + |
| 36 | + log4js.configure( |
| 37 | + { |
| 38 | + appenders: [ |
| 39 | + { type: 'cheese', flavour: 'gouda' } |
| 40 | + ] |
| 41 | + }, |
| 42 | + { pants: 'yes' } |
| 43 | + ); |
| 44 | + t.ok(testAppender.configureCalled, 'should load appender'); |
| 45 | + t.equal(testAppender.config.flavour, 'gouda', 'should pass config to appender'); |
| 46 | + t.equal(testAppender.options.pants, 'yes', 'should pass log4js options to appender'); |
| 47 | + t.end(); |
| 48 | + }); |
| 49 | + |
| 50 | + batch.test('when core appender loaded via loadAppender', (t) => { |
| 51 | + const testAppender = makeTestAppender(); |
| 52 | + const log4js = sandbox.require( |
| 53 | + '../../lib/log4js', |
| 54 | + { |
| 55 | + singleOnly: true, |
| 56 | + requires: { './appenders/cheese': testAppender } |
| 57 | + } |
| 58 | + ); |
| 59 | + |
| 60 | + log4js.loadAppender('cheese'); |
| 61 | + |
| 62 | + t.ok(log4js.appenders.cheese, 'should load appender from ../../lib/appenders'); |
| 63 | + t.type(log4js.appenderMakers.cheese, 'function', 'should add appender configure function to appenderMakers'); |
| 64 | + t.end(); |
| 65 | + }); |
| 66 | + |
| 67 | + batch.test('when appender in node_modules loaded via loadAppender', (t) => { |
| 68 | + const testAppender = makeTestAppender(); |
| 69 | + const log4js = sandbox.require( |
| 70 | + '../../lib/log4js', |
| 71 | + { |
| 72 | + singleOnly: true, |
| 73 | + requires: { 'some/other/external': testAppender } |
| 74 | + } |
| 75 | + ); |
| 76 | + |
| 77 | + log4js.loadAppender('some/other/external'); |
| 78 | + t.ok(log4js.appenders['some/other/external'], 'should load appender via require'); |
| 79 | + t.type( |
| 80 | + log4js.appenderMakers['some/other/external'], 'function', |
| 81 | + 'should add appender configure function to appenderMakers' |
| 82 | + ); |
| 83 | + t.end(); |
| 84 | + }); |
| 85 | + |
| 86 | + batch.test('when appender object loaded via loadAppender', (t) => { |
| 87 | + const testAppender = makeTestAppender(); |
| 88 | + const log4js = sandbox.require('../../lib/log4js'); |
| 89 | + |
| 90 | + log4js.loadAppender('some/other/external', testAppender); |
| 91 | + |
| 92 | + t.ok(log4js.appenders['some/other/external'], 'should load appender with provided object'); |
| 93 | + t.type( |
| 94 | + log4js.appenderMakers['some/other/external'], 'function', |
| 95 | + 'should add appender configure function to appenderMakers' |
| 96 | + ); |
| 97 | + t.end(); |
| 98 | + }); |
| 99 | + |
| 100 | + batch.test('when configuration file loaded via LOG4JS_CONFIG env variable', (t) => { |
| 101 | + process.env.LOG4JS_CONFIG = 'some/path/to/mylog4js.json'; |
| 102 | + let fileRead = 0; |
| 103 | + const modulePath = 'some/path/to/mylog4js.json'; |
| 104 | + const pathsChecked = []; |
| 105 | + const mtime = new Date(); |
| 106 | + |
| 107 | + const fakeFS = { |
| 108 | + config: { |
| 109 | + appenders: [{ type: 'console', layout: { type: 'messagePassThrough' } }], |
| 110 | + levels: { 'a-test': 'INFO' } |
| 111 | + }, |
| 112 | + readdirSync: function (dir) { |
| 113 | + return require('fs').readdirSync(dir); |
| 114 | + }, |
| 115 | + readFileSync: function (file, encoding) { |
| 116 | + fileRead += 1; |
| 117 | + t.type(file, 'string'); |
| 118 | + t.equal(file, modulePath); |
| 119 | + t.equal(encoding, 'utf8'); |
| 120 | + return JSON.stringify(fakeFS.config); |
| 121 | + }, |
| 122 | + statSync: function (path) { |
| 123 | + pathsChecked.push(path); |
| 124 | + if (path === modulePath) { |
| 125 | + return { mtime: mtime }; |
| 126 | + } |
| 127 | + throw new Error('no such file'); |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + sandbox.require( |
| 132 | + '../../lib/log4js', |
| 133 | + { |
| 134 | + requires: { |
| 135 | + fs: fakeFS, |
| 136 | + } |
| 137 | + } |
| 138 | + ); |
| 139 | + |
| 140 | + delete process.env.LOG4JS_CONFIG; |
| 141 | + |
| 142 | + t.equal(fileRead, 1, 'should load the specified local config file'); |
| 143 | + t.end(); |
| 144 | + }); |
| 145 | + |
| 146 | + batch.end(); |
| 147 | +}); |
0 commit comments