Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ccca1d

Browse files
committed
tests: more tests
1 parent e5aabaa commit 5ccca1d

13 files changed

Lines changed: 394 additions & 14 deletions

resources/.redocly.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ lint:
1010
no-invalid-media-type-examples: error
1111
operation-description: off
1212
path-http-verbs-order: error
13+
boolean-parameter-prefixes: error
1314
transformers:
1415
local/duplicate-description: off

resources/petstore-with-errors.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ paths:
4646
schema:
4747
type: integer
4848
format: int32
49+
- name: test
50+
in: path
51+
description: How many items to return at one time (max 100)
52+
required: false
53+
schema:
54+
type: boolean
4955
responses:
5056
'200':
5157
description: A paged array of pets

src/__tests__/normalizeVisitors.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Normalize visitors', () => {
2121
Object.keys(ruleset).map((ruleId) => ({
2222
ruleId,
2323
severity: 'error' as 'error',
24-
visitor: ruleset[ruleId](),
24+
visitor: ruleset[ruleId]({}),
2525
})),
2626
);
2727

@@ -65,7 +65,7 @@ describe('Normalize visitors', () => {
6565
Object.keys(ruleset).map((ruleId) => ({
6666
ruleId,
6767
severity: 'error' as 'error',
68-
visitor: ruleset[ruleId](),
68+
visitor: ruleset[ruleId]({}),
6969
})),
7070
);
7171

@@ -103,7 +103,7 @@ describe('Normalize visitors', () => {
103103
Object.keys(ruleset).map((ruleId) => ({
104104
ruleId,
105105
severity: 'error' as 'error',
106-
visitor: ruleset[ruleId](),
106+
visitor: ruleset[ruleId]({}),
107107
})),
108108
);
109109

@@ -138,7 +138,7 @@ describe('Normalize visitors', () => {
138138
Object.keys(ruleset).map((ruleId) => ({
139139
ruleId,
140140
severity: 'error' as 'error',
141-
visitor: ruleset[ruleId](),
141+
visitor: ruleset[ruleId]({}),
142142
})),
143143
);
144144

src/__tests__/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ export function parseYamlToDocument(body: string, absoluteRef: string = ''): Doc
1414
}
1515

1616
export function replaceSourceWithRef(results: NormalizedReportMessage[], cwd?: string) {
17+
const cwdRegexp = cwd ? new RegExp(cwd + path.sep, 'g') : /$^/;
1718
return results.map((r) => {
1819
const mapped = {
1920
...r,
21+
message: r.message.replace(cwdRegexp, ''),
2022
location: r.location.map((l) => ({
2123
...l,
2224
source: cwd ? path.relative(cwd, l.source.absoluteRef) : l.source.absoluteRef,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: '
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test: 1
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
import { parseYamlToDocument, replaceSourceWithRef } from '../../__tests__/utils';
2+
import { outdent } from 'outdent';
3+
import { validateDocument } from '../../validate';
4+
import { LintConfig } from '../..';
5+
import path = require('path');
6+
7+
describe('oas3 boolean-parameter-prefixes', () => {
8+
it('should report on unresolved $ref', async () => {
9+
const document = parseYamlToDocument(
10+
outdent`
11+
openapi: 3.0.0
12+
paths:
13+
'/test':
14+
put:
15+
requestBody:
16+
$ref: 'invalid.yaml'
17+
`,
18+
path.join(__dirname, 'foobar.yaml'),
19+
);
20+
21+
const results = await validateDocument({
22+
document,
23+
config: new LintConfig({
24+
extends: [],
25+
rules: {
26+
'no-unresolved-refs': 'error',
27+
},
28+
}),
29+
});
30+
31+
expect(replaceSourceWithRef(results, __dirname)).toMatchInlineSnapshot(`
32+
Array [
33+
Object {
34+
"location": Array [
35+
Object {
36+
"pointer": "#/paths/~1test/put/requestBody",
37+
"reportOnKey": false,
38+
"source": "foobar.yaml",
39+
},
40+
],
41+
"message": "Can't resolve $ref: ENOENT: no such file or directory, open 'invalid.yaml'",
42+
"ruleId": "no-unresolved-refs",
43+
"severity": "error",
44+
"suggest": Array [],
45+
},
46+
]
47+
`);
48+
});
49+
50+
it('should report on unresolved $ref yaml error', async () => {
51+
const document = parseYamlToDocument(
52+
outdent`
53+
openapi: 3.0.0
54+
paths:
55+
'/test':
56+
put:
57+
requestBody:
58+
$ref: 'fixtures/invalid-yaml.yaml'
59+
`,
60+
path.join(__dirname, 'foobar.yaml'),
61+
);
62+
63+
const results = await validateDocument({
64+
document,
65+
config: new LintConfig({
66+
extends: [],
67+
rules: {
68+
'no-unresolved-refs': 'error',
69+
},
70+
}),
71+
});
72+
73+
expect(replaceSourceWithRef(results, __dirname)).toMatchInlineSnapshot(`
74+
Array [
75+
Object {
76+
"location": Array [
77+
Object {
78+
"pointer": undefined,
79+
"reportOnKey": false,
80+
"source": "fixtures/invalid-yaml.yaml",
81+
"start": Object {
82+
"col": 1,
83+
"line": 2,
84+
},
85+
},
86+
],
87+
"message": "Failed to parse: unexpected end of the stream within a single quoted scalar in \\"fixtures/invalid-yaml.yaml\\" at line 2, column 1:",
88+
"ruleId": "no-unresolved-refs",
89+
"severity": "error",
90+
"suggest": Array [],
91+
},
92+
Object {
93+
"location": Array [
94+
Object {
95+
"pointer": "#/paths/~1test/put/requestBody",
96+
"reportOnKey": false,
97+
"source": "foobar.yaml",
98+
},
99+
],
100+
"message": "Can't resolve $ref: unexpected end of the stream within a single quoted scalar in \\"fixtures/invalid-yaml.yaml\\" at line 2, column 1:",
101+
"ruleId": "no-unresolved-refs",
102+
"severity": "error",
103+
"suggest": Array [],
104+
},
105+
]
106+
`);
107+
});
108+
109+
it('should report on unresolved $ref yaml error', async () => {
110+
const document = parseYamlToDocument(
111+
outdent`
112+
openapi: 3.0.0
113+
paths:
114+
'/test':
115+
put:
116+
requestBody:
117+
$ref: 'fixtures/ref.yaml'
118+
`,
119+
path.join(__dirname, 'foobar.yaml'),
120+
);
121+
122+
const results = await validateDocument({
123+
document,
124+
config: new LintConfig({
125+
extends: [],
126+
rules: {
127+
'no-unresolved-refs': 'error',
128+
},
129+
}),
130+
});
131+
132+
expect(replaceSourceWithRef(results, __dirname)).toMatchInlineSnapshot(`Array []`);
133+
});
134+
135+
it('should report on unresolved localr ref', async () => {
136+
const document = parseYamlToDocument(
137+
outdent`
138+
openapi: 3.0.0
139+
paths:
140+
'/test':
141+
put:
142+
requestBody:
143+
$ref: '#/components/requestBodies/a'
144+
`,
145+
path.join(__dirname, 'foobar.yaml'),
146+
);
147+
148+
const results = await validateDocument({
149+
document,
150+
config: new LintConfig({
151+
extends: [],
152+
rules: {
153+
'no-unresolved-refs': 'error',
154+
},
155+
}),
156+
});
157+
158+
expect(replaceSourceWithRef(results, __dirname)).toMatchInlineSnapshot(`
159+
Array [
160+
Object {
161+
"location": Array [
162+
Object {
163+
"pointer": "#/paths/~1test/put/requestBody",
164+
"reportOnKey": false,
165+
"source": "foobar.yaml",
166+
},
167+
],
168+
"message": "Can't resolve $ref",
169+
"ruleId": "no-unresolved-refs",
170+
"severity": "error",
171+
"suggest": Array [],
172+
},
173+
]
174+
`);
175+
});
176+
});

src/rules/no-unresolved-refs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ export function reportUnresolvedRef(resolved: ResolveResult<any>, report: (m: Re
2727
});
2828
}
2929

30+
const message = resolved.error?.message;
31+
3032
report({
31-
message: `Can't resolve $ref: ${resolved.error?.message}`,
33+
message: `Can't resolve $ref${message ? ': ' + message : ''}`,
3234
});
3335
}
3436

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { outdent } from 'outdent';
2+
3+
import { parseYamlToDocument, replaceSourceWithRef } from '../../../__tests__/utils';
4+
import { validateDocument } from '../../../validate';
5+
import { LintConfig } from '../../..';
6+
7+
describe('oas3 boolean-parameter-prefixes', () => {
8+
it('should report on boolean param without prefix', async () => {
9+
const document = parseYamlToDocument(
10+
outdent`
11+
openapi: 3.0.0
12+
paths:
13+
'/test':
14+
parameters:
15+
- name: a
16+
in: path
17+
schema:
18+
type: boolean
19+
`,
20+
'foobar.yaml',
21+
);
22+
23+
const results = await validateDocument({
24+
document,
25+
config: new LintConfig({ extends: [], rules: { 'boolean-parameter-prefixes': 'error' } }),
26+
});
27+
28+
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
29+
Array [
30+
Object {
31+
"location": Array [
32+
Object {
33+
"pointer": "#/paths/~1test/parameters/0/name",
34+
"reportOnKey": false,
35+
"source": "foobar.yaml",
36+
},
37+
],
38+
"message": "Boolean parameter \`a\` should have \`is\` or \`has\` prefix",
39+
"ruleId": "boolean-parameter-prefixes",
40+
"severity": "error",
41+
"suggest": Array [],
42+
},
43+
]
44+
`);
45+
});
46+
47+
it('should not report on boolean param with prefix', async () => {
48+
const document = parseYamlToDocument(
49+
outdent`
50+
openapi: 3.0.0
51+
paths:
52+
'/test':
53+
parameters:
54+
- name: hasA
55+
in: path
56+
schema:
57+
type: boolean
58+
- name: isA
59+
in: path
60+
schema:
61+
type: boolean
62+
- name: has_a
63+
in: path
64+
schema:
65+
type: boolean
66+
- name: is-a
67+
in: path
68+
schema:
69+
type: boolean
70+
`,
71+
'foobar.yaml',
72+
);
73+
74+
const results = await validateDocument({
75+
document,
76+
config: new LintConfig({ extends: [], rules: { 'boolean-parameter-prefixes': 'error' } }),
77+
});
78+
79+
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
80+
});
81+
82+
it('should not report on boolean param with custom prefix', async () => {
83+
const document = parseYamlToDocument(
84+
outdent`
85+
openapi: 3.0.0
86+
paths:
87+
'/test':
88+
parameters:
89+
- name: should-a
90+
in: query
91+
schema:
92+
type: boolean
93+
`,
94+
'foobar.yaml',
95+
);
96+
97+
const results = await validateDocument({
98+
document,
99+
config: new LintConfig({
100+
extends: [],
101+
rules: {
102+
'boolean-parameter-prefixes': {
103+
severity: 'error',
104+
prefixes: ['should'],
105+
},
106+
},
107+
}),
108+
});
109+
110+
expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`Array []`);
111+
});
112+
});

0 commit comments

Comments
 (0)