rollingFileStream-test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. var vows = require('vows')
  3. , async = require('async')
  4. , assert = require('assert')
  5. , events = require('events')
  6. , fs = require('fs')
  7. , semver = require('semver')
  8. , streams
  9. , RollingFileStream;
  10. if (semver.satisfies(process.version, '>=0.10.0')) {
  11. streams = require('stream');
  12. } else {
  13. streams = require('readable-stream');
  14. }
  15. RollingFileStream = require('../../lib/streams').RollingFileStream;
  16. function remove(filename) {
  17. try {
  18. fs.unlinkSync(filename);
  19. } catch (e) {
  20. //doesn't really matter if it failed
  21. }
  22. }
  23. function create(filename) {
  24. fs.writeFileSync(filename, "test file");
  25. }
  26. vows.describe('RollingFileStream').addBatch({
  27. 'arguments': {
  28. topic: function() {
  29. remove(__dirname + "/test-rolling-file-stream");
  30. return new RollingFileStream("test-rolling-file-stream", 1024, 5);
  31. },
  32. 'should take a filename, file size (bytes), no. backups, return Writable': function(stream) {
  33. assert.instanceOf(stream, streams.Writable);
  34. assert.equal(stream.filename, "test-rolling-file-stream");
  35. assert.equal(stream.size, 1024);
  36. assert.equal(stream.backups, 5);
  37. },
  38. 'with default settings for the underlying stream': function(stream) {
  39. assert.equal(stream.theStream.mode, 420);
  40. assert.equal(stream.theStream.flags, 'a');
  41. //encoding isn't a property on the underlying stream
  42. //assert.equal(stream.theStream.encoding, 'utf8');
  43. }
  44. },
  45. 'with stream arguments': {
  46. topic: function() {
  47. remove(__dirname + '/test-rolling-file-stream');
  48. return new RollingFileStream(
  49. 'test-rolling-file-stream',
  50. 1024,
  51. 5,
  52. { mode: parseInt('0666', 8) }
  53. );
  54. },
  55. 'should pass them to the underlying stream': function(stream) {
  56. assert.equal(stream.theStream.mode, parseInt('0666', 8));
  57. }
  58. },
  59. 'without size': {
  60. topic: function() {
  61. try {
  62. new RollingFileStream(__dirname + "/test-rolling-file-stream");
  63. } catch (e) {
  64. return e;
  65. }
  66. },
  67. 'should throw an error': function(err) {
  68. assert.instanceOf(err, Error);
  69. }
  70. },
  71. 'without number of backups': {
  72. topic: function() {
  73. remove('test-rolling-file-stream');
  74. return new RollingFileStream(__dirname + "/test-rolling-file-stream", 1024);
  75. },
  76. 'should default to 1 backup': function(stream) {
  77. assert.equal(stream.backups, 1);
  78. }
  79. },
  80. 'writing less than the file size': {
  81. topic: function() {
  82. remove(__dirname + "/test-rolling-file-stream-write-less");
  83. var that = this
  84. , stream = new RollingFileStream(
  85. __dirname + "/test-rolling-file-stream-write-less",
  86. 100
  87. );
  88. stream.write("cheese", "utf8", function() {
  89. stream.end();
  90. fs.readFile(__dirname + "/test-rolling-file-stream-write-less", "utf8", that.callback);
  91. });
  92. },
  93. 'should write to the file': function(contents) {
  94. assert.equal(contents, "cheese");
  95. },
  96. 'the number of files': {
  97. topic: function() {
  98. fs.readdir(__dirname, this.callback);
  99. },
  100. 'should be one': function(files) {
  101. assert.equal(
  102. files.filter(
  103. function(file) {
  104. return file.indexOf('test-rolling-file-stream-write-less') > -1;
  105. }
  106. ).length,
  107. 1
  108. );
  109. }
  110. }
  111. },
  112. 'writing more than the file size': {
  113. topic: function() {
  114. remove(__dirname + "/test-rolling-file-stream-write-more");
  115. remove(__dirname + "/test-rolling-file-stream-write-more.1");
  116. var that = this
  117. , stream = new RollingFileStream(
  118. __dirname + "/test-rolling-file-stream-write-more",
  119. 45
  120. );
  121. async.forEach(
  122. [0, 1, 2, 3, 4, 5, 6],
  123. function(i, cb) {
  124. stream.write(i +".cheese\n", "utf8", cb);
  125. },
  126. function() {
  127. stream.end();
  128. that.callback();
  129. }
  130. );
  131. },
  132. 'the number of files': {
  133. topic: function() {
  134. fs.readdir(__dirname, this.callback);
  135. },
  136. 'should be two': function(files) {
  137. assert.equal(files.filter(
  138. function(file) {
  139. return file.indexOf('test-rolling-file-stream-write-more') > -1;
  140. }
  141. ).length, 2);
  142. }
  143. },
  144. 'the first file': {
  145. topic: function() {
  146. fs.readFile(__dirname + "/test-rolling-file-stream-write-more", "utf8", this.callback);
  147. },
  148. 'should contain the last two log messages': function(contents) {
  149. assert.equal(contents, '5.cheese\n6.cheese\n');
  150. }
  151. },
  152. 'the second file': {
  153. topic: function() {
  154. fs.readFile(__dirname + '/test-rolling-file-stream-write-more.1', "utf8", this.callback);
  155. },
  156. 'should contain the first five log messages': function(contents) {
  157. assert.equal(contents, '0.cheese\n1.cheese\n2.cheese\n3.cheese\n4.cheese\n');
  158. }
  159. }
  160. },
  161. 'when many files already exist': {
  162. topic: function() {
  163. remove(__dirname + '/test-rolling-stream-with-existing-files.11');
  164. remove(__dirname + '/test-rolling-stream-with-existing-files.20');
  165. remove(__dirname + '/test-rolling-stream-with-existing-files.-1');
  166. remove(__dirname + '/test-rolling-stream-with-existing-files.1.1');
  167. remove(__dirname + '/test-rolling-stream-with-existing-files.1');
  168. create(__dirname + '/test-rolling-stream-with-existing-files.11');
  169. create(__dirname + '/test-rolling-stream-with-existing-files.20');
  170. create(__dirname + '/test-rolling-stream-with-existing-files.-1');
  171. create(__dirname + '/test-rolling-stream-with-existing-files.1.1');
  172. create(__dirname + '/test-rolling-stream-with-existing-files.1');
  173. var that = this
  174. , stream = new RollingFileStream(
  175. __dirname + "/test-rolling-stream-with-existing-files",
  176. 45,
  177. 5
  178. );
  179. async.forEach(
  180. [0, 1, 2, 3, 4, 5, 6],
  181. function(i, cb) {
  182. stream.write(i +".cheese\n", "utf8", cb);
  183. },
  184. function() {
  185. stream.end();
  186. that.callback();
  187. }
  188. );
  189. },
  190. 'the files': {
  191. topic: function() {
  192. fs.readdir(__dirname, this.callback);
  193. },
  194. 'should be rolled': function(files) {
  195. assert.include(files, 'test-rolling-stream-with-existing-files');
  196. assert.include(files, 'test-rolling-stream-with-existing-files.1');
  197. assert.include(files, 'test-rolling-stream-with-existing-files.2');
  198. assert.include(files, 'test-rolling-stream-with-existing-files.11');
  199. assert.include(files, 'test-rolling-stream-with-existing-files.20');
  200. }
  201. }
  202. }
  203. }).exportTo(module);