Skip to content

Commit

Permalink
fix(protect): keep the same line endings when patching
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed May 19, 2021
1 parent 63e4818 commit c5b208c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
34 changes: 24 additions & 10 deletions packages/snyk-protect/src/lib/patch.ts
Expand Up @@ -20,6 +20,14 @@ export function extractTargetFilePathFromPatch(patchContents: string): string {
return filename;
}

const getNextLine = (currentLine: string, patchLine: string): string => {
const maybeCarriageReturn =
currentLine.endsWith('\r') && !patchLine.endsWith('\r') ? '\r' : '';
return patchLine.substring(1) + maybeCarriageReturn;
};

const getPatchType = (patchLine: string): string => patchLine.charAt(0);

export function patchString(
patchContents: string,
contentsToPatch: string,
Expand All @@ -45,29 +53,35 @@ export function patchString(

for (const patchLine of patchLines) {
lineToPatch += 1;
switch (patchLine.charAt(0)) {
case '-':
const currentLine = contentsToPatchLines[lineToPatch];
const nextLine = getNextLine(currentLine, patchLine);
switch (getPatchType(patchLine)) {
case '-': {
contentsToPatchLines.splice(lineToPatch, 1);
break;

case '+':
contentsToPatchLines.splice(lineToPatch, 0, patchLine.substring(1));
}
case '+': {
contentsToPatchLines.splice(lineToPatch, 0, nextLine);
break;

case ' ':
if (contentsToPatchLines[lineToPatch] !== patchLine.slice(1)) {
}
case ' ': {
if (currentLine !== nextLine) {
console.log(
'Expected\n line from local file\n',
contentsToPatchLines[lineToPatch],
JSON.stringify(currentLine),
'\n to match patch line\n',
JSON.stringify(nextLine),
'\n',
);
console.log('\n to match patch line\n', patchLine.slice(1), '\n');
throw new Error(
// `File ${filename} to be patched does not match, not patching`,
`File to be patched does not match, not patching`,
);
}
break;
}
}
}

return contentsToPatchLines.join('\n');
}
33 changes: 32 additions & 1 deletion packages/snyk-protect/test/unit/patch.spec.ts
Expand Up @@ -32,7 +32,38 @@ describe(patchString.name, () => {
'utf-8',
);
expect(patchedContents).toBe(expectedPatchedContents);
expect(0).toBe(0);
});

it('keeps the same line endings', () => {
const fixtureFolder = path.join(
__dirname,
'../fixtures/patchable-file-lodash',
);
const patchFilePath = path.join(fixtureFolder, 'lodash.patch');

const patchContents = fs.readFileSync(patchFilePath, 'utf-8');

const targetFilePath = path.join(
fixtureFolder,
extractTargetFilePathFromPatch(patchContents),
);
const contentsToPatch = fs
.readFileSync(targetFilePath, 'utf-8')
.split('\n')
.join('\r\n');

const patchedContents = patchString(patchContents, contentsToPatch);

const expectedPatchedContentsFilePath = path.join(
fixtureFolder,
'lodash-expected-patched.js',
);

const expectedPatchedContents = fs
.readFileSync(expectedPatchedContentsFilePath, 'utf-8')
.split('\n')
.join('\r\n');
expect(patchedContents).toBe(expectedPatchedContents);
});

// if the patch is not compatible with the target, make sure we throw an Error and do patch
Expand Down

0 comments on commit c5b208c

Please sign in to comment.