1
1
var vows = require ( 'vows' ) ,
2
2
assert = require ( 'assert' ) ,
3
3
fs = require ( 'fs' ) ,
4
- DateRollingFileStream = require ( '../../lib/streams' ) . DateRollingFileStream ;
4
+ DateRollingFileStream = require ( '../../lib/streams' ) . DateRollingFileStream ,
5
+ testTime = new Date ( 2012 , 8 , 12 , 10 , 37 , 11 ) ;
6
+
7
+ function cleanUp ( filename ) {
8
+ return function ( ) {
9
+ fs . unlink ( filename ) ;
10
+ } ;
11
+ }
12
+
13
+ function now ( ) {
14
+ return testTime . getTime ( ) ;
15
+ }
5
16
6
17
vows . describe ( 'DateRollingFileStream' ) . addBatch ( {
7
18
'arguments' : {
8
- topic : new DateRollingFileStream ( 'test-date-rolling-file-stream' , 'yyyy-mm-dd.hh' ) ,
19
+ topic : new DateRollingFileStream ( __dirname + '/test-date-rolling-file-stream-1' , 'yyyy-mm-dd.hh' ) ,
20
+ teardown : cleanUp ( __dirname + '/test-date-rolling-file-stream-1' ) ,
9
21
10
22
'should take a filename and a pattern and return a FileWriteStream' : function ( stream ) {
11
- assert . equal ( stream . filename , ' test-date-rolling-file-stream') ;
23
+ assert . equal ( stream . filename , __dirname + '/ test-date-rolling-file-stream-1 ') ;
12
24
assert . equal ( stream . pattern , 'yyyy-mm-dd.hh' ) ;
13
25
assert . instanceOf ( stream , fs . FileWriteStream ) ;
14
26
} ,
@@ -20,29 +32,91 @@ vows.describe('DateRollingFileStream').addBatch({
20
32
} ,
21
33
22
34
'default arguments' : {
23
- topic : new DateRollingFileStream ( 'test-date-rolling-file-stream' ) ,
35
+ topic : new DateRollingFileStream ( __dirname + '/test-date-rolling-file-stream-2' ) ,
36
+ teardown : cleanUp ( __dirname + '/test-date-rolling-file-stream-2' ) ,
24
37
25
- 'pattern should be yyyy-mm -dd' : function ( stream ) {
26
- assert . equal ( stream . pattern , 'yyyy-mm -dd' ) ;
38
+ 'pattern should be . yyyy-MM -dd' : function ( stream ) {
39
+ assert . equal ( stream . pattern , '. yyyy-MM -dd' ) ;
27
40
}
28
41
} ,
29
42
30
43
'with stream arguments' : {
31
- topic : new DateRollingFileStream ( 'test-rolling-file-stream' , 'yyyy-mm-dd' , { mode : 0666 } ) ,
44
+ topic : new DateRollingFileStream ( __dirname + '/test-date-rolling-file-stream-3' , 'yyyy-MM-dd' , { mode : 0666 } ) ,
45
+ teardown : cleanUp ( __dirname + '/test-date-rolling-file-stream-3' ) ,
32
46
33
47
'should pass them to the underlying stream' : function ( stream ) {
34
48
assert . equal ( stream . mode , 0666 ) ;
35
49
}
36
50
} ,
37
51
38
52
'with stream arguments but no pattern' : {
39
- topic : new DateRollingFileStream ( 'test-rolling-file-stream' , { mode : 0666 } ) ,
53
+ topic : new DateRollingFileStream ( __dirname + '/test-date-rolling-file-stream-4' , { mode : 0666 } ) ,
54
+ teardown : cleanUp ( __dirname + '/test-date-rolling-file-stream-4' ) ,
40
55
41
56
'should pass them to the underlying stream' : function ( stream ) {
42
57
assert . equal ( stream . mode , 0666 ) ;
43
58
} ,
44
59
'should use default pattern' : function ( stream ) {
45
- assert . equal ( stream . pattern , 'yyyy-mm-dd' ) ;
60
+ assert . equal ( stream . pattern , '.yyyy-MM-dd' ) ;
61
+ }
62
+ } ,
63
+
64
+ 'with a pattern of .yyyy-MM-dd' : {
65
+ topic : function ( ) {
66
+ var that = this ,
67
+ stream = new DateRollingFileStream ( __dirname + '/test-date-rolling-file-stream-5' , '.yyyy-MM-dd' , null , now ) ;
68
+ stream . on ( "open" , function ( ) {
69
+ stream . write ( "First message\n" ) ;
70
+ //wait for the file system to catch up with us
71
+ that . callback ( null , stream ) ;
72
+ } ) ;
73
+ } ,
74
+ teardown : cleanUp ( __dirname + '/test-date-rolling-file-stream-5' ) ,
75
+
76
+ 'should create a file with the base name' : {
77
+ topic : function ( stream ) {
78
+ fs . readFile ( __dirname + '/test-date-rolling-file-stream-5' , this . callback ) ;
79
+ } ,
80
+ 'file should contain first message' : function ( result ) {
81
+ assert . equal ( result . toString ( ) , "First message\n" ) ;
82
+ }
83
+ } ,
84
+
85
+ 'when the day changes' : {
86
+ topic : function ( stream ) {
87
+ testTime = new Date ( 2012 , 8 , 13 , 0 , 10 , 12 ) ;
88
+ stream . write ( "Second message\n" ) ;
89
+ setTimeout ( this . callback , 100 ) ;
90
+ } ,
91
+ teardown : cleanUp ( __dirname + '/test-date-rolling-file-stream-5.2012-09-12' ) ,
92
+
93
+
94
+ 'the number of files' : {
95
+ topic : function ( ) {
96
+ fs . readdir ( __dirname , this . callback ) ;
97
+ } ,
98
+ 'should be two' : function ( files ) {
99
+ assert . equal ( files . filter ( function ( file ) { return file . indexOf ( 'test-date-rolling-file-stream-5' ) > - 1 ; } ) . length , 2 ) ;
100
+ }
101
+ } ,
102
+
103
+ 'the file without a date' : {
104
+ topic : function ( ) {
105
+ fs . readFile ( __dirname + '/test-date-rolling-file-stream-5' , this . callback ) ;
106
+ } ,
107
+ 'should contain the second message' : function ( contents ) {
108
+ assert . equal ( contents . toString ( ) , "Second message\n" ) ;
109
+ }
110
+ } ,
111
+
112
+ 'the file with the date' : {
113
+ topic : function ( ) {
114
+ fs . readFile ( __dirname + '/test-date-rolling-file-stream-5.2012-09-12' , this . callback ) ;
115
+ } ,
116
+ 'should contain the first message' : function ( contents ) {
117
+ assert . equal ( contents . toString ( ) , "First message\n" ) ;
118
+ }
119
+ }
46
120
}
47
121
}
48
122
0 commit comments