From 269a6268c4345c2f4112095bdd317b2f4cf16614 Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Sat, 6 Feb 2016 18:50:47 +0000 Subject: [PATCH] Fix parsing on Windows Replace windows EOL by Unix EOL --- src/diff-parser.js | 1 + test/diff-parser-tests.js | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/diff-parser-tests.js diff --git a/src/diff-parser.js b/src/diff-parser.js index e21ab55..9bdd39a 100644 --- a/src/diff-parser.js +++ b/src/diff-parser.js @@ -124,6 +124,7 @@ var diffLines = diffInput.replace(/\\ No newline at end of file/g, '') + .replace(/\r\n?/g, '\n') .split('\n'); /* Diff */ diff --git a/test/diff-parser-tests.js b/test/diff-parser-tests.js new file mode 100644 index 0000000..f2b924c --- /dev/null +++ b/test/diff-parser-tests.js @@ -0,0 +1,47 @@ +var assert = require('assert'); + +var DiffParser = require('../src/diff-parser.js').DiffParser; + +describe('DiffParser', function() { + describe('generateDiffJson', function() { + + it('should parse linux with \n diff', function() { + var diff = + 'diff --git a/sample b/sample\n' + + 'index 0000001..0ddf2ba\n' + + '--- a/sample\n' + + '+++ b/sample\n' + + '@@ -1 +1 @@\n' + + '-test\n' + + '+test1r\n'; + var result = Diff2Html.getJsonFromDiff(diff); + var file1 = result[0]; + assert.equal(1, result.length); + assert.equal(1, file1.addedLines); + assert.equal(1, file1.deletedLines); + assert.equal('sample', file1.oldName); + assert.equal('sample', file1.newName); + assert.equal(1, file1.blocks.length); + }); + + it('should parse windows with \r\n diff', function() { + var diff = + 'diff --git a/sample b/sample\r\n' + + 'index 0000001..0ddf2ba\r\n' + + '--- a/sample\r\n' + + '+++ b/sample\r\n' + + '@@ -1 +1 @@\r\n' + + '-test\r\n' + + '+test1r\n'; + var result = Diff2Html.getJsonFromDiff(diff); + var file1 = result[0]; + assert.equal(1, result.length); + assert.equal(1, file1.addedLines); + assert.equal(1, file1.deletedLines); + assert.equal('sample', file1.oldName); + assert.equal('sample', file1.newName); + assert.equal(1, file1.blocks.length); + }); + + }); +});