forked from wbcsd/pact-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.ts
254 lines (232 loc) · 7.05 KB
/
validate.ts
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import * as fs from "fs";
import * as path from "path";
import {
ExtensionJsonSchema,
ExtensionParser,
SolutionJsonSchema,
SolutionParser,
TestResultParser,
UserParser,
validateSchemaJson,
WorkingGroupParser,
} from "./frontend/lib/catalog-types.schema";
async function validateExtension(
namespace: string,
name: string,
version: string,
extensionDir: string
): Promise<number> {
console.log(`checking ${extensionDir}`);
try {
const extensionFile = path.join(extensionDir, "extension.json");
const extension: ExtensionJsonSchema = ExtensionParser.parse(JSON.parse(fs.readFileSync(extensionFile, "utf8")));
if (extension.id.namespace !== namespace) {
console.error(
`The namespace ${extension.id.namespace} does not match the directory ${namespace}`
);
return 1;
}
if (extension.id.extension_name !== name) {
console.error(
`The extension_name ${extension.id.extension_name} does not match the directory ${extension}`
);
return 1;
}
if (extension.id.version !== version) {
console.error(
`The version ${extension.id.version} does not match the directory ${version}`
);
return 1;
}
} catch (error) {
console.error(error);
return 1;
}
try {
const schemaFile = path.join(extensionDir, "schema.json");
const schema = fs.readFileSync(schemaFile, "utf8");
let validated = validateSchemaJson(schema);
if (!validated.validSchemaJson) {
let { message } = validated;
console.error(message);
return 1;
}
} catch (error) {
console.error(error);
return 1;
}
return 0;
}
async function validateAllExtensions(): Promise<void> {
const extensionsDir = "./catalog/data-model-extensions";
let errorCode = 0;
for (const publisher of fs.readdirSync(extensionsDir)) {
const publisherDir = path.join(extensionsDir, publisher);
if (!fs.statSync(publisherDir).isDirectory()) {
continue;
}
for (const extension of fs.readdirSync(publisherDir)) {
const extensionDir = path.join(publisherDir, extension);
if (!fs.statSync(extensionDir).isDirectory()) {
continue;
}
for (const version of fs.readdirSync(extensionDir)) {
const versionDir = path.join(extensionDir, version);
if (!fs.statSync(versionDir).isDirectory()) {
continue;
}
const subdirErrorCode = await validateExtension(
publisher,
extension,
version,
versionDir
);
if (subdirErrorCode !== 0) {
errorCode = 1;
console.error(`${versionDir} is not a valid extension`);
}
}
}
}
if (errorCode !== 0) {
process.exit(errorCode);
}
}
async function validateSolution(
id: string,
version: string,
solutionDir: string
): Promise<number> {
console.log(`checking ${solutionDir}`);
try {
const solutionFile = path.join(solutionDir, "solution.json");
const solution: SolutionJsonSchema = SolutionParser.parse(JSON.parse(fs.readFileSync(solutionFile, "utf8")));
if (solution.id !== id) {
console.error(
`The version ${solution.id} does not match the directory ${id}`
);
return 1;
}
if (solution.version !== version) {
console.error(
`The version ${solution.version} does not match the directory ${version}`
);
return 1;
}
} catch (error) {
console.error(error);
return 1;
}
return 0;
}
async function validateAllSolutions(): Promise<void> {
const solutionsDir = "./catalog/solutions";
let errorCode = 0;
for (const solution of fs.readdirSync(solutionsDir)) {
const solutionDir = path.join(solutionsDir, solution);
if (!fs.statSync(solutionDir).isDirectory()) {
continue;
}
for (const version of fs.readdirSync(solutionDir)) {
const versionDir = path.join(solutionDir, version);
if (!fs.statSync(versionDir).isDirectory()) {
continue;
}
const subdirErrorCode = await validateSolution(
solution,
version,
versionDir
);
if (subdirErrorCode !== 0) {
errorCode = 1;
console.error(`${versionDir} is not a valid solution`);
}
}
}
if (errorCode !== 0) {
process.exit(errorCode);
}
}
async function validateTestResult(testResultFile: string): Promise<number> {
try {
const solutionsDir = path.join(process.cwd(), "catalog/solutions");
const solutionsList: string[] = [];
for (const solution of fs.readdirSync(solutionsDir)) {
const solutionPath = path.join(solutionsDir, solution);
if (fs.statSync(solutionPath).isDirectory()) {
const solutionVersions = fs.readdirSync(solutionPath);
for (const version of solutionVersions) {
solutionsList.push(`${solution}/${version}`);
}
}
}
const testResultJson = TestResultParser.parse(JSON.parse(fs.readFileSync(testResultFile, "utf8")));
const { solution_id, version } = testResultJson.tested_solution;
if (!solutionsList.includes(`${solution_id}/${version}`)) {
if (!fs.readdirSync(solutionsDir).includes(solution_id)) {
console.error(
`There is no solution with id ${solution_id} in the catalog. Please make sure the conformance test json is correct.`
);
return 1;
} else {
console.error(
`The version ${version} of solution ${solution_id} is not yet in the catalog.`
);
return 1;
}
}
} catch (error) {
console.error(error);
return 1;
}
return 0;
}
async function validateAllTestResults(): Promise<void> {
let errorCode = 0;
const conformanceTestDir = path.join(
process.cwd(),
"catalog/conformance-tests"
);
const conformanceTests = fs.readdirSync(conformanceTestDir);
for (const conformanceTest of conformanceTests) {
const conformanceTestFile = path.join(conformanceTestDir, conformanceTest);
const subdirErrorCode = await validateTestResult(conformanceTestFile);
if (subdirErrorCode !== 0) {
errorCode = 1;
}
}
if (errorCode !== 0) {
return Promise.reject("At least 1 conformance test is not valid.")
}
}
async function validateAll(): Promise<void> {
await validateAllExtensions();
await validateAllSolutions();
await validateAllTestResults();
for (const { dir, parser } of [
{ dir: "./catalog/conformance-tests", parser: TestResultParser },
{ dir: "./catalog/users", parser: UserParser },
{ dir: "./catalog/working-groups", parser: WorkingGroupParser },
]) {
for (const file of fs.readdirSync(dir).filter((file) => {
return path.extname(file).toLowerCase() === ".json";
})) {
const jsonFile = path.join(dir, file);
console.log(`checking ${jsonFile}`);
try {
const json = JSON.parse(fs.readFileSync(jsonFile, "utf8"));
parser.parse(json);
} catch (error) {
console.error(error);
console.error(`${file} is invalid!`);
process.exit(1);
}
}
}
}
if (require.main === module) {
validateAll().catch((e) => {
console.error(e);
process.exit(1);
});
}