Permalink
Browse files

init

  • Loading branch information...
0 parents commit 24a389f8113754c056cbca3d90a536ba3d6a2ec5 Kir Belevich committed Nov 30, 2013
Showing with 332 additions and 0 deletions.
  1. +16 −0 .editorconfig
  2. +6 −0 .gitignore
  3. +24 −0 LICENSE
  4. +3 −0 README.md
  5. +3 −0 bin/poncho
  6. +7 −0 examples/script.js
  7. +34 −0 examples/test.html
  8. +11 −0 examples/test.js
  9. +65 −0 index.js
  10. +38 −0 lib/adapters/mocha.js
  11. +17 −0 lib/bridge.js
  12. +16 −0 lib/cli.js
  13. +34 −0 lib/reporters/json.js
  14. +26 −0 lib/reporters/lcov.js
  15. +32 −0 package.json
@@ -0,0 +1,16 @@
+; editorconfig.org
+root = true
+
+[*]
+indent_style = space
+indent_size = 4
+end_of_line = lf
+charset = utf-8
+trim_trailing_whitespace = true
+insert_final_newline = true
+
+[Makefile]
+indent_style = tab
+
+[*.json]
+indent_size = 2
@@ -0,0 +1,6 @@
+node_modules/
+tmp/
+*.sublime-*
+*.log
+.DS_Store
+.coveralls.yml
@@ -0,0 +1,24 @@
+The MIT License
+
+Copyright © 2013 Kir Belevich
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
+### PhantomJS + Blanket = Poncho
+
+more info coming soon.
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('../lib/cli.js');
@@ -0,0 +1,7 @@
+window.greeting = function(a) {
+ if(a === 1) {
+ return 'test';
+ };
+
+ return 'hello';
+};
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+
+<head>
+ <meta charset="utf-8"/>
+ <title>Mocha Test</title>
+ <base href="../node_modules/"/>
+ <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>
+
+ <!-- blanket -->
+ <script src="blanket/dist/qunit/blanket.min.js"></script>
+ <script src="../lib/adapters/mocha.js"></script>
+ <script>
+ if(window._phantom) {
+ blanket.options('reporter', '../lib/reporters/json.js');
+ }
+ </script>
+
+ <!-- target -->
+ <script src="../examples/script.js" data-cover></script>
+
+ <!-- test -->
+ <script src="../examples/test.js"></script>
+
+ <!-- run mocha -->
+ <script>mocha.run()</script>
+</body>
@@ -0,0 +1,11 @@
+function assert(expr, msg) {
+ if (!expr) throw new Error(msg || 'failed');
+}
+
+describe('test', function() {
+
+ it('must return a greeting', function() {
+ assert(window.greeting() === 'hello!');
+ });
+
+});
@@ -0,0 +1,65 @@
+var http = require('http'),
+ path = require('path'),
+ Static = require('node-static'),
+ spawn = require('child_process').spawn;
+
+module.exports = function(params, callback) {
+
+ // default port
+ params.port = params.port || '8082';
+
+ var file = new Static.Server('./', {
+ cache: false,
+ headers: { 'Cache-Control': 'no-cache, must-revalidate, max-age=0' }
+ }),
+ server;
+
+ // create HTTP server
+ server = http.createServer(function(req, res) {
+
+ var data = '';
+
+ // if coverage was requested – collect POST data…
+ if(req.url === '/coverage') {
+ req.on('data', function(chunk) {
+ data += chunk;
+ });
+ }
+
+ req.on('end', function() {
+ if(data) {
+ // …and callback it
+ callback(data);
+ server.close();
+ }
+
+ // serve static files
+ file.serve(req, res);
+ }).resume();
+
+ }).listen(params.port);
+
+ // spawn PhantomJS with arguments
+ var phantomjs = spawn('phantomjs', [
+ path.resolve(__dirname, './lib/bridge.js'),
+ params.filename,
+ params.port
+ ]);
+
+ // phantomjs.stderr.pipe(process.stdout);
+ // phantomjs.stdout.pipe(process.stdout);
+
+ // bind any PhantomJS errors to current process
+ phantomjs.on('exit', function(code) {
+ if(code === 127) {
+ console.error('Perhaps phantomjs is not installed?\n');
+ }
+
+ if(code > 0) {
+ server.close(function() {
+ process.exit(code);
+ });
+ }
+ });
+
+};
@@ -0,0 +1,38 @@
+(function() {
+
+ (function(runner) {
+ runner.on('start', function() {
+ blanket.setupCoverage();
+ });
+
+ runner.on('end', function() {
+ blanket.onTestsDone();
+ });
+
+ runner.on('suite', function() {
+ blanket.onModuleStart();
+ });
+
+ runner.on('test', function() {
+ blanket.onTestStart();
+ });
+
+ runner.on('test end', function(test) {
+ blanket.onTestDone(test.parent.tests.length, test.state === 'passed');
+ });
+ })(Mocha.Runner.prototype);
+
+ var oldRun = mocha.run,
+ oldCallback;
+
+ mocha.run = function(finishCallback) {
+ oldCallback = finishCallback;
+ };
+
+ blanket.beforeStartTestRunner({
+ callback: function(){
+ oldRun(oldCallback);
+ }
+ });
+
+})();
@@ -0,0 +1,17 @@
+var page = require('webpage').create(),
+ args = require('system').args;
+
+var filename = args[1],
+ port = args[2],
+ urlGet = 'http://127.0.0.1:' + port + '/' + filename,
+ urlPost = 'http://127.0.0.1:' + port + '/coverage';
+
+page.onAlert = function(data) {
+ page.open(urlPost, 'POST', data, phantom.exit);
+};
+
+page.open(urlGet, function(status) {
+ if(status !== 'success') {
+ phantom.exit(1);
+ }
+});
@@ -0,0 +1,16 @@
+var program = require('commander'),
+ pkg = require('../package.json'),
+ poncho = require('../');
+
+program
+ .version(pkg.version)
+ .usage('<file>')
+ .parse(process.argv);
+
+if(!program.args.length) {
+ program.help();
+}
+
+poncho({ filename: program.args[0] }, function(data) {
+ console.log(JSON.parse(data));
+});
@@ -0,0 +1,34 @@
+blanket.customReporter = function(report) {
+
+ var a = document.createElement('a'),
+ out = [];
+
+ for(var filename in report.files) {
+ a.href = filename;
+
+ var data = report.files[filename],
+ ret = {
+ filename: 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.push(ret);
+ }
+
+ alert(JSON.stringify(out));
+
+};
@@ -0,0 +1,26 @@
+blanket.customReporter = function(report) {
+
+ var a = document.createElement('a'),
+ str = '';
+
+ for(var filename in report.files) {
+ var data = report.files[filename];
+
+ a.href = filename;
+
+ str += 'SF:' + a.pathname.slice(1) + '\n';
+
+ for(var i = 0; i < data.source.length; i++) {
+ var num = i + 1;
+
+ if (data[num] !== undefined) {
+ str += 'DA:' + num + ',' + data[num] + '\n';
+ }
+ }
+
+ str += 'end_of_record\n';
+ }
+
+ alert(str);
+
+};
@@ -0,0 +1,32 @@
+{
+ "name": "poncho",
+ "version": "0.0.0",
+ "description": "Client-side JS code coverage using PhantomJS and Blanket",
+ "keywords": [ "phantomjs", "blanket", "coverage", "coveralls" ],
+ "homepage": "https://github.com/deepsweet/poncho",
+ "author": "Kir Belevich <kir@soulshine.in> (https://github.com/deepsweet)",
+ "repository": {
+ "type": "git",
+ "url": "git@github.com:deepsweet/poncho.git"
+ },
+ "bin": {
+ "poncho": "./bin/poncho"
+ },
+ "dependencies": {
+ "commander": "",
+ "phantomjs": "",
+ "blanket": "",
+ "node-static": ""
+ },
+ "devDependencies": {
+ "eslint": "",
+ "jscs": ""
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "licenses": [{
+ "type": "MIT",
+ "url": "https://github.com/deepsweet/poncho/blob/master/LICENSE"
+ }]
+}

0 comments on commit 24a389f

Please sign in to comment.