Skip to content
Browse files

bump

  • Loading branch information...
1 parent 90d1878 commit 668dd9d3cb458f4cdbd7cf48f7194d14d154ea23 Kir Belevich committed
Showing with 320 additions and 40 deletions.
  1. +73 −0 .eslintrc
  2. +33 −0 .jscs.json
  3. +14 −0 .travis.yml
  4. +12 −0 Makefile
  5. +66 −1 README.md
  6. +11 −4 index.js
  7. +11 −9 lib/{adapters/mocha.js → adapter.js}
  8. +8 −1 lib/bridge.js
  9. +18 −4 lib/cli.js
  10. +2 −2 lib/inject.html
  11. +10 −6 lib/reporters/json.js
  12. +10 −6 lib/reporters/lcov.js
  13. +42 −0 lib/reporters/plain.js
  14. +10 −7 package.json
View
73 .eslintrc
@@ -0,0 +1,73 @@
+{
+ "env": {
+ "node": true,
+ "browser": true
+ },
+
+ "rules": {
+ "no-debugger": 2,
+ "no-empty": 2,
+ "no-obj-calls": 2,
+ "no-unreachable": 2,
+ "no-dupe-keys": 2,
+ "no-func-assign": 2,
+ "no-empty-class": 2,
+ "no-caller": 2,
+ "no-eval": 2,
+ "no-with": 2,
+ "no-undef": 2,
+ "no-undef-init": 2,
+ "no-delete-var": 2,
+ "no-label-var": 2,
+ "no-self-compare": 2,
+ "no-eq-null": 2,
+ "wrap-iife": 2,
+ "curly": 2,
+ "eqeqeq": 2,
+ "use-isnan": 2,
+ "camelcase": 2,
+ "quotes": [2, "single"],
+ "no-loop-func": 2,
+ "no-empty-label": 2,
+ "no-unused-vars": 2,
+ "no-script-url": 2,
+ "no-shadow": 2,
+ "no-wrap-func": 2,
+ "semi": 2,
+ "strict": 2,
+
+ "no-comma-dangle": 1,
+ "no-catch-shadow": 1,
+ "no-new": 1,
+ "no-multi-str": 1,
+ "no-proto": 1,
+ "no-iterator": 1,
+ "no-else-return": 1,
+ "no-use-before-define": 1,
+ "no-mixed-requires": 1,
+ "no-new-array": 1,
+ "no-new-object": 1,
+ "no-new-wrappers": 1,
+ "no-octal": 1,
+ "new-parens": 1,
+ "new-cap": 1,
+ "no-console": 1,
+ "dot-notation": 1,
+ "unnecessary-strict": 1,
+ "consistent-this": [1, "that"],
+ "complexity": [1, 10],
+ "no-bitwise": 1,
+ "guard-for-in": 1,
+
+ "no-alert": 0,
+ "no-unused-expressions": 0,
+ "no-global-strict": 0,
+ "no-return-assign": 0,
+ "no-underscore-dangle": 0,
+ "no-fallthrough": 0,
+ "one-var": 0,
+ "quote-props": 0,
+ "brace-style": 0,
+ "no-ternary": 0
+ }
+}
View
33 .jscs.json
@@ -0,0 +1,33 @@
+{
+ "excludeFiles": [
+ "node_modules/**",
+ "test/**"
+ ],
+ "requireCurlyBraces": ["if", "else", "for", "while", "do"],
+ "requireSpaceAfterKeywords": ["do", "else"],
+ "disallowSpaceAfterKeywords": ["if", "for", "while", "switch"],
+ "disallowSpacesInFunctionExpression": {
+ "beforeOpeningRoundBrace": true
+ },
+ "requireSpacesInsideObjectBrackets": "all",
+ "requireSpacesInsideArrayBrackets": "all",
+ "disallowQuotedKeysInObjects": true,
+ "disallowSpaceAfterObjectKeys": true,
+ "requireLeftStickedOperators": [","],
+ "disallowLeftStickedOperators": [
+ "?", "+", "-", "/", "*", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="
+ ],
+ "requireRightStickedOperators": ["!"],
+ "disallowRightStickedOperators": [
+ "?", "-", "/", "*", ":", ",", "=", "==", "===", "!=", "!==", ">", ">=", "<", "<="
+ ],
+ "disallowKeywords": ["with"],
+ "disallowMultipleLineBreaks": true,
+ "disallowKeywordsOnNewLine": ["else"],
+ "requireLineFeedAtFileEnd": true,
+ "validateJSDoc": {
+ "checkParamNames": true,
+ "checkRedundantParams": true,
+ "requireParamTypes": true
+ }
+}
View
14 .travis.yml
@@ -0,0 +1,14 @@
+language: node_js
+
+node_js:
+ - 0.8
+ - 0.10
+ - 0.11
+
+matrix:
+ allow_failures:
+ - node_js: 0.11
+
+branches:
+ only:
+ - master
View
12 Makefile
@@ -0,0 +1,12 @@
+BIN = ./node_modules/.bin
+
+.PHONY: eslint
+eslint:
+ @$(BIN)/eslint index.js lib/
+
+.PHONY: jscs
+jscs:
+ @$(BIN)/jscs index.js lib/
+
+.PHONY: test
+test: eslint jscs
View
67 README.md
@@ -1,3 +1,68 @@
### PhantomJS + Mocha + Blanket = Poncho
-more info coming soon.
+Client-side JS code coverage using [PhantomJS](https://github.com/ariya/phantomjs), [Mocha](https://github.com/visionmedia/mocha) and [Blanket](https://github.com/alex-seville/blanket).
+
+#### Install
+
+```sh
+$ npm i --save-dev poncho
+```
+
+#### Usage
+
+Let's imagine that you already have PhantomJS + Mocha client-side tests (with [mocha-phantomjs](https://github.com/metaskills/mocha-phantomjs), for example), something like this `test/test.html`:
+
+```html
+<!DOCTYPE html>
+
+<head>
+ <meta charset="utf-8"/>
+ <title>Mocha Test</title>
+ <link rel="stylesheet" href="mocha/mocha.css"/>
+</head>
+
+<body>
+ <div id="mocha"></div>
+
+ <!-- mocha -->
+ <script src="mocha/mocha.js"></script>
+ <script>mocha.setup('bdd');</script>
+
+ <!-- target script -->
+ <script src="script.js"></script>
+ <!-- test file -->
+ <script src="test.js"></script>
+
+ <!-- run -->
+ <script>window.mochaPhantomJS ? mochaPhantomJS.run() : mocha.run();</script>
+</body>
+```
+
+All you need to do is add `data-cover` attribute to the target script tag:
+
+```html
+<script src="script.js" data-cover></script>
+```
+
+…and run Poncho:
+
+```sh
+$ poncho --reporter num test/test.html
+```
+
+#### Send data to [coveralls.io](https://coveralls.io/)
+
+Install [node-coveralls](https://github.com/cainus/node-coveralls):
+
+```sh
+$ npm i --save-dev coveralls
+```
+…and pipe Poncho's `lcov` reporter to it:
+
+```sh
+$ poncho --reporter lcov test/test.html | coveralls
+```
+
+#### How it works?
+
+You don't want to know. Seriosly. It's so hacky way that it can cause [the blood from your eyes](http://funkyimg.com/i/EdqD.gif).
View
15 index.js
@@ -1,3 +1,5 @@
+'use strict';
+
var http = require('http'),
path = require('path'),
Static = require('node-static'),
@@ -17,19 +19,21 @@ module.exports = function(params, callback) {
// create HTTP server
server = http.createServer(function(req, res) {
- var data = '';
+ var post = '';
// if coverage was requested – collect POST data…
if(req.url === '/coverage') {
req.on('data', function(chunk) {
- data += chunk;
+ post += chunk;
});
}
req.on('end', function() {
- if(data) {
+ if(post) {
// …and callback it
- callback(data);
+ post = JSON.parse(post);
+ callback(post.data);
+
server.close();
}
@@ -51,6 +55,8 @@ module.exports = function(params, callback) {
params.file,
// server port
params.port,
+ // reporter
+ params.reporter
]);
// phantomjs.stderr.pipe(process.stdout);
@@ -59,6 +65,7 @@ module.exports = function(params, callback) {
// bind any PhantomJS errors to current process
phantomjs.on('exit', function(code) {
if(code === 127) {
+ /*eslint no-console:0*/
console.error('PhantomJS is not installed?');
}
View
20 lib/adapters/mocha.js → lib/adapter.js
@@ -1,35 +1,37 @@
(function() {
+ 'use strict';
+
(function(runner) {
runner.on('start', function() {
- blanket.setupCoverage();
+ window.blanket.setupCoverage();
});
runner.on('end', function() {
- blanket.onTestsDone();
+ window.blanket.onTestsDone();
});
runner.on('suite', function() {
- blanket.onModuleStart();
+ window.blanket.onModuleStart();
});
runner.on('test', function() {
- blanket.onTestStart();
+ window.blanket.onTestStart();
});
runner.on('test end', function(test) {
- blanket.onTestDone(test.parent.tests.length, test.state === 'passed');
+ window.blanket.onTestDone(test.parent.tests.length, test.state === 'passed');
});
- })(Mocha.Runner.prototype);
+ })(window.Mocha.Runner.prototype);
- var oldRun = mocha.run,
+ var oldRun = window.mocha.run,
oldCallback;
- mocha.run = function(finishCallback) {
+ window.mocha.run = function(finishCallback) {
oldCallback = finishCallback;
};
- blanket.beforeStartTestRunner({
+ window.blanket.beforeStartTestRunner({
callback: function(){
oldRun(oldCallback);
}
View
9 lib/bridge.js
@@ -1,4 +1,7 @@
-var page = require('webpage').create(),
+/*global phantom*/
+'use strict';
+
+var page = require('webpage'),
args = require('system').args,
fs = require('fs');
@@ -6,11 +9,14 @@ var ponchoDir = args[1],
injectPath = args[2],
filePath = args[3],
port = args[4],
+ reporter = args[5],
content,
inject,
urlGet = 'http://127.0.0.1:' + port + '/' + filePath,
urlPost = 'http://127.0.0.1:' + port + '/coverage';
+page = page.create();
+
page.onAlert = function(data) {
page.open(urlPost, 'POST', data, phantom.exit);
};
@@ -21,6 +27,7 @@ content = fs.read(filePath);
inject = fs.read(injectPath);
inject = inject.split('{PONCHO}').join(ponchoDir);
+inject = inject.replace('{REPORTER}', reporter);
content = content.replace(/(<script.+?data-cover)/, inject + '$1');
View
22 lib/cli.js
@@ -1,16 +1,30 @@
+/*eslint no-console:0*/
+'use strict';
+
var program = require('commander'),
pkg = require('../package.json'),
poncho = require('../');
program
.version(pkg.version)
- .usage('<file>')
+ .usage('[options] <file>')
+ .option('-R, --reporter [type]', 'reporter: plain (default) | lcov | json')
.parse(process.argv);
if(!program.args.length) {
program.help();
}
-poncho({ file: program.args[0] }, function(data) {
- console.log(JSON.parse(data));
-});
+poncho(
+ {
+ file: program.args[0],
+ reporter: program.reporter || 'plain'
+ },
+ function(data) {
+ if(typeof data === 'object') {
+ data = JSON.stringify(data);
+ }
+
+ console.log(data);
+ }
+);
View
4 lib/inject.html
@@ -1,3 +1,3 @@
<script src="{PONCHO}/node_modules/blanket/dist/qunit/blanket.min.js"></script>
-<script src="{PONCHO}/lib/adapters/mocha.js"></script>
-<script>blanket.options("reporter", "{PONCHO}/lib/reporters/json.js");</script>
+<script src="{PONCHO}/lib/adapter.js"></script>
+<script>blanket.options("reporter", "{PONCHO}/lib/reporters/{REPORTER}.js");</script>
View
16 lib/reporters/json.js
@@ -1,14 +1,18 @@
-blanket.customReporter = function(report) {
+window.blanket.customReporter = function(report) {
+
+ /*globals alert*/
+ /*eslint guard-for-in:0*/
+ 'use strict';
var a = document.createElement('a'),
out = [];
- for(var filename in report.files) {
- a.href = filename;
+ for(var file in report.files) {
+ a.href = file;
- var data = report.files[filename],
+ var data = report.files[file],
ret = {
- filename: a.pathname.slice(1),
+ file: a.pathname.slice(1),
coverage: 0,
hits: 0,
misses: 0,
@@ -29,6 +33,6 @@ blanket.customReporter = function(report) {
out.push(ret);
}
- alert(JSON.stringify(out));
+ alert(JSON.stringify({ data: out }));
};
View
16 lib/reporters/lcov.js
@@ -1,19 +1,23 @@
-blanket.customReporter = function(report) {
+window.blanket.customReporter = function(report) {
+
+ /*globals alert*/
+ /*eslint guard-for-in:0*/
+ 'use strict';
var a = document.createElement('a'),
str = '';
- for(var filename in report.files) {
- var data = report.files[filename];
+ for(var file in report.files) {
+ var data = report.files[file];
- a.href = filename;
+ a.href = file;
str += 'SF:' + a.pathname.slice(1) + '\n';
for(var i = 0; i < data.source.length; i++) {
var num = i + 1;
- if (data[num] !== undefined) {
+ if(data[num] !== undefined) {
str += 'DA:' + num + ',' + data[num] + '\n';
}
}
@@ -21,6 +25,6 @@ blanket.customReporter = function(report) {
str += 'end_of_record\n';
}
- alert(str);
+ alert(JSON.stringify({ data: str }));
};
View
42 lib/reporters/plain.js
@@ -0,0 +1,42 @@
+window.blanket.customReporter = function(report) {
+
+ /*globals alert*/
+ /*eslint guard-for-in:0*/
+ 'use strict';
+
+ var a = document.createElement('a'),
+ out = '';
+
+ for(var file in report.files) {
+ a.href = file;
+
+ var data = report.files[file],
+ ret = {
+ file: a.pathname.slice(1),
+ coverage: 0,
+ hits: 0,
+ misses: 0,
+ sloc: 0
+ };
+
+ for(var i = 1; i <= data.source.length; i++) {
+ if(data[i] === 0) {
+ ret.misses++;
+ ret.sloc++;
+ } else if(data[i] !== undefined) {
+ ret.hits++;
+ ret.sloc++;
+ }
+ }
+ ret.coverage = ret.hits / ret.sloc * 100;
+
+ out += (out ? '\n\n' : '') + 'file: ' + ret.file + '\n';
+ out += 'coverage: ' + ret.coverage + '\n';
+ out += 'hist: ' + ret.hits + '\n';
+ out += 'misses: ' + ret.misses + '\n';
+ out += 'sloc: ' + ret.sloc;
+ }
+
+ alert(JSON.stringify({ data: out }));
+
+};
View
17 package.json
@@ -1,6 +1,6 @@
{
"name": "poncho",
- "version": "0.0.0",
+ "version": "0.1.0",
"description": "Client-side JS code coverage using PhantomJS, Mocha and Blanket",
"keywords": [ "phantomjs", "blanket", "coverage", "coveralls" ],
"homepage": "https://github.com/deepsweet/poncho",
@@ -13,18 +13,21 @@
"poncho": "./bin/poncho"
},
"dependencies": {
- "commander": "",
- "phantomjs": "",
- "blanket": "",
- "node-static": ""
+ "commander": "~2.1.0",
+ "phantomjs": "~1.9.2-4",
+ "blanket": "~1.1.5",
+ "node-static": "~0.7.2"
},
"devDependencies": {
- "eslint": "",
- "jscs": ""
+ "eslint": "~0.1.3",
+ "jscs": "~1.0.13"
},
"scripts": {
"test": "make test"
},
+ "engines": {
+ "node": ">=0.8.0"
+ },
"licenses": [{
"type": "MIT",
"url": "https://github.com/deepsweet/poncho/blob/master/LICENSE"

0 comments on commit 668dd9d

Please sign in to comment.
Something went wrong with that request. Please try again.