-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from snyk/feat/gradle-config-attributes-class…
…path-filter pass configattrs to javaCallGraphBuilder
- Loading branch information
Showing
4 changed files
with
128 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import * as path from 'path'; | ||
import { fixtureDir } from '../common'; | ||
import { test } from 'tap'; | ||
import { inspect, formatArgWithWhiteSpace } from '../../lib'; | ||
import * as fs from 'fs'; | ||
import * as sinon from 'sinon'; | ||
import * as javaCallGraphBuilder from '@snyk/java-call-graph-builder'; | ||
import { CallGraph } from '@snyk/cli-interface/legacy/common'; | ||
import { inspect } from '../../lib'; | ||
|
||
const rootNoWrapper = fixtureDir('no wrapper'); | ||
const withInitScript = fixtureDir('with-init-script'); | ||
|
@@ -59,73 +55,6 @@ test('run inspect() with on project that depends on gradle init script', async ( | |
); | ||
}); | ||
|
||
test('run inspect() with reachableVulns', async (t) => { | ||
const gradleCallGraph = JSON.parse( | ||
fs.readFileSync( | ||
path.join(fixtureDir('call-graphs'), 'simple.json'), | ||
'utf-8', | ||
), | ||
); | ||
const javaCallGraphBuilderStub = sinon | ||
.stub(javaCallGraphBuilder, 'getCallGraphGradle') | ||
.resolves(gradleCallGraph as CallGraph); | ||
const result = await inspect('.', path.join(rootNoWrapper, 'build.gradle'), { | ||
reachableVulns: true, | ||
}); | ||
|
||
const pkgs = result.dependencyGraph.getDepPkgs(); | ||
const nodeIds: string[] = []; | ||
Object.keys(pkgs).forEach((id) => { | ||
nodeIds.push(`${pkgs[id].name}@${pkgs[id].version}`); | ||
}); | ||
|
||
t.ok( | ||
nodeIds.indexOf('com.android.tools:[email protected]') !== -1, | ||
'correct version found', | ||
); | ||
t.ok(javaCallGraphBuilderStub.calledOnce, 'called to the call graph builder'); | ||
t.ok( | ||
javaCallGraphBuilderStub.calledWith( | ||
path.join('.', rootNoWrapper), | ||
'gradle', | ||
), | ||
'call graph builder was called with the correct path', | ||
); | ||
t.same(gradleCallGraph, result.callGraph, 'returns expected callgraph'); | ||
|
||
const resultWithInit = await inspect( | ||
'.', | ||
path.join(rootNoWrapper, 'build.gradle'), | ||
{ | ||
reachableVulns: true, | ||
initScript: path.join(rootNoWrapper, 'init.gradle'), | ||
}, | ||
); | ||
|
||
// test with init script param | ||
t.ok( | ||
javaCallGraphBuilderStub.calledTwice, | ||
'called to the call graph builder', | ||
); | ||
t.ok( | ||
javaCallGraphBuilderStub.calledWith( | ||
path.join('.', rootNoWrapper), | ||
'gradle', | ||
formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes | ||
), | ||
'call graph builder was called with the correct path and init file', | ||
); | ||
t.same( | ||
gradleCallGraph, | ||
resultWithInit.callGraph, | ||
'returns expected callgraph', | ||
); | ||
|
||
t.teardown(() => { | ||
javaCallGraphBuilderStub.restore(); | ||
}); | ||
}); | ||
|
||
test('multi-config: both compile and runtime deps picked up by default', async (t) => { | ||
const result = await inspect( | ||
'.', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import * as path from 'path'; | ||
import { fixtureDir } from '../common'; | ||
import { test } from 'tap'; | ||
import { inspect, formatArgWithWhiteSpace } from '../../lib'; | ||
import * as fs from 'fs'; | ||
import * as sinon from 'sinon'; | ||
import * as javaCallGraphBuilder from '@snyk/java-call-graph-builder'; | ||
import { CallGraph } from '@snyk/cli-interface/legacy/common'; | ||
|
||
const rootNoWrapper = fixtureDir('no wrapper'); | ||
|
||
test('reachableVulns', async (t) => { | ||
const gradleCallGraph = JSON.parse( | ||
fs.readFileSync( | ||
path.join(fixtureDir('call-graphs'), 'simple.json'), | ||
'utf-8', | ||
), | ||
); | ||
const javaCallGraphBuilderStub = sinon | ||
.stub(javaCallGraphBuilder, 'getCallGraphGradle') | ||
.resolves(gradleCallGraph as CallGraph); | ||
|
||
t.test('simple reachability scenario', async (t) => { | ||
const result = await inspect( | ||
'.', | ||
path.join(rootNoWrapper, 'build.gradle'), | ||
{ | ||
reachableVulns: true, | ||
}, | ||
); | ||
|
||
const pkgs = result.dependencyGraph.getDepPkgs(); | ||
const nodeIds: string[] = []; | ||
Object.keys(pkgs).forEach((id) => { | ||
nodeIds.push(`${pkgs[id].name}@${pkgs[id].version}`); | ||
}); | ||
|
||
t.ok( | ||
nodeIds.indexOf('com.android.tools:[email protected]') !== -1, | ||
'correct version found', | ||
); | ||
t.ok( | ||
javaCallGraphBuilderStub.calledOnce, | ||
'called to the call graph builder', | ||
); | ||
t.ok( | ||
javaCallGraphBuilderStub.calledWith( | ||
path.join('.', rootNoWrapper), | ||
'gradle', | ||
), | ||
'call graph builder was called with the correct path', | ||
); | ||
t.same(gradleCallGraph, result.callGraph, 'returns expected callgraph'); | ||
}); | ||
|
||
t.test('with init script', async (t) => { | ||
const resultWithInit = await inspect( | ||
'.', | ||
path.join(rootNoWrapper, 'build.gradle'), | ||
{ | ||
reachableVulns: true, | ||
initScript: path.join(rootNoWrapper, 'init.gradle'), | ||
}, | ||
); | ||
|
||
t.ok( | ||
javaCallGraphBuilderStub.calledTwice, | ||
'called to the call graph builder', | ||
); | ||
t.ok( | ||
javaCallGraphBuilderStub.calledWith( | ||
path.join('.', rootNoWrapper), | ||
'gradle', | ||
formatArgWithWhiteSpace(path.join(rootNoWrapper, 'init.gradle')), // arg should be normalized with quotes | ||
), | ||
'call graph builder was called with the correct path and init file', | ||
); | ||
t.same( | ||
gradleCallGraph, | ||
resultWithInit.callGraph, | ||
'returns expected callgraph', | ||
); | ||
}); | ||
|
||
t.test('with configuration attributes', async (t) => { | ||
const resultWithConfigAttrs = await inspect( | ||
'.', | ||
path.join(rootNoWrapper, 'build.gradle'), | ||
{ | ||
reachableVulns: true, | ||
'configuration-attributes': | ||
'buildtype:release,usage:java-runtime,newdim:appA', | ||
}, | ||
); | ||
|
||
t.ok( | ||
javaCallGraphBuilderStub.calledThrice, | ||
'called to the call graph builder', | ||
); | ||
t.ok( | ||
javaCallGraphBuilderStub.calledWith( | ||
path.join('.', rootNoWrapper), | ||
'gradle', | ||
undefined, | ||
'buildtype:release,usage:java-runtime,newdim:appA', | ||
), | ||
'call graph builder was called with the correct path and init file', | ||
); | ||
t.same( | ||
gradleCallGraph, | ||
resultWithConfigAttrs.callGraph, | ||
'returns expected callgraph', | ||
); | ||
}); | ||
|
||
t.teardown(() => { | ||
javaCallGraphBuilderStub.restore(); | ||
}); | ||
}); |