file-maxfiles-test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 be a valid transporter": function (transportTest) {
  29. helpers.assertFile(transportTest);
  30. },
  31. "should set the maxFiles option correctly": function (transportTest) {
  32. assert.isNumber(transportTest.maxFiles);
  33. }
  34. },
  35. "when delete old test files": {
  36. topic: function () {
  37. exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles*'), this.callback);
  38. },
  39. "and when passed more files than the maxFiles": {
  40. topic: function () {
  41. var that = this,
  42. created = 0;
  43. function data(ch) {
  44. return new Array(1018).join(String.fromCharCode(65 + ch));
  45. };
  46. function logKbytes(kbytes, txt) {
  47. //
  48. // With no timestamp and at the info level,
  49. // winston adds exactly 7 characters:
  50. // [info](4)[ :](2)[\n](1)
  51. //
  52. for (var i = 0; i < kbytes; i++) {
  53. maxfilesTransport.log('info', data(txt), null, function () { });
  54. }
  55. }
  56. maxfilesTransport.on('logged', function () {
  57. if (++created === 6) {
  58. return that.callback();
  59. }
  60. logKbytes(4, created);
  61. });
  62. logKbytes(4, created);
  63. },
  64. "should be only 3 files called 5.log, 4.log and 3.log": function () {
  65. for (var num = 0; num < 6; num++) {
  66. var file = !num ? 'testmaxfiles.log' : 'testmaxfiles' + num + '.log',
  67. fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);
  68. // There should be no files with that name
  69. if (num >= 0 && num < 3) {
  70. assert.throws(function () {
  71. fs.statSync(fullpath);
  72. }, Error);
  73. } else {
  74. // The other files should be exist
  75. assert.doesNotThrow(function () {
  76. fs.statSync(fullpath);
  77. }, Error);
  78. }
  79. }
  80. },
  81. "should have the correct content": function () {
  82. ['D', 'E', 'F'].forEach(function (name, inx) {
  83. var counter = inx + 3,
  84. logsDir = path.join(__dirname, '..', 'fixtures', 'logs'),
  85. content = fs.readFileSync(path.join(logsDir, 'testmaxfiles' + counter + '.log'), 'utf-8');
  86. // The content minus the 7 characters added by winston
  87. assert.lengthOf(content.match(new RegExp(name, 'g')), 4068);
  88. });
  89. }
  90. }
  91. }
  92. }
  93. }).export(module);