BaseRollingFileStream-test.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var should = require('should')
  3. , fs = require('fs')
  4. , sandbox = require('sandboxed-module');
  5. describe('BaseRollingFileStream', function() {
  6. describe('when no filename is passed', function() {
  7. it('should throw an error', function() {
  8. var BaseRollingFileStream = require('../lib/BaseRollingFileStream');
  9. (function() {
  10. new BaseRollingFileStream();
  11. }).should.throw();
  12. });
  13. });
  14. describe('default behaviour', function() {
  15. var stream;
  16. before(function() {
  17. var BaseRollingFileStream = require('../lib/BaseRollingFileStream');
  18. stream = new BaseRollingFileStream('basetest.log');
  19. });
  20. after(function(done) {
  21. fs.unlink('basetest.log', done);
  22. });
  23. it('should not want to roll', function() {
  24. stream.shouldRoll().should.eql(false);
  25. });
  26. it('should not roll', function() {
  27. var cbCalled = false;
  28. //just calls the callback straight away, no async calls
  29. stream.roll('basetest.log', function() { cbCalled = true; });
  30. cbCalled.should.eql(true);
  31. });
  32. it('should pass options to the underlying write stream', function() {
  33. var underlyingStreamOptions;
  34. var BaseRollingFileStream = sandbox.require(
  35. '../lib/BaseRollingFileStream',
  36. {
  37. requires: {
  38. 'fs': {
  39. createWriteStream: function(filename, options) {
  40. underlyingStreamOptions = options;
  41. return {
  42. on: function() {}
  43. };
  44. }
  45. }
  46. },
  47. singleOnly: true
  48. }
  49. );
  50. var stream = new BaseRollingFileStream('cheese.log', { encoding: 'utf904'});
  51. stream.openTheStream();
  52. underlyingStreamOptions.should.eql({ encoding: 'utf904', mode: 420, flags: 'a'});
  53. });
  54. });
  55. describe('when end is called', function() {
  56. it('should close the underlying stream', function(done) {
  57. var stream = new (require('../lib/BaseRollingFileStream'))('cheese.log');
  58. stream.theStream.on('close', function() {
  59. done();
  60. });
  61. stream.end();
  62. });
  63. });
  64. describe('when the file is in a non-existent directory', function() {
  65. var stream;
  66. before(function() {
  67. var BaseRollingFileStream = require('../lib/BaseRollingFileStream');
  68. stream = new BaseRollingFileStream('subdir/test.log');
  69. });
  70. after(function() {
  71. fs.unlinkSync('subdir/test.log');
  72. fs.rmdir('subdir');
  73. });
  74. it('should create the directory', function() {
  75. fs.existsSync('subdir/test.log').should.eql(true);
  76. stream.end();
  77. });
  78. });
  79. });