Files
elasticsearch-js/test/unit/specs/file_logger.js
Dominykas Blyžė f1de944809 Upgrade to lodash v4 (#660)
* npm install lodash-2

Someone handily published a lodash-2 v4.17.4 - it is exactly the same as lodash v4.17.4, so it is safe to use during the migration.

* use lodash-2 in tests

* update tests to split utils vs lodash

* remove Utils.nextTick usage

Utils.nextTick with a single argument is the same as process.nextTick

* lowercase utils

Because it seems that this is the coding style in this repo

* upgrade lodash in grunt/*

* keep lodash-2 as a dev dep for now

* use lodash-2 in scripts

* use snakeCase from utils

It was a mistake in my previous commit to not update this usage

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* fix naming gruntUtils vs utils

As all three - gruntUtils, utils and lodash (_) are getting passed into templates, it makes sense to keep the naming consistent

* split utils vs lodash in scripts/generate

Also use lodash-2 where it is easy to do so

* use utils.get until lodash upgrade

* remove lodash.isempty; lodash-2 now used in prod (in src/lib/apis/ code)

* unbundle lodash from utils

* upgrade to lodash 4

* remove lodash.get and lodash.trimEnd

* clean out unused code

* clean out unused code

* fix a breaking change listed under "notable changes" rather than under "breaking changes"...
2018-05-14 11:37:23 -07:00

90 lines
2.5 KiB
JavaScript

describe('File Logger', function () {
var Log = require('../../../src/lib/log');
var FileLogger = require('../../../src/lib/loggers/file');
var once = require('events').EventEmitter.prototype.once;
var _ = require('lodash');
var utils = require('../../../src/lib/utils');
var parentLog;
var logger;
var expect = require('expect.js');
var fs = require('fs');
var stub = require('../../utils/auto_release_stub').make();
beforeEach(function () {
parentLog = new Log();
});
afterEach(function () {
parentLog.close();
logger && utils.clearWriteStreamBuffer(logger.stream);
});
function makeLogger(parent, levels) {
parent = parent || parentLog;
logger = new FileLogger(parent, {
levels: Log.parseLevels(levels || 'trace'),
path: 'test.log'
});
return logger;
}
after(function () {
fs.unlinkSync('test.log');
});
require('../generic_logger_tests')(makeLogger);
describe('buffer flush', function () {
if (require('stream').Writable) {
it('writes everything in the buffer to console.error', function () {
var line = 'This string is written 10 times to create buffered output\n';
var exitHandler;
stub(process, 'once', function (event, handler) {
if (event === 'exit') {
exitHandler = handler;
}
once.call(process, event, handler);
});
var logger = makeLogger();
// write the line 10 times
_.times(10, function () {
logger.onDebug(line);
});
// collect everything that is written to fs.appendFileSync
var flushedOutput = '';
stub(fs, 'appendFileSync', function (path, str) {
flushedOutput += str;
});
// call the event handler
exitHandler.call(process);
// the first line is sent immediately to _write and there is nothing we can do about that
expect(flushedOutput).to.match(new RegExp(line));
expect(flushedOutput.match(new RegExp(line, 'g')).length).to.be(9);
});
} else {
it('does not fall apart with non streams2 streams', function () {
var exitHandler;
stub(process, 'once', function (event, handler) {
if (event === 'exit') {
exitHandler = handler;
}
once.call(process, event, handler);
});
var logger = makeLogger();
expect(function () {
// call the event handler
exitHandler.call(process);
}).to.not.throwError();
});
}
});
});