Gaël Métais 10 lat temu
rodzic
commit
2a316ebb1e

+ 5 - 3
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: {

+ 1 - 1
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) {
             

+ 2 - 6
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;
     };
 };
 

+ 16 - 12
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);
+
 
-    // TODO : error handler
-    /*run.catch(function(err) {
-        console.log('The run failed');
+        deferred.resolve(data);
+
+    }).fail(function(err) {
+        console.log('Run failed');
         console.log(err);
-    });*/
 
-    run.done(data);
+        deferred.reject(err);
+    });
+
 
-    return run;
+    return deferred.promise;
 };
 
 module.exports = Runner;

+ 3 - 2
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);
             }
         });
 

+ 0 - 1
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);
         });
     }

+ 31 - 24
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) {

+ 3 - 15
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);
     });
 });

+ 31 - 7
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.toolsResults.should.be.an('object');
                 
-                data.should.have.a.property('metrics').that.is.an('object');
-                data.metrics.should.have.a.property('requests').that.equals(1);
+                // 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.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.rules.should.have.a.property('DOMelementMaxDepth').that.is.an('object');
+                data.rules.DOMelementMaxDepth.should.deep.equal({
+                    policy: {
+                        "tool": "phantomas",
+                        "label": "DOM max depth",
+                        "message": "<p>A deep DOM makes the CSS matching with DOM elements difficult.</p><p>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.</p>",
+                        "isOkThreshold": 10,
+                        "isBadThreshold": 20,
+                        "isAbnormalThreshold": 30
+                    },
+                    "value": 1,
+                    "bad": false,
+                    "abnormal": false,
+                    "score": 100,
+                    "abnormalityScore": 0,
+                    "offenders": ["body > h1[1]"]
+                });
 
                 done();
             }).fail(function(err) {

+ 22 - 20
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
+            }
         }
     }
 }