1
- import { resolve , sep } from "path" ;
1
+ import { resolve , sep , relative , dirname } from "path" ;
2
2
import { Diagnostic , Range } from "vscode-languageserver-types" ;
3
3
import { TextDocument } from "vscode-languageserver" ;
4
4
import { readJsonSync , readFileSync } from "fs-extra" ;
@@ -14,38 +14,76 @@ export const OUTPUT_LSP_RESPONSE_FILE_NAME = "output-lsp-response.json";
14
14
export async function snapshotTestLSPDiagnostic (
15
15
testDir : string
16
16
) : Promise < void > {
17
+ const pkgJsonPath = require . resolve (
18
+ "@ui5-language-assistant/language-server/package.json"
19
+ ) ;
20
+ const languageServerDir = dirname ( pkgJsonPath ) ;
21
+ // This is the project root directory
22
+ const rootDir = resolve ( languageServerDir , ".." , ".." ) ;
23
+
24
+ // Note: the original `input.xml` snippet acts as **both**:
25
+ // - The sample input.
26
+ // - A snapshot for marker ranges.
27
+ const originalXMLSnippet = readInputXMLSnippet ( testDir , false ) ;
17
28
const diagnosticsSnapshots = readSnapshotDiagnosticsLSPResponse ( testDir ) ;
29
+
30
+ // Check consistency of the ranges between the input xml and the snapshot response
31
+ // (for example, if one of them was changed manually)
32
+ const snapshotRanges = map ( diagnosticsSnapshots , ( _ ) => _ . range ) ;
33
+ const snapshotXMLWithMarkedRanges = computeXMLWithMarkedRanges (
34
+ testDir ,
35
+ snapshotRanges
36
+ ) ;
37
+
38
+ expect (
39
+ originalXMLSnippet ,
40
+ `The XML input snippet range markers don't match the snapshot response ranges. Did you forget to run "yarn update-snapshots"?
41
+ Input xml is at: ${ relative ( rootDir , getInputXMLSnippetPath ( testDir ) ) }
42
+ Snapshot response is at: ${ relative (
43
+ rootDir ,
44
+ getSnapshotDiagnosticsLSPResponsePath ( testDir )
45
+ ) } `
46
+ ) . to . equal ( snapshotXMLWithMarkedRanges ) ;
47
+
18
48
const newlyComputedDiagnostics = await computeNewDiagnosticLSPResponse (
19
49
testDir
20
50
) ;
21
51
expect (
22
52
newlyComputedDiagnostics ,
23
- "Snapshot Mismatch in LSP Diagnostics response"
53
+ `Snapshot Mismatch in LSP Diagnostics response.
54
+ Snapshot is at: ${ relative (
55
+ rootDir ,
56
+ getSnapshotDiagnosticsLSPResponsePath ( testDir )
57
+ ) } `
24
58
) . to . deep . equal ( diagnosticsSnapshots ) ;
25
59
26
60
const newlyCommutedRanges = map ( newlyComputedDiagnostics , ( _ ) => _ . range ) ;
27
61
const newlyComputedXMLWithMarkedRanges = computeXMLWithMarkedRanges (
28
62
testDir ,
29
63
newlyCommutedRanges
30
64
) ;
31
- // Note the original `input.xml` snippet acts as **both**:
32
- // - The sample input.
33
- // - A snapshot for marker ranges.
34
- const originalXMLSnippet = readInputXMLSnippet ( testDir , false ) ;
65
+
35
66
expect (
36
67
originalXMLSnippet ,
37
- "The XML input snippet range markers are incorrect"
68
+ `The XML input snippet range markers are incorrect.
69
+ Snapshot is at: ${ relative ( rootDir , getInputXMLSnippetPath ( testDir ) ) } `
38
70
) . to . equal ( newlyComputedXMLWithMarkedRanges ) ;
39
71
}
40
72
41
- export function readSnapshotDiagnosticsLSPResponse (
42
- testDir : string
43
- ) : Diagnostic [ ] {
44
- const sourcesTestDir = toSourcesTestDir ( testDir ) ;
45
- const lspResponsePath = resolve (
73
+ export function getSnapshotDiagnosticsLSPResponsePath (
74
+ sourcesTestDir : string
75
+ ) : string {
76
+ const snapshotResponsePath = resolve (
46
77
sourcesTestDir ,
47
78
OUTPUT_LSP_RESPONSE_FILE_NAME
48
79
) ;
80
+ return snapshotResponsePath ;
81
+ }
82
+
83
+ export function readSnapshotDiagnosticsLSPResponse (
84
+ sourcesTestDir : string
85
+ ) : Diagnostic [ ] {
86
+ const lspResponsePath = getSnapshotDiagnosticsLSPResponsePath ( sourcesTestDir ) ;
49
87
const expectedDiagnostics = readJsonSync ( lspResponsePath ) ;
50
88
return expectedDiagnostics ;
51
89
}
@@ -108,8 +146,7 @@ export function computeXMLWithMarkedRanges(
108
146
return xmlSnippetWithRangeMarkers ;
109
147
}
110
148
111
- export function getInputXMLSnippetPath ( testDir : string ) : string {
112
- const sourcesTestDir = toSourcesTestDir ( testDir ) ;
149
+ export function getInputXMLSnippetPath ( sourcesTestDir : string ) : string {
113
150
const inputXMLSnippetPath = resolve ( sourcesTestDir , INPUT_FILE_NAME ) ;
114
151
return inputXMLSnippetPath ;
115
152
}
@@ -127,7 +164,7 @@ function readInputXMLSnippet(
127
164
return xmlOriginalContent ;
128
165
}
129
166
130
- function toSourcesTestDir ( libTestDir : string ) : string {
167
+ export function toSourcesTestDir ( libTestDir : string ) : string {
131
168
// replace TypeScript output dir (lib) with the corresponding sources (test) dir.
132
169
return libTestDir . replace ( sep + "lib" , "" ) ;
133
170
}
0 commit comments