file-maxsize-test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * file-test.js: Tests for instances of the File transport
  3. *
  4. * (C) 2010 Charlie Robbins
  5. * MIT LICENSE
  6. *
  7. */
  8. var assert = require('assert'),
  9. exec = require('child_process').exec,
  10. fs = require('fs'),
  11. path = require('path'),
  12. vows = require('vows'),
  13. winston = require('../../lib/winston'),
  14. helpers = require('../helpers');
  15. var maxsizeTransport = new winston.transports.File({
  16. timestamp: false,
  17. json: false,
  18. filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize.log'),
  19. maxsize: 4096
  20. });
  21. vows.describe('winston/transports/file/maxsize').addBatch({
  22. "An instance of the File Transport": {
  23. "when passed a valid filename": {
  24. "the log() method": {
  25. topic: function () {
  26. exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize*'), this.callback);
  27. },
  28. "when passed more than the maxsize": {
  29. topic: function () {
  30. var that = this,
  31. data = new Array(1018).join('-');
  32. //
  33. // Setup a list of files which we will later stat.
  34. //
  35. that.files = [];
  36. function logKbytes (kbytes) {
  37. //
  38. // With no timestamp and at the info level,
  39. // winston adds exactly 7 characters:
  40. // [info](4)[ :](2)[\n](1)
  41. //
  42. for (var i = 0; i < kbytes; i++) {
  43. maxsizeTransport.log('info', data, null, function () { });
  44. }
  45. }
  46. maxsizeTransport.on('open', function (file) {
  47. var match = file.match(/(\d+)\.log$/),
  48. count = match ? match[1] : 0;
  49. that.files.push(file);
  50. if (that.files.length === 5) {
  51. return that.callback();
  52. }
  53. logKbytes(4);
  54. });
  55. logKbytes(4);
  56. },
  57. "should create multiple files correctly": function () {
  58. this.files.forEach(function (file) {
  59. try {
  60. var stats = fs.statSync(file);
  61. assert.equal(stats.size, 4096);
  62. }
  63. catch (ex) {
  64. assert.isNull(ex);
  65. }
  66. });
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }).export(module);