From d92e223790a19e2cba0359e598d4b3a6435e4425 Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Tue, 7 Jun 2016 18:12:41 +0000 Subject: [PATCH] Correctly handle file names containing spaces --- package.json | 1 + src/patch/parse.js | 3 ++- test/patch/apply.js | 56 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) mode change 100644 => 100755 package.json mode change 100644 => 100755 src/patch/parse.js mode change 100644 => 100755 test/patch/apply.js diff --git a/package.json b/package.json old mode 100644 new mode 100755 index f0466532..3c4c6a2a --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "babel-preset-es2015-mod": "^6.3.13", "chai": "^3.3.0", "colors": "^1.1.2", + "deindent": "^0.1.0", "eslint": "^1.6.0", "grunt": "^0.4.5", "grunt-babel": "^6.0.0", diff --git a/src/patch/parse.js b/src/patch/parse.js old mode 100644 new mode 100755 index e371a07d..cad784f4 --- a/src/patch/parse.js +++ b/src/patch/parse.js @@ -52,7 +52,8 @@ export function parsePatch(uniDiff, options = {}) { // Parses the --- and +++ headers, if none are found, no lines // are consumed. function parseFileHeader(index) { - let fileHeader = (/^(\-\-\-|\+\+\+)\s+(\S*)\s?(.*?)\s*$/).exec(diffstr[i]); + const headerPattern = /^(---|\+\+\+)\s+([\S ]*)(?:\t(.*?)\s*)?$/; + const fileHeader = headerPattern.exec(diffstr[i]); if (fileHeader) { let keyPrefix = fileHeader[1] === '---' ? 'old' : 'new'; index[keyPrefix + 'FileName'] = fileHeader[2]; diff --git a/test/patch/apply.js b/test/patch/apply.js old mode 100644 new mode 100755 index 3e278f61..175fb569 --- a/test/patch/apply.js +++ b/test/patch/apply.js @@ -3,6 +3,7 @@ import {parsePatch} from '../../lib/patch/parse'; import {createPatch} from '../../lib/patch/create'; import {expect} from 'chai'; +import deindent from 'deindent'; describe('patch/apply', function() { describe('#applyPatch', function() { @@ -602,5 +603,60 @@ describe('patch/apply', function() { complete: done }); }); + + it('should handle file names containing spaces', done => { + const patch = deindent + `=================================================================== + --- test file\theader1 + +++ test file\theader2 + @@ -1,2 +1,3 @@ + line1 + +line2 + line3 + =================================================================== + --- test file 2\theader1 + +++ test file 2\theader2 + @@ -1,2 +1,3 @@ + foo1 + +foo2 + foo3 + `; + + const contents = { + 'test file': deindent + `line1 + line3 + `, + 'test file 2': deindent + `foo1 + foo3 + ` + }; + + const expected = { + 'test file': deindent + `line1 + line2 + line3 + `, + 'test file 2': deindent + `foo1 + foo2 + foo3 + ` + }; + + applyPatches(patch, { + loadFile(index, callback) { + callback(undefined, contents[index.oldFileName]); + }, + patched(index, content) { + expect(content) + .to.equal(expected[index.newFileName]) + .to.not.be.undefined; + }, + complete: done + }); + }); }); });