BaseRollingFileStream-test.js
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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() {}
};
}
}
},
singleOnly: true
}
);
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();
});
});
});