diff --git a/Gruntfile.js b/Gruntfile.js index 25bf0fb..1644902 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -49,8 +49,10 @@ module.exports = function(grunt) { }, copy: { coverage: { - src: ['test/**'], - dest: 'coverage/' + files: [ + {src: ['test/**'], dest: 'coverage/'}, + {src: ['lib/metadata/**'], dest: 'coverage/'} + ] } }, blanket: { @@ -74,7 +76,7 @@ module.exports = function(grunt) { options: { reporter: 'spec', }, - src: ['coverage/test/api/rulesCheckerTest.js'] + src: ['coverage/test/api/yellowlabtoolsTest.js'] }, coverage: { options: { diff --git a/bin/cli.js b/bin/cli.js index e862f88..3937df3 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -23,7 +23,7 @@ var url = process.argv[2]; then(function(data) { console.log('Success'); - console.log(JSON.stringify(data.rules, null, 2)); + console.log(JSON.stringify(data, null, 2)); }).fail(function(err) { diff --git a/lib/rulesChecker.js b/lib/rulesChecker.js index d2659ac..042d73e 100644 --- a/lib/rulesChecker.js +++ b/lib/rulesChecker.js @@ -4,7 +4,6 @@ var RulesChecker = function() { 'use strict'; this.check = function(data, policies) { - var deferred = Q.defer(); var results = {}; var err = null; @@ -24,7 +23,7 @@ var RulesChecker = function() { if (data.toolsResults[policy.tool].offenders && data.toolsResults[policy.tool].offenders[metricName] && data.toolsResults[policy.tool].offenders[metricName].length > 0) { - //rule.offenders = data.toolsResults[policy.tool].offenders[metricName]; + rule.offenders = data.toolsResults[policy.tool].offenders[metricName]; } rule.bad = rule.value > policy.isOkThreshold; @@ -42,10 +41,7 @@ var RulesChecker = function() { } } - data.rules = results; - deferred.resolve(data); - - return deferred.promise; + return results; }; }; diff --git a/lib/runner.js b/lib/runner.js index 6270e09..67e641c 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -6,6 +6,8 @@ var rulesChecker = require('./rulesChecker'); var Runner = function(params) { 'use strict'; + var deferred = Q.defer(); + // The pivot format var data = { params: params, @@ -13,26 +15,28 @@ var Runner = function(params) { }; // Execute Phantomas first - var run = phantomasWrapper.execute(data); + phantomasWrapper.execute(data).then(function(phantomasResults) { + data.toolsResults.phantomas = phantomasResults; - // Other tools go here + // Other tools go there - // Read each policy and save the results - run.then(function() { + // Rules checker var policies = require('./metadata/policies.json'); - return rulesChecker.check(data, policies); + data.rules = rulesChecker.check(data, policies); + + + deferred.resolve(data); + + }).fail(function(err) { + console.log('Run failed'); + console.log(err); + + deferred.reject(err); }); - // TODO : error handler - /*run.catch(function(err) { - console.log('The run failed'); - console.log(err); - });*/ - run.done(data); - - return run; + return deferred.promise; }; module.exports = Runner; \ No newline at end of file diff --git a/lib/tools/phantomasWrapper.js b/lib/tools/phantomasWrapper.js index b1e89ef..7186941 100644 --- a/lib/tools/phantomasWrapper.js +++ b/lib/tools/phantomasWrapper.js @@ -97,8 +97,9 @@ var PhantomasWrapper = function() { console.log('All ' + triesNumber + ' attemps failed for the test'); deferred.reject(err); } else { - data.toolsResults.phantomas = json; - deferred.resolve(data); + + // Success!!! + deferred.resolve(json); } }); diff --git a/lib/yellowlabtools.js b/lib/yellowlabtools.js index 929339f..a0f4e38 100644 --- a/lib/yellowlabtools.js +++ b/lib/yellowlabtools.js @@ -30,7 +30,6 @@ var YellowLabTools = function(url, options) { var runner = new Runner(params); runner.then(function(data) { - console.log(data); deferred.resolve(data); }); } diff --git a/test/api/phantomasWrapperTest.js b/test/api/phantomasWrapperTest.js index a800abd..785bdbe 100644 --- a/test/api/phantomasWrapperTest.js +++ b/test/api/phantomasWrapperTest.js @@ -13,16 +13,19 @@ describe('phantomasWrapper', function() { this.timeout(15000); phantomasWrapper.execute({ - url: url, - options: {} - }).then(function(json) { - json.should.be.an('object'); - json.should.have.a.property('generator'); - json.generator.should.contain('phantomas'); - json.should.have.a.property('url').that.equals(url); - json.should.have.a.property('metrics').that.is.an('object').not.empty; - json.should.have.a.property('offenders').that.is.an('object').not.empty; - json.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('array').not.empty; + params: { + url: url, + options: {} + } + }).then(function(data) { + + data.should.be.an('object'); + data.should.have.a.property('generator'); + data.generator.should.contain('phantomas'); + data.should.have.a.property('url').that.equals(url); + data.should.have.a.property('metrics').that.is.an('object').not.empty; + data.should.have.a.property('offenders').that.is.an('object').not.empty; + data.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('array').not.empty; done(); }).fail(function(err) { @@ -36,9 +39,11 @@ describe('phantomasWrapper', function() { this.timeout(15000); phantomasWrapper.execute({ - url: url, - options: {} - }).then(function(json) { + params: { + url: url, + options: {} + } + }).then(function(data) { done('Error: unwanted success'); @@ -56,19 +61,21 @@ describe('phantomasWrapper', function() { this.timeout(5000); phantomasWrapper.execute({ - url: url, - options: { - timeout: 1 + params: { + url: url, + options: { + timeout: 1 + } } - }).then(function(json) { + }).then(function(data) { - json.should.be.an('object'); - json.should.have.a.property('generator'); - json.generator.should.contain('phantomas'); - json.should.have.a.property('url').that.equals(url); - json.should.have.a.property('metrics').that.is.an('object').not.empty; - json.should.have.a.property('offenders').that.is.an('object').not.empty; - json.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('array').not.empty; + data.should.be.an('object'); + data.should.have.a.property('generator'); + data.generator.should.contain('phantomas'); + data.should.have.a.property('url').that.equals(url); + data.should.have.a.property('metrics').that.is.an('object').not.empty; + data.should.have.a.property('offenders').that.is.an('object').not.empty; + data.offenders.should.have.a.property('javascriptExecutionTree').that.is.a('array').not.empty; done(); }).fail(function(err) { diff --git a/test/api/rulesCheckerTest.js b/test/api/rulesCheckerTest.js index d4e3ad7..841c1ab 100644 --- a/test/api/rulesCheckerTest.js +++ b/test/api/rulesCheckerTest.js @@ -7,24 +7,12 @@ describe('rulesChecker', function() { rulesChecker.should.have.property('check').that.is.a('function'); }); - it('should produce a nice rules object', function(done) { + it('should produce a nice rules object', function() { var data = require('../fixtures/rulesCheckerInput.json'); var policies = require('../fixtures/rulesCheckerPolicies.json'); var expected = require('../fixtures/rulesCheckerOutput.json'); - var checker = rulesChecker.check(data, policies); - - checker.then(function(results) { - try { - results.should.deep.equals(expected); - done(); - } catch(e) { - done(e); - } - }); - - checker.fail(function(err) { - done(err); - }); + var results = rulesChecker.check(data, policies); + results.should.deep.equals(expected); }); }); diff --git a/test/api/yellowlabtoolsTest.js b/test/api/yellowlabtoolsTest.js index 1e3eeff..ddc285e 100644 --- a/test/api/yellowlabtoolsTest.js +++ b/test/api/yellowlabtoolsTest.js @@ -32,16 +32,40 @@ describe('yellowlabtools', function() { var ylt = new YellowLabTools(url) .then(function(data) { - data.should.be.an('object'); - data.should.have.a.property('url').that.equals(url); - - data.should.have.a.property('metrics').that.is.an('object'); - data.metrics.should.have.a.property('requests').that.equals(1); - data.should.have.a.property('offenders').that.is.an('object'); - data.offenders.should.have.a.property('DOMelementMaxDepth'); - data.offenders.DOMelementMaxDepth.should.have.length(1); - data.offenders.DOMelementMaxDepth[0].should.equal('body > h1[1]'); + data.should.be.an('object'); + data.toolsResults.should.be.an('object'); + + // Test Phantomas + data.toolsResults.phantomas.should.be.an('object'); + data.toolsResults.phantomas.should.have.a.property('url').that.equals(url); + data.toolsResults.phantomas.should.have.a.property('metrics').that.is.an('object'); + data.toolsResults.phantomas.metrics.should.have.a.property('requests').that.equals(1); + data.toolsResults.phantomas.should.have.a.property('offenders').that.is.an('object'); + data.toolsResults.phantomas.offenders.should.have.a.property('DOMelementMaxDepth'); + data.toolsResults.phantomas.offenders.DOMelementMaxDepth.should.have.length(1); + data.toolsResults.phantomas.offenders.DOMelementMaxDepth[0].should.equal('body > h1[1]'); + + // Test rules + data.should.have.a.property('rules').that.is.an('object'); + + data.rules.should.have.a.property('DOMelementMaxDepth').that.is.an('object'); + data.rules.DOMelementMaxDepth.should.deep.equal({ + policy: { + "tool": "phantomas", + "label": "DOM max depth", + "message": "

A deep DOM makes the CSS matching with DOM elements difficult.

It also slows down Javascript modifications to the DOM because changing the dimensions of an element makes the browser re-calculate the dimensions of it's parents. Same thing for Javascript events, that bubble up to the document root.

", + "isOkThreshold": 10, + "isBadThreshold": 20, + "isAbnormalThreshold": 30 + }, + "value": 1, + "bad": false, + "abnormal": false, + "score": 100, + "abnormalityScore": 0, + "offenders": ["body > h1[1]"] + }); done(); }).fail(function(err) { diff --git a/test/fixtures/rulesCheckerInput.json b/test/fixtures/rulesCheckerInput.json index 96b949c..fd05a45 100644 --- a/test/fixtures/rulesCheckerInput.json +++ b/test/fixtures/rulesCheckerInput.json @@ -1,25 +1,27 @@ { - "tool1": { - "metrics": { - "metric1": 1236, - "metric2": 222, - "metric3": 6666, - "metric4": 1000, - "metric5": 3000, - "metric6": 0, - "metric7": 5000 + "toolsResults": { + "tool1": { + "metrics": { + "metric1": 1236, + "metric2": 222, + "metric3": 6666, + "metric4": 1000, + "metric5": 3000, + "metric6": 0, + "metric7": 5000 + }, + "offenders": { + "metric1": [], + "metric2": [], + "metric3": ["offender1", "offender2"], + "metric5": [] + } }, - "offenders": { - "metric1": [], - "metric2": [], - "metric3": ["offender1", "offender2"], - "metric5": [] - } - }, - "tool2": { - "metrics": { - "metric1": 22, - "metric10": 22 + "tool2": { + "metrics": { + "metric1": 22, + "metric10": 22 + } } } } \ No newline at end of file