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 #1325 from snyk/feat/add-snyk-cpp-plugin
feat: add snyk-cpp-plugin
- Loading branch information
Showing
12 changed files
with
223 additions
and
0 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
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,62 @@ | ||
import * as cppPlugin from 'snyk-cpp-plugin'; | ||
import { Options } from './types'; | ||
import { TestCommandResult } from '../cli/commands/types'; | ||
|
||
interface Artifact { | ||
type: string; | ||
data: any; | ||
meta?: { [key: string]: any }; | ||
} | ||
|
||
interface ScanResult { | ||
artifacts: Artifact[]; | ||
} | ||
|
||
export interface EcosystemPlugin { | ||
scan: (options: Options) => Promise<ScanResult[]>; | ||
display: (scanResults: ScanResult[]) => Promise<string>; | ||
} | ||
|
||
export type Ecosystem = 'cpp'; | ||
|
||
const EcosystemPlugins: { | ||
readonly [ecosystem in Ecosystem]: EcosystemPlugin; | ||
} = { | ||
cpp: cppPlugin, | ||
}; | ||
|
||
export function getPlugin(ecosystem: Ecosystem): EcosystemPlugin { | ||
return EcosystemPlugins[ecosystem]; | ||
} | ||
|
||
export function getEcosystem(options: Options): Ecosystem | null { | ||
if (options.source) { | ||
return 'cpp'; | ||
} | ||
return null; | ||
} | ||
|
||
export async function testEcosystem( | ||
ecosystem: Ecosystem, | ||
paths: string[], | ||
options: Options, | ||
): Promise<TestCommandResult> { | ||
const plugin = getPlugin(ecosystem); | ||
let allScanResults: ScanResult[] = []; | ||
for (const path of paths) { | ||
options.path = path; | ||
const scanResults = await plugin.scan(options); | ||
allScanResults = allScanResults.concat(scanResults); | ||
} | ||
|
||
const stringifiedData = JSON.stringify(allScanResults, null, 2); | ||
if (options.json) { | ||
return TestCommandResult.createJsonTestCommandResult(stringifiedData); | ||
} | ||
|
||
const readableResult = await plugin.display(allScanResults); | ||
return TestCommandResult.createHumanReadableTestCommandResult( | ||
readableResult, | ||
stringifiedData, | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Options } from '../src/lib/types'; | ||
import * as cppPlugin from 'snyk-cpp-plugin'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import { getPlugin, getEcosystem, testEcosystem } from '../src/lib/ecosystems'; | ||
import { TestCommandResult } from '../src/cli/commands/types'; | ||
|
||
describe('ecosystems', () => { | ||
describe('getPlugin', () => { | ||
it('should return c++ plugin when cpp ecosystem is given', () => { | ||
const actual = getPlugin('cpp'); | ||
const expected = cppPlugin; | ||
expect(actual).toBe(expected); | ||
}); | ||
|
||
it('should return undefined when ecosystem is not supported', () => { | ||
const actual = getPlugin('unsupportedEcosystem' as any); | ||
const expected = undefined; | ||
expect(actual).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('getEcosystem', () => { | ||
it('should return c++ ecosystem when options source is true', () => { | ||
const options: Options = { | ||
source: true, | ||
path: '', | ||
}; | ||
const actual = getEcosystem(options); | ||
const expected = 'cpp'; | ||
expect(actual).toBe(expected); | ||
}); | ||
|
||
it('should return null when options source is false', () => { | ||
const options: Options = { | ||
source: false, | ||
path: '', | ||
}; | ||
const actual = getEcosystem(options); | ||
const expected = null; | ||
expect(actual).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe('testEcosystem', () => { | ||
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'); | ||
} | ||
|
||
beforeAll(() => { | ||
process.chdir(fixturePath); | ||
}); | ||
afterAll(() => { | ||
process.chdir(cwd); | ||
}); | ||
|
||
it('should return human readable result when no json option given', async () => { | ||
const display = readFixture('display.txt'); | ||
const scan = readFixture('scan.json'); | ||
const stringifiedData = JSON.stringify(JSON.parse(scan), null, 2); | ||
const expected = TestCommandResult.createHumanReadableTestCommandResult( | ||
display, | ||
stringifiedData, | ||
); | ||
const actual = await testEcosystem('cpp', ['.'], { path: '' }); | ||
expect(actual).toEqual(expected); | ||
}); | ||
|
||
it('should return json result when json option', async () => { | ||
const scan = readFixture('scan.json'); | ||
const stringifiedData = JSON.stringify(JSON.parse(scan), null, 2); | ||
const expected = TestCommandResult.createJsonTestCommandResult( | ||
stringifiedData, | ||
); | ||
const actual = await testEcosystem('cpp', ['.'], { | ||
path: '', | ||
json: true, | ||
}); | ||
expect(actual).toEqual(expected); | ||
}); | ||
}); | ||
}); |
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,4 @@ | ||
int add(int x, int y) | ||
{ | ||
return x + y; | ||
} |
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 @@ | ||
int add(int x, int y); |
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,3 @@ | ||
52d1b046047db9ea0c581cafd4c68fe5 add.cpp | ||
aeca71a6e39f99a24ecf4c088eee9cb8 add.h | ||
ad3365b3370ef6b1c3e778f875055f19 main.cpp |
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,7 @@ | ||
#include <iostream> | ||
#include "add.h" | ||
|
||
int main() { | ||
std::cout << "The sum of 3 and 4 is " << add(3, 4) << '\n'; | ||
return 0; | ||
} |
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,23 @@ | ||
[ | ||
{ | ||
"artifacts": [ | ||
{ | ||
"type": "cpp-fingerprints", | ||
"data": [ | ||
{ | ||
"filePath": "add.cpp", | ||
"hash": "52d1b046047db9ea0c581cafd4c68fe5" | ||
}, | ||
{ | ||
"filePath": "add.h", | ||
"hash": "aeca71a6e39f99a24ecf4c088eee9cb8" | ||
}, | ||
{ | ||
"filePath": "main.cpp", | ||
"hash": "ad3365b3370ef6b1c3e778f875055f19" | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
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