file-maxfiles-test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * file-maxfiles-test.js: Tests for instances of the File transport setting the max file size,
  3. * and setting a number for max files created.
  4. * maxSize * maxFiles = total storage used by winston.
  5. *
  6. * (C) 2011 Daniel Aristizabal
  7. * MIT LICENSE
  8. *
  9. */
  10. var assert = require('assert'),
  11. exec = require('child_process').exec,
  12. fs = require('fs'),
  13. path = require('path'),
  14. vows = require('vows'),
  15. winston = require('../../lib/winston'),
  16. helpers = require('../helpers');
  17. var maxfilesTransport = new winston.transports.File({
  18. timestamp: false,
  19. json: false,
  20. filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles.log'),
  21. maxsize: 4096,
  22. maxFiles: 3
  23. });
  24. vows.describe('winston/transports/file/maxfiles').addBatch({
  25. "An instance of the File Transport": {
  26. "when passed a valid filename": {
  27. topic: maxfilesTransport,
  28. "should set the maxFiles option correctly": function (transportTest) {
  29. assert.isNumber(transportTest.maxFiles);
  30. }
  31. },
  32. "when delete old test files": {
  33. topic: function () {
  34. exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles*'), this.callback);
  35. },
  36. "and when passed more files than the maxFiles": {
  37. topic: function () {
  38. var that = this,
  39. created = 0;
  40. function data(ch) {
  41. return new Array(1018).join(String.fromCharCode(65 + ch));
  42. };
  43. function logKbytes(kbytes, txt) {
  44. //
  45. // With no timestamp and at the info level,
  46. // winston adds exactly 7 characters:
  47. // [info](4)[ :](2)[\n](1)
  48. //
  49. for (var i = 0; i < kbytes; i++) {
  50. maxfilesTransport.log('info', data(txt), null, function () { });
  51. }
  52. }
  53. maxfilesTransport.on('logged', function () {
  54. if (++created === 6) {
  55. return that.callback();
  56. }
  57. logKbytes(4, created);
  58. });
  59. logKbytes(4, created);
  60. },
  61. "should be only 3 files called 5.log, 4.log and 3.log": function () {
  62. for (var num = 0; num < 6; num++) {
  63. var file = !num ? 'testmaxfiles.log' : 'testmaxfiles' + num + '.log',
  64. fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);
  65. // There should be no files with that name
  66. if (num >= 0 && num < 3) {
  67. assert.throws(function () {
  68. fs.statSync(fullpath);
  69. }, Error);
  70. } else {
  71. // The other files should be exist
  72. assert.doesNotThrow(function () {
  73. fs.statSync(fullpath);
  74. }, Error);
  75. }
  76. }
  77. },
  78. "should have the correct content": function () {
  79. ['D', 'E', 'F'].forEach(function (name, inx) {
  80. var counter = inx + 3,
  81. logsDir = path.join(__dirname, '..', 'fixtures', 'logs'),
  82. content = fs.readFileSync(path.join(logsDir, 'testmaxfiles' + counter + '.log'), 'utf-8');
  83. // The content minus the 7 characters added by winston
  84. assert.lengthOf(content.match(new RegExp(name, 'g')), 4068);
  85. });
  86. }
  87. }
  88. }
  89. }
  90. }).export(module);