Skip to content

Commit e8fed38

Browse files
author
Jahed Ahmed
committed
refactor(protect): move stdout logs to top level
1 parent 20feb67 commit e8fed38

File tree

4 files changed

+19
-23
lines changed

4 files changed

+19
-23
lines changed

packages/snyk-protect/src/lib/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function protect(projectFolderPath: string) {
2626
const snykFilePath = path.resolve(projectFolderPath, '.snyk');
2727

2828
if (!fs.existsSync(snykFilePath)) {
29-
console.log('No .snyk file found');
29+
console.log('No .snyk file found.');
3030
sendAnalytics({
3131
type: ProtectResultType.NO_SNYK_FILE,
3232
});
@@ -67,7 +67,7 @@ async function protect(projectFolderPath: string) {
6767
> = await getAllPatches(vulnIdAndPackageNames, packageNameToVersionsMap);
6868

6969
if (packageAtVersionsToPatches.size === 0) {
70-
console.log('Nothing to patch');
70+
console.log('Nothing to patch.');
7171
sendAnalytics({
7272
type: ProtectResultType.NOTHING_TO_PATCH,
7373
});
@@ -83,7 +83,8 @@ async function protect(projectFolderPath: string) {
8383
vuldIdAndPatches?.forEach((vp) => {
8484
vp.patches.forEach((patchDiffs) => {
8585
patchDiffs.patchDiffs.forEach((diff) => {
86-
applyPatchToFile(diff, fpp.path);
86+
const patchedPath = applyPatchToFile(diff, fpp.path);
87+
console.log(`Patched: ${patchedPath}`);
8788
});
8889
});
8990
patchedModules.push({
@@ -94,7 +95,7 @@ async function protect(projectFolderPath: string) {
9495
});
9596
});
9697

97-
console.log('Successfully applied Snyk patches');
98+
console.log('Applied Snyk patches.');
9899

99100
sendAnalytics({
100101
type: ProtectResultType.APPLIED_PATCHES,

packages/snyk-protect/src/lib/patch.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function applyPatchToFile(patchContents: string, baseFolder: string) {
99
const contentsToPatch = fs.readFileSync(targetFilePath, 'utf-8');
1010
const patchedContents = patchString(patchContents, contentsToPatch);
1111
fs.writeFileSync(targetFilePath, patchedContents);
12-
console.log(`patched ${targetFilePath}`);
12+
return targetFilePath;
1313
}
1414

1515
export function extractTargetFilePathFromPatch(patchContents: string): string {
@@ -39,13 +39,11 @@ export function patchString(
3939
const contentsToPatchLines = contentsToPatch.split('\n');
4040

4141
if (!patchContentLines[2]) {
42-
// return;
43-
throw new Error('Invalid patch');
42+
throw new Error('Invalid patch.');
4443
}
4544
const unparsedLineToPatch = /^@@ -(\d*),.*@@/.exec(patchContentLines[2]);
4645
if (!unparsedLineToPatch || !unparsedLineToPatch[1]) {
47-
// return;
48-
throw new Error('Invalid patch');
46+
throw new Error('Invalid patch.');
4947
}
5048
let lineToPatch = parseInt(unparsedLineToPatch[1], 10) - 2;
5149

@@ -66,16 +64,13 @@ export function patchString(
6664
}
6765
case ' ': {
6866
if (currentLine !== nextLine) {
69-
console.log(
70-
'Expected\n line from local file\n',
71-
JSON.stringify(currentLine),
72-
'\n to match patch line\n',
73-
JSON.stringify(nextLine),
74-
'\n',
75-
);
7667
throw new Error(
77-
// `File ${filename} to be patched does not match, not patching`,
78-
`File to be patched does not match, not patching`,
68+
'File does not match patch contents.' +
69+
' Expected\n' +
70+
' line from local file\n' +
71+
` ${JSON.stringify(currentLine)}\n` +
72+
' to match patch line\n' +
73+
` ${JSON.stringify(nextLine)}\n`,
7974
);
8075
}
8176
break;

packages/snyk-protect/test/acceptance/fix-pr.smoke.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('Fix PR', () => {
1616
).toEqual(
1717
expect.objectContaining<RunCLIResult>({
1818
code: 0,
19-
stdout: expect.stringContaining('Successfully applied Snyk patches'),
19+
stdout: expect.stringContaining('Applied Snyk patches.'),
2020
stderr: expect.any(String),
2121
}),
2222
);

packages/snyk-protect/test/acceptance/protect.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('@snyk/protect', () => {
2929
project.read('node_modules/nyc/node_modules/lodash/lodash.js'),
3030
).resolves.toEqual(patchedLodash);
3131

32-
expect(log).toHaveBeenCalledWith('Successfully applied Snyk patches');
32+
expect(log).toHaveBeenCalledWith('Applied Snyk patches.');
3333
expect(postJsonSpy).toHaveBeenCalledTimes(1);
3434
expect(postJsonSpy.mock.calls[0][1]).toEqual({
3535
data: {
@@ -68,7 +68,7 @@ describe('@snyk/protect', () => {
6868
project.read('node_modules/lodash/lodash.js'),
6969
).resolves.toEqual(patchedLodash);
7070

71-
expect(log).toHaveBeenCalledWith('Successfully applied Snyk patches');
71+
expect(log).toHaveBeenCalledWith('Applied Snyk patches.');
7272
expect(postJsonSpy).toHaveBeenCalledTimes(1);
7373
expect(postJsonSpy.mock.calls[0][1]).toEqual({
7474
data: {
@@ -107,7 +107,7 @@ describe('@snyk/protect', () => {
107107

108108
await protect(project.path());
109109

110-
expect(log).toHaveBeenCalledWith('Nothing to patch');
110+
expect(log).toHaveBeenCalledWith('Nothing to patch.');
111111

112112
expect(postJsonSpy).toHaveBeenCalledTimes(1);
113113
expect(postJsonSpy.mock.calls[0][1]).toEqual({
@@ -142,7 +142,7 @@ describe('@snyk/protect', () => {
142142

143143
await protect(project.path());
144144

145-
expect(log).toHaveBeenCalledWith('No .snyk file found');
145+
expect(log).toHaveBeenCalledWith('No .snyk file found.');
146146

147147
expect(postJsonSpy).toHaveBeenCalledTimes(1);
148148
expect(postJsonSpy.mock.calls[0][1]).toEqual({

0 commit comments

Comments
 (0)