Skip to content

Commit

Permalink
fix(protect): skip previously patched files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed Aug 19, 2021
1 parent ca2177a commit 5e824c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/snyk-protect/src/lib/patch.ts
Expand Up @@ -6,9 +6,16 @@ export function applyPatchToFile(patchContents: string, baseFolder: string) {
baseFolder,
extractTargetFilePathFromPatch(patchContents),
);

const flagPath = `${targetFilePath}.snyk-protect.flag`;
if (fs.existsSync(flagPath)) {
return targetFilePath;
}

const contentsToPatch = fs.readFileSync(targetFilePath, 'utf-8');
const patchedContents = patchString(patchContents, contentsToPatch);
fs.writeFileSync(targetFilePath, patchedContents);
fs.writeFileSync(flagPath, '');
return targetFilePath;
}

Expand Down
18 changes: 17 additions & 1 deletion packages/snyk-protect/test/acceptance/fix-pr.smoke.spec.ts
Expand Up @@ -9,6 +9,22 @@ describe('Fix PR', () => {
const project = await createProject('fix-pr');
const patchedLodash = await getPatchedLodash();

expect(
await runCommand('npm', ['install'], {
cwd: project.path(),
}),
).toEqual(
expect.objectContaining<RunCLIResult>({
code: 0,
stdout: expect.stringContaining('Applied Snyk patches'),
stderr: expect.not.stringMatching(/snyk/gi),
}),
);

await expect(
project.read('node_modules/lodash/lodash.js'),
).resolves.toEqual(patchedLodash);

expect(
await runCommand('npm', ['install'], {
cwd: project.path(),
Expand All @@ -17,7 +33,7 @@ describe('Fix PR', () => {
expect.objectContaining<RunCLIResult>({
code: 0,
stdout: expect.stringContaining('Applied Snyk patches.'),
stderr: expect.any(String),
stderr: expect.not.stringMatching(/snyk/gi),
}),
);

Expand Down

0 comments on commit 5e824c0

Please sign in to comment.