From f0e691f1f628f04ddaacbd40c1d81fbb71734b36 Mon Sep 17 00:00:00 2001 From: Nazar Hussain Date: Thu, 2 Aug 2018 13:15:57 +0200 Subject: [PATCH] Add config option to supress console warning messages --- index.js | 8 +++++--- test/index.js | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 510843d..1a320cc 100644 --- a/index.js +++ b/index.js @@ -218,7 +218,9 @@ function Runner(appJsConfig, cb) { if (warnings && warnings.length > 0) { var warningText = JSON.stringify(warnings); if (self.config.swagger.startWithWarnings) { - console.error(warningText, 2); + if(!self.config.swagger.suppressWarnings) { + console.error(warningText, 2); + } } else { var err = new Error('Swagger validation warnings:'); err.validationWarnings = warnings; @@ -245,7 +247,7 @@ function Runner(appJsConfig, cb) { console.error("\t#" + i + ".: " + err.validationErrors[i].message + " in swagger config at: >" + err.validationErrors[i].path.join('/') + "<"); } } - + process.nextTick(function() { throw err; }); }) } @@ -330,4 +332,4 @@ function readEnvConfig() { }); debug('loaded env vars: %j', config); return config; -} \ No newline at end of file +} diff --git a/test/index.js b/test/index.js index 1697307..e60f7fb 100644 --- a/test/index.js +++ b/test/index.js @@ -4,6 +4,7 @@ var should = require('should'); var path = require('path'); var _ = require('lodash'); var util = require('util'); +var sinon = require('sinon'); var SwaggerRunner = require('..'); @@ -375,7 +376,7 @@ describe('index', function() { }); }); }); - + it('should accept null body from pipe interface', function(done) { var config = _.clone(DEFAULT_PROJECT_CONFIG); config.configDir = path.resolve(DEFAULT_PROJECT_ROOT, "config_auto"); @@ -400,7 +401,6 @@ describe('index', function() { }); }); - it('should fail without callback', function() { (function() { SwaggerRunner.create(DEFAULT_PROJECT_CONFIG) }).should.throw('callback is required'); }); @@ -445,14 +445,32 @@ describe('index', function() { it('should continue with swagger warnings if startWithWarnings is true', function(done) { var config = _.clone(DEFAULT_PROJECT_CONFIG); + sinon.spy(console, 'error'); config.startWithWarnings = true; config.swagger = SWAGGER_WITH_WARNINGS; SwaggerRunner.create(config, function(err, runner) { should.not.exist(err); + console.error.calledOnce.should.be.true(); + console.error.calledWith('[{"code":"UNUSED_DEFINITION","message":"Definition is not used: #/definitions/SomeUnusedDefinition","path":["definitions","SomeUnusedDefinition"]}]').should.be.true(); + console.error.restore(); done(); }); }); + it('should continue with swagger warnings and not show error in console if startWithWarnings is true and suppressWarnings is true', function(done) { + var config = _.clone(DEFAULT_PROJECT_CONFIG); + sinon.spy(console, 'error'); + config.startWithWarnings = true; + config.suppressWarnings = true; + config.swagger = SWAGGER_WITH_WARNINGS; + SwaggerRunner.create(config, function(err, runner) { + should.not.exist(err); + console.error.notCalled.should.be.true(); + console.error.restore(); + done(); + }); + }); + it('should allow paths using global security', function(done) { var config = _.clone(DEFAULT_PROJECT_CONFIG); config.startWithWarnings = true;