|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const test = require('tap').test; |
| 4 | +const sandbox = require('sandboxed-module'); |
| 5 | + |
| 6 | +function setupConsoleTest() { |
| 7 | + const fakeConsole = {}; |
| 8 | + const logEvents = []; |
| 9 | + |
| 10 | + ['trace', 'debug', 'log', 'info', 'warn', 'error'].forEach((fn) => { |
| 11 | + fakeConsole[fn] = function () { |
| 12 | + throw new Error('this should not be called.'); |
| 13 | + }; |
| 14 | + }); |
| 15 | + |
| 16 | + const log4js = sandbox.require( |
| 17 | + '../../lib/log4js', |
| 18 | + { |
| 19 | + globals: { |
| 20 | + console: fakeConsole |
| 21 | + } |
| 22 | + } |
| 23 | + ); |
| 24 | + |
| 25 | + log4js.clearAppenders(); |
| 26 | + log4js.addAppender((evt) => { |
| 27 | + logEvents.push(evt); |
| 28 | + }); |
| 29 | + |
| 30 | + return { log4js: log4js, logEvents: logEvents, fakeConsole: fakeConsole }; |
| 31 | +} |
| 32 | + |
| 33 | +test('reload configuration', (batch) => { |
| 34 | + batch.test('with config file changing', (t) => { |
| 35 | + const pathsChecked = []; |
| 36 | + const logEvents = []; |
| 37 | + const modulePath = 'path/to/log4js.json'; |
| 38 | + |
| 39 | + const fakeFS = { |
| 40 | + lastMtime: Date.now(), |
| 41 | + config: { |
| 42 | + appenders: [ |
| 43 | + { type: 'console', layout: { type: 'messagePassThrough' } } |
| 44 | + ], |
| 45 | + levels: { 'a-test': 'INFO' } |
| 46 | + }, |
| 47 | + readFileSync: function (file, encoding) { |
| 48 | + t.equal(file, modulePath); |
| 49 | + t.equal(encoding, 'utf8'); |
| 50 | + return JSON.stringify(fakeFS.config); |
| 51 | <
10000
span class="diff-text-marker">+ }, |
| 52 | + statSync: function (path) { |
| 53 | + pathsChecked.push(path); |
| 54 | + if (path === modulePath) { |
| 55 | + fakeFS.lastMtime += 1; |
| 56 | + return { mtime: new Date(fakeFS.lastMtime) }; |
| 57 | + } |
| 58 | + throw new Error('no such file'); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const fakeConsole = { |
| 63 | + name: 'console', |
| 64 | + appender: function () { |
| 65 | + return function (evt) { |
| 66 | + logEvents.push(evt); |
| 67 | + }; |
| 68 | + }, |
| 69 | + configure: function () { |
| 70 | + return fakeConsole.appender(); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + let setIntervalCallback; |
| 75 | + |
| 76 | + const fakeSetInterval = function (cb) { |
| 77 | + setIntervalCallback = cb; |
| 78 | + }; |
| 79 | + |
| 80 | + const log4js = sandbox.require( |
| 81 | + '../../lib/log4js', |
| 82 | + { |
| 83 | + requires: { |
| 84 | + fs: fakeFS, |
| 85 | + './appenders/console': fakeConsole |
| 86 | + }, |
| 87 | + globals: { |
| 88 | + console: fakeConsole, |
| 89 | + setInterval: fakeSetInterval, |
| 90 | + } |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + log4js.configure('path/to/log4js.json', { reloadSecs: 30 }); |
| 95 | + const logger = log4js.getLogger('a-test'); |
| 96 | + logger.info('info1'); |
| 97 | + logger.debug('debug2 - should be ignored'); |
| 98 | + fakeFS.config.levels['a-test'] = 'DEBUG'; |
| 99 | + setIntervalCallback(); |
| 100 | + logger.info('info3'); |
| 101 | + logger.debug('debug4'); |
| 102 | + |
| 103 | + t.test('should configure log4js from first log4js.json found', (assert) => { |
| 104 | + assert.equal(logEvents[0].data[0], 'info1'); |
| 105 | + assert.equal(logEvents[1].data[0], 'info3'); |
| 106 | + assert.equal(logEvents[2].data[0], 'debug4'); |
| 107 | + assert.equal(logEvents.length, 3); |
| 108 | + assert.end(); |
| 109 | + }); |
| 110 | + t.end(); |
| 111 | + }); |
| 112 | + |
| 113 | + batch.test('with config file staying the same', (t) => { |
| 114 | + const pathsChecked = []; |
| 115 | + let fileRead = 0; |
| 116 | + const logEvents = []; |
| 117 | + const modulePath = require('path').normalize(`${__dirname}/../../lib/log4js.json`); |
| 118 | + const mtime = new Date(); |
| 119 | + |
| 120 | + const fakeFS = { |
| 121 | + config: { |
| 122 | + appenders: [ |
| 123 | + { type: 'console', layout: { type: 'messagePassThrough' } } |
| 124 | + ], |
| 125 | + levels: { 'a-test': 'INFO' } |
| 126 | + }, |
| 127 | + readFileSync: function (file, encoding) { |
| 128 | + fileRead += 1; |
| 129 | + t.type(file, 'string'); |
| 130 | + t.equal(file, modulePath); |
| 131 | + t.equal(encoding, 'utf8'); |
| 132 | + return JSON.stringify(fakeFS.config); |
| 133 | + }, |
| 134 | + statSync: function (path) { |
| 135 | + pathsChecked.push(path); |
| 136 | + if (path === modulePath) { |
| 137 | + return { mtime: mtime }; |
| 138 | + } |
| 139 | + throw new Error('no such file'); |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + const fakeConsole = { |
| 144 | + name: 'console', |
| 145 | + appender: function () { |
| 146 | + return function (evt) { |
| 147 | + logEvents.push(evt); |
| 148 | + }; |
| 149 | + }, |
| 150 | + configure: function () { |
| 151 | + return fakeConsole.appender(); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + let setIntervalCallback; |
| 156 | + |
| 157 | + const fakeSetInterval = function (cb) { |
| 158 | + setIntervalCallback = cb; |
| 159 | + }; |
| 160 | + |
| 161 | + const log4js = sandbox.require( |
| 162 | + '../../lib/log4js', |
| 163 | + { |
| 164 | + requires: { |
| 165 | + fs: fakeFS, |
| 166 | + './appenders/console': fakeConsole |
| 167 | + }, |
| 168 | + globals: { |
| 169 | + console: fakeConsole, |
| 170 | + setInterval: fakeSetInterval, |
| 171 | + } |
| 172 | + } |
| 173 | + ); |
| 174 | + |
| 175 | + log4js.configure(modulePath, { reloadSecs: 3 }); |
| 176 | + const logger = log4js.getLogger('a-test'); |
| 177 | + logger.info('info1'); |
| 178 | + logger.debug('debug2 - should be ignored'); |
| 179 | + setIntervalCallback(); |
| 180 | + logger.info('info3'); |
| 181 | + logger.debug('debug4'); |
| 182 | + |
| 183 | + t.equal(fileRead, 1, 'should only read the configuration file once'); |
| 184 | + t.test('should configure log4js from first log4js.json found', (assert) => { |
| 185 | + assert.equal(logEvents.length, 2); |
| 186 | + assert.equal(logEvents[0].data[0], 'info1'); |
| 187 | + assert.equal(logEvents[1].data[0], 'info3'); |
| 188 | + assert.end(); |
| 189 | + }); |
| 190 | + t.end(); |
| 191 | + }); |
| 192 | + |
| 193 | + batch.test('when config file is removed', (t) => { |
| 194 | + let fileRead = 0; |
| 195 | + const logEvents = []; |
| 196 | + const modulePath = require('path').normalize(`${__dirname}/../../lib/log4js.json`); |
| 197 | + |
| 198 | + const fakeFS = { |
| 199 | + config: { |
| 200 | + appenders: [ |
| 201 | + { type: 'console', layout: { type: 'messagePassThrough' } } |
| 202 | + ], |
| 203 | + levels: { 'a-test': 'INFO' } |
| 204 | + }, |
| 205 | + readFileSync: function (file, encoding) { |
| 206 | + fileRead += 1; |
| 207 | + t.type(file, 'string'); |
| 208 | + t.equal(file, modulePath); |
| 209 | + t.equal(encoding, 'utf8'); |
| 210 | + return JSON.stringify(fakeFS.config); |
| 211 | + }, |
| 212 | + statSync: function () { |
| 213 | + this.statSync = function () { |
| 214 | + throw new Error('no such file'); |
| 215 | + }; |
| 216 | + return { mtime: new Date() }; |
| 217 | + } |
| 218 | + }; |
| 219 | + |
| 220 | + const fakeConsole = { |
| 221 | + name: 'console', |
| 222 | + appender: function () { |
| 223 | + return function (evt) { |
| 224 | + logEvents.push(evt); |
| 225 | + }; |
| 226 | + }, |
| 227 | + configure: function () { |
| 228 | + return fakeConsole.appender(); |
| 229 | + } |
| 230 | + }; |
| 231 | + |
| 232 | + let setIntervalCallback; |
| 233 | + |
| 234 | + const fakeSetInterval = function (cb) { |
| 235 | + setIntervalCallback = cb; |
| 236 | + }; |
| 237 | + |
| 238 | + const log4js = sandbox.require( |
| 239 | + '../../lib/log4js', |
| 240 | + { |
| 241 | + requires: { |
| 242 | + fs: fakeFS, |
| 243 | + './appenders/console': fakeConsole |
| 244 | + }, |
| 245 | + globals: { |
| 246 | + console: fakeConsole, |
| 247 | + setInterval: fakeSetInterval, |
| 248 | + } |
| 249 | + } |
| 250 | + ); |
| 251 | + |
| 252 | + log4js.configure(modulePath, { reloadSecs: 3 }); |
| 253 | + const logger = log4js.getLogger('a-test'); |
| 254 | + logger.info('info1'); |
| 255 | + logger.debug('debug2 - should be ignored'); |
| 256 | + setIntervalCallback(); |
| 257 | + logger.info('info3'); |
| 258 | + logger.debug('debug4'); |
| 259 | + |
| 260 | + t.equal(fileRead, 1, 'should only read the configuration file once'); |
| 261 | + t.test('should not clear configuration when config file not found', (assert) => { |
| 262 | + assert.equal(logEvents.length, 3); |
| 263 | + assert.equal(logEvents[0].data[0], 'info1'); |
| 264 | + assert.equal(logEvents[1].level.toString(), 'WARN'); |
| 265 | + assert.include(logEvents[1].data[0], 'Failed to load configuration file'); |
| 266 | + assert.equal(logEvents[2].data[0], 'info3'); |
| 267 | + assert.end(); |
| 268 | + }); |
| 269 | + t.end(); |
| 270 | + }); |
| 271 | + |
| 272 | + batch.test('when passed an object', (t) => { |
| 273 | + const setup = setupConsoleTest(); |
| 274 | + setup.log4js.configure({}, { reloadSecs: 30 }); |
| 275 | + const events = setup.logEvents; |
| 276 | + |
| 277 | + t.test('should log a warning', (assert) => { |
| 278 | + assert.equal(events[0].level.toString(), 'WARN'); |
| 279 | + assert.equal( |
| 280 | + events[0].data[0], |
| 281 | + 'Ignoring configuration reload parameter for "object" configuration.' |
| 282 | + ); |
| 283 | + assert.end(); |
| 284 | + }); |
| 285 | + t.end(); |
| 286 | + }); |
| 287 | + |
| 288 | + batch.test('when called twice with reload options', (t) => { |
| 289 | + const modulePath = require('path').normalize(`${__dirname}/../../lib/log4js.json`); |
| 290 | + |
| 291 | + const fakeFS = { |
| 292 | + readFileSync: function () { |
| 293 | + return JSON.stringify({}); |
| 294 | + }, |
| 295 | + statSync: function () { |
| 296 | + return { mtime: new Date() }; |
| 297 | + } |
| 298 | + }; |
| 299 | + |
| 300 | + const fakeConsole = { |
| 301 | + name: 'console', |
| 302 | + appender: function () { |
| 303 | + return function () { |
| 304 | + }; |
| 305 | + }, |
| 306 | + configure: function () { |
| 307 | + return fakeConsole.appender(); |
| 308 | + } |
| 309 | + }; |
| 310 | + |
| 311 | + let setIntervalCallback; // eslint-disable-line |
| 312 | + let intervalCleared = false; |
| 313 | + let clearedId; |
| 314 | + |
| 315 | + const fakeSetInterval = function (cb) { |
| 316 | + setIntervalCallback = cb; |
| 317 | + return 1234; |
| 318 | + }; |
| 319 | + |
| 320 | + const log4js = sandbox.require( |
| 321 | + '../../lib/log4js', |
| 322 | + { |
| 323 | + requires: { |
| 324 | + fs: fakeFS, |
| 325 | + './appenders/console': fakeConsole |
| 326 | + }, |
| 327 | + globals: { |
| 328 | + console: fakeConsole, |
| 329 | + setInterval: fakeSetInterval, |
| 330 | + clearInterval: function (interval) { |
| 331 | + intervalCleared = true; |
| 332 | + clearedId = interval; |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + ); |
| 337 | + |
| 338 | + log4js.configure(modulePath, { reloadSecs: 3 }); |
| 339 | + log4js.configure(modulePath, { reloadSecs: 15 }); |
| 340 | + |
| 341 | + t.test('should clear the previous interval', (assert) => { |
| 342 | + assert.ok(intervalCleared); |
| 343 | + assert.equal(clearedId, 1234); |
| 344 | + assert.end(); |
| 345 | + }); |
| 346 | + t.end(); |
| 347 | + }); |
| 348 | + |
| 349 | + batch.end(); |
| 350 | +}); |
0 commit comments