@@ -4,18 +4,34 @@ import * as path from 'path';
4
4
import * as uuid from 'uuid' ;
5
5
import * as fse from 'fs-extra' ;
6
6
7
+ type TestProject = {
8
+ path : string ;
9
+ file : ( filePath : string ) => Promise < string > ;
10
+ } ;
11
+
7
12
describe ( '@snyk/protect' , ( ) => {
8
13
let tempFolder : string ;
9
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
+
10
28
beforeAll ( ( ) => {
11
29
tempFolder = path . join ( __dirname , '__output__' , uuid . v4 ( ) ) ;
12
30
fs . mkdirSync ( tempFolder , { recursive : true } ) ;
13
31
} ) ;
14
32
15
33
afterAll ( ( ) => {
16
- fs . rmdirSync ( tempFolder , {
17
- recursive : true ,
18
- } ) ;
34
+ fs . rmdirSync ( tempFolder , { recursive : true } ) ;
19
35
} ) ;
20
36
21
37
afterEach ( ( ) => {
@@ -24,81 +40,55 @@ describe('@snyk/protect', () => {
24
40
25
41
describe ( 'applies patch(es)' , ( ) => {
26
42
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 ( ) ;
40
50
} ) ;
41
-
51
+
42
52
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 ( ) ;
62
63
} ) ;
63
64
} ) ;
64
65
65
66
describe ( 'does not apply any patches and does not fail' , ( ) => {
66
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
67
68
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' ) ;
72
70
const log = jest . spyOn ( global . console , 'log' ) ;
73
- await fse . copy ( fixtureFolder , modulePath ) ;
74
- await protect ( modulePath ) ;
71
+
72
+ await protect ( project . path ) ;
75
73
76
74
expect ( log ) . toHaveBeenCalledWith ( 'Nothing to patch, done' ) ;
77
75
} ) ;
78
76
79
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
80
78
// and in order to do that, we need to first switch over to the new endpoint
81
79
// 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');
86
81
// const log = jest.spyOn(global.console, 'log');
87
- // await fse.copy(fixtureFolder, modulePath);
88
- // await protect(modulePath);
89
-
82
+ // await protect(project.path);
90
83
// expect(log).toHaveBeenCalledWith('Nothing to patch, done');
91
84
// });
92
85
93
86
// fixture has a lodash@4.14.1 which we don't have patches for
94
87
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' ) ;
99
89
const log = jest . spyOn ( global . console , 'log' ) ;
100
- await fse . copy ( fixtureFolder , modulePath ) ;
101
- await protect ( modulePath ) ;
90
+
91
+ await protect ( project . path ) ;
102
92
103
93
expect ( log ) . toHaveBeenCalledWith ( 'No .snyk file found' ) ;
104
94
} ) ;
0 commit comments