Skip to content

Commit

Permalink
Merge pull request #138 from asifrc/mac-eol
Browse files Browse the repository at this point in the history
Fixed HTML reporter issue with Macintosh End-of-Lines (CR)
  • Loading branch information
gotwarlost committed Jan 21, 2014
2 parents 322d765 + 8aed832 commit 78d3533
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/instrumenter.js
Expand Up @@ -590,7 +590,7 @@
this.fixColumnPositions(this.coverState); this.fixColumnPositions(this.coverState);
} }
if (this.opts.embedSource) { if (this.opts.embedSource) {
this.coverState.code = sourceCode.split(/\r?\n/); this.coverState.code = sourceCode.split(/(?:\r?\n)|\r/);
} }
coverState = this.opts.debug ? JSON.stringify(this.coverState, undefined, 4) : JSON.stringify(this.coverState); coverState = this.opts.debug ? JSON.stringify(this.coverState, undefined, 4) : JSON.stringify(this.coverState);
code = [ code = [
Expand Down
2 changes: 1 addition & 1 deletion lib/report/html.js
Expand Up @@ -376,7 +376,7 @@ Report.mix(HtmlReport, {
templateData = opts.templateData, templateData = opts.templateData,
sourceText = fileCoverage.code && Array.isArray(fileCoverage.code) ? sourceText = fileCoverage.code && Array.isArray(fileCoverage.code) ?
fileCoverage.code.join('\n') + '\n' : sourceStore.get(fileCoverage.path), fileCoverage.code.join('\n') + '\n' : sourceStore.get(fileCoverage.path),
code = sourceText.split(/\r?\n/), code = sourceText.split(/(?:\r?\n)|\r/),
count = 0, count = 0,
structured = code.map(function (str) { count += 1; return { line: count, covered: null, text: new InsertionText(str, true) }; }), structured = code.map(function (str) { count += 1; return { line: count, covered: null, text: new InsertionText(str, true) }; }),
context; context;
Expand Down
99 changes: 99 additions & 0 deletions test/cli/test-html-report.js
Expand Up @@ -73,6 +73,105 @@ module.exports = {
test.done(); test.done();
}, },


"should test files written with Unix Line Endings (LF)": function (test) {
var barPath = path.resolve(DIR, 'lib', 'bar.js'),
oldBar = fs.readFileSync(barPath, 'utf8'),
eol = /(\r?\n|\r)/g,
newBar = oldBar.replace(eol, "\n"),
file = path.resolve(OUTPUT_DIR, 'coverage.json'),
htmlReport = path.resolve(process.cwd(), 'html-report'),
reporter = new Reporter(),
obj,
collector = new Collector(),
fileFor = function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(htmlReport);
return path.resolve.apply(null, args);
};

fs.writeFileSync(barPath, newBar);
obj = JSON.parse(fs.readFileSync(file, 'utf8'));
collector.add(obj);
try {
reporter.writeReport(collector, true);
} catch(err) {
test.ok(false);
} finally {
fs.writeFileSync(barPath, oldBar);
}
test.ok(existsSync(htmlReport));
test.ok(existsSync(fileFor('index.html')));
test.ok(existsSync(fileFor('lib', 'bar.js.html')));
test.ok(fs.readFileSync(fileFor('lib', 'bar.js.html'), 'utf8') !== '');
test.done();
},

"should test files written with Windows Line Endings (CRLF)": function (test) {
var barPath = path.resolve(DIR, 'lib', 'bar.js'),
oldBar = fs.readFileSync(barPath, 'utf8'),
eol = /(\r?\n|\r)/g,
newBar = oldBar.replace(eol, "\r\n"),
file = path.resolve(OUTPUT_DIR, 'coverage.json'),
htmlReport = path.resolve(process.cwd(), 'html-report'),
reporter = new Reporter(),
obj,
collector = new Collector(),
fileFor = function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(htmlReport);
return path.resolve.apply(null, args);
};

fs.writeFileSync(barPath, newBar);
obj = JSON.parse(fs.readFileSync(file, 'utf8'));
collector.add(obj);
try {
reporter.writeReport(collector, true);
} catch(err) {
test.ok(false);
} finally {
fs.writeFileSync(barPath, oldBar);
}
test.ok(existsSync(htmlReport));
test.ok(existsSync(fileFor('index.html')));
test.ok(existsSync(fileFor('lib', 'bar.js.html')));
test.ok(fs.readFileSync(fileFor('lib', 'bar.js.html'), 'utf8') !== '');
test.done();
},

"should test files written with Macintosh Line Endings (CR)": function (test) {
var barPath = path.resolve(DIR, 'lib', 'bar.js'),
oldBar = fs.readFileSync(barPath, 'utf8'),
eol = /(\r?\n|\r)/g,
newBar = oldBar.replace(eol, "\r"),
file = path.resolve(OUTPUT_DIR, 'coverage.json'),
htmlReport = path.resolve(process.cwd(), 'html-report'),
reporter = new Reporter(),
obj,
collector = new Collector(),
fileFor = function () {
var args = Array.prototype.slice.call(arguments);
args.unshift(htmlReport);
return path.resolve.apply(null, args);
};

fs.writeFileSync(barPath, newBar);
obj = JSON.parse(fs.readFileSync(file, 'utf8'));
collector.add(obj);
try {
reporter.writeReport(collector, true);
} catch(err) {
test.ok(false);
} finally {
fs.writeFileSync(barPath, oldBar);
}
test.ok(existsSync(htmlReport));
test.ok(existsSync(fileFor('index.html')));
test.ok(existsSync(fileFor('lib', 'bar.js.html')));
test.ok(fs.readFileSync(fileFor('lib', 'bar.js.html'), 'utf8') !== '');
test.done();
},

"should test files written when code packed into coverage object": function (test) { "should test files written when code packed into coverage object": function (test) {
var file = path.resolve(OUTPUT_DIR, 'coverage.json'), var file = path.resolve(OUTPUT_DIR, 'coverage.json'),
htmlReport = path.resolve(OUTPUT_DIR), htmlReport = path.resolve(OUTPUT_DIR),
Expand Down

0 comments on commit 78d3533

Please sign in to comment.