Skip to content

Commit

Permalink
Merge pull request #889 from snyk/feat/support-mvn-all-jars-arg
Browse files Browse the repository at this point in the history
feat: support mvn --scan-all-unmanaged
  • Loading branch information
orsagie committed Dec 3, 2019
2 parents 09e16ad + ac81480 commit cfaa1b3
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"find-circular": "npm run build && madge --circular ./dist",
"format": "prettier --write '{src,test,scripts}/**/*.{js,ts}'",
"prepare": "npm run build",
"test-common": "npm run check-tests && npm run build && npm run lint && node --require ts-node/register src/cli test --org=snyk",
"tap": "tap test/*.test.* test/acceptance/**/*.test.* test/system/*.test.* -Rspec --timeout=300 --node-arg=-r --node-arg=ts-node/register",
"test": "npm run test-common && npm run tap",
"test-common": "npm run check-tests && npm run build && npm run lint && node --require ts-node/register src/cli test --org=snyk",
"lint": "run-p --aggregate-output lint:*",
"lint:js": "eslint --color --cache 'src/**/*.{js,ts}'",
"lint:formatting": "prettier --check '{src,test,scripts}/**/*.{js,ts}'",
Expand Down Expand Up @@ -79,7 +79,7 @@
"snyk-go-plugin": "1.11.1",
"snyk-gradle-plugin": "3.2.2",
"snyk-module": "1.9.1",
"snyk-mvn-plugin": "2.6.1",
"snyk-mvn-plugin": "2.7.0",
"snyk-nodejs-lockfile-parser": "1.16.1",
"snyk-nuget-plugin": "1.13.1",
"snyk-php-plugin": "1.7.0",
Expand Down
1 change: 1 addition & 0 deletions src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export function args(rawArgv: string[]): Args {
'sub-project',
'gradle-sub-project',
'skip-unresolved',
'scan-all-unmanaged',
]) {
if (argv[dashedArg]) {
const camelCased = dashToCamelCase(dashedArg);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async function monitor(...args0: MethodArgs): Promise<any> {
let packageManager = detect.detectPackageManager(path, options);

const targetFile =
options.docker && !options.file // snyk monitor --docker (without --file)
!options.scanAllUnmanaged && options.docker && !options.file // snyk monitor --docker (without --file)
? undefined
: options.file || detect.detectPackageFile(path);

Expand Down
6 changes: 6 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ async function main() {
let failed = false;
let exitCode = EXIT_CODES.ERROR;
try {
if (args.options.scanAllUnmanaged && args.options.file) {
throw new UnsupportedOptionCombinationError([
'file',
'scan-all-unmanaged',
]);
}
if (
args.options.file &&
typeof args.options.file === 'string' &&
Expand Down
2 changes: 2 additions & 0 deletions src/lib/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export function detectPackageManager(root, options) {
}
file = options.file;
packageManager = detectPackageManagerFromFile(file);
} else if (options.scanAllUnmanaged) {
packageManager = 'maven';
} else {
debug('no file specified. Trying to autodetect in base folder ' + root);
file = detectPackageFile(root);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/errors/unsupported-option-combination-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export class UnsupportedOptionCombinationError extends CustomError {

constructor(options: string[]) {
super(
UnsupportedOptionCombinationError.ERROR_MESSAGE + JSON.stringify(options),
UnsupportedOptionCombinationError.ERROR_MESSAGE + options.join(' + '),
);
this.code = 422;
this.userMessage =
UnsupportedOptionCombinationError.ERROR_MESSAGE + JSON.stringify(options);
UnsupportedOptionCombinationError.ERROR_MESSAGE + options.join(' + ');
}
}
1 change: 1 addition & 0 deletions src/lib/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Options {
composerIsFine?: boolean;
composerPharIsFine?: boolean;
systemVersions?: object;
scanAllUnmanaged?: boolean;
}

export interface Plugin {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/snyk-test/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ async function getDepsFromPlugin(
root,
options: Options,
): Promise<pluginApi.MultiProjectResult> {
options.file = options.file || detect.detectPackageFile(root);
// don't override options.file if scanning multiple files at once
if (!options.scanAllUnmanaged) {
options.file = options.file || detect.detectPackageFile(root);
}
if (!options.docker && !(options.file || options.packageManager)) {
throw NoSupportedManifestsFoundError([...root]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface Options {
dev?: boolean;
'print-deps'?: boolean;
'remote-repo-url'?: string;
scanAllUnmanaged?: boolean;
}

// TODO(kyegupov): catch accessing ['undefined-properties'] via noImplicitAny
Expand All @@ -70,6 +71,7 @@ export interface MonitorOptions {
'project-name'?: string;
'print-deps'?: boolean;
'experimental-dep-graph'?: boolean;
scanAllUnmanaged?: boolean;

// An experimental flag to allow monitoring of bigtrees (with degraded deps info and remediation advice).
'prune-repeated-subdependencies'?: boolean;
Expand Down
23 changes: 21 additions & 2 deletions test/acceptance/cli-args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('`test multiple paths with --project-name=NAME`', (t) => {
}
t.match(
stdout.trim(),
'The following option combination is not currently supported: ["multiple paths","project-name"]',
'The following option combination is not currently supported: multiple paths + project-name',
'correct error output',
);
},
Expand All @@ -64,7 +64,26 @@ test('`test --file=file.sln --project-name=NAME`', (t) => {
}
t.match(
stdout.trim(),
'The following option combination is not currently supported: ["file=*.sln","project-name"]',
'The following option combination is not currently supported: file=*.sln + project-name',
'correct error output',
);
},
);
});

test('`test --file=blah --scan-all-unmanaged`', (t) => {
t.plan(1);

exec(
`node ${main} test --file=blah --scan-all-unmanaged`,
(err, stdout, stderr) => {
if (err) {
throw err;
}
console.log(stdout.trim());
t.match(
stdout.trim(),
'The following option combination is not currently supported: file + scan-all-unmanaged',
'correct error output',
);
},
Expand Down
27 changes: 27 additions & 0 deletions test/acceptance/cli-monitor/cli-monitor.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,33 @@ test('`monitor maven-app-with-jars --file=example.war` sends package info', asyn
t.match(req.url, '/monitor/maven', 'puts at correct url');
});

test('`test maven-app-with-jars --scan-all-unmanaged` sends package info', async (t) => {
chdirWorkspaces();
const plugin = {
async inspect() {
return {
package: {},
plugin: { name: 'testplugin', runtime: 'testruntime' },
};
},
};
const spyPlugin = sinon.spy(plugin, 'inspect');
const loadPlugin = sinon.stub(plugins, 'loadPlugin');
t.teardown(loadPlugin.restore);
loadPlugin.withArgs('maven').returns(plugin);
await cli.monitor('maven-app-with-jars', {
scanAllUnmanaged: true,
});
const req = server.popRequest();
t.equal(req.method, 'PUT', 'makes PUT request');
t.equal(
req.headers['x-snyk-cli-version'],
versionNumber,
'sends version number',
);
t.match(req.url, '/monitor/maven', 'puts at correct url');
});

test('`monitor yarn-app`', async (t) => {
chdirWorkspaces('yarn-app');
await cli.monitor();
Expand Down
50 changes: 50 additions & 0 deletions test/acceptance/cli-test/cli-test.maven.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,55 @@ export const MavenTests: AcceptanceTests = {
'calls mvn plugin',
);
},

'`test maven-app-with-jars --scan-all-unmanaged` sends package info': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
const plugin = {
async inspect() {
return {
package: {},
plugin: { name: 'testplugin', runtime: 'testruntime' },
};
},
};
const spyPlugin = sinon.spy(plugin, 'inspect');
const loadPlugin = sinon.stub(params.plugins, 'loadPlugin');
t.teardown(loadPlugin.restore);
loadPlugin.withArgs('maven').returns(plugin);
await params.cli.test('maven-app-with-jars', {
scanAllUnmanaged: true,
});
const req = params.server.popRequest();
t.equal(req.method, 'POST', 'makes POST request');
t.equal(
req.headers['x-snyk-cli-version'],
params.versionNumber,
'sends version number',
);
t.match(req.url, '/test-dep-graph', 'posts to correct url');
t.match(req.body.targetFile, undefined, 'target is undefined');
t.equal(req.body.depGraph.pkgManager.name, 'maven');
t.same(
spyPlugin.getCall(0).args,
[
'maven-app-with-jars',
undefined, // no specified target file
{
args: null,
// file: undefined, no file
org: null,
projectName: null,
packageManager: 'maven',
path: 'maven-app-with-jars',
showVulnPaths: 'some',
scanAllUnmanaged: true,
},
],
'calls mvn plugin',
);
},
},
};
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2019-04-03T10:53:44.704Z
2019-04-03T10:53:44.704Z

0 comments on commit cfaa1b3

Please sign in to comment.