cocalc/src / smc-project / node_modules / forever / node_modules / winston / test / transports / file-maxfiles-test.js
50663 views/*1* file-maxfiles-test.js: Tests for instances of the File transport setting the max file size,2* and setting a number for max files created.3* maxSize * maxFiles = total storage used by winston.4*5* (C) 2011 Daniel Aristizabal6* MIT LICENSE7*8*/910var assert = require('assert'),11exec = require('child_process').exec,12fs = require('fs'),13path = require('path'),14vows = require('vows'),15winston = require('../../lib/winston'),16helpers = require('../helpers');1718var maxfilesTransport = new winston.transports.File({19timestamp: false,20json: false,21filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles.log'),22maxsize: 4096,23maxFiles: 324});2526vows.describe('winston/transports/file/maxfiles').addBatch({27"An instance of the File Transport": {28"when passed a valid filename": {29topic: maxfilesTransport,30"should be a valid transporter": function (transportTest) {31helpers.assertFile(transportTest);32},33"should set the maxFiles option correctly": function (transportTest) {34assert.isNumber(transportTest.maxFiles);35}36},37"when delete old test files": {38topic: function () {39exec('rm -rf ' + path.join(__dirname, '..', 'fixtures', 'logs', 'testmaxfiles*'), this.callback);40},41"and when passed more files than the maxFiles": {42topic: function () {43var that = this,44created = 0;4546function data(ch) {47return new Array(1018).join(String.fromCharCode(65 + ch));48};4950function logKbytes(kbytes, txt) {51//52// With no timestamp and at the info level,53// winston adds exactly 7 characters:54// [info](4)[ :](2)[\n](1)55//56for (var i = 0; i < kbytes; i++) {57maxfilesTransport.log('info', data(txt), null, function () { });58}59}6061maxfilesTransport.on('logged', function () {62if (++created === 6) {63return that.callback();64}6566logKbytes(4, created);67});6869logKbytes(4, created);70},71"should be only 3 files called 5.log, 4.log and 3.log": function () {72for (var num = 0; num < 6; num++) {73var file = !num ? 'testmaxfiles.log' : 'testmaxfiles' + num + '.log',74fullpath = path.join(__dirname, '..', 'fixtures', 'logs', file);7576// There should be no files with that name77if (num >= 0 && num < 3) {78return assert.throws(function () {79fs.statSync(file);80}, Error);81}8283// The other files should be exist84assert.doesNotThrow(function () {85fs.statSync(file);86}, Error);87}88},89"should have the correct content": function () {90['D', 'E', 'F'].forEach(function (name, inx) {91var counter = inx + 3,92logsDir = path.join(__dirname, '..', 'fixtures', 'logs'),93content = fs.readFileSync(path.join(logsDir, 'testmaxfiles' + counter + '.log'), 'utf-8');94// The content minus the 7 characters added by winston95assert.lengthOf(content.match(new RegExp(name, 'g')), 4068);96});97}98}99}100}101}).export(module);102103