Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added jshint configs and testing.

Cleaned up reporter code.
Moved coverage and report into tools.
  • Loading branch information...
commit 4cfcb3938d59daa46e885170fdbbdae4afef72f2 1 parent 435ed15
@NeoPhi authored
View
74 config/jshint.config.json
@@ -0,0 +1,74 @@
+{
+ // Settings
+ "passfail" : false, // Stop on first error.
+ "maxerr" : 100, // Maximum error before stopping.
+
+
+ // Predefined globals whom JSHint will ignore.
+ "browser" : false, // Standard browser globals e.g. `window`, `document`.
+
+ "node" : true,
+ "rhino" : false,
+ "couch" : false,
+ "wsh" : false, // Windows Scripting Host.
+
+ "jquery" : false,
+ "prototypejs" : false,
+ "mootools" : false,
+ "dojo" : false,
+
+ "predef" : [ // Custom globals.
+ "Exception",
+ "jasmine",
+ "java",
+ "top",
+ "__phantom_writeFile"
+ ],
+
+
+ // Development.
+ "debug" : false, // Allow debugger statements e.g. browser breakpoints.
+ "devel" : false, // Allow developments statements e.g. `console.log();`.
+
+
+ // ECMAScript 5.
+ "es5" : false, // Allow ECMAScript 5 syntax.
+ "strict" : false, // Require `use strict` pragma in every file.
+ "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
+
+
+ // The Good Parts.
+ "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
+ "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
+ "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
+ "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
+ "curly" : true, // Require {} for every new block or scope.
+ "eqeqeq" : true, // Require triple equals i.e. `===`.
+ "eqnull" : false, // Tolerate use of `== null`.
+ "evil" : false, // Tolerate use of `eval`.
+ "expr" : false, // Tolerate `ExpressionStatement` as Programs.
+ "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
+ "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
+ "latedef" : true, // Prohipit variable use before definition.
+ "loopfunc" : false, // Allow functions to be defined within loops.
+ "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
+ "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions.
+ "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
+ "scripturl" : true, // Tolerate script-targeted URLs.
+ "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
+ "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
+ "undef" : true, // Require all non-global variables be declared before they are used.
+
+
+ // Personal styling preferences.
+ "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
+ "noempty" : true, // Prohibit use of empty blocks.
+ "nonew" : true, // Prohibit use of constructors for side-effects.
+ "nomen" : false, // Prohibit use of initial or trailing underbars in names.
+ "onevar" : false, // Allow only one `var` statement per function.
+ "plusplus" : true, // Prohibit use of `++` & `--`.
+ "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
+ "trailing" : true, // Prohibit trailing whitespaces.
+ "white" : false, // Check against strict whitespace and indentation rules.
+ "indent" : 2 // Specify indentation spacing
+}
View
75 config/spec.jshint.config.json
@@ -0,0 +1,75 @@
+{
+ // Settings
+ "passfail" : false, // Stop on first error.
+ "maxerr" : 100, // Maximum error before stopping.
+
+
+ // Predefined globals whom JSHint will ignore.
+ "browser" : false, // Standard browser globals e.g. `window`, `document`.
+
+ "node" : true,
+ "rhino" : false,
+ "couch" : false,
+ "wsh" : false, // Windows Scripting Host.
+
+ "jquery" : false,
+ "prototypejs" : false,
+ "mootools" : false,
+ "dojo" : false,
+
+ "predef" : [ // Custom globals.
+ "beforeEach",
+ "describe",
+ "expect",
+ "it",
+ "jasmine",
+ "spyOn"
+ ],
+
+
+ // Development.
+ "debug" : false, // Allow debugger statements e.g. browser breakpoints.
+ "devel" : false, // Allow developments statements e.g. `console.log();`.
+
+
+ // ECMAScript 5.
+ "es5" : false, // Allow ECMAScript 5 syntax.
+ "strict" : false, // Require `use strict` pragma in every file.
+ "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
+
+
+ // The Good Parts.
+ "asi" : false, // Tolerate Automatic Semicolon Insertion (no semicolons).
+ "laxbreak" : false, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
+ "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
+ "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
+ "curly" : true, // Require {} for every new block or scope.
+ "eqeqeq" : true, // Require triple equals i.e. `===`.
+ "eqnull" : false, // Tolerate use of `== null`.
+ "evil" : false, // Tolerate use of `eval`.
+ "expr" : false, // Tolerate `ExpressionStatement` as Programs.
+ "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
+ "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
+ "latedef" : true, // Prohipit variable use before definition.
+ "loopfunc" : false, // Allow functions to be defined within loops.
+ "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
+ "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions.
+ "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
+ "scripturl" : true, // Tolerate script-targeted URLs.
+ "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
+ "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
+ "undef" : true, // Require all non-global variables be declared before they are used.
+
+
+ // Personal styling preferences.
+ "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
+ "noempty" : true, // Prohibit use of empty blocks.
+ "nonew" : true, // Prohibit use of constructors for side-effects.
+ "nomen" : false, // Prohibit use of initial or trailing underbars in names.
+ "onevar" : false, // Allow only one `var` statement per function.
+ "plusplus" : true, // Prohibit use of `++` & `--`.
+ "sub" : true, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
+ "trailing" : true, // Prohibit trailing whitespaces.
+ "white" : false, // Check against strict whitespace and indentation rules.
+ "indent" : 2 // Specify indentation spacing
+}
View
34 coverage.js
@@ -1,34 +0,0 @@
-var wrench = require('wrench');
-var exec = require('child_process').exec;
-
-var run = function() {
- wrench.mkdirSyncRecursive('build/test');
- wrench.copyDirSyncRecursive('test', 'build/test');
-
- wrench.rmdirSyncRecursive('reports');
- wrench.mkdirSyncRecursive('reports/coverage');
- wrench.copyDirSyncRecursive('template/', 'reports/');
-
- require('./node_modules/jasmine-node/lib/jasmine-node/index.js');
- require('./src/jasmine.jscoverage_reporter');
- var jasmineEnv = jasmine.getEnv();
- jasmineEnv.addReporter(new jasmine.JSCoverageReporter('./reports'));
- require('./node_modules/jasmine-node/lib/jasmine-node/cli.js');
-};
-
-wrench.rmdirSyncRecursive('build');
-wrench.mkdirSyncRecursive('build/src');
-exec('jscoverage src build/src',
- function (error, stdout, stderr) {
- if (stdout) {
- console.log(stdout);
- }
- if (stderr) {
- console.error(stderr);
- }
- if (error) {
- console.error('exec error', error);
- process.exit(1);
- }
- run();
-});
View
13 package.json
@@ -1,15 +1,18 @@
{
- "name": "jscoverage",
+ "name": "jscoverage-reporter",
"version": "0.1.0",
"private": true,
- "dependencies": {
- "jasmine-node": "1.0.13",
+ "devDependencies": {
+ "jshint": "0.5.8",
+ "jasmine-node": "1.0.21",
"wrench": "1.3.5"
},
"engines": {
"node": ">= 0.6.0"
},
"scripts": {
- "test": "node coverage --junitreport build/test"
+ "pretest": "jshint src tools --config config/jshint.config.json && jshint test --config config/spec.jshint.config.json",
+ "test": "node tools/coverage --junitreport build/test",
+ "report": "node tools/report.js reports/"
}
-}
+}
View
82 src/jasmine.jscoverage_reporter.js
@@ -1,13 +1,16 @@
(function() {
if (!jasmine) {
- throw new Exception("jasmine library does not exist in global namespace!");
+ throw new Exception('jasmine library does not exist in global namespace!');
}
// When running in node setup the global variable JSCoverage looks for
- if (!global.top) {
- global.top = {};
- }
+ try {
+ if (global && !global.top) {
+ global.top = {};
+ }
+ } catch(e) {}
+ // writeFile from: https://github.com/larrymyers/jasmine-reporters
var writeFile = function(filename, text) {
// Rhino
try {
@@ -15,24 +18,20 @@
out.write(text);
out.close();
return;
- } catch (e) {
- }
- // PhantomJS, via pyphantomjs and the saveToFile plugin
- // http://dev.umaclan.com/projects/pyphantomjs/wiki/Plugins#Save-to-File
+ } catch (e) {}
+ // PhantomJS, via a method injected by phantomjs-testrunner.js
try {
- phantom.saveToFile(text, filename);
+ __phantom_writeFile(filename, text);
return;
- } catch (f) {
- }
- // Node
+ } catch (f) {}
+ // Node.js
try {
- var fs = require("fs");
- var fd = fs.openSync(filename, "w");
+ var fs = require('fs');
+ var fd = fs.openSync(filename, 'w');
fs.writeSync(fd, text, 0);
fs.closeSync(fd);
return;
- } catch (g) {
- }
+ } catch (g) {}
};
var calculateCoverage = function(stats, metric) {
@@ -44,16 +43,16 @@
var calculateStats = function(coverage) {
var stats = {
- packagesCovered : 1,
- packagesTotal : 1,
- classesCovered : 0,
- classesTotal : 0,
- methodsCovered : 1,
- methodsTotal : 1,
- srcfilesCovered : 0,
- srcfilesTotal : 0,
- srclinesCovered : 0,
- srclinesTotal : 0
+ packagesCovered: 1,
+ packagesTotal: 1,
+ classesCovered: 0,
+ classesTotal: 0,
+ methodsCovered: 1,
+ methodsTotal: 1,
+ srcfilesCovered: 0,
+ srcfilesTotal: 0,
+ srclinesCovered: 0,
+ srclinesTotal: 0
};
for (var file in coverage) {
if (coverage.hasOwnProperty(file)) {
@@ -98,13 +97,15 @@
xml.push(' </all>');
xml.push(' </data>');
xml.push('</report>');
- writeFile(savePath + '/emmaCoverage.xml', xml.join('\n'));
+ writeFile(savePath + '/coverage.xml', xml.join('\n'));
};
+ // jscoverage_pad from: http://siliconforks.com/jscoverage/
var jscoverage_pad = function(s) {
return '0000'.substr(s.length) + s;
};
+ // jscoverage_quote from: http://siliconforks.com/jscoverage/
var jscoverage_quote = function(s) {
return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, function (c) {
switch (c) {
@@ -133,8 +134,8 @@
}) + '"';
};
+ // jscoverage_serializeCoverageToJSON from: http://siliconforks.com/jscoverage/
var jscoverage_serializeCoverageToJSON = function(_$jscoverage) {
- var line;
var json = [];
for (var file in _$jscoverage) {
if (! _$jscoverage.hasOwnProperty(file)) {
@@ -144,8 +145,9 @@
var coverage = _$jscoverage[file];
var array = [];
+ var line;
var length = coverage.length;
- for (line = 0; line < length; line++) {
+ for (line = 0; line < length; line += 1) {
var value = coverage[line];
if (value === undefined || value === null) {
value = 'null';
@@ -156,7 +158,7 @@
var source = coverage.source;
var lines = [];
length = source.length;
- for (line = 0; line < length; line++) {
+ for (line = 0; line < length; line += 1) {
lines.push(jscoverage_quote(source[line]));
}
@@ -169,27 +171,34 @@
writeFile(savePath + '/jscoverage.json', jscoverage_serializeCoverageToJSON(coverage));
};
+ var getCoverage = function() {
+ try {
+ return top._$jscoverage;
+ } catch(e) {}
+ return {};
+ };
+
var JSCoverageReporter = function(savePath) {
this.savePath = savePath || '';
};
JSCoverageReporter.prototype = {
- reportSpecStarting : function(spec) {
+ reportSpecStarting: function(spec) {
},
- reportSpecResults : function(spec) {
+ reportSpecResults: function(spec) {
},
- reportSuiteResults : function(suite) {
+ reportSuiteResults: function(suite) {
},
- reportRunnerResults : function(runner) {
- var coverage = global.top._$jscoverage;
+ reportRunnerResults: function(runner) {
+ var coverage = getCoverage();
writeEmmaReport(this.savePath, coverage);
writeCoverageData(this.savePath, coverage);
},
- log : function(str) {
+ log: function(str) {
var console = jasmine.getGlobal().console;
if (console && console.log) {
@@ -198,6 +207,5 @@
}
};
- // export public
jasmine.JSCoverageReporter = JSCoverageReporter;
}());
View
28 test/jasmine.jscoverage_reporter.spec.js
@@ -1,16 +1,24 @@
describe('jasmine.jscoverage_reporter.js', function() {
- var original;
+ var fs = require('fs');
- beforeEach(function() {
+ // This will replace the jasmine.JSCoverageReporter instance
+ // with the JSCoverage augmented one
+ var original = jasmine.JSCoverageReporter;
+ require('../src/jasmine.jscoverage_reporter.js');
+
+ it('installs reporter in jasmine', function() {
expect(jasmine.JSCoverageReporter).toBeDefined();
- original = spyOn(jasmine, 'JSCoverageReporter');
+ expect(jasmine.JSCoverageReporter).not.toBe(original);
});
-
- describe('covered version', function() {
- it('installs reporter in jasmine', function() {
- require('../src/jasmine.jscoverage_reporter.js');
- expect(jasmine.JSCoverageReporter).toBeDefined();
- expect(jasmine.JSCoverageReporter).not.toBe(original);
- });
+
+ it('saves two files', function() {
+ var reporter = new jasmine.JSCoverageReporter('.');
+ spyOn(fs, 'openSync');
+ spyOn(fs, 'writeSync');
+ spyOn(fs, 'closeSync');
+ reporter.reportRunnerResults();
+ expect(fs.openSync.callCount).toBe(2);
+ expect(fs.openSync.argsForCall[0][0]).toBe('./coverage.xml');
+ expect(fs.openSync.argsForCall[1][0]).toBe('./jscoverage.json');
});
});
View
33 tools/coverage.js
@@ -0,0 +1,33 @@
+var wrench = require('wrench');
+var exec = require('child_process').exec;
+
+var run = function() {
+ wrench.mkdirSyncRecursive('build/test');
+ wrench.copyDirSyncRecursive('test', 'build/test');
+
+ wrench.rmdirSyncRecursive('reports', true);
+ wrench.mkdirSyncRecursive('reports/coverage');
+ wrench.copyDirSyncRecursive('template/', 'reports/');
+
+ require('../node_modules/jasmine-node/lib/jasmine-node/index.js');
+ require('../src/jasmine.jscoverage_reporter');
+ var jasmineEnv = jasmine.getEnv();
+ jasmineEnv.addReporter(new jasmine.JSCoverageReporter('./reports'));
+ require('../node_modules/jasmine-node/lib/jasmine-node/cli.js');
+};
+
+wrench.rmdirSyncRecursive('build', true);
+wrench.mkdirSyncRecursive('build/src');
+exec('jscoverage src build/src', function(err, stdout, stderr) {
+ if (stdout) {
+ console.log(stdout);
+ }
+ if (stderr) {
+ console.error(stderr);
+ }
+ if (err) {
+ console.error('exec error', err);
+ process.exit(1);
+ }
+ run();
+});
View
12 src/viewReport.js → tools/report.js
@@ -1,13 +1,13 @@
+if (process.argv.length < 3) {
+ console.log('Usage: node report.js <report directory>');
+ process.exit(1);
+}
+
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
-if (process.argv.length < 3) {
- console.log('Usage: node viewReport.js <report directory>');
- process.exit(1);
-}
-
var MIME = {
'default': 'application/octet-stream',
'.js': 'application/javascript',
@@ -16,7 +16,7 @@ var MIME = {
'.css': 'text/css',
'.gif': 'image/gif'
};
-var PORT = 8080;
+var PORT = process.env.PORT || 8080;
http.createServer(function(request, response) {
var parsedUrl = url.parse(request.url);
Please sign in to comment.
Something went wrong with that request. Please try again.