Permalink
Browse files

Merge pull request #138 from asifrc/mac-eol

Fixed HTML reporter issue with Macintosh End-of-Lines (CR)
  • Loading branch information...
gotwarlost committed Jan 21, 2014
2 parents 322d765 + 8aed832 commit 78d3533c4ae21cec364ecde77a873136c14ef48b
Showing with 101 additions and 2 deletions.
  1. +1 −1 lib/instrumenter.js
  2. +1 −1 lib/report/html.js
  3. +99 −0 test/cli/test-html-report.js
View
@@ -590,7 +590,7 @@
this.fixColumnPositions(this.coverState);
}
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);
code = [
View
@@ -376,7 +376,7 @@ Report.mix(HtmlReport, {
templateData = opts.templateData,
sourceText = fileCoverage.code && Array.isArray(fileCoverage.code) ?
fileCoverage.code.join('\n') + '\n' : sourceStore.get(fileCoverage.path),
- code = sourceText.split(/\r?\n/),
+ code = sourceText.split(/(?:\r?\n)|\r/),
count = 0,
structured = code.map(function (str) { count += 1; return { line: count, covered: null, text: new InsertionText(str, true) }; }),
context;
@@ -73,6 +73,105 @@ module.exports = {
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) {
var file = path.resolve(OUTPUT_DIR, 'coverage.json'),
htmlReport = path.resolve(OUTPUT_DIR),

0 comments on commit 78d3533

Please sign in to comment.