DateRollingFileStream-test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. "use strict";
  2. var vows = require('vows')
  3. , assert = require('assert')
  4. , fs = require('fs')
  5. , semver = require('semver')
  6. , streams
  7. , DateRollingFileStream
  8. , testTime = new Date(2012, 8, 12, 10, 37, 11);
  9. if (semver.satisfies(process.version, '>=0.10.0')) {
  10. streams = require('stream');
  11. } else {
  12. streams = require('readable-stream');
  13. }
  14. DateRollingFileStream = require('../../lib/streams').DateRollingFileStream;
  15. function cleanUp(filename) {
  16. return function() {
  17. fs.unlink(filename);
  18. };
  19. }
  20. function now() {
  21. return testTime.getTime();
  22. }
  23. vows.describe('DateRollingFileStream').addBatch({
  24. 'arguments': {
  25. topic: new DateRollingFileStream(
  26. __dirname + '/test-date-rolling-file-stream-1',
  27. 'yyyy-mm-dd.hh'
  28. ),
  29. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-1'),
  30. 'should take a filename and a pattern and return a WritableStream': function(stream) {
  31. assert.equal(stream.filename, __dirname + '/test-date-rolling-file-stream-1');
  32. assert.equal(stream.pattern, 'yyyy-mm-dd.hh');
  33. assert.instanceOf(stream, streams.Writable);
  34. },
  35. 'with default settings for the underlying stream': function(stream) {
  36. assert.equal(stream.theStream.mode, 420);
  37. assert.equal(stream.theStream.flags, 'a');
  38. //encoding is not available on the underlying stream
  39. //assert.equal(stream.encoding, 'utf8');
  40. }
  41. },
  42. 'default arguments': {
  43. topic: new DateRollingFileStream(__dirname + '/test-date-rolling-file-stream-2'),
  44. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-2'),
  45. 'pattern should be .yyyy-MM-dd': function(stream) {
  46. assert.equal(stream.pattern, '.yyyy-MM-dd');
  47. }
  48. },
  49. 'with stream arguments': {
  50. topic: new DateRollingFileStream(
  51. __dirname + '/test-date-rolling-file-stream-3',
  52. 'yyyy-MM-dd',
  53. { mode: parseInt('0666', 8) }
  54. ),
  55. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-3'),
  56. 'should pass them to the underlying stream': function(stream) {
  57. assert.equal(stream.theStream.mode, parseInt('0666', 8));
  58. }
  59. },
  60. 'with stream arguments but no pattern': {
  61. topic: new DateRollingFileStream(
  62. __dirname + '/test-date-rolling-file-stream-4',
  63. { mode: parseInt('0666', 8) }
  64. ),
  65. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-4'),
  66. 'should pass them to the underlying stream': function(stream) {
  67. assert.equal(stream.theStream.mode, parseInt('0666', 8));
  68. },
  69. 'should use default pattern': function(stream) {
  70. assert.equal(stream.pattern, '.yyyy-MM-dd');
  71. }
  72. },
  73. 'with a pattern of .yyyy-MM-dd': {
  74. topic: function() {
  75. var that = this,
  76. stream = new DateRollingFileStream(
  77. __dirname + '/test-date-rolling-file-stream-5', '.yyyy-MM-dd',
  78. null,
  79. now
  80. );
  81. stream.write("First message\n", 'utf8', function() {
  82. that.callback(null, stream);
  83. });
  84. },
  85. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5'),
  86. 'should create a file with the base name': {
  87. topic: function(stream) {
  88. fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
  89. },
  90. 'file should contain first message': function(result) {
  91. assert.equal(result.toString(), "First message\n");
  92. }
  93. },
  94. 'when the day changes': {
  95. topic: function(stream) {
  96. testTime = new Date(2012, 8, 13, 0, 10, 12);
  97. stream.write("Second message\n", 'utf8', this.callback);
  98. },
  99. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-5.2012-09-12'),
  100. 'the number of files': {
  101. topic: function() {
  102. fs.readdir(__dirname, this.callback);
  103. },
  104. 'should be two': function(files) {
  105. assert.equal(
  106. files.filter(
  107. function(file) {
  108. return file.indexOf('test-date-rolling-file-stream-5') > -1;
  109. }
  110. ).length,
  111. 2
  112. );
  113. }
  114. },
  115. 'the file without a date': {
  116. topic: function() {
  117. fs.readFile(__dirname + '/test-date-rolling-file-stream-5', this.callback);
  118. },
  119. 'should contain the second message': function(contents) {
  120. assert.equal(contents.toString(), "Second message\n");
  121. }
  122. },
  123. 'the file with the date': {
  124. topic: function() {
  125. fs.readFile(__dirname + '/test-date-rolling-file-stream-5.2012-09-12', this.callback);
  126. },
  127. 'should contain the first message': function(contents) {
  128. assert.equal(contents.toString(), "First message\n");
  129. }
  130. }
  131. }
  132. },
  133. 'with alwaysIncludePattern': {
  134. topic: function() {
  135. var that = this,
  136. testTime = new Date(2012, 8, 12, 0, 10, 12),
  137. stream = new DateRollingFileStream(
  138. __dirname + '/test-date-rolling-file-stream-pattern',
  139. '.yyyy-MM-dd',
  140. {alwaysIncludePattern: true},
  141. now
  142. );
  143. stream.write("First message\n", 'utf8', function() {
  144. that.callback(null, stream);
  145. });
  146. },
  147. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12'),
  148. 'should create a file with the pattern set': {
  149. topic: function(stream) {
  150. fs.readFile(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-12', this.callback);
  151. },
  152. 'file should contain first message': function(result) {
  153. assert.equal(result.toString(), "First message\n");
  154. }
  155. },
  156. 'when the day changes': {
  157. topic: function(stream) {
  158. testTime = new Date(2012, 8, 13, 0, 10, 12);
  159. stream.write("Second message\n", 'utf8', this.callback);
  160. },
  161. teardown: cleanUp(__dirname + '/test-date-rolling-file-stream-pattern.2012-09-13'),
  162. 'the number of files': {
  163. topic: function() {
  164. fs.readdir(__dirname, this.callback);
  165. },
  166. 'should be two': function(files) {
  167. assert.equal(
  168. files.filter(
  169. function(file) {
  170. return file.indexOf('test-date-rolling-file-stream-pattern') > -1;
  171. }
  172. ).length,
  173. 2
  174. );
  175. }
  176. },
  177. 'the file with the later date': {
  178. topic: function() {
  179. fs.readFile(
  180. __dirname + '/test-date-rolling-file-stream-pattern.2012-09-13',
  181. this.callback
  182. );
  183. },
  184. 'should contain the second message': function(contents) {
  185. assert.equal(contents.toString(), "Second message\n");
  186. }
  187. },
  188. 'the file with the date': {
  189. topic: function() {
  190. fs.readFile(
  191. __dirname + '/test-date-rolling-file-stream-pattern.2012-09-12',
  192. this.callback
  193. );
  194. },
  195. 'should contain the first message': function(contents) {
  196. assert.equal(contents.toString(), "First message\n");
  197. }
  198. }
  199. }
  200. }
  201. }).exportTo(module);