Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update acceptance tests for @snyk/protect #1867

Merged
merged 5 commits into from May 4, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -143,10 +143,11 @@
"devDependencies": {
"@types/agent-base": "^4.2.1",
"@types/diff": "^3.5.2",
"@types/fs-extra": "^9.0.11",
"@types/jest": "^26.0.20",
"@types/lodash": "^4.14.161",
"@types/needle": "^2.0.4",
"@types/node": "^10.0.0",
"@types/node": "^14.14.31",
"@types/restify": "^8.4.2",
"@types/sarif": "^2.1.2",
"@types/sinon": "^7.5.0",
Expand All @@ -155,6 +156,7 @@
"@typescript-eslint/parser": "^2.0.0",
"eslint": "6.8.0",
"eslint-config-prettier": "^6.1.0",
"fs-extra": "^9.1.0",
"jest": "^26.6.3",
"lodash": "^4.17.20",
"lodash.countby": "^4.6.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/snyk-protect/src/lib/index.ts
Expand Up @@ -9,6 +9,11 @@ import { PhysicalModuleToPatch } from './types';
async function protect(projectFolderPath: string) {
const snykFilePath = path.resolve(projectFolderPath, '.snyk');

if (!fs.existsSync(snykFilePath)) {
console.log('No .snyk file found');
return;
}

const snykFileContents = fs.readFileSync(snykFilePath, 'utf8');
const snykFilePatchMetadata = extractPatchMetadata(snykFileContents);

Expand Down
51,361 changes: 51,361 additions & 0 deletions packages/snyk-protect/test/acceptance/__snapshots__/protect.spec.ts.snap

Large diffs are not rendered by default.

This file was deleted.

96 changes: 96 additions & 0 deletions packages/snyk-protect/test/acceptance/protect.spec.ts
@@ -0,0 +1,96 @@
import * as fs from 'fs';
import protect from '../../src/lib';
import * as path from 'path';
import * as uuid from 'uuid';
import * as fse from 'fs-extra';

type TestProject = {
path: string;
file: (filePath: string) => Promise<string>;
};

describe('@snyk/protect', () => {
let tempFolder: string;

const createProject = async (fixture: string): Promise<TestProject> => {
const fixturePath = path.join(__dirname, '../fixtures', fixture);
const projectPath = path.join(tempFolder, fixture);
await fse.copy(fixturePath, projectPath);
return {
path: projectPath,
file: (filePath: string) => {
const fullFilePath = path.join(projectPath, filePath);
return fs.promises.readFile(fullFilePath, 'utf-8');
},
};
};

beforeAll(() => {
tempFolder = path.join(__dirname, '__output__', uuid.v4());
fs.mkdirSync(tempFolder, { recursive: true });
});

afterAll(() => {
fs.rmdirSync(tempFolder, { recursive: true });
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('applies patch(es)', () => {
it('works for project with a single patchable module', async () => {
const project = await createProject('single-patchable-module');

await protect(project.path);

expect(
project.file('node_modules/nyc/node_modules/lodash/lodash.js'),
).resolves.toMatchSnapshot();
});

it('works for project with multiple patchable modules', async () => {
const project = await createProject('multiple-matching-paths');

await protect(project.path);

expect(
project.file('node_modules/nyc/node_modules/lodash/lodash.js'),
).resolves.toMatchSnapshot();
expect(
project.file('node_modules/lodash/lodash.js'),
).resolves.toMatchSnapshot();
});
});

describe('does not apply any patches and does not fail', () => {
// 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
it('for project with no modules with the target package name', async () => {
const project = await createProject('no-matching-paths');
const log = jest.spyOn(global.console, 'log');

await protect(project.path);

expect(log).toHaveBeenCalledWith('Nothing to patch, done');
});

// skipped because we need to check the versions of the found modules before we attempt to patch them which we don't currently do
// and in order to do that, we need to first switch over to the new endpoint
// it('for a project that has an instance of the target module but we have no patches for its version', async () => {
// const project = await createProject('target-module-exists-but-no-patches-for-version');
// const log = jest.spyOn(global.console, 'log');
// await protect(project.path);
// expect(log).toHaveBeenCalledWith('Nothing to patch, done');
// });

// fixture has a lodash@4.14.1 which we don't have patches for
it('for project with no .snyk file', async () => {
const project = await createProject('no-snyk-file');
const log = jest.spyOn(global.console, 'log');

await protect(project.path);

expect(log).toHaveBeenCalledWith('No .snyk file found');
});
});
});