/*
 * file-maxfiles-test.js: Tests for instances of the File transport setting the max file size,
 * and setting a number for max files created.
 * maxSize * maxFiles = total storage used by winston.
 *
 * (C) 2011 Daniel Aristizabal
 * 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 maxfilesTransport = new winston.transports.File({
  timestamp: false,
  json: false,
  filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles.log'),
  maxsize: 4096,
  maxFiles: 3
});
    
vows.describe('winston/transports/file/maxfiles').addBatch({
  "An instance of the File Transport": {
    "when passed a valid filename": {
      topic: maxfilesTransport,
      "should be a valid transporter": function (transportTest) {
        helpers.assertFile(transportTest);
      },
      "should set the maxFiles option correctly": function (transportTest) {
        assert.isNumber(transportTest.maxFiles);
      }
    },
    "when delete old test files": {
      topic: function () {
        exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles*'), this.callback);
      },
      "and when passed more files than the maxFiles": {
        topic: function () {
          var that = this,
              created = 0;
              
          function data(ch) {
            return new Array(1018).join(String.fromCharCode(65 + ch));
          };
      
          function logKbytes(kbytes, txt) {
            //
            // 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++) {
              maxfilesTransport.log('info', data(txt), null, function () { });
            }
          }
          
          maxfilesTransport.on('logged', function () {
            if (++created === 6) {
              return that.callback();
            }
            
            logKbytes(4, created);
          });
         
          logKbytes(4, created);
        },
        "should be only 3 files called 5.log, 4.log and 3.log": function () {
          for (var num = 0; num < 6; num++) {
            var file = !num ? 'testmaxfiles.log' : 'testmaxfiles' + num + '.log',
                fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);
            
            // There should be no files with that name
            if (num >= 0 && num < 3) {
              return assert.throws(function () {
                fs.statSync(file);
              }, Error);
            } 

            // The other files should be exist
            assert.doesNotThrow(function () {
              fs.statSync(file);
            }, Error);
          }
        },
        "should have the correct content": function () {
          ['D', 'E', 'F'].forEach(function (name, inx) {
            var counter = inx + 3,
                logsDir = path.join(__dirname, '..', 'fixtures', 'logs'),
                content = fs.readFileSync(path.join(logsDir, 'testmaxfiles' + counter + '.log'), 'utf-8');
            // The content minus the 7 characters added by winston
            assert.lengthOf(content.match(new RegExp(name, 'g')), 4068);
          });
        }
      }
    }
  }
}).export(module);
@@ -690,20 +690,16 @@
 : 3%0A%7D);%0A
-    
 %0Avows.de
@@ -1439,30 +1439,16 @@
 ed = 0;%0A
-              
 %0A       
@@ -1554,22 +1554,16 @@
      %7D;%0A
-      
 %0A       
@@ -1719,17 +1719,16 @@
 racters:
- 
 %0A       
@@ -1926,26 +1926,16 @@
       %7D%0A
-          
 %0A       
@@ -2069,28 +2069,16 @@
       %7D%0A
-            
 %0A       
@@ -2119,25 +2119,16 @@
     %7D);%0A
-         
 %0A       
@@ -2451,28 +2451,16 @@
  file);%0A
-            
 %0A       
@@ -2560,23 +2560,16 @@
         
-return 
 assert.t
@@ -2609,35 +2609,39 @@
    fs.statSync(f
-ile
+ullpath
 );%0A             
@@ -2666,18 +2666,25 @@
       %7D 
-%0A%0A
+else %7B%0A  
         
@@ -2730,24 +2730,26 @@
             
+  
 assert.doesN
@@ -2776,32 +2776,36 @@
 %7B%0A              
+ 
+   
 fs.statSync(file
@@ -2801,22 +2801,28 @@
 atSync(f
-ile
+ullpath
 );%0A
+  
         
@@ -2828,32 +2828,46 @@
      %7D, Error);%0A
+            %7D%0A
           %7D%0A    
@@ -3399,12 +3399,13 @@
 ort(module);
+%0A

fix maxfiles test