UNPKG

4.53 kBJavaScriptView Raw
1/*
2 * file-test.js: Tests for instances of the File transport
3 *
4 * (C) 2010 Charlie Robbins
5 * MIT LICENSE
6 *
7 */
8
9var path = require('path'),
10 vows = require('vows'),
11 fs = require('fs'),
12 assert = require('assert'),
13 winston = require('../../lib/winston'),
14 stdMocks = require('std-mocks'),
15 helpers = require('../helpers');
16
17var transport = require('./transport');
18
19var stream = fs.createWriteStream(
20 path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log')
21 ),
22 fileTransport = new (winston.transports.File)({
23 filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testfilename.log')
24 }),
25 failedFileTransport = new (winston.transports.File)({
26 filename: path.join(__dirname, '..', 'fixtures', 'logs', 'dir404', 'testfile.log')
27 }),
28 streamTransport = new (winston.transports.File)({ stream: stream });
29
30vows.describe('winston/transports/file').addBatch({
31 "An instance of the File Transport": {
32 "when passed a valid filename": {
33 "should have the proper methods defined": function () {
34 helpers.assertFile(fileTransport);
35 },
36 "the log() method": helpers.testNpmLevels(fileTransport, "should respond with true", function (ign, err, logged) {
37 assert.isNull(err);
38 assert.isTrue(logged);
39 })
40 },
41 "when passed an invalid filename": {
42 "should have proper methods defined": function () {
43 helpers.assertFile(failedFileTransport);
44 },
45 "should enter noop failed state": function () {
46 helpers.assertFailedTransport(failedFileTransport);
47 }
48 },
49 "when passed a valid file stream": {
50 "should have the proper methods defined": function () {
51 helpers.assertFile(streamTransport);
52 },
53 "the log() method": helpers.testNpmLevels(streamTransport, "should respond with true", function (ign, err, logged) {
54 assert.isNull(err);
55 assert.isTrue(logged);
56 })
57 },
58 "streaming to stdout": {
59 topic: function () {
60 var transport = new (winston.transports.File)({
61 stream: process.stdout, timestamp: false, json: false
62 });
63 stdMocks.use();
64 return transport;
65 },
66 "with showLevel off": {
67 topic: function (stdoutStreamTransport) {
68 stdoutStreamTransport.showLevel = false;
69 stdoutStreamTransport.log('info', '', undefined, this.callback);
70 },
71 "should not have level prepended": function () {
72 var output = stdMocks.flush(),
73 line = output.stdout[0];
74
75 assert.equal(line, '\n');
76 }
77 },
78 // there would be a "with showLevel on" here but I think it's a bug in
79 // this version of vows. ugprading causes even more problems
80 teardown: function() {
81 stdMocks.restore();
82 }
83 }
84 }
85}).addBatch({
86 "These tests have a non-deterministic end": {
87 topic: function () {
88 setTimeout(this.callback, 200);
89 },
90 "and this should be fixed before releasing": function () {
91 assert.isTrue(true);
92 }
93 }
94}).addBatch({
95 "Error object in metadata #610": {
96 topic: function () {
97 var myErr = new Error("foo");
98
99 fileTransport.log('info', 'test message', myErr, this.callback.bind(this, null, myErr));
100 },
101 "should not be modified": function (err, myErr) {
102 assert.equal(myErr.message, "foo");
103 // Not sure if this is the best possible way to check if additional props appeared
104 assert.deepEqual(Object.getOwnPropertyNames(myErr), Object.getOwnPropertyNames(new Error("foo")));
105 }
106 }
107}).addBatch({
108 "Date object in metadata": {
109 topic: function () {
110 var obj = new Date(1000);
111
112 fileTransport.log('info', 'test message', obj, this.callback.bind(this, null, obj));
113 },
114 "should not be modified": function (err, obj) {
115 // Not sure if this is the best possible way to check if additional props appeared
116 assert.deepEqual(Object.getOwnPropertyNames(obj), Object.getOwnPropertyNames(new Date()));
117 }
118 }
119}).addBatch({
120 "Plain object in metadata": {
121 topic: function () {
122 var obj = { message: "foo" };
123
124 fileTransport.log('info', 'test message', obj, this.callback.bind(this, null, obj));
125 },
126 "should not be modified": function (err, obj) {
127 assert.deepEqual(obj, { message: "foo" });
128 }
129 }
130}).addBatch({
131 "An instance of the File Transport": transport(winston.transports.File, {
132 filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log')
133 })
134}).export(module);