Skip to content

Commit 66f78a5

Browse files
authored
refactor: auto-generate snapshot test cases from folders (#104)
1 parent b57824c commit 66f78a5

File tree

4 files changed

+87
-27
lines changed

4 files changed

+87
-27
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as klawSync from "klaw-sync";
2+
import { map, forEach, filter } from "lodash";
3+
import { basename, relative, dirname } from "path";
4+
import {
5+
snapshotTestLSPDiagnostic,
6+
toSourcesTestDir,
7+
INPUT_FILE_NAME,
8+
} from "./snapshots-utils";
9+
10+
describe(`The language server diagnostics capability`, () => {
11+
// The test files are in the source dir, not lib
12+
const snapshotTestsDir = toSourcesTestDir(__dirname);
13+
const klawItems = klawSync(snapshotTestsDir, { nodir: true });
14+
const inputFiles = filter(klawItems, (item) => {
15+
return item.path.endsWith(INPUT_FILE_NAME);
16+
});
17+
const testDirs = map(inputFiles, (_) => dirname(_.path));
18+
19+
forEach(testDirs, (dirPath) => {
20+
const dirName = basename(dirPath);
21+
22+
// UNCOMMENT THE LINES BELOW AND CHANGE onlyTestDirName TO ONLY RUN A SPECIFIC SNAPSHOT TEST
23+
// const onlyTestDirName = "my-test-dir";
24+
// if (dirName !== onlyTestDirName) {
25+
// return;
26+
// }
27+
28+
it(`Can create diagnostic for ${dirName.replace(/-/g, " ")} (${relative(
29+
snapshotTestsDir,
30+
dirPath
31+
)})`, async () => {
32+
await snapshotTestLSPDiagnostic(dirPath);
33+
});
34+
});
35+
});

packages/language-server/test/snapshots/xml-view-diagnostics/snapshots-utils.ts

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolve, sep } from "path";
1+
import { resolve, sep, relative, dirname } from "path";
22
import { Diagnostic, Range } from "vscode-languageserver-types";
33
import { TextDocument } from "vscode-languageserver";
44
import { readJsonSync, readFileSync } from "fs-extra";
@@ -14,38 +14,76 @@ export const OUTPUT_LSP_RESPONSE_FILE_NAME = "output-lsp-response.json";
1414
export async function snapshotTestLSPDiagnostic(
1515
testDir: string
1616
): 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);
1728
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+
1848
const newlyComputedDiagnostics = await computeNewDiagnosticLSPResponse(
1949
testDir
2050
);
2151
expect(
2252
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+
)}`
2458
).to.deep.equal(diagnosticsSnapshots);
2559

2660
const newlyCommutedRanges = map(newlyComputedDiagnostics, (_) => _.range);
2761
const newlyComputedXMLWithMarkedRanges = computeXMLWithMarkedRanges(
2862
testDir,
2963
newlyCommutedRanges
3064
);
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+
3566
expect(
3667
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))}`
3870
).to.equal(newlyComputedXMLWithMarkedRanges);
3971
}
4072

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(
4677
sourcesTestDir,
4778
OUTPUT_LSP_RESPONSE_FILE_NAME
4879
);
80+
return snapshotResponsePath;
81+
}
82+
83+
export function readSnapshotDiagnosticsLSPResponse(
84+
sourcesTestDir: string
85+
): Diagnostic[] {
86+
const lspResponsePath = getSnapshotDiagnosticsLSPResponsePath(sourcesTestDir);
4987
const expectedDiagnostics = readJsonSync(lspResponsePath);
5088
return expectedDiagnostics;
5189
}
@@ -108,8 +146,7 @@ export function computeXMLWithMarkedRanges(
108146
return xmlSnippetWithRangeMarkers;
109147
}
110148

111-
export function getInputXMLSnippetPath(testDir: string): string {
112-
const sourcesTestDir = toSourcesTestDir(testDir);
149+
export function getInputXMLSnippetPath(sourcesTestDir: string): string {
113150
const inputXMLSnippetPath = resolve(sourcesTestDir, INPUT_FILE_NAME);
114151
return inputXMLSnippetPath;
115152
}
@@ -127,7 +164,7 @@ function readInputXMLSnippet(
127164
return xmlOriginalContent;
128165
}
129166

130-
function toSourcesTestDir(libTestDir: string): string {
167+
export function toSourcesTestDir(libTestDir: string): string {
131168
// replace TypeScript output dir (lib) with the corresponding sources (test) dir.
132169
return libTestDir.replace(sep + "lib", "");
133170
}

packages/language-server/test/snapshots/xml-view-diagnostics/unknown-enum-value/diagnostic-spec.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/language-server/test/snapshots/xml-view-diagnostics/use-of-deprecated-class/diagnostic-spec.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)