From 7dfc02784f23424d219e581587d53d32fa1e5265 Mon Sep 17 00:00:00 2001 From: ghe Date: Thu, 13 Aug 2020 14:35:13 +0100 Subject: [PATCH] test: kotlin monorepo --all-projects --- .../cli-monitor.all-projects.spec.ts | 104 ++++++++++++++- .../cli-test/cli-test.all-projects.spec.ts | 119 +++++++++++++++++- .../workspaces/kotlin-monorepo/Gemfile | 4 + .../workspaces/kotlin-monorepo/Gemfile.lock | 15 +++ .../kotlin-monorepo/build.gradle.kts | 1 + .../kotlin-monorepo/subproj/build.gradle.kts | 1 + 6 files changed, 241 insertions(+), 3 deletions(-) create mode 100644 test/acceptance/workspaces/kotlin-monorepo/Gemfile create mode 100644 test/acceptance/workspaces/kotlin-monorepo/Gemfile.lock create mode 100644 test/acceptance/workspaces/kotlin-monorepo/build.gradle.kts create mode 100644 test/acceptance/workspaces/kotlin-monorepo/subproj/build.gradle.kts diff --git a/test/acceptance/cli-monitor/cli-monitor.all-projects.spec.ts b/test/acceptance/cli-monitor/cli-monitor.all-projects.spec.ts index 97603ca3488..00bd0affe5b 100644 --- a/test/acceptance/cli-monitor/cli-monitor.all-projects.spec.ts +++ b/test/acceptance/cli-monitor/cli-monitor.all-projects.spec.ts @@ -801,7 +801,6 @@ export const AllProjectsTests: AcceptanceTests = { }; }, }; - const spyPlugin = sinon.spy(plugin, 'inspect'); const loadPlugin = sinon.stub(params.plugins, 'loadPlugin'); t.teardown(loadPlugin.restore); loadPlugin.withArgs('gradle').returns(plugin); @@ -842,5 +841,108 @@ export const AllProjectsTests: AcceptanceTests = { ); }); }, + '`monitor kotlin-monorepo --all-projects` scans kotlin files': ( + params, + utils, + ) => async (t) => { + utils.chdirWorkspaces(); + const simpleGradleGraph = depGraphLib.createFromJSON({ + schemaVersion: '1.2.0', + pkgManager: { + name: 'gradle', + }, + pkgs: [ + { + id: 'gradle-monorepo@0.0.0', + info: { + name: 'gradle-monorepo', + version: '0.0.0', + }, + }, + ], + graph: { + rootNodeId: 'root-node', + nodes: [ + { + nodeId: 'root-node', + pkgId: 'gradle-monorepo@0.0.0', + deps: [], + }, + ], + }, + }); + const plugin = { + async inspect() { + return { + plugin: { + name: 'bundled:gradle', + runtime: 'unknown', + meta: {}, + }, + scannedProjects: [ + { + meta: { + gradleProjectName: 'root-proj', + versionBuildInfo: { + gradleVersion: '6.5', + }, + }, + depGraph: simpleGradleGraph, + }, + { + meta: { + gradleProjectName: 'root-proj/subproj', + versionBuildInfo: { + gradleVersion: '6.5', + }, + }, + depGraph: simpleGradleGraph, + }, + ], + }; + }, + }; + const loadPlugin = sinon.stub(params.plugins, 'loadPlugin'); + t.teardown(loadPlugin.restore); + loadPlugin.withArgs('gradle').returns(plugin); + loadPlugin.callThrough(); + + const result = await params.cli.monitor('kotlin-monorepo', { + allProjects: true, + detectionDepth: 3, + }); + t.ok(loadPlugin.withArgs('rubygems').calledOnce, 'calls rubygems plugin'); + t.ok(loadPlugin.withArgs('gradle').calledOnce, 'calls gradle plugin'); + + t.match( + result, + 'gradle/graph/some/project-id', + 'gradle project was monitored', + ); + t.match( + result, + 'rubygems/graph/some/project-id', + 'rubygems project was monitored', + ); + // Pop one extra call to server and filter out call to `featureFlag` endpoint + const requests = params.server + .popRequests(4) + .filter((req) => req.url.includes('/monitor/')); + t.equal(requests.length, 3, 'correct amount of monitor requests'); + requests.forEach((req) => { + t.match( + req.url, + /\/api\/v1\/monitor\/(rubygems\/graph|gradle\/graph)/, + 'puts at correct url', + ); + t.notOk(req.body.targetFile, "doesn't send the targetFile"); + t.equal(req.method, 'PUT', 'makes PUT request'); + t.equal( + req.headers['x-snyk-cli-version'], + params.versionNumber, + 'sends version number', + ); + }); + }, }, }; diff --git a/test/acceptance/cli-test/cli-test.all-projects.spec.ts b/test/acceptance/cli-test/cli-test.all-projects.spec.ts index 6a71a1372f0..90862be8034 100644 --- a/test/acceptance/cli-test/cli-test.all-projects.spec.ts +++ b/test/acceptance/cli-test/cli-test.all-projects.spec.ts @@ -1,12 +1,127 @@ -import { AcceptanceTests } from './cli-test.acceptance.test'; -import { getWorkspaceJSON } from '../workspace-helper'; import * as path from 'path'; import * as sinon from 'sinon'; +import * as depGraphLib from '@snyk/dep-graph'; import { CommandResult } from '../../../src/cli/commands/types'; +import { AcceptanceTests } from './cli-test.acceptance.test'; +import { getWorkspaceJSON } from '../workspace-helper'; export const AllProjectsTests: AcceptanceTests = { language: 'Mixed', tests: { + '`test kotlin-monorepo --all-projects` scans kotlin files': ( + params, + utils, + ) => async (t) => { + utils.chdirWorkspaces(); + const simpleGradleGraph = depGraphLib.createFromJSON({ + schemaVersion: '1.2.0', + pkgManager: { + name: 'gradle', + }, + pkgs: [ + { + id: 'gradle-monorepo@0.0.0', + info: { + name: 'gradle-monorepo', + version: '0.0.0', + }, + }, + ], + graph: { + rootNodeId: 'root-node', + nodes: [ + { + nodeId: 'root-node', + pkgId: 'gradle-monorepo@0.0.0', + deps: [], + }, + ], + }, + }); + const plugin = { + async inspect() { + return { + plugin: { + name: 'bundled:gradle', + runtime: 'unknown', + meta: {}, + }, + scannedProjects: [ + { + meta: { + gradleProjectName: 'root-proj', + versionBuildInfo: { + gradleVersion: '6.5', + }, + }, + depGraph: simpleGradleGraph, + }, + { + meta: { + gradleProjectName: 'root-proj/subproj', + versionBuildInfo: { + gradleVersion: '6.5', + }, + }, + depGraph: simpleGradleGraph, + }, + ], + }; + }, + }; + const loadPlugin = sinon.stub(params.plugins, 'loadPlugin'); + t.teardown(loadPlugin.restore); + loadPlugin.withArgs('gradle').returns(plugin); + loadPlugin.callThrough(); + + const result: CommandResult = await params.cli.test('kotlin-monorepo', { + allProjects: true, + detectionDepth: 3, + }); + t.ok(loadPlugin.withArgs('rubygems').calledOnce, 'calls rubygems plugin'); + t.ok(loadPlugin.withArgs('gradle').calledOnce, 'calls gradle plugin'); + + params.server.popRequests(2).forEach((req) => { + 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, '/api/v1/test-dep-graph', 'posts to correct url'); + t.ok(req.body.depGraph, 'body contains depGraph'); + t.match( + req.body.depGraph.pkgManager.name, + /(gradle|rubygems)/, + 'depGraph has package manager', + ); + }); + t.match( + result.getDisplayResults(), + 'Tested 3 projects', + 'Detected 3 projects', + ); + t.match( + result.getDisplayResults(), + 'Package manager: rubygems', + 'contains package manager rubygems', + ); + t.match( + result.getDisplayResults(), + 'Package manager: gradle', + 'contains package manager gradle', + ); + t.match( + result.getDisplayResults(), + 'Target file: Gemfile.lock', + 'contains target file Gemfile.lock', + ); + t.match( + result.getDisplayResults(), + 'Target file: build.gradle.kts', + 'contains target file build.gradle.kts', + ); + }, '`test yarn-out-of-sync` out of sync fails': (params, utils) => async ( t, ) => { diff --git a/test/acceptance/workspaces/kotlin-monorepo/Gemfile b/test/acceptance/workspaces/kotlin-monorepo/Gemfile new file mode 100644 index 00000000000..e0f77a325fc --- /dev/null +++ b/test/acceptance/workspaces/kotlin-monorepo/Gemfile @@ -0,0 +1,4 @@ +source :rubygems + +gem "json" +gem "lynx", "0.4.0" diff --git a/test/acceptance/workspaces/kotlin-monorepo/Gemfile.lock b/test/acceptance/workspaces/kotlin-monorepo/Gemfile.lock new file mode 100644 index 00000000000..1664b0b0b57 --- /dev/null +++ b/test/acceptance/workspaces/kotlin-monorepo/Gemfile.lock @@ -0,0 +1,15 @@ +GEM + remote: http://rubygems.org/ + specs: + json (2.0.2) + lynx (0.4.0) + +PLATFORMS + ruby + +DEPENDENCIES + json + lynx (= 0.4.0) + +BUNDLED WITH + 1.13.5 diff --git a/test/acceptance/workspaces/kotlin-monorepo/build.gradle.kts b/test/acceptance/workspaces/kotlin-monorepo/build.gradle.kts new file mode 100644 index 00000000000..17189e77e6e --- /dev/null +++ b/test/acceptance/workspaces/kotlin-monorepo/build.gradle.kts @@ -0,0 +1 @@ +// I will be mocked diff --git a/test/acceptance/workspaces/kotlin-monorepo/subproj/build.gradle.kts b/test/acceptance/workspaces/kotlin-monorepo/subproj/build.gradle.kts new file mode 100644 index 00000000000..17189e77e6e --- /dev/null +++ b/test/acceptance/workspaces/kotlin-monorepo/subproj/build.gradle.kts @@ -0,0 +1 @@ +// I will be mocked