From d69548354698b8a11ae4788f56cfcc16f3098d3a Mon Sep 17 00:00:00 2001 From: Stuart Campbell Date: Tue, 4 Aug 2015 11:47:30 +0100 Subject: [PATCH] Allow option to only fail build on errors and allow lower severities to pass --- complexity.json | 3 ++- tasks/complexity.js | 9 +++++++-- test/eventReporter-test.js | 12 ++++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/complexity.json b/complexity.json index 4a5faa2..1134560 100644 --- a/complexity.json +++ b/complexity.json @@ -7,6 +7,7 @@ "cyclomatic": 3, "halstead": 8, "maintainability": 100, - "broadcast": false + "broadcast": false, + "allowWarnings": true } } diff --git a/tasks/complexity.js b/tasks/complexity.js index d80be35..4744ae6 100644 --- a/tasks/complexity.js +++ b/tasks/complexity.js @@ -18,7 +18,8 @@ module.exports = function(grunt) { cyclomatic: [3, 7, 12], halstead: [8, 13, 20], maintainability: 100, - hideComplexFunctions: false + hideComplexFunctions: false, + allowWarnings: false }, normalizeOptions: function(options) { @@ -117,7 +118,11 @@ module.exports = function(grunt) { }, this); } - grunt.fail.errorcount += complicatedFunctions.length; + var errors = complicatedFunctions.filter(function(data){ + return data.severity === "error"; + }); + + grunt.fail.errorcount += (options.allowWarnings ? errors.length : complicatedFunctions.length); reporter.complexity(filepath, complicatedFunctions); diff --git a/test/eventReporter-test.js b/test/eventReporter-test.js index 69e3190..9ea75db 100644 --- a/test/eventReporter-test.js +++ b/test/eventReporter-test.js @@ -7,16 +7,16 @@ describe('Event Reporter', function() { it ('triggers a maintainability event', function(done) { var targetFile = __dirname + '/fixtures/sample.js'; - var reporter = Complexity.buildReporter([targetFile], { broadcast: true }) + var reporter = Complexity.buildReporter([targetFile], { broadcast: true }); grunt.event.on('grunt-complexity.maintainability', function(report) { - expect(report.filepath).to.equal(targetFile) - expect(report.valid).to.equal(true) - done() - }) + expect(report.filepath).to.equal(targetFile); + expect(report.valid).to.equal(true); + done(); + }); Complexity.analyze(reporter, [targetFile], Complexity.normalizeOptions({ maintainability: 0 - })) + })); }); });