Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: count pkgs from graph if count is undefined
  • Loading branch information
lili2311 committed Jun 29, 2020
1 parent 1168d09 commit a88c6c6
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 9 deletions.
9 changes: 8 additions & 1 deletion src/cli/index.ts
Expand Up @@ -250,7 +250,7 @@ async function main() {
}

if (args.options.file && args.options.yarnWorkspaces) {
throw new UnsupportedOptionCombinationError(['file', 'all-projects']);
throw new UnsupportedOptionCombinationError(['file', 'yarn-workspaces']);
}

if (args.options.file && args.options.allProjects) {
Expand Down Expand Up @@ -282,6 +282,13 @@ async function main() {
throw new UnsupportedOptionCombinationError(['docker', 'all-projects']);
}

if (args.options.docker && args.options.yarnWorkspaces) {
throw new UnsupportedOptionCombinationError([
'docker',
'yarn-workspaces',
]);
}

if (args.options.allSubProjects && args.options.yarnWorkspaces) {
throw new UnsupportedOptionCombinationError([
'all-sub-projects',
Expand Down
4 changes: 3 additions & 1 deletion src/lib/snyk-test/run-test.ts
Expand Up @@ -193,7 +193,9 @@ async function parseRes(

// For Node.js: inject additional information (for remediation etc.) into the response.
if (payload.modules) {
res.dependencyCount = payload.modules.numDependencies;
res.dependencyCount =
payload.modules.numDependencies ||
depGraph.countPathsToRoot(depGraph.rootPkg);
if (res.vulnerabilities) {
res.vulnerabilities.forEach((vuln) => {
if (payload.modules && payload.modules.pluck) {
Expand Down
5 changes: 2 additions & 3 deletions test/acceptance/cli-args.test.ts
Expand Up @@ -120,7 +120,6 @@ const argsNotAllowedWithYarnWorkspaces = [
'project-name',
'docker',
'all-sub-projects',
'all-projects',
];

argsNotAllowedWithYarnWorkspaces.forEach((arg) => {
Expand All @@ -132,7 +131,7 @@ argsNotAllowedWithYarnWorkspaces.forEach((arg) => {
}
t.match(
stdout.trim(),
`The following option combination is not currently supported: ${arg} + all-projects`,
`The following option combination is not currently supported: ${arg} + yarn-workspaces`,
'when using test',
);
});
Expand All @@ -142,7 +141,7 @@ argsNotAllowedWithYarnWorkspaces.forEach((arg) => {
}
t.match(
stdout.trim(),
`The following option combination is not currently supported: ${arg} + all-projects`,
`The following option combination is not currently supported: ${arg} + yarn-workspaces`,
'when using monitor',
);
});
Expand Down
124 changes: 120 additions & 4 deletions test/acceptance/cli-test/cli-test.yarn-workspaces.spec.ts
Expand Up @@ -135,6 +135,14 @@ export const YarnWorkspacesTests: AcceptanceTests = {
const loadPlugin = sinon.spy(params.plugins, 'loadPlugin');
// the parser is used directly
t.ok(loadPlugin.withArgs('yarn').notCalled, 'skips load plugin');
t.teardown(() => {
loadPlugin.restore();
});
t.match(
result.getDisplayResults(),
'✓ Tested 1 dependencies for known vulnerabilities, no vulnerable paths found.',
'correctly showing dep number',
);
t.match(result.getDisplayResults(), 'Package manager: yarn\n');
t.match(
result.getDisplayResults(),
Expand All @@ -156,6 +164,7 @@ export const YarnWorkspacesTests: AcceptanceTests = {
'Tested 3 projects, no vulnerable paths were found.',
'no vulnerable paths found as both policies detected and applied.',
);
let policyCount = 0;

params.server.popRequests(3).forEach((req) => {
t.equal(req.method, 'POST', 'makes POST request');
Expand All @@ -166,19 +175,126 @@ export const YarnWorkspacesTests: AcceptanceTests = {
);
t.match(req.url, '/api/v1/test-dep-graph', 'posts to correct url');
t.ok(req.body.depGraph, 'body contains depGraph');
t.ok(req.body.policy, 'body contains policy');

if (req.body.targetFileRelativePath.endsWith('apples/package.json')) {
t.match(
req.body.policy,
'npm:node-uuid:20160328',
'policy is as expected',
);
t.ok(req.body.policy, 'body contains policy');
policyCount += 1;
} else if (
req.body.targetFileRelativePath.endsWith('tomatoes/package.json')
) {
t.notOk(req.body.policy, 'body does not contain policy');
} else if (
req.body.targetFileRelativePath.endsWith(
'yarn-workspaces/package.json',
)
) {
t.match(
req.body.policy,
'npm:node-uuid:20111130',
'policy is as expected',
);
t.ok(req.body.policy, 'body contains policy');
policyCount += 1;
}
t.equal(
req.body.depGraph.pkgManager.name,
'yarn',
'depGraph has package manager',
);
});
t.equal(policyCount, 2, '2 policies found in a workspace');
},
'test --yarn-workspaces --detection-depth=5 multiple workspaces found': (
params,
utils,
) => async (t) => {
utils.chdirWorkspaces();
const result = await params.cli.test({
yarnWorkspaces: true,
detectionDepth: 5,
strictOutOfSync: false,
});
const loadPlugin = sinon.spy(params.plugins, 'loadPlugin');
// the parser is used directly
t.ok(loadPlugin.withArgs('yarn').notCalled, 'skips load plugin');
t.teardown(() => {
loadPlugin.restore();
});
t.match(
result.getDisplayResults(),
'✓ Tested 1 dependencies for known vulnerabilities, no vulnerable paths found.',
'correctly showing dep number',
);
t.match(result.getDisplayResults(), 'Package manager: yarn\n');
t.match(
result.getDisplayResults(),
'Project name: package.json',
'yarn project in output',
);
t.match(
result.getDisplayResults(),
'Project name: tomatoes',
'yarn project in output',
);
t.match(
result.getDisplayResults(),
'Project name: apples',
'yarn project in output',
);
t.match(
result.getDisplayResults(),
'Tested 6 projects, no vulnerable paths were found.',
'Tested 6 projects',
);
let policyCount = 0;

params.server.popRequests(3).forEach((req) => {
t.equal(req.method, 'POST', 'makes POST request');
t.equal(
req.body.policyLocations,
['yarn-workspaces', 'yarn-workspaces/apples'],
'policy locations',
req.headers['x-snyk-cli-version'],
params.versionNumber,
'sends version number',
);
t.match(req.url, '/api/v1/test-dep-graph', 'posts to correct url');
t.ok(req.body.depGraph, 'body contains depGraph');

if (req.body.targetFileRelativePath.endsWith('apples/package.json')) {
t.match(
req.body.policy,
'npm:node-uuid:20160328',
'policy is as expected',
);
t.ok(req.body.policy, 'body contains policy');
policyCount += 1;
} else if (
req.body.targetFileRelativePath.endsWith('tomatoes/package.json')
) {
t.notOk(req.body.policy, 'body does not contain policy');
} else if (
req.body.targetFileRelativePath.endsWith(
'yarn-workspaces/package.json',
)
) {
t.match(
req.body.policy,
'npm:node-uuid:20111130',
'policy is as expected',
);
t.ok(req.body.policy, 'body contains policy');
policyCount += 1;
}
t.equal(
req.body.depGraph.pkgManager.name,
'yarn',
'depGraph has package manager',
);
});
t.equal(policyCount, 2, '2 policies found in a workspace');
},
},
};

0 comments on commit a88c6c6

Please sign in to comment.