Skip to content

Commit 376a71f

Browse files
Jahed Ahmedmaxjeffos
Jahed Ahmed
authored andcommittedMay 3, 2021
refactor: encapsulate test project logic
1 parent edc798f commit 376a71f

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed
 

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

+45-55
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,34 @@ import * as path from 'path';
44
import * as uuid from 'uuid';
55
import * as fse from 'fs-extra';
66

7+
type TestProject = {
8+
path: string;
9+
file: (filePath: string) => Promise<string>;
10+
};
11+
712
describe('@snyk/protect', () => {
813
let tempFolder: string;
914

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+
1028
beforeAll(() => {
1129
tempFolder = path.join(__dirname, '__output__', uuid.v4());
1230
fs.mkdirSync(tempFolder, { recursive: true });
1331
});
1432

1533
afterAll(() => {
16-
fs.rmdirSync(tempFolder, {
17-
recursive: true,
18-
});
34+
fs.rmdirSync(tempFolder, { recursive: true });
1935
});
2036

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

2541
describe('applies patch(es)', () => {
2642
it('works for project with a single patchable module', async () => {
27-
const fixture = 'single-patchable-module';
28-
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
29-
const modulePath = path.join(tempFolder, fixture);
30-
const targeFilePath = path.join(
31-
modulePath,
32-
'node_modules/nyc/node_modules/lodash/lodash.js',
33-
);
34-
35-
await fse.copy(fixtureFolder, modulePath);
36-
await protect(modulePath);
37-
38-
const actualPatchedFileContents = fs.readFileSync(targeFilePath, 'utf-8');
39-
expect(actualPatchedFileContents).toMatchSnapshot();
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();
4050
});
41-
51+
4252
it('works for project with multiple patchable modules', async () => {
43-
const fixture = 'multiple-matching-paths';
44-
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
45-
const modulePath = path.join(tempFolder, fixture);
46-
const targeFilePath1 = path.join(
47-
modulePath,
48-
'node_modules/nyc/node_modules/lodash/lodash.js',
49-
);
50-
const targeFilePath2 = path.join(
51-
modulePath,
52-
'node_modules/lodash/lodash.js',
53-
);
54-
55-
await fse.copy(fixtureFolder, modulePath);
56-
await protect(modulePath);
57-
58-
const actualPatchedFileContents = fs.readFileSync(targeFilePath1, 'utf-8');
59-
expect(actualPatchedFileContents).toMatchSnapshot();
60-
const actualPatchedFileContents2 = fs.readFileSync(targeFilePath2, 'utf-8');
61-
expect(actualPatchedFileContents2).toMatchSnapshot();
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();
6263
});
6364
});
6465

6566
describe('does not apply any patches and does not fail', () => {
6667
// 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
6768
it('for project with no modules with the target package name', async () => {
68-
const fixture = 'no-matching-paths';
69-
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
70-
const modulePath = path.join(tempFolder, fixture);
71-
69+
const project = await createProject('no-matching-paths');
7270
const log = jest.spyOn(global.console, 'log');
73-
await fse.copy(fixtureFolder, modulePath);
74-
await protect(modulePath);
71+
72+
await protect(project.path);
7573

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

7977
// skipped because we need to check the versions of the found modules before we attempt to patch them which we don't currently do
8078
// and in order to do that, we need to first switch over to the new endpoint
8179
// it('for a project that has an instance of the target module but we have no patches for its version', async () => {
82-
// const fixture = 'target-module-exists-but-no-patches-for-version';
83-
// const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
84-
// const modulePath = path.join(tempFolder, fixture);
85-
80+
// const project = await createProject('target-module-exists-but-no-patches-for-version');
8681
// const log = jest.spyOn(global.console, 'log');
87-
// await fse.copy(fixtureFolder, modulePath);
88-
// await protect(modulePath);
89-
82+
// await protect(project.path);
9083
// expect(log).toHaveBeenCalledWith('Nothing to patch, done');
9184
// });
9285

9386
// fixture has a lodash@4.14.1 which we don't have patches for
9487
it('for project with no .snyk file', async () => {
95-
const fixture = 'no-snyk-file';
96-
const fixtureFolder = path.join(__dirname, '../fixtures', fixture);
97-
const modulePath = path.join(tempFolder, fixture);
98-
88+
const project = await createProject('no-snyk-file');
9989
const log = jest.spyOn(global.console, 'log');
100-
await fse.copy(fixtureFolder, modulePath);
101-
await protect(modulePath);
90+
91+
await protect(project.path);
10292

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

0 commit comments

Comments
 (0)
Please sign in to comment.