-
-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathadd-test-ids.js
More file actions
91 lines (70 loc) · 2.3 KB
/
add-test-ids.js
File metadata and controls
91 lines (70 loc) · 2.3 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
import * as fs from "node:fs";
import { parse, modify, applyEdits } from "jsonc-parser";
import { normalize } from "./normalize.js";
import { loadRemotes } from "./load-remotes.js";
import generateTestId from "./utils/generateTestIds.js";
const DIALECT_MAP = {
"draft2020-12": "https://json-schema.org/draft/2020-12/schema",
"draft2019-09": "https://json-schema.org/draft/2019-09/schema",
"draft7": "http://json-schema.org/draft-07/schema#",
"draft6": "http://json-schema.org/draft-06/schema#",
"draft4": "http://json-schema.org/draft-04/schema#"
};
async function addIdsToFile(filePath, dialectUri) {
console.log("Reading:", filePath);
const text = fs.readFileSync(filePath, "utf8");
const tests = parse(text);
let edits = [];
let added = 0;
for (let i = 0; i < tests.length; i++) {
const testCase = tests[i];
const normalizedSchema = await normalize(testCase.schema, dialectUri);
for (let j = 0; j < testCase.tests.length; j++) {
const test = testCase.tests[j];
if (!test.id) {
const id = generateTestId(
normalizedSchema,
test.data,
test.valid
);
const path = [i, "tests", j, "id"];
edits.push(
...modify(text, path, id, {
formattingOptions: {
insertSpaces: true,
tabSize: 4
}
})
);
added++;
}
}
}
if (added > 0) {
const updatedText = applyEdits(text, edits);
fs.writeFileSync(filePath, updatedText);
console.log(` Added ${added} IDs`);
} else {
console.log(" All tests already have IDs");
}
}
//CLI stuff
const dialectArg = process.argv[2];
if (!dialectArg || !DIALECT_MAP[dialectArg]) {
console.error("Usage: node add-test-ids.js <dialect> [file-path]");
console.error("Available dialects:", Object.keys(DIALECT_MAP).join(", "));
process.exit(1);
}
const dialectUri = DIALECT_MAP[dialectArg];
const filePath = process.argv[3];
// Load remotes only for the specified dialect
loadRemotes(dialectUri, "./remotes");
if (filePath) {
await addIdsToFile(filePath, dialectUri);
} else {
const testDir = `tests/${dialectArg}`;
const files = fs.readdirSync(testDir).filter(f => f.endsWith(".json"));
for (const file of files) {
await addIdsToFile(`${testDir}/${file}`, dialectUri);
}
}