From 7c79cc32dbc9812cf945ff93c97c89f1ef20191e Mon Sep 17 00:00:00 2001 From: Rodrigo Fernandes Date: Mon, 27 Jun 2016 21:13:25 +0100 Subject: [PATCH] Fix parsing body lines starting with --- and +++ --- src/diff-parser.js | 8 ++++---- test/diff-parser-tests.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/diff-parser.js b/src/diff-parser.js index 4d9695d..3ecc077 100644 --- a/src/diff-parser.js +++ b/src/diff-parser.js @@ -183,8 +183,8 @@ ( currentFile && // If we already have some file in progress and ( - currentFile.oldName && utils.startsWith(line, '---') || // Either we reached a old file identification line - currentFile.newName && utils.startsWith(line, '+++') // Or we reached a new file identification line + currentFile.oldName && utils.startsWith(line, '--- ') || // Either we reached a old file identification line + currentFile.newName && utils.startsWith(line, '+++ ') // Or we reached a new file identification line ) ) ) { @@ -198,7 +198,7 @@ * --- 2002-02-21 23:30:39.942229878 -0800 */ if (currentFile && !currentFile.oldName && - utils.startsWith(line, '---') && (values = getSrcFilename(line, config))) { + utils.startsWith(line, '--- ') && (values = getSrcFilename(line, config))) { currentFile.oldName = values; currentFile.language = getExtension(currentFile.oldName, currentFile.language); return; @@ -209,7 +209,7 @@ * +++ 2002-02-21 23:30:39.942229878 -0800 */ if (currentFile && !currentFile.newName && - utils.startsWith(line, '+++') && (values = getDstFilename(line, config))) { + utils.startsWith(line, '+++ ') && (values = getDstFilename(line, config))) { currentFile.newName = values; currentFile.language = getExtension(currentFile.newName, currentFile.language); return; diff --git a/test/diff-parser-tests.js b/test/diff-parser-tests.js index af8d3b6..d431024 100644 --- a/test/diff-parser-tests.js +++ b/test/diff-parser-tests.js @@ -424,5 +424,34 @@ describe('DiffParser', function() { assert.deepEqual(linesContent, ['-test', '+test1r', '+test2r']); }); + it('should parse diff with --- and +++ in the context lines', function() { + var diff = + '--- sample.js\n' + + '+++ sample.js\n' + + '@@ -1,15 +1,12 @@\n' + + ' test\n' + + ' \n' + + '----\n' + + '+test\n' + + ' \n' + + ' test\n' + + '----\n' + + '\\ No newline at end of file'; + + var result = DiffParser.generateDiffJson(diff); + var file1 = result[0]; + assert.equal(1, result.length); + assert.equal(1, file1.addedLines); + assert.equal(2, file1.deletedLines); + assert.equal('sample.js', file1.oldName); + assert.equal('sample.js', file1.newName); + assert.equal(1, file1.blocks.length); + + var linesContent = file1.blocks[0].lines.map(function(line) { + return line.content; + }); + assert.deepEqual(linesContent, [' test', ' ', '----', '+test', ' ', ' test', '----']); + }); + }); });