file.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const path = require('path');
  3. const winston = require('../../');
  4. const helpers = require('../helpers');
  5. const fs = require('fs');
  6. const { MESSAGE } = require('triple-beam');
  7. const split = require('split2');
  8. const assume = require('assume');
  9. function noop() {};
  10. describe('File({ filename })', function () {
  11. this.timeout(10 * 1000);
  12. it('should write to the file when logged to with expected object', function (done) {
  13. var filename = path.join(__dirname, '..', 'fixtures', 'file', 'simple.log');
  14. var transport = new winston.transports.File({
  15. filename: filename
  16. });
  17. var info = { [MESSAGE]: 'this is my log message' };
  18. var logged = 0;
  19. var read = 0
  20. function cleanup() {
  21. fs.unlinkSync(filename);
  22. }
  23. transport.log(info, noop);
  24. setImmediate(function () {
  25. helpers.tryRead(filename)
  26. .on('error', function (err) {
  27. assume(err).false();
  28. cleanup();
  29. done();
  30. })
  31. .pipe(split())
  32. .on('data', function (d) {
  33. assume(++read).lte(logged);
  34. assume(d).to.equal(info[MESSAGE]);
  35. })
  36. .on('end', function () {
  37. cleanup();
  38. done();
  39. });
  40. });
  41. transport.once('logged', function () {
  42. logged++;
  43. });
  44. });
  45. //
  46. // TODO: Rewrite these tests in mocha
  47. //
  48. // "Error object in metadata #610": {
  49. // topic: function () {
  50. // var myErr = new Error("foo");
  51. //
  52. // fileTransport.log('info', 'test message', myErr, this.callback.bind(this, null, myErr));
  53. // },
  54. // "should not be modified": function (err, myErr) {
  55. // assert.equal(myErr.message, "foo");
  56. // // Not sure if this is the best possible way to check if additional props appeared
  57. // assert.deepEqual(Object.getOwnPropertyNames(myErr), Object.getOwnPropertyNames(new Error("foo")));
  58. // }
  59. // }
  60. //
  61. // "Date object in metadata": {
  62. // topic: function () {
  63. // var obj = new Date(1000);
  64. // fileTransport.log('info', 'test message', obj, this.callback.bind(this, null, obj));
  65. // },
  66. // "should not be modified": function (err, obj) {
  67. // // Not sure if this is the best possible way to check if additional props appeared
  68. // assert.deepEqual(Object.getOwnPropertyNames(obj), Object.getOwnPropertyNames(new Date()));
  69. // }
  70. // }
  71. //
  72. // "Plain object in metadata": {
  73. // topic: function () {
  74. // var obj = { message: "foo" };
  75. // fileTransport.log('info', 'test message', obj, this.callback.bind(this, null, obj));
  76. // },
  77. // "should not be modified": function (err, obj) {
  78. // assert.deepEqual(obj, { message: "foo" });
  79. // }
  80. // }
  81. //
  82. // "An instance of the File Transport": require('./transport')(winston.transports.File, {
  83. // filename: path.join(__dirname, '..', 'fixtures', 'logs', 'testfile.log')
  84. // })
  85. });
  86. describe('File({ stream })', function () {
  87. it('should display the deprecation notice');
  88. it('should write to the stream when logged to with expected object', function (done) {
  89. var streamfile = path.join(__dirname, '..', 'fixtures', 'file', 'simple-stream.log');
  90. var stream = fs.createWriteStream(streamfile);
  91. var streamTransport = new winston.transports.File({
  92. stream: stream
  93. });
  94. done();
  95. //
  96. // TODO: Flesh out these assertions
  97. //
  98. });
  99. });
  100. require('abstract-winston-transport')({
  101. name: 'File',
  102. Transport: winston.transports.File,
  103. construct: {
  104. filename: path.join(__dirname, '..', 'fixtures', 'file', 'abstract.log')
  105. },
  106. after(opts, done) {
  107. const abstractFile = opts.construct.filename;
  108. fs.unlink(abstractFile, done.bind(null, null));
  109. }
  110. });