Skip to content

Commit

Permalink
[wip] Test maxsize options on file transport.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Mar 9, 2018
1 parent d795dc9 commit 5eb3c05
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lib/winston/transports/file.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ File.prototype._createStream = function () {
.on('drain', this._onDrain) .on('drain', this._onDrain)
.on('open', function () { .on('open', function () {
debug('file open ok', fullpath); debug('file open ok', fullpath);
self.emit('open'); self.emit('open', fullpath);
self._stream.pipe(self._dest); self._stream.pipe(self._dest);
}); });


Expand Down
82 changes: 0 additions & 82 deletions test/transports/file-maxsize-test.js

This file was deleted.

84 changes: 84 additions & 0 deletions test/transports/file-maxsize.test.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* file-test.js: Tests for instances of the File transport
*
* (C) 2010 Charlie Robbins
* MIT LICENSE
*
*/

const assert = require('assert');
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const assume = require('assume');
const winston = require('../../');

const MESSAGE = Symbol.for('message');
const kbStr = new Array(1024).join('-');
const maxsizeTransport = new winston.transports.File({
level: 'info',
format: winston.format.printf(info => info.message),
filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize.log'),
maxsize: 4096
});

//
// Log the specified kbytes to the transport
//
function logKbytes (kbytes) {
//
// With printf format that displays the message only
// winston adds exactly 0 characters.
//
for (var i = 0; i < kbytes; i++) {
maxsizeTransport.log({ level: 'info', [MESSAGE]: kbStr });
}
}

describe('File (maxsize)', function () {
this.timeout(10000);

before(function (done) {
exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize*'), done);
});

it('should create multiple files correctly when passed more than the maxsize', function (done) {
//
// Setup a list of files which we will later stat.
//
const files = [];

//
// Assets all the files have been created with the
// correct filesize
//
function assumeFilesCreated() {
files.forEach(function (file) {
let stats;
try {
stats = fs.statSync(file);
assume(stats.size).equals(4096);
} catch (ex) {
assume(stats).is.an('object', `${file} failed to open: ${ex.message}`);
}
});

done();
}

maxsizeTransport.on('open', function (file) {
console.dir(arguments);
const match = file.match(/(\d+)\.log$/);
const count = match ? match[1] : 0;

files.push(file);
if (files.length === 5) {
return assumeFilesCreated();
}

logKbytes(4);
});

logKbytes(4);
});
});

0 comments on commit 5eb3c05

Please sign in to comment.