@@ -2,19 +2,44 @@ import { expect } from 'chai'
2
2
import fs from 'fs'
3
3
import sinon from "sinon" ;
4
4
import Java_maven from '../../src/providers/java_maven.js'
5
+ import esmock from 'esmock' ;
6
+ import path from 'path' ;
5
7
6
8
let clock
7
9
8
- /** this function is parsing the outputfile path from the given command, and write that file the providerContent supplied.
9
- *
10
- * @param {Array<string> }args - the arguments to pass to the binary
11
- * @param {string }providerContent - the content of the mocked data to replace original content in intercepted temp file
12
- * @param {string }outputFileParameter - name of the parameter indicating the output file of the command invocation, including '='.
13
- * @private
14
- */
15
- function interceptAndOverwriteDataWithMock ( args , providerContent , outputFileParameter ) {
16
- const interceptedFilePath = args . find ( arg => arg . includes ( outputFileParameter ) ) . split ( "=" ) [ 1 ]
17
- fs . writeFileSync ( interceptedFilePath , providerContent )
10
+ async function mockProvider ( cwd ) {
11
+
12
+ const mockInvokeCommand = ( ) => {
13
+ return '' ;
14
+ } ;
15
+
16
+ const mockGitRootDir = ( cwd ) => {
17
+ return cwd ;
18
+ }
19
+
20
+ const mockFs = {
21
+ mkdtempSync : ( pathName ) => pathName ,
22
+ readFileSync : ( filePath ) => {
23
+ const output = path . join ( cwd , path . basename ( filePath ) ) ;
24
+ return fs . readFileSync ( output ) ;
25
+ } ,
26
+ rmSync : ( ) => { }
27
+ }
28
+
29
+ return esmock ( '../../src/providers/java_maven.js' , {
30
+ fs : mockFs ,
31
+ '../../src/providers/base_java.js' : await esmock ( '../../src/providers/base_java.js' , {
32
+ '../../src/tools.js' : {
33
+ invokeCommand : mockInvokeCommand ,
34
+ getGitRootDir : mockGitRootDir
35
+ }
36
+ } )
37
+ } ) ;
38
+ }
39
+
40
+ async function createMockProvider ( testPath ) {
41
+ const Java_maven = await mockProvider ( testPath ) ;
42
+ return new Java_maven ( ) ;
18
43
}
19
44
20
45
suite ( 'testing the java-maven data provider' , ( ) => {
@@ -42,42 +67,16 @@ suite('testing the java-maven data provider', () => {
42
67
"pom_deps_with_no_ignore_common_paths"
43
68
] . forEach ( testCase => {
44
69
let scenario = testCase . replace ( 'pom_deps_' , '' ) . replaceAll ( '_' , ' ' )
45
- // test(`custom adhoc test`, async () => {
46
- //
47
- // // let options = {
48
- // // 'EXHORT_SNYK_TOKEN': 'insert-token'
49
- // // }
50
- // // let httpStatus = await exhort.validateToken(options);
51
- // analysisReport = await exhort.stackAnalysis(`/tmp/pom-xml/pom.xml`,false);
52
- // console.log(analysisReport)
53
- // let pom = fs.readFileSync(`/tmp/pom-xml/pom.xml`,).toString().trim()
54
- // let analysisReport = await exhort.componentAnalysis("pom.xml", pom);
55
- // console.log(analysisReport)
56
- // analysisReport = await exhort.stackAnalysis(`/tmp/pom-xml/pom.xml`,true);
57
- // console.log(analysisReport)
58
- //
59
- // }).timeout(process.env.GITHUB_ACTIONS ? 30000 : 5000)
60
70
61
71
test ( `verify maven data provided for stack analysis with scenario ${ scenario } ` , async ( ) => {
62
72
// load the expected graph for the scenario
63
73
let expectedSbom = fs . readFileSync ( `test/providers/tst_manifests/maven/${ testCase } /stack_analysis_expected_sbom.json` , ) . toString ( ) . trim ( )
64
- let dependencyTreeTextContent = fs . readFileSync ( `test/providers/tst_manifests/maven/${ testCase } /dep-tree.txt` , ) . toString ( )
74
+ // let dependencyTreeTextContent = fs.readFileSync(`test/providers/tst_manifests/maven/${testCase}/dep-tree.txt`,).toString()
65
75
expectedSbom = JSON . stringify ( JSON . parse ( expectedSbom ) , null , 4 )
66
- let mockedExecFunction = function ( bin , args ) {
67
- if ( args . find ( arg => arg . includes ( ":tree" ) ) ) {
68
- interceptAndOverwriteDataWithMock ( args , dependencyTreeTextContent , "DoutputFile=" )
69
- }
70
- }
71
- let javaMvnProvider = new Java_maven ( )
72
- Object . getPrototypeOf ( Object . getPrototypeOf ( javaMvnProvider ) ) . _invokeCommand = mockedExecFunction
76
+ let javaMvnProvider = await createMockProvider ( `test/providers/tst_manifests/maven/${ testCase } ` ) ;
73
77
// invoke sut stack analysis for scenario manifest
74
78
let providedDataForStack = javaMvnProvider . provideStack ( `test/providers/tst_manifests/maven/${ testCase } /pom.xml` )
75
79
// verify returned data matches expectation
76
- // expect(providedDataForStack).to.deep.equal({
77
- // ecosystem: 'maven',
78
- // contentType: 'application/vnd.cyclonedx+json',
79
- // content: expectedSbom
80
- // })
81
80
let beautifiedOutput = JSON . stringify ( JSON . parse ( providedDataForStack . content ) , null , 4 ) ;
82
81
expect ( beautifiedOutput ) . to . deep . equal ( expectedSbom )
83
82
@@ -89,14 +88,7 @@ suite('testing the java-maven data provider', () => {
89
88
let expectedSbom = fs . readFileSync ( `test/providers/tst_manifests/maven/${ testCase } /component_analysis_expected_sbom.json` , ) . toString ( ) . trim ( )
90
89
// read target manifest file
91
90
expectedSbom = JSON . stringify ( JSON . parse ( expectedSbom ) )
92
- let effectivePomContent = fs . readFileSync ( `test/providers/tst_manifests/maven/${ testCase } /effective-pom.xml` , ) . toString ( )
93
- let mockedExecFunction = function ( bin , args ) {
94
- if ( args . find ( arg => arg . includes ( ":effective-pom" ) ) ) {
95
- interceptAndOverwriteDataWithMock ( args , effectivePomContent , "Doutput=" ) ;
96
- }
97
- }
98
- let javaMvnProvider = new Java_maven ( )
99
- Object . getPrototypeOf ( Object . getPrototypeOf ( javaMvnProvider ) ) . _invokeCommand = mockedExecFunction
91
+ let javaMvnProvider = await createMockProvider ( `test/providers/tst_manifests/maven/${ testCase } ` ) ;
100
92
// invoke sut component analysis for scenario manifest
101
93
let providedDataForStack = javaMvnProvider . provideComponent ( `test/providers/tst_manifests/maven/${ testCase } /pom.xml` )
102
94
// verify returned data matches expectation
@@ -124,14 +116,7 @@ suite('testing the java-maven data provider with modules', () => {
124
116
let expectedSbom = fs . readFileSync ( `test/providers/tst_manifests/maven/${ testCase } /component_analysis_expected_sbom.json` , ) . toString ( ) . trim ( )
125
117
// read target manifest file
126
118
expectedSbom = JSON . stringify ( JSON . parse ( expectedSbom ) )
127
- let effectivePomContent = fs . readFileSync ( `test/providers/tst_manifests/maven/${ testCase } /effectivePom.xml` , ) . toString ( )
128
- let mockedExecFunction = function ( command , args ) {
129
- if ( args . find ( arg => arg . includes ( ":effective-pom" ) ) ) {
130
- interceptAndOverwriteDataWithMock ( args , effectivePomContent , "Doutput=" ) ;
131
- }
132
- }
133
- let javaMvnProvider = new Java_maven ( )
134
- Object . getPrototypeOf ( Object . getPrototypeOf ( javaMvnProvider ) ) . _invokeCommand = mockedExecFunction
119
+ let javaMvnProvider = await createMockProvider ( `test/providers/tst_manifests/maven/${ testCase } ` ) ;
135
120
// invoke sut component analysis for scenario manifest
136
121
let provideDataForComponent = javaMvnProvider . provideComponent ( `test/providers/tst_manifests/maven/${ testCase } /pom.xml` , { } )
137
122
// verify returned data matches expectation
@@ -140,12 +125,9 @@ suite('testing the java-maven data provider with modules', () => {
140
125
contentType : 'application/vnd.cyclonedx+json' ,
141
126
content : expectedSbom
142
127
} )
143
- // expect(beautifiedOutput).to.deep.equal(expectedSbom)
144
-
145
128
// these test cases takes ~2500-2700 ms each pr >10000 in CI (for the first test-case)
146
129
} ) . timeout ( process . env . GITHUB_ACTIONS ? 40000 : 10000 )
147
130
148
-
149
131
// these test cases takes ~1400-2000 ms each pr >10000 in CI (for the first test-case)
150
132
151
133
} )
0 commit comments