file-test.js 4.4 KB

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