Skip to content

Commit

Permalink
feat: monitor cpp projects
Browse files Browse the repository at this point in the history
  • Loading branch information
gitphill authored and anthogez committed Nov 13, 2020
1 parent 80b5a4d commit 3a91100
Show file tree
Hide file tree
Showing 10 changed files with 537 additions and 200 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"proxy-from-env": "^1.0.0",
"semver": "^6.0.0",
"snyk-config": "4.0.0-rc.2",
"snyk-cpp-plugin": "2.0.0",
"snyk-cpp-plugin": "2.1.0",
"snyk-docker-plugin": "4.7.2",
"snyk-go-plugin": "1.16.2",
"snyk-gradle-plugin": "3.10.2",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ModeData {

const modes: Record<string, ModeData> = {
source: {
allowedCommands: ['test'],
allowedCommands: ['test', 'monitor'],
config: (args): [] => {
args['source'] = true;
return args;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ecosystems/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Ecosystem, EcosystemPlugin } from './types';
const EcosystemPlugins: {
readonly [ecosystem in Ecosystem]: EcosystemPlugin;
} = {
cpp: cppPlugin,
cpp: cppPlugin as EcosystemPlugin,
// TODO: not any
docker: dockerPlugin as any,
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/plugins/get-extra-project-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function getExtraProjectCount(
options: Options,
inspectResult: pluginApi.InspectResult,
): Promise<number | undefined> {
if (options.docker) {
if (options.docker || options.source) {
return undefined;
}
if (
Expand Down
73 changes: 73 additions & 0 deletions test/all-projects-from-plugins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Options } from '../src/lib/types';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import { getExtraProjectCount } from '../src/lib/plugins/get-extra-project-count';

describe('Detect extra projects available that could be tested using --all-projects', () => {
it('should return `undefined` when exists a single project', async () => {
const root = '';
const inspectResult = {
plugin: { meta: { allSubProjectNames: ['gradle-woof'] } },
} as pluginApi.InspectResult;

const options = {} as Options;
const actualResult = await getExtraProjectCount(
root,
options,
inspectResult,
);
const expectedResult = undefined;
expect(actualResult).toBe(expectedResult);
});

it('should return `extra-project-count = 2` when exists more than a single project', async () => {
const root = '';
const inspectResult = {
plugin: { meta: { allSubProjectNames: ['gradle-woof', 'npm-webapp'] } },
} as pluginApi.InspectResult;

const options = {} as Options;
const actualResult = await getExtraProjectCount(
root,
options,
inspectResult,
);
const expectedResult = 2;
expect(actualResult).toBe(expectedResult);
});

it('should return `undefined` when `source` command for cpp is being used', async () => {
const root = '';
const inspectResult = {
plugin: { meta: { allSubProjectNames: ['gradle-woof', 'npm-webapp'] } },
} as pluginApi.InspectResult;

const options = { source: true } as Options;
const actualResult = await getExtraProjectCount(
root,
options,
inspectResult,
);
const expectedResult = undefined;
expect(actualResult).toBe(expectedResult);
});

it('should return `undefined` when `docker` command is being used', async () => {
const root = '';
const inspectResult = {
plugin: {
meta: {
allSubProjectNames: ['gradle-goof', 'npm-node', ['yarn-yarn']],
},
},
} as pluginApi.InspectResult;

const options = { source: true } as Options;
const actualResult = await getExtraProjectCount(
root,
options,
inspectResult,
);
const expectedResult = undefined;
expect(actualResult).toBe(expectedResult);
});
});
144 changes: 144 additions & 0 deletions test/ecosystems-monitor-cpp.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import * as fs from 'fs';
import * as path from 'path';
import * as config from '../src/lib/config';
import * as request from '../src/lib/request/promise';

import { Options } from '../src/lib/types';
import * as ecosystems from '../src/lib/ecosystems';
import * as ecosystemsTypes from '../src/lib/ecosystems/types';
import { getFormattedMonitorOutput } from '../src/lib/ecosystems/monitor';
import { GoodResult, BadResult } from '../src/cli/commands/monitor/types';

describe('monitorEcosystem cpp', () => {
const fixturePath = path.join(__dirname, 'fixtures', 'cpp-project');
const cwd = process.cwd();

function readFixture(filename: string) {
const filePath = path.join(fixturePath, filename);
return fs.readFileSync(filePath, 'utf-8');
}

function readJsonFixture(filename: string) {
const contents = readFixture(filename);
return JSON.parse(contents);
}

beforeAll(() => {
process.chdir(fixturePath);
});

afterEach(() => {
jest.resetAllMocks();
});

afterAll(() => {
process.chdir(cwd);
});

it('should return successful monitorResults from monitorEcosystem', async () => {
const monitorDependenciesResponse = readJsonFixture(
'monitor-dependencies-response.json',
) as ecosystemsTypes.MonitorDependenciesResponse;

const makeRequestSpy = jest
.spyOn(request, 'makeRequest')
.mockResolvedValue(monitorDependenciesResponse);

const results: Array<GoodResult | BadResult> = [];

const [monitorResults, monitorErrors] = await ecosystems.monitorEcosystem(
'cpp',
['.'],
{
path: '',
},
);

const actualFormattedMonitorOutput = await getFormattedMonitorOutput(
results,
monitorResults,
monitorErrors,
{
source: true,
} as Options,
);

expect(makeRequestSpy.mock.calls[0][0]).toEqual({
method: 'PUT',
url: expect.stringContaining('/monitor-dependencies'),
json: true,
headers: {
'x-is-ci': expect.any(Boolean),
authorization: expect.stringContaining('token'),
},
body: {
scanResult: {
facts: [
{
type: 'cpp-fingerprints',
data: [
{
filePath: 'add.cpp',
hash: '52d1b046047db9ea0c581cafd4c68fe5',
},
{ filePath: 'add.h', hash: 'aeca71a6e39f99a24ecf4c088eee9cb8' },
{
filePath: 'main.cpp',
hash: 'ad3365b3370ef6b1c3e778f875055f19',
},
],
},
],
identity: { type: 'cpp' },
name: expect.any(String),
target: {
branch: expect.any(String),
remoteUrl: expect.any(String),
},
},
method: 'cli',
},
qs: {},
});

expect(actualFormattedMonitorOutput).toContain('Explore this snapshot');
});

it('should throw error when response code is not 200', async () => {
const error = {
code: 401,
message: `Authentication failed. Please check the API token on ${config.ROOT}`,
};
jest.spyOn(request, 'makeRequest').mockRejectedValue(error);
const expected = new Error(error.message);
expect.assertions(1);
try {
await ecosystems.monitorEcosystem('cpp', ['.'], {
path: '',
});
} catch (error) {
expect(error).toEqual(expected);
}
});

it('should return error when there was a problem monitoring dependencies', async () => {
jest
.spyOn(request, 'makeRequest')
.mockRejectedValue('Something went wrong');
const expectedErrorMessage = 'Could not monitor dependencies';
const [monitorResults, monitorErrors] = await ecosystems.monitorEcosystem(
'cpp',
['.'],
{
path: '',
},
);

const hasExpectedErrorMessage = monitorErrors[0].error.includes(
expectedErrorMessage,
);

expect(monitorResults.length).toBe(0);
expect(hasExpectedErrorMessage).toBeTruthy();
});
});

0 comments on commit 3a91100

Please sign in to comment.