Skip to content

Commit 8e4862d

Browse files
authoredMay 4, 2021
Merge pull request #1867 from snyk/test/update-acceptance-tests
Update acceptance tests for @snyk/protect
2 parents 2220db1 + 99900c8 commit 8e4862d

File tree

15 files changed

+53402
-18747
lines changed

15 files changed

+53402
-18747
lines changed
 

‎.github/CODEOWNERS

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ src/lib/errors/unsupported-options-iac-error.ts @snyk/cloudconfig
2424
help/commands-docs/iac-examples.md @snyk/cloudconfig
2525
help/commands-docs/iac.md @snyk/cloudconfig
2626
packages/snyk-fix/ @snyk/tech-services
27+
packages/snyk-protect/ @snyk/hammer

‎package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,11 @@
143143
"devDependencies": {
144144
"@types/agent-base": "^4.2.1",
145145
"@types/diff": "^3.5.2",
146+
"@types/fs-extra": "^9.0.11",
146147
"@types/jest": "^26.0.20",
147148
"@types/lodash": "^4.14.161",
148149
"@types/needle": "^2.0.4",
149-
"@types/node": "^10.0.0",
150+
"@types/node": "^14.14.31",
150151
"@types/restify": "^8.4.2",
151152
"@types/sarif": "^2.1.2",
152153
"@types/sinon": "^7.5.0",
@@ -155,6 +156,7 @@
155156
"@typescript-eslint/parser": "^2.0.0",
156157
"eslint": "6.8.0",
157158
"eslint-config-prettier": "^6.1.0",
159+
"fs-extra": "^9.1.0",
158160
"jest": "^26.6.3",
159161
"lodash": "^4.17.20",
160162
"lodash.countby": "^4.6.0",

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

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { PhysicalModuleToPatch } from './types';
99
async function protect(projectFolderPath: string) {
1010
const snykFilePath = path.resolve(projectFolderPath, '.snyk');
1111

12+
if (!fs.existsSync(snykFilePath)) {
13+
console.log('No .snyk file found');
14+
return;
15+
}
16+
1217
const snykFileContents = fs.readFileSync(snykFilePath, 'utf8');
1318
const snykFilePatchMetadata = extractPatchMetadata(snykFileContents);
1419

‎packages/snyk-protect/test/acceptance/__snapshots__/protect.spec.ts.snap

+51,361
Large diffs are not rendered by default.

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

-40
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import * as fs from 'fs';
2+
import protect from '../../src/lib';
3+
import * as path from 'path';
4+
import * as uuid from 'uuid';
5+
import * as fse from 'fs-extra';
6+
7+
type TestProject = {
8+
path: string;
9+
file: (filePath: string) => Promise<string>;
10+
};
11+
12+
describe('@snyk/protect', () => {
13+
let tempFolder: string;
14+
15+
const createProject = async (fixture: string): Promise<TestProject> => {
16+
const fixturePath = path.join(__dirname, '../fixtures', fixture);
17+
const projectPath = path.join(tempFolder, fixture);
18+
await fse.copy(fixturePath, projectPath);
19+
return {
20+
path: projectPath,
21+
file: (filePath: string) => {
22+
const fullFilePath = path.join(projectPath, filePath);
23+
return fs.promises.readFile(fullFilePath, 'utf-8');
24+
},
25+
};
26+
};
27+
28+
beforeAll(() => {
29+
tempFolder = path.join(__dirname, '__output__', uuid.v4());
30+
fs.mkdirSync(tempFolder, { recursive: true });
31+
});
32+
33+
afterAll(() => {
34+
fs.rmdirSync(tempFolder, { recursive: true });
35+
});
36+
37+
afterEach(() => {
38+
jest.restoreAllMocks();
39+
});
40+
41+
describe('applies patch(es)', () => {
42+
it('works for project with a single patchable module', async () => {
43+
const project = await createProject('single-patchable-module');
44+
45+
await protect(project.path);
46+
47+
expect(
48+
project.file('node_modules/nyc/node_modules/lodash/lodash.js'),
49+
).resolves.toMatchSnapshot();
50+
});
51+
52+
it('works for project with multiple patchable modules', async () => {
53+
const project = await createProject('multiple-matching-paths');
54+
55+
await protect(project.path);
56+
57+
expect(
58+
project.file('node_modules/nyc/node_modules/lodash/lodash.js'),
59+
).resolves.toMatchSnapshot();
60+
expect(
61+
project.file('node_modules/lodash/lodash.js'),
62+
).resolves.toMatchSnapshot();
63+
});
64+
});
65+
66+
describe('does not apply any patches and does not fail', () => {
67+
// in this scenario .snyk file has a vulnId which corresponds to the `lodash` package, but there are not instances of lodash in the node_modules
68+
it('for project with no modules with the target package name', async () => {
69+
const project = await createProject('no-matching-paths');
70+
const log = jest.spyOn(global.console, 'log');
71+
72+
await protect(project.path);
73+
74+
expect(log).toHaveBeenCalledWith('Nothing to patch, done');
75+
});
76+
77+
// skipped because we need to check the versions of the found modules before we attempt to patch them which we don't currently do
78+
// and in order to do that, we need to first switch over to the new endpoint
79+
// it('for a project that has an instance of the target module but we have no patches for its version', async () => {
80+
// const project = await createProject('target-module-exists-but-no-patches-for-version');
81+
// const log = jest.spyOn(global.console, 'log');
82+
// await protect(project.path);
83+
// expect(log).toHaveBeenCalledWith('Nothing to patch, done');
84+
// });
85+
86+
// fixture has a lodash@4.14.1 which we don't have patches for
87+
it('for project with no .snyk file', async () => {
88+
const project = await createProject('no-snyk-file');
89+
const log = jest.spyOn(global.console, 'log');
90+
91+
await protect(project.path);
92+
93+
expect(log).toHaveBeenCalledWith('No .snyk file found');
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)
Please sign in to comment.