Pushing changes

This commit is contained in:
2017-03-23 23:52:08 -05:00
parent 6075860b82
commit ac667ec74f
1465 changed files with 345149 additions and 3 deletions

View File

@ -0,0 +1,93 @@
"use strict";
var should = require('should')
, fs = require('fs')
, sandbox = require('sandboxed-module');
describe('BaseRollingFileStream', function() {
describe('when no filename is passed', function() {
it('should throw an error', function() {
var BaseRollingFileStream = require('../lib/BaseRollingFileStream');
(function() {
new BaseRollingFileStream();
}).should.throw();
});
});
describe('default behaviour', function() {
var stream;
before(function() {
var BaseRollingFileStream = require('../lib/BaseRollingFileStream');
stream = new BaseRollingFileStream('basetest.log');
});
after(function(done) {
fs.unlink('basetest.log', done);
});
it('should not want to roll', function() {
stream.shouldRoll().should.eql(false);
});
it('should not roll', function() {
var cbCalled = false;
//just calls the callback straight away, no async calls
stream.roll('basetest.log', function() { cbCalled = true; });
cbCalled.should.eql(true);
});
it('should pass options to the underlying write stream', function() {
var underlyingStreamOptions;
var BaseRollingFileStream = sandbox.require(
'../lib/BaseRollingFileStream',
{
requires: {
'fs': {
createWriteStream: function(filename, options) {
underlyingStreamOptions = options;
return {
on: function() {}
};
}
}
}
}
);
var stream = new BaseRollingFileStream('cheese.log', { encoding: 'utf904'});
stream.openTheStream();
underlyingStreamOptions.should.eql({ encoding: 'utf904', mode: 420, flags: 'a'});
});
});
describe('when end is called', function() {
it('should close the underlying stream', function(done) {
var stream = new (require('../lib/BaseRollingFileStream'))('cheese.log');
stream.theStream.on('close', function() {
done();
});
stream.end();
});
});
describe('when the file is in a non-existent directory', function() {
var stream;
before(function() {
var BaseRollingFileStream = require('../lib/BaseRollingFileStream');
stream = new BaseRollingFileStream('subdir/test.log');
});
after(function() {
fs.unlinkSync('subdir/test.log');
fs.rmdir('subdir');
});
it('should create the directory', function() {
fs.existsSync('subdir/test.log').should.eql(true);
stream.end();
});
});
});

View File

@ -0,0 +1,508 @@
"use strict";
var should = require('should')
, fs = require('fs')
, zlib = require('zlib')
, util = require('util')
, async = require('async')
, format = require('date-format')
, streams = require('readable-stream')
, DateRollingFileStream
, testTime = new Date(2012, 8, 12, 10, 37, 11);
DateRollingFileStream = require('../lib').DateRollingFileStream;
function remove(filename, cb) {
fs.unlink(filename, function () {
cb();
});
}
function now() {
return testTime.getTime();
}
describe('DateRollingFileStream', function () {
describe('arguments', function () {
var stream;
before(function (done) {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-1',
'yyyy-mm-dd.hh'
);
done();
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-1', done);
});
it('should take a filename and a pattern and return a WritableStream', function (done) {
stream.filename.should.eql(__dirname + '/test-date-rolling-file-stream-1');
stream.pattern.should.eql('yyyy-mm-dd.hh');
stream.should.be.instanceOf(streams.Writable);
done();
});
it('with default settings for the underlying stream', function (done) {
stream.theStream.mode.should.eql(420);
stream.theStream.flags.should.eql('a');
//encoding is not available on the underlying stream
//assert.equal(stream.encoding, 'utf8');
done();
});
});
describe('default arguments', function () {
var stream;
before(function(done) {
stream = new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2');
done();
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-2', done);
});
it('should have pattern of .yyyy-MM-dd', function (done) {
stream.pattern.should.eql('.yyyy-MM-dd');
done();
});
});
describe('with stream arguments', function () {
var stream;
before(function(done) {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-3',
'yyyy-MM-dd',
{mode: parseInt('0666', 8)}
);
done();
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-3', done);
});
it('should pass them to the underlying stream', function (done) {
stream.theStream.mode.should.eql(parseInt('0666', 8));
done();
});
});
describe('with stream arguments but no pattern', function () {
var stream;
before(function (done) {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-4',
{mode: parseInt('0666', 8)}
);
done();
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-4', done);
});
it('should pass them to the underlying stream', function (done) {
stream.theStream.mode.should.eql(parseInt('0666', 8));
done();
});
it('should use default pattern', function (done) {
stream.pattern.should.eql('.yyyy-MM-dd');
done();
});
});
describe('with a pattern of .yyyy-MM-dd', function () {
var stream;
before(function (done) {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd',
null,
now
);
stream.write("First message\n", 'utf8', done);
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-5', done);
});
it('should create a file with the base name', function (done) {
fs.readFile(__dirname + '/test-date-rolling-file-stream-5', 'utf8', function (err, contents) {
contents.should.eql("First message\n");
done(err);
});
});
describe('when the day changes', function () {
before(function (done) {
testTime = new Date(2012, 8, 13, 0, 10, 12);
stream.write("Second message\n", 'utf8', done);
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', done);
});
describe('the number of files', function () {
var files = [];
before(function (done) {
fs.readdir(__dirname, function (err, list) {
files = list;
done(err);
});
});
it('should be two', function (done) {
files.filter(
function (file) {
return file.indexOf('test-date-rolling-file-stream-5') > -1;
}
).should.have.length(2);
done();
});
});
describe('the file without a date', function () {
it('should contain the second message', function (done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-5', 'utf8',
function (err, contents) {
contents.should.eql("Second message\n");
done(err);
}
);
});
});
describe('the file with the date', function () {
it('should contain the first message', function (done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-5.2012-09-12', 'utf8',
function (err, contents) {
contents.should.eql("First message\n");
done(err);
}
);
});
});
});
});
describe('with alwaysIncludePattern', function () {
var stream;
before(function (done) {
testTime = new Date(2012, 8, 12, 0, 10, 12);
remove(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12',
function () {
stream = new DateRollingFileStream(
__dirname + '/test-date-rolling-file-stream-pattern',
'.yyyy-MM-dd',
{alwaysIncludePattern: true},
now
);
stream.write("First message\n", 'utf8', done);
}
);
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', done);
});
it('should create a file with the pattern set', function (done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', 'utf8',
function (err, contents) {
contents.should.eql("First message\n");
done(err);
}
);
});
describe('when the day changes', function () {
before(function (done) {
testTime = new Date(2012, 8, 13, 0, 10, 12);
stream.write("Second message\n", 'utf8', done);
});
after(function (done) {
remove(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', done);
});
describe('the number of files', function () {
it('should be two', function (done) {
fs.readdir(__dirname, function (err, files) {
files.filter(
function (file) {
return file.indexOf('test-date-rolling-file-stream-pattern') > -1;
}
).should.have.length(2);
done(err);
});
});
});
describe('the file with the later date', function () {
it('should contain the second message', function (done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13', 'utf8',
function (err, contents) {
contents.should.eql("Second message\n");
done(err);
}
);
});
});
describe('the file with the date', function () {
it('should contain the first message', function (done) {
fs.readFile(
__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', 'utf8',
function (err, contents) {
contents.should.eql("First message\n");
done(err);
}
);
});
});
});
});
describe('with compress option', function () {
var stream;
before(function (done) {
testTime = new Date(2012, 8, 12, 0, 10, 12);
stream = new DateRollingFileStream(
__dirname + '/compressed.log',
'.yyyy-MM-dd',
{compress: true},
now
);
stream.write("First message\n", 'utf8', done);
});
describe('when the day changes', function () {
before(function (done) {
testTime = new Date(2012, 8, 13, 0, 10, 12);
stream.write("Second message\n", 'utf8', done);
});
it('should be two files, one compressed', function (done) {
fs.readdir(__dirname, function (err, files) {
var logFiles = files.filter(
function (file) {
return file.indexOf('compressed.log') > -1;
}
);
logFiles.should.have.length(2);
zlib.gunzip(
fs.readFileSync(__dirname + '/compressed.log.2012-09-12.gz'),
function (err, contents) {
contents.toString('utf8').should.eql('First message\n');
fs.readFileSync(__dirname + '/compressed.log', 'utf8').should.eql('Second message\n');
done(err);
}
);
});
});
});
after(function (done) {
remove(
__dirname + '/compressed.log',
function () {
remove(__dirname + '/compressed.log.2012-09-12.gz', done);
}
);
});
});
describe('with daysToKeep option', function () {
var stream;
var daysToKeep = 4;
var numOriginalLogs = 10;
before(function (done) {
var day = 0;
var streams = [];
async.whilst(
function () {
return day < numOriginalLogs;
},
function (nextCallback) {
testTime = new Date(2012, 8, 20 - day, 0, 10, 12);
var currentStream = new DateRollingFileStream(
__dirname + '/daysToKeep.log',
'.yyyy-MM-dd',
{
alwaysIncludePattern: true,
daysToKeep: daysToKeep
},
now
);
async.waterfall([
function (callback) {
currentStream.write(util.format("Message on day %d\n", day), 'utf8', callback);
},
function (callback) {
fs.utimes(currentStream.filename, testTime, testTime, callback);
}
],
function (err) {
day++;
streams.push(currentStream);
nextCallback(err);
});
},
function (err, n) {
stream = streams[0];
done(err);
});
describe('when the day changes', function () {
before(function (done) {
testTime = new Date(2012, 8, 21, 0, 10, 12);
stream.write("Second message\n", 'utf8', done);
});
it('should be daysToKeep + 1 files left from numOriginalLogs', function (done) {
fs.readdir(__dirname, function (err, files) {
var logFiles = files.filter(
function (file) {
return file.indexOf('daysToKeep.log') > -1;
}
);
logFiles.should.have.length(daysToKeep + 1);
done(err);
});
});
});
after(function (done) {
fs.readdir(__dirname, function (err, files) {
var logFiles = files.filter(
function (file) {
return file.indexOf('daysToKeep.log') > -1;
}
);
async.each(logFiles, function (logFile, nextCallback) {
remove(__dirname + "/" + logFile, nextCallback);
},
function (err) {
done(err);
});
});
});
});
});
describe('with daysToKeep and compress options', function () {
var stream;
var daysToKeep = 4;
var numOriginalLogs = 10;
before(function (done) {
var day = 0;
var streams = [];
async.whilst(
function () {
return day < numOriginalLogs;
},
function (nextCallback) {
testTime = new Date(2012, 8, 20 - day, 0, 10, 12);
var currentStream = new DateRollingFileStream(
__dirname + '/compressedDaysToKeep.log',
'.yyyy-MM-dd',
{
alwaysIncludePattern: true,
compress: true,
daysToKeep: daysToKeep
},
now
);
async.waterfall([
function (callback) {
currentStream.write(util.format("Message on day %d\n", day), 'utf8', callback);
},
function (callback) {
currentStream.compress(currentStream.filename, callback);
},
function (callback) {
fs.utimes(currentStream.filename + ".gz", testTime, testTime, callback);
}
],
function (err) {
day++;
streams.push(currentStream);
nextCallback(err);
});
},
function (err, n) {
// Uncompress the most recent stream which will be the one we roll over
// for testing
stream = streams[0];
var compressedFilename = stream.filename + '.gz';
var gzip = zlib.createGzip();
var inp = fs.createReadStream(compressedFilename);
var out = fs.createWriteStream(stream.filename);
inp.pipe(gzip).pipe(out);
out.on('finish', function (err) {
fs.unlink(compressedFilename, done);
});
});
});
describe('when the day changes', function () {
before(function (done) {
testTime = new Date(2012, 8, 21, 0, 10, 12);
stream.write("New file message\n", 'utf8', done);
});
it('should be 4 files left from original 3', function (done) {
fs.readdir(__dirname, function (err, files) {
var logFiles = files.filter(
function (file) {
return file.indexOf('compressedDaysToKeep.log') > -1;
}
);
logFiles.should.have.length(daysToKeep + 1);
done(err);
});
});
});
after(function (done) {
fs.readdir(__dirname, function (err, files) {
var logFiles = files.filter(
function (file) {
return file.indexOf('compressedDaysToKeep.log') > -1;
}
);
async.each(logFiles, function (logFile, nextCallback) {
remove(__dirname + "/" + logFile, nextCallback);
},
function (err) {
done(err);
});
});
});
});
});

View File

@ -0,0 +1,357 @@
"use strict";
var async = require('async')
, should = require('should')
, fs = require('fs')
, path = require('path')
, zlib = require('zlib')
, streams = require('readable-stream')
, RollingFileStream = require('../lib').RollingFileStream;
function remove(filename, cb) {
fs.unlink(filename, function(err) { cb(); });
}
function create(filename, cb) {
fs.writeFile(filename, "test file", cb);
}
describe('RollingFileStream', function() {
describe('arguments', function() {
var stream;
before(function(done) {
remove(__dirname + "/test-rolling-file-stream", function() {
stream = new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024, 5);
done();
});
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream", done);
});
it('should take a filename, file size (bytes), no. backups, return Writable', function() {
stream.should.be.an.instanceOf(streams.Writable);
stream.filename.should.eql(__dirname + "/test-rolling-file-stream");
stream.size.should.eql(1024);
stream.backups.should.eql(5);
});
it('should apply default settings to the underlying stream', function() {
stream.theStream.mode.should.eql(420);
stream.theStream.flags.should.eql('a');
//encoding isn't a property on the underlying stream
//assert.equal(stream.theStream.encoding, 'utf8');
});
});
describe('with stream arguments', function() {
it('should pass them to the underlying stream', function() {
var stream = new RollingFileStream(
__dirname + '/test-rolling-file-stream',
1024,
5,
{ mode: parseInt('0666', 8) }
);
stream.theStream.mode.should.eql(parseInt('0666', 8));
});
after(function(done) {
remove(__dirname + '/test-rolling-file-stream', done);
});
});
describe('without size', function() {
it('should default to max int size', function() {
var stream = new RollingFileStream(__dirname + "/test-rolling-file-stream");
stream.size.should.eql(Number.MAX_SAFE_INTEGER);
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream", done);
});
});
describe('without number of backups', function() {
it('should default to 1 backup', function() {
var stream = new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024);
stream.backups.should.eql(1);
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream", done);
});
});
describe('writing less than the file size', function() {
before(function(done) {
remove(__dirname + "/test-rolling-file-stream-write-less", function() {
var stream = new RollingFileStream(
__dirname + "/test-rolling-file-stream-write-less",
100
);
stream.write("cheese", "utf8", function() {
stream.end(done);
});
});
});
after(function(done) {
remove(__dirname + "/test-rolling-file-stream-write-less", done);
});
it('should write to the file', function(done) {
fs.readFile(
__dirname + "/test-rolling-file-stream-write-less", "utf8",
function(err, contents) {
contents.should.eql("cheese");
done(err);
}
);
});
it('should write one file', function(done) {
fs.readdir(__dirname, function(err, files) {
files.filter(
function(file) { return file.indexOf('test-rolling-file-stream-write-less') > -1; }
).should.have.length(1);
done(err);
});
});
});
describe('writing more than the file size', function() {
before(function(done) {
async.forEach(
[
__dirname + "/test-rolling-file-stream-write-more",
__dirname + "/test-rolling-file-stream-write-more.1"
],
remove,
function() {
var stream = new RollingFileStream(
__dirname + "/test-rolling-file-stream-write-more",
45
);
async.forEachSeries(
[0, 1, 2, 3, 4, 5, 6],
function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
},
function() {
stream.end(done);
}
);
}
);
});
after(function(done) {
async.forEach(
[
__dirname + "/test-rolling-file-stream-write-more",
__dirname + "/test-rolling-file-stream-write-more.1"
],
remove,
done
);
});
it('should write two files' , function(done) {
fs.readdir(__dirname, function(err, files) {
files.filter(
function(file) {
return file.indexOf('test-rolling-file-stream-write-more') > -1;
}
).should.have.length(2);
done(err);
});
});
it('should write the last two log messages to the first file', function(done) {
fs.readFile(
__dirname + "/test-rolling-file-stream-write-more", "utf8",
function(err, contents) {
contents.should.eql('5.cheese\n6.cheese\n');
done(err);
});
});
it('should write the first five log messages to the second file', function(done) {
fs.readFile(
__dirname + '/test-rolling-file-stream-write-more.1', "utf8",
function(err, contents) {
contents.should.eql('0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n');
done(err);
}
);
});
});
describe('with options.compress = true', function() {
before(function(done) {
var stream = new RollingFileStream(
path.join(__dirname, 'compressed-backups.log'),
30, //30 bytes max size
2, //two backup files to keep
{ compress: true }
);
async.forEachSeries(
[
"This is the first log message.",
"This is the second log message.",
"This is the third log message.",
"This is the fourth log message."
],
function(i, cb) {
stream.write(i + "\n", "utf8", cb);
},
function() {
stream.end(done);
}
);
});
it('should produce three files, with the backups compressed', function(done) {
fs.readdir(__dirname, function(err, files) {
var testFiles = files.filter(
function(f) { return f.indexOf('compressed-backups.log') > -1; }
).sort();
testFiles.length.should.eql(3);
testFiles.should.eql([
'compressed-backups.log',
'compressed-backups.log.1.gz',
'compressed-backups.log.2.gz',
]);
fs.readFile(path.join(__dirname, testFiles[0]), 'utf8', function(err, contents) {
contents.should.eql('This is the fourth log message.\n');
zlib.gunzip(fs.readFileSync(path.join(__dirname, testFiles[1])),
function(err, contents) {
contents.toString('utf8').should.eql('This is the third log message.\n');
zlib.gunzip(fs.readFileSync(path.join(__dirname, testFiles[2])),
function(err, contents) {
contents.toString('utf8').should.eql('This is the second log message.\n');
done(err);
}
);
}
);
});
});
});
after(function(done) {
async.forEach([
path.join(__dirname, 'compressed-backups.log'),
path.join(__dirname, 'compressed-backups.log.1.gz'),
path.join(__dirname, 'compressed-backups.log.2.gz'),
], remove, done);
});
});
describe('when many files already exist', function() {
before(function(done) {
async.forEach(
[
__dirname + '/test-rolling-stream-with-existing-files.11',
__dirname + '/test-rolling-stream-with-existing-files.20',
__dirname + '/test-rolling-stream-with-existing-files.-1',
__dirname + '/test-rolling-stream-with-existing-files.1.1',
__dirname + '/test-rolling-stream-with-existing-files.1'
],
remove,
function(err) {
if (err) done(err);
async.forEach(
[
__dirname + '/test-rolling-stream-with-existing-files.11',
__dirname + '/test-rolling-stream-with-existing-files.20',
__dirname + '/test-rolling-stream-with-existing-files.-1',
__dirname + '/test-rolling-stream-with-existing-files.1.1',
__dirname + '/test-rolling-stream-with-existing-files.1'
],
create,
function(err) {
if (err) done(err);
var stream = new RollingFileStream(
__dirname + "/test-rolling-stream-with-existing-files",
45,
5
);
async.forEachSeries(
[0, 1, 2, 3, 4, 5, 6],
function(i, cb) {
stream.write(i +".cheese\n", "utf8", cb);
},
function() {
stream.end(done);
}
);
}
);
}
);
});
after(function(done) {
async.forEach([
__dirname + '/test-rolling-stream-with-existing-files',
__dirname + '/test-rolling-stream-with-existing-files.0',
__dirname + '/test-rolling-stream-with-existing-files.1',
__dirname + '/test-rolling-stream-with-existing-files.2',
__dirname + '/test-rolling-stream-with-existing-files.3',
__dirname + '/test-rolling-stream-with-existing-files.4',
__dirname + '/test-rolling-stream-with-existing-files.5',
__dirname + '/test-rolling-stream-with-existing-files.11',
__dirname + '/test-rolling-stream-with-existing-files.20'
], remove, done);
});
it('should roll the files', function(done) {
fs.readdir(__dirname, function(err, files) {
files.should.include('test-rolling-stream-with-existing-files');
files.should.include('test-rolling-stream-with-existing-files.1');
files.should.include('test-rolling-stream-with-existing-files.2');
files.should.include('test-rolling-stream-with-existing-files.11');
files.should.include('test-rolling-stream-with-existing-files.20');
done(err);
});
});
});
describe('when the directory gets deleted', function() {
var stream;
before(function(done) {
stream = new RollingFileStream(path.join('subdir', 'test-rolling-file-stream'), 5, 5);
stream.write('initial', 'utf8', done);
});
after(function() {
fs.unlinkSync(path.join('subdir', 'test-rolling-file-stream'));
fs.rmdirSync('subdir');
});
it('handles directory deletion gracefully', function(done) {
stream.theStream.on('error', done);
remove(path.join('subdir', 'test-rolling-file-stream'), function() {
fs.rmdir('subdir', function() {
stream.write('rollover', 'utf8', function() {
fs.readFileSync(path.join('subdir', 'test-rolling-file-stream'), 'utf8')
.should.eql('rollover');
done();
});
});
});
});
});
});

28
node_modules/streamroller/test/read-only-file-test.js generated vendored Normal file
View File

@ -0,0 +1,28 @@
"use strict";
var should = require('should')
, fs = require('fs')
, path = require('path')
, streamroller = require('../lib/index.js');
describe('when the destination file is read-only', function() {
var testFile = path.join(__dirname, 'read-only-file.log');
before(function() {
fs.writeFileSync(
testFile,
"Some test content"
);
fs.chmodSync(testFile, 292 /* 0o444 - octal literals not allowed in old node */);
});
it('should generate an error when writing', function(done) {
var stream = new streamroller.RollingFileStream(testFile);
stream.on('error', function(e) {
e.code.should.eql('EACCES');
done();
});
});
after(function() {
fs.unlinkSync(testFile);
});
});