/
ecosystems.spec.ts
138 lines (124 loc) · 4.32 KB
/
ecosystems.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import * as fs from 'fs';
import * as path from 'path';
import * as cppPlugin from 'snyk-cpp-plugin';
import * as ecosystems from '../src/lib/ecosystems';
import * as request from '../src/lib/request/promise';
import { Options } from '../src/lib/types';
import { TestCommandResult } from '../src/cli/commands/types';
describe('ecosystems', () => {
describe('getPlugin', () => {
it('should return cpp plugin when cpp ecosystem is given', () => {
const actual = ecosystems.getPlugin('cpp');
const expected = cppPlugin;
expect(actual).toBe(expected);
});
it('should return undefined when ecosystem is not supported', () => {
const actual = ecosystems.getPlugin('unsupportedEcosystem' as any);
const expected = undefined;
expect(actual).toBe(expected);
});
});
describe('getEcosystem', () => {
it('should return cpp ecosystem when options source is true', () => {
const options: Options = {
source: true,
path: '',
};
const actual = ecosystems.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 = ecosystems.getEcosystem(options);
const expected = null;
expect(actual).toBe(expected);
});
});
describe('testEcosystem', () => {
describe('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);
}
const displayTxt = readFixture('display.txt');
const errorTxt = readFixture('error.txt');
const testResult = readJsonFixture(
'testResults.json',
) as ecosystems.TestResult;
const stringifyTestResults = JSON.stringify([testResult], null, 2);
beforeAll(() => {
process.chdir(fixturePath);
});
afterEach(() => {
jest.resetAllMocks();
});
afterAll(() => {
process.chdir(cwd);
});
it('should return human readable result when no json option given', async () => {
const mock = jest
.spyOn(request, 'makeRequest')
.mockResolvedValue(testResult);
const expected = TestCommandResult.createHumanReadableTestCommandResult(
displayTxt,
stringifyTestResults,
);
const actual = await ecosystems.testEcosystem('cpp', ['.'], {
path: '',
});
expect(mock).toHaveBeenCalled();
expect(actual).toEqual(expected);
});
it('should return json result when json option', async () => {
const mock = jest
.spyOn(request, 'makeRequest')
.mockResolvedValue(testResult);
const expected = TestCommandResult.createJsonTestCommandResult(
stringifyTestResults,
);
const actual = await ecosystems.testEcosystem('cpp', ['.'], {
path: '',
json: true,
});
expect(mock).toHaveBeenCalled();
expect(actual).toEqual(expected);
});
it('should throw error when response code is not 200', async () => {
const error = { code: 401, message: 'Invalid auth token' };
jest.spyOn(request, 'makeRequest').mockRejectedValue(error);
const expected = new Error(error.message);
expect.assertions(1);
try {
await ecosystems.testEcosystem('cpp', ['.'], {
path: '',
});
} catch (error) {
expect(error).toEqual(expected);
}
});
it('should return error when there was a problem testing dependencies', async () => {
jest
.spyOn(request, 'makeRequest')
.mockRejectedValue('Something went wrong');
const expected = TestCommandResult.createHumanReadableTestCommandResult(
errorTxt,
'[]',
);
const actual = await ecosystems.testEcosystem('cpp', ['.'], {
path: '',
});
expect(actual).toEqual(expected);
});
});
});
});