configuration-test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. var assert = require('assert')
  3. , vows = require('vows')
  4. , sandbox = require('sandboxed-module');
  5. function makeTestAppender() {
  6. return {
  7. configure: function(config, options) {
  8. this.configureCalled = true;
  9. this.config = config;
  10. this.options = options;
  11. return this.appender();
  12. },
  13. appender: function() {
  14. var self = this;
  15. return function(logEvt) { self.logEvt = logEvt; };
  16. }
  17. };
  18. }
  19. vows.describe('log4js configure').addBatch({
  20. 'appenders': {
  21. 'when specified by type': {
  22. topic: function() {
  23. var testAppender = makeTestAppender(),
  24. log4js = sandbox.require(
  25. '../lib/log4js',
  26. {
  27. requires: {
  28. './appenders/cheese': testAppender
  29. }
  30. }
  31. );
  32. log4js.configure(
  33. {
  34. appenders: [
  35. { type: "cheese", flavour: "gouda" }
  36. ]
  37. },
  38. { pants: "yes" }
  39. );
  40. return testAppender;
  41. },
  42. 'should load appender': function(testAppender) {
  43. assert.ok(testAppender.configureCalled);
  44. },
  45. 'should pass config to appender': function(testAppender) {
  46. assert.equal(testAppender.config.flavour, 'gouda');
  47. },
  48. 'should pass log4js options to appender': function(testAppender) {
  49. assert.equal(testAppender.options.pants, 'yes');
  50. }
  51. },
  52. 'when core appender loaded via loadAppender': {
  53. topic: function() {
  54. var testAppender = makeTestAppender(),
  55. log4js = sandbox.require(
  56. '../lib/log4js',
  57. { requires: { './appenders/cheese': testAppender } }
  58. );
  59. log4js.loadAppender('cheese');
  60. return log4js;
  61. },
  62. 'should load appender from ../lib/appenders': function(log4js) {
  63. assert.ok(log4js.appenders.cheese);
  64. },
  65. 'should add appender configure function to appenderMakers' : function(log4js) {
  66. assert.isFunction(log4js.appenderMakers.cheese);
  67. }
  68. },
  69. 'when appender in node_modules loaded via loadAppender': {
  70. topic: function() {
  71. var testAppender = makeTestAppender(),
  72. log4js = sandbox.require(
  73. '../lib/log4js',
  74. { requires: { 'some/other/external': testAppender } }
  75. );
  76. log4js.loadAppender('some/other/external');
  77. return log4js;
  78. },
  79. 'should load appender via require': function(log4js) {
  80. assert.ok(log4js.appenders['some/other/external']);
  81. },
  82. 'should add appender configure function to appenderMakers': function(log4js) {
  83. assert.isFunction(log4js.appenderMakers['some/other/external']);
  84. }
  85. },
  86. 'when configuration file loaded via LOG4JS_CONFIG environment variable': {
  87. topic: function() {
  88. process.env.LOG4JS_CONFIG = 'some/path/to/mylog4js.json';
  89. var fileRead = 0,
  90. modulePath = 'some/path/to/mylog4js.json',
  91. pathsChecked = [],
  92. mtime = new Date(),
  93. fakeFS = {
  94. config: { appenders: [ { type: 'console', layout: { type: 'messagePassThrough' } } ],
  95. levels: { 'a-test' : 'INFO' } },
  96. readdirSync: function(dir) {
  97. return require('fs').readdirSync(dir);
  98. },
  99. readFileSync: function (file, encoding) {
  100. fileRead += 1;
  101. assert.isString(file);
  102. assert.equal(file, modulePath);
  103. assert.equal(encoding, 'utf8');
  104. return JSON.stringify(fakeFS.config);
  105. },
  106. statSync: function (path) {
  107. pathsChecked.push(path);
  108. if (path === modulePath) {
  109. return { mtime: mtime };
  110. } else {
  111. throw new Error("no such file");
  112. }
  113. }
  114. },
  115. log4js = sandbox.require(
  116. '../lib/log4js',
  117. {
  118. requires: {
  119. 'fs': fakeFS,
  120. }
  121. }
  122. );
  123. delete process.env.LOG4JS_CONFIG;
  124. return fileRead;
  125. },
  126. 'should load the specified local configuration file' : function(fileRead) {
  127. assert.equal(fileRead, 1);
  128. }
  129. }
  130. }
  131. }).exportTo(module);