Skip to content

Commit

Permalink
refactor: encapsulate test project logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahed Ahmed committed Apr 30, 2021
1 parent 310df30 commit 1b5ba88
Showing 1 changed file with 45 additions and 55 deletions.
100 changes: 45 additions & 55 deletions packages/snyk-protect/test/acceptance/protect.spec.ts
Expand Up @@ -4,18 +4,34 @@ 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,
});
fs.rmdirSync(tempFolder, { recursive: true });
});

afterEach(() => {
Expand All @@ -24,81 +40,55 @@ describe('@snyk/protect', () => {

describe('applies patch(es)', () => {
it('works for project with a single patchable module', async () => {
const fixture = 'single-patchable-module';
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
const modulePath = path.join(tempFolder, fixture);
const targeFilePath = path.join(
modulePath,
'node_modules/nyc/node_modules/lodash/lodash.js',
);

await fse.copy(fixtureFolder, modulePath);
await protect(modulePath);

const actualPatchedFileContents = fs.readFileSync(targeFilePath, 'utf-8');
expect(actualPatchedFileContents).toMatchSnapshot();
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 fixture = 'multiple-matching-paths';
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
const modulePath = path.join(tempFolder, fixture);
const targeFilePath1 = path.join(
modulePath,
'node_modules/nyc/node_modules/lodash/lodash.js',
);
const targeFilePath2 = path.join(
modulePath,
'node_modules/lodash/lodash.js',
);

await fse.copy(fixtureFolder, modulePath);
await protect(modulePath);

const actualPatchedFileContents = fs.readFileSync(targeFilePath1, 'utf-8');
expect(actualPatchedFileContents).toMatchSnapshot();
const actualPatchedFileContents2 = fs.readFileSync(targeFilePath2, 'utf-8');
expect(actualPatchedFileContents2).toMatchSnapshot();
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 fixture = 'no-matching-paths';
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
const modulePath = path.join(tempFolder, fixture);

const project = await createProject('no-matching-paths');
const log = jest.spyOn(global.console, 'log');
await fse.copy(fixtureFolder, modulePath);
await protect(modulePath);

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 fixture = 'target-module-exists-but-no-patches-for-version';
// const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
// const modulePath = path.join(tempFolder, fixture);

// const project = await createProject('target-module-exists-but-no-patches-for-version');
// const log = jest.spyOn(global.console, 'log');
// await fse.copy(fixtureFolder, modulePath);
// await protect(modulePath);

// 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 fixture = 'no-snyk-file';
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
const modulePath = path.join(tempFolder, fixture);

const project = await createProject('no-snyk-file');
const log = jest.spyOn(global.console, 'log');
await fse.copy(fixtureFolder, modulePath);
await protect(modulePath);

await protect(project.path);

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

0 comments on commit 1b5ba88

Please sign in to comment.