summaryrefslogtreecommitdiff
path: root/node_modules/winston/test/transports/file-maxsize-test.js
blob: 7d20e08938d97ca1d48a4dccb6b7d7679d60fb5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * file-test.js: Tests for instances of the File transport
 *
 * (C) 2010 Charlie Robbins
 * MIT LICENSE
 *
 */

var assert = require('assert'),
    exec = require('child_process').exec,
    fs = require('fs'),
    path = require('path'),
    vows = require('vows'),
    winston = require('../../lib/winston'),
    helpers = require('../helpers');

var maxsizeTransport = new winston.transports.File({
  timestamp: false,
  json: false,
  filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize.log'),
  maxsize: 4096
});
    
vows.describe('winston/transports/file/maxsize').addBatch({
  "An instance of the File Transport": {
    "when passed a valid filename": {
      "the log() method": {
        topic: function () {
          exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxsize*'), this.callback);
        },
        "when passed more than the maxsize": {
          topic: function () {
            var that = this,
                data = new Array(1018).join('-');
            
            //
            // Setup a list of files which we will later stat.
            //
            that.files = [];
            
            function logKbytes (kbytes) {
              //
              // With no timestamp and at the info level,
              // winston adds exactly 7 characters: 
              // [info](4)[ :](2)[\n](1)
              //
              for (var i = 0; i < kbytes; i++) {
                maxsizeTransport.log('info', data, null, function () { });
              }
            }
            
            maxsizeTransport.on('open', function (file) {
              var match = file.match(/(\d+)\.log$/),
                  count = match ? match[1] : 0;
              
              that.files.push(file);
              
              if (that.files.length === 5) {
                return that.callback();
              }
              
              logKbytes(4);
            });
            
            logKbytes(4);
          },
          "should create multiple files correctly": function () {
            this.files.forEach(function (file) {
              try {
                var stats = fs.statSync(file);
                assert.equal(stats.size, 4096);
              }
              catch (ex) {
                assert.isNull(ex);
              }
            });
          }
        }
      }
    }
  }
}).export(module);