1
1
/* eslint-disable require-jsdoc */
2
2
import { expect } from 'chai' ;
3
3
4
- import type { IRuleResult } from '@stoplight/spectral-core' ;
4
+ import { IRuleResult , Ruleset } from '@stoplight/spectral-core' ;
5
+ import { truthy , falsy } from '@stoplight/spectral-functions' ;
5
6
import { DiagnosticSeverity as SpectralDiagnosticSeverity } from '@stoplight/types' ;
6
- import { makeDiagnostic , makePublishDiagnosticsParams } from '../../src/util' ;
7
+ import { getRuleDocumentationUrl , makeDiagnostic , makePublishDiagnosticsParams } from '../../src/util' ;
7
8
import { DiagnosticSeverity as VSCodeDiagnosticSeverity } from 'vscode-languageserver' ;
8
9
9
10
function createResult ( source ?: string ) : IRuleResult {
@@ -28,10 +29,33 @@ function createResult(source?: string): IRuleResult {
28
29
} ;
29
30
}
30
31
32
+ function createRuleset ( rulesetDoc : string | undefined ) : Ruleset {
33
+ return new Ruleset ( {
34
+ documentationUrl : rulesetDoc ,
35
+ rules : {
36
+ 'with-direct-doc' : {
37
+ documentationUrl : 'https://example.com/direct' ,
38
+ given : '$' ,
39
+ severity : 'error' ,
40
+ then : {
41
+ function : truthy ,
42
+ } ,
43
+ } ,
44
+ 'without-direct-doc' : {
45
+ given : '$' ,
46
+ severity : 'error' ,
47
+ then : {
48
+ function : falsy ,
49
+ } ,
50
+ } ,
51
+ } ,
52
+ } ) ;
53
+ }
54
+
31
55
describe ( 'makeDiagnostic' , ( ) => {
32
56
it ( 'sets the source to spectral' , ( ) => {
33
57
const result = createResult ( ) ;
34
- const actual = makeDiagnostic ( result ) ;
58
+ const actual = makeDiagnostic ( result , undefined ) ;
35
59
expect ( actual . source ) . to . eql ( 'spectral' ) ;
36
60
} ) ;
37
61
@@ -47,13 +71,12 @@ describe('makeDiagnostic', () => {
47
71
const result = createResult ( ) ;
48
72
result . severity = input ;
49
73
50
- const actual = makeDiagnostic ( result ) ;
74
+ const actual = makeDiagnostic ( result , undefined ) ;
51
75
expect ( actual . severity ) . to . eql ( expected ) ;
52
76
} ) ;
53
77
} ) ;
54
78
} ) ;
55
79
56
-
57
80
describe ( 'makePublishDiagnosticsParams' , ( ) => {
58
81
const sources : string [ ] = [
59
82
'file:///c%3A/folder/test.txt' ,
@@ -63,7 +86,7 @@ describe('makePublishDiagnosticsParams', () => {
63
86
describe ( 'returns an empty array of diagnostics for the root file being analyzed even when it has no issues' , ( ) => {
64
87
sources . forEach ( ( sourceUri ) => {
65
88
it ( sourceUri , ( ) => {
66
- const actual = makePublishDiagnosticsParams ( sourceUri , [ ] , [ ] ) ;
89
+ const actual = makePublishDiagnosticsParams ( sourceUri , [ ] , [ ] , undefined ) ;
67
90
68
91
expect ( actual ) . to . have . length ( 1 ) ;
69
92
expect ( actual [ 0 ] . uri ) . to . eql ( sourceUri ) ;
@@ -76,7 +99,7 @@ describe('makePublishDiagnosticsParams', () => {
76
99
sources . forEach ( ( sourceUri ) => {
77
100
it ( sourceUri , ( ) => {
78
101
const fakeRoot = 'file:///different/root' ;
79
- const actual = makePublishDiagnosticsParams ( fakeRoot , [ sourceUri ] , [ ] ) ;
102
+ const actual = makePublishDiagnosticsParams ( fakeRoot , [ sourceUri ] , [ ] , undefined ) ;
80
103
81
104
expect ( actual ) . to . have . length ( 2 ) ;
82
105
@@ -118,7 +141,7 @@ describe('makePublishDiagnosticsParams', () => {
118
141
createResult ( 'four' ) ,
119
142
] ;
120
143
121
- const actual = makePublishDiagnosticsParams ( 'file:///one' , [ ] , problems ) ;
144
+ const actual = makePublishDiagnosticsParams ( 'file:///one' , [ ] , problems , undefined ) ;
122
145
123
146
expect ( actual ) . to . have . length ( 5 ) ;
124
147
@@ -149,4 +172,27 @@ describe('makePublishDiagnosticsParams', () => {
149
172
}
150
173
} ) ;
151
174
} ) ;
175
+
176
+ describe ( 'getRuleDocumentationUrl' , ( ) => {
177
+ it ( 'uses the rule\'s documentation if it exists' , ( ) => {
178
+ const ruleset = createRuleset ( undefined ) ;
179
+ const documentationUrl = getRuleDocumentationUrl ( ruleset , 'with-direct-doc' ) ;
180
+ expect ( documentationUrl ) . to . eql ( 'https://example.com/direct' ) ;
181
+ } ) ;
182
+ it ( 'uses the rule\'s documentation if it exists, even if the ruleset has its own' , ( ) => {
183
+ const ruleset = createRuleset ( 'https://example.com' ) ;
184
+ const documentationUrl = getRuleDocumentationUrl ( ruleset , 'with-direct-doc' ) ;
185
+ expect ( documentationUrl ) . to . eql ( 'https://example.com/direct' ) ;
186
+ } ) ;
187
+ it ( 'uses the ruleset\'s documentation and #code if the rule has no direct doc and the ruleset has one' , ( ) => {
188
+ const ruleset = createRuleset ( 'https://example.com' ) ;
189
+ const documentationUrl = getRuleDocumentationUrl ( ruleset , 'without-direct-doc' ) ;
190
+ expect ( documentationUrl ) . to . eql ( 'https://example.com#without-direct-doc' ) ;
191
+ } ) ;
192
+ it ( 'returns undefined if neither the rule or ruleset has a documentation' , ( ) => {
193
+ const ruleset = createRuleset ( undefined ) ;
194
+ const documentationUrl = getRuleDocumentationUrl ( ruleset , 'without-direct-doc' ) ;
195
+ expect ( documentationUrl ) . to . be . undefined ;
196
+ } ) ;
197
+ } ) ;
152
198
} ) ;
0 commit comments