Permalink
Browse files

Fixed merge issues and bad code

  • Loading branch information...
davglass committed Oct 8, 2012
2 parents 72415fa + 7dcc85d commit e94af50f243163a46bc5f8e363958c32deeb6f4d
Showing with 230 additions and 2 deletions.
  1. +10 −0 bin/grover.js
  2. +130 −0 lib/coverage.js
  3. +25 −2 lib/options.js
  4. +65 −0 tests/test.js
View
@@ -44,6 +44,13 @@ var path = require('path'),
cover.options(options);
+ //Remove an exsisting coverage file if we are writing coverage data
+ if (options.coverageFileName) {
+
+ if (existsSync(options.coverageFileName)) {
+ fs.unlinkSync(options.coverageFileName);
+ }
+ }
testResults.forEach(function(json) {
var i;
res.passed += json.passed;
@@ -52,6 +59,9 @@ var path = require('path'),
res.ignored += json.ignored;
if (json.coverage) {
cover.set(json.coverage);
+ if (options.coverageFileName) {
+ cover.printLcovReport(json, options);
+ }
}
});
View
@@ -1,6 +1,9 @@
var istanbul = require('istanbul');
var Table = require('cli-table');
var util = require('../lib/index');
+var fs = require('fs');
+var path = require('path');
+var existsSync = fs.existsSync || path.existsSync;
var coverageInfo = {};
var coverageType = 'yuitest';
@@ -259,3 +262,130 @@ exports.status = function(json) {
return out;
};
+var getIstanbulCoverageReport = function (json) {
+ var report = {coverage: json.coverage};
+ //istanbul implementation
+ return report;
+};
+
+exports.getIstanbulCoverageReport = getIstanbulCoverageReport;
+
+var getYUITestCoverage = function (testName, prefix, coverage) {
+ var lcovreport = {},
+ iter = 0,
+ keys = Object.keys(coverage),
+ file,
+ data,
+ match,
+ name,
+ line,
+ exec,
+ fn,
+ fnkeys,
+ lineKeys,
+ j = 0;
+
+ for (iter; iter < keys.length; iter += 1) {
+ file = keys[iter];
+ data = coverage[file];
+ lcovreport.TN = testName;
+ lcovreport.SF = prefix ? prefix + path.basename(file) : path.relative(file) + "/" + file;
+ lcovreport.DA = [];
+ lcovreport.FN = [];
+ lcovreport.FNDA = [];
+ lcovreport.FNF = data.coveredFunctions;
+ lcovreport.FNH = data.calledFunctions;
+ lcovreport.LF = data.coveredLines;
+ lcovreport.LH = data.calledLines;
+
+ fnkeys = Object.keys(data.functions);
+ for (j; j < fnkeys.length; j += 1) {
+ fn = fnkeys[j];
+ exec = data.functions[fn];
+ match = fn.match(/^([\w ()_]+):(\d)+$/);
+ name = match[1];
+ line = match[2];
+
+ lcovreport.FN.push({name: name, line: line});
+ lcovreport.FNDA.push({name: name, exec: exec});
+ }
+
+ lineKeys = Object.keys(data.lines);
+ for (j = 0; j < lineKeys.length; j += 1) {
+ line = lineKeys[j];
+ exec = data.lines[line];
+ lcovreport.DA.push({line: line, exec: exec});
+ }
+ }
+ return lcovreport;
+};
+exports.getYUITestCoverage = getYUITestCoverage;
+
+var getYUITestLcovReport = function (report) {
+ var text = '',
+ i = 0,
+ fn,
+ line;
+
+ text += "TN:" + report.TN + "\n";
+ text += "SF:" + report.SF + "\n";
+
+ for (i; i < report.FN.length; i += 1) {
+ fn = report.FN[i];
+ text += "FN:" + fn.line + "," + fn.name + "\n";
+ }
+ text += "\n";
+
+ for (i = 0; i < report.FNDA.length; i += 1) {
+ fn = report.FNDA[i];
+ text += "FNDA:" + fn.exec + "," + fn.name + "\n";
+ }
+ text += "\n";
+
+ text += "FNF:" + report.FNF + "\n";
+ text += "FNH:" + report.FNH + "\n";
+
+ for (i = 0; i < report.DA.length; i += 1) {
+ line = report.DA[i];
+ text += "DA:" + line.line + "," + line.exec + "\n";
+ }
+ text += "\n";
+ text += "LF:" + report.LF + "\n";
+ text += "LH:" + report.LH + "\n\n";
+ text += "end_of_record\n";
+ return text;
+};
+exports.getYUITestLcovReport = getYUITestLcovReport;
+
+var getCoverageReport = function (json, prefix) {
+ var out;
+ if (isIstanbul(json)) {
+ out = getIstanbulCoverageReport(json);
+ } else {
+ out = getYUITestLcovReport(getYUITestCoverage(json.name, prefix, json.coverage));
+ }
+ return out;
+};
+exports.getCoverageReport = getCoverageReport;
+
+var printLcovReport = function (json, options) {
+ var filename = options.coverageFileName,
+ sourceFilePrefix = options.sourceFilePrefix,
+ reportText = getCoverageReport(json, sourceFilePrefix);
+
+ if (filename) {
+
+ if (typeof fs.appendFileSync === "function") {
+ //Only available in Node 0.8.x, creates a file if one doesn't exist, appends when one does.
+ fs.appendFileSync(filename, reportText, 'utf8');
+ } else {
+ //Node 0.6.x
+ if (existsSync(filename)) {
+ reportText = fs.readFileSync(filename) + reportText;
+ }
+ fs.writeFileSync(filename, reportText, 'utf8');
+ }
+ }
+};
+
+exports.printLcovReport = printLcovReport;
View
@@ -14,8 +14,10 @@ var path = require('path'),
silent: false,
run: true,
quiet: false,
- exitOnFail: false
- }, newPath, paths, a, p, t, v, concurrent,
+ exitOnFail: false,
+ sourceFilePrefix: false,
+ coverageFileName: false
+ }, newPath, paths, a, p, t, v, concurrent, coverageFileName, sourceFilePrefix,
getPaths = function(opt) {
var g = glob.sync(opt, {
cwd: process.cwd()
@@ -42,6 +44,24 @@ var path = require('path'),
case "--coverage":
options.coverage = true;
break;
+ case "-co":
+ case "--coverageFileName":
+ coverageFileName = args.shift();
+ if (coverageFileName) {
+ options.coverageFileName = coverageFileName;
+ } else {
+ throw("-co requires an argument");
+ }
+ break;
+ case "-sp":
+ case "--sourceFilePrefix":
+ sourceFilePrefix = args.shift();
+ if (sourceFilePrefix) {
+ options.sourceFilePrefix = sourceFilePrefix;
+ } else {
+ throw("-sp requires an argument");
+ }
+ break;
case "--no-run":
options.run = false;
break;
@@ -109,6 +129,7 @@ var path = require('path'),
break;
case "-h":
case "--help":
+ /*jshint maxlen: 200*/
util.log('Grover is a YUITest wrapper to run tests in PhantomJS');
console.log('Version ' + VERSION);
console.log('');
@@ -134,6 +155,8 @@ var path = require('path'),
console.log(' --port <Number> The port to start the server on');
console.log(' --coverage Generate a coverage report and print it to the screen (you must instrument your own files first)');
console.log(' --coverage-warn <Number> The percentage to highlight as low coverage: default is 80');
+ console.log(' -co --coverageFileName <path to export coverage file> The coverage data in lcov format.');
+ console.log(' -sp --sourceFilePrefix <path to sourcefile> The relative path to the original source file for use in the coverage results.');
console.log(' --no-run Do not execute the tests, just prep the server (for other testing)');
process.exit();
break;
View
@@ -159,9 +159,74 @@ suite.add(new YUITest.TestCase({
Assert.areEqual(opts.server, process.cwd(), 'failed to set server config');
Assert.areEqual(opts.port, 9000, 'Failed to set port');
+ },
+ 'check coverage prefix (sp)': function() {
+ var opts = parse(['-sp', '../']);
+
+ Assert.areEqual('../', opts.sourceFilePrefix, 'Failed to parse source file prefix.');
+ },
+ 'check coverage prefix (sourceFilePrefix)': function() {
+ var opts = parse(['--sourceFilePrefix', '../']);
+
+ Assert.areEqual('../', opts.sourceFilePrefix, 'Failed to parse source file prefix.');
+ },
+ 'check coverage filename (co)': function() {
+ var opts = parse(['-co', 'lcov.info']);
+
+ Assert.areEqual('lcov.info', opts.coverageFileName, 'Failed to parse coverage filename.');
+ },
+ 'check coverage filename (coverageFileName)': function() {
+ var opts = parse(['--coverageFileName', 'lcov.info']);
+
+ Assert.areEqual('lcov.info', opts.coverageFileName, 'Failed to parse coverage filename.');
}
}));
+var cover = require(path.join(__dirname, '../lib/coverage'));
+
+suite.add(new YUITest.TestCase({
+ name: 'Coverage',
+ coverageData: {"foo.js": {
+ lines: {
+ '1': 1,
+ '2': 2,
+ '3': 3,
+ '4': 0
+ },
+ functions: {
+ 'init:1': 1,
+ 'foo:2': 2,
+ '(anonymous 1):3': 3
+ },
+ coveredLines: 4,
+ calledLines: 3,
+ coveredFunctions: 3,
+ calledFunctions: 3,
+ path: 'build/foo/foo.js'
+ }},
+ coverageFile: "TN:foo\n\
+SF:../foo.js\n\
+FN:1,init\n\
+FN:2,foo\n\
+FN:3,(anonymous 1)\n\n\
+FNDA:1,init\n\
+FNDA:2,foo\n\
+FNDA:3,(anonymous 1)\n\n\
+FNF:3\n\
+FNH:3\n\
+DA:1,1\n\
+DA:2,2\n\
+DA:3,3\n\
+DA:4,0\n\n\
+LF:4\n\
+LH:3\n\n\
+end_of_record\n",
+ 'Should get lcov data for yui tests': function() {
+ var report = cover.getCoverageReport({name: 'foo', coverage: this.coverageData}, '../');
+ Assert.areEqual(this.coverageFile, report, 'Failed to produce correct lcov report.');
+ },
+}));
+
YUITest.TestRunner.add(suite);

0 comments on commit e94af50

Please sign in to comment.