-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathrun-validator.test.js
More file actions
105 lines (91 loc) · 3.75 KB
/
Copy pathrun-validator.test.js
File metadata and controls
105 lines (91 loc) · 3.75 KB
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
/**
* Copyright 2017 - 2023 IBM Corporation.
* SPDX-License-Identifier: Apache2.0
*/
const { getCapturedText, testValidator } = require('../../test-utils');
describe('run-validator tests', function () {
let consoleSpy;
const originalWarn = console.warn;
const originalError = console.error;
const originalInfo = console.info;
beforeEach(() => {
consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => {});
console.warn = console.log;
console.error = console.log;
console.info = console.log;
});
afterEach(() => {
consoleSpy.mockRestore();
console.warn = originalWarn;
console.error = originalError;
console.info = originalInfo;
});
it('should show error/exit code 1 when warnings limit exceeded (config)', async function () {
const exitCode = await testValidator([
'--config',
'./test/cli-validator/mock-files/config/five-warnings.json',
'./test/cli-validator/mock-files/oas3/warn-threshold.yml',
]);
const capturedText = getCapturedText(consoleSpy.mock.calls);
// originalError(`Captured text: ${JSON.stringify(capturedText, null, 2)}`);
expect(exitCode).toEqual(1);
expect(capturedText[3]).toMatch(
/^\[ERROR\] Number of warnings .* exceeds warnings limit/
);
});
it.each(['-w5', '--warnings-limit=5'])(
'should show error/exit code 1 when -w/--warnings-limit used',
async function (option) {
const exitCode = await testValidator([
option,
'./test/cli-validator/mock-files/oas3/warn-threshold.yml',
]);
const capturedText = getCapturedText(consoleSpy.mock.calls);
// originalError(`Captured text: ${JSON.stringify(capturedText, null, 2)}`);
expect(exitCode).toEqual(1);
expect(capturedText[3]).toMatch(
/^\[ERROR\] Number of warnings .* exceeds warnings limit/
);
}
);
it('should show errors/use default config when config file fails validation', async function () {
const exitCode = await testValidator([
'-c',
'./test/cli-validator/mock-files/config/invalid-values.json',
'./test/cli-validator/mock-files/oas3/clean.yml',
]);
// The config file is invalid, so we should end up using the default config instead.
const capturedText = getCapturedText(consoleSpy.mock.calls);
// originalError(`Captured text: ${JSON.stringify(capturedText, null, 2)}`);
expect(exitCode).toEqual(0);
const allOutput = capturedText.join('');
expect(allOutput).toMatch(/Invalid configuration file/);
expect(allOutput).toMatch(
/schema validation error: '\/errorsOnly': must be boolean/
);
expect(allOutput).toMatch(/validator will use a default config/);
});
it('should get exit code 0 when warnings limit not exceeded', async function () {
const exitCode = await testValidator([
'./test/cli-validator/mock-files/oas3/clean.yml',
'-c',
'./test/cli-validator/mock-files/config/zero-warnings.json',
]);
expect(exitCode).toEqual(0);
});
it('should print errors/use default config when config file is invalid JSON', async function () {
const exitCode = await testValidator([
'-c',
'./test/cli-validator/mock-files/config/invalid-json.json',
'./test/cli-validator/mock-files/oas3/clean.yml',
]);
// The config file is invalid, so we should end up using the default config instead.
const capturedText = getCapturedText(consoleSpy.mock.calls);
// originalError(`Captured text: ${JSON.stringify(capturedText, null, 2)}`);
expect(exitCode).toEqual(0);
const allOutput = capturedText.join('');
expect(allOutput).toMatch(/Unable to load config file/);
expect(allOutput).toMatch(/SyntaxError: Unexpected token/);
expect(allOutput).toMatch(/validator will use a default config/);
});
});