forked from OAI/OpenAPI-Specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema-test-coverage.mjs
More file actions
161 lines (136 loc) · 4.9 KB
/
schema-test-coverage.mjs
File metadata and controls
161 lines (136 loc) · 4.9 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
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
import { readFileSync } from "node:fs";
import { readdir, readFile } from "node:fs/promises";
import YAML from "yaml";
import { join } from "node:path";
import { argv } from "node:process";
import { registerSchema, validate } from "@hyperjump/json-schema/openapi-3-1";
import "@hyperjump/json-schema/draft-04";
import { BASIC, defineVocabulary } from "@hyperjump/json-schema/experimental";
/**
* @import { EvaluationPlugin } from "@hyperjump/json-schema/experimental"
* @import { Json } from "@hyperjump/json-pointer"
*/
import contentTypeParser from "content-type";
import { addMediaTypePlugin } from "@hyperjump/browser";
import { buildSchemaDocument } from "@hyperjump/json-schema/experimental";
addMediaTypePlugin("application/schema+yaml", {
parse: async (response) => {
const contentType = contentTypeParser.parse(
response.headers.get("content-type") ?? "",
);
const contextDialectId =
contentType.parameters.schema ?? contentType.parameters.profile;
const foo = YAML.parse(await response.text());
return buildSchemaDocument(foo, response.url, contextDialectId);
},
fileMatcher: (path) => path.endsWith(".yaml"),
});
/** @implements EvaluationPlugin */
class TestCoveragePlugin {
constructor() {
/** @type Set<string> */
this.visitedLocations = new Set();
}
beforeSchema(_schemaUri, _instance, context) {
if (this.allLocations) {
return;
}
/** @type Set<string> */
this.allLocations = [];
for (const schemaLocation in context.ast) {
if (
schemaLocation === "metaData" ||
// Do not require coverage of standard JSON Schema
schemaLocation.includes("json-schema.org") ||
// Do not require coverage of default $dynamicAnchor
// schemas, as they are not expected to be reached
schemaLocation.endsWith("/schema/WORK-IN-PROGRESS#/$defs/schema")
) {
continue;
}
if (Array.isArray(context.ast[schemaLocation])) {
for (const keyword of context.ast[schemaLocation]) {
if (Array.isArray(keyword)) {
this.allLocations.push(keyword[1]);
}
}
}
}
}
beforeKeyword([, schemaUri]) {
this.visitedLocations.add(schemaUri);
}
}
/** @type (testDirectory: string) => AsyncGenerator<[string,Json]> */
const tests = async function* (testDirectory) {
for (const file of await readdir(testDirectory, {
recursive: true,
withFileTypes: true,
})) {
if (!file.isFile() || !file.name.endsWith(".yaml")) {
continue;
}
const testPath = join(file.parentPath, file.name);
const testJson = await readFile(testPath, "utf8");
yield [testPath, YAML.parse(testJson)];
}
};
/**
* @typedef {{
* allLocations: string[];
* visitedLocations: Set<string>;
* }} Coverage
*/
/** @type (schemaUri: string, testDirectory: string) => Promise<Coverage> */
const runTests = async (schemaUri, testDirectory) => {
const testCoveragePlugin = new TestCoveragePlugin();
const validateOpenApi = await validate(schemaUri);
for await (const [name, test] of tests(testDirectory)) {
const result = validateOpenApi(test, {
outputFormat: BASIC,
plugins: [testCoveragePlugin],
});
if (!result.valid) {
console.log("Failed:", name, result.errors);
}
}
return {
allLocations: testCoveragePlugin.allLocations ?? new Set(),
visitedLocations: testCoveragePlugin.visitedLocations
};
};
const parseYamlFromFile = (filePath) => {
const schemaYaml = readFileSync(filePath, "utf8");
return YAML.parse(schemaYaml, { prettyErrors: true });
};
const meta = parseYamlFromFile("./src/schemas/validation/meta.yaml");
const oasBaseVocab = Object.keys(meta.$vocabulary)[0];
defineVocabulary(oasBaseVocab, {
"discriminator": "https://spec.openapis.org/oas/3.0/keyword/discriminator",
"example": "https://spec.openapis.org/oas/3.0/keyword/example",
"externalDocs": "https://spec.openapis.org/oas/3.0/keyword/externalDocs",
"xml": "https://spec.openapis.org/oas/3.0/keyword/xml"
});
registerSchema(meta);
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));
///////////////////////////////////////////////////////////////////////////////
const { allLocations, visitedLocations } = await runTests(argv[2], argv[3]);
const notCovered = allLocations.filter(
(location) => !visitedLocations.has(location),
);
if (notCovered.length > 0) {
console.log("NOT Covered:", notCovered.length, "of", allLocations.length);
const maxNotCovered = 20;
const firstNotCovered = notCovered.slice(0, maxNotCovered);
if (notCovered.length > maxNotCovered) firstNotCovered.push("...");
console.log(firstNotCovered);
process.exitCode = 1;
}
console.log(
"Covered:",
(allLocations.length - notCovered.length),
"of",
allLocations.length,
"(" + Math.floor(((allLocations.length - notCovered.length) / allLocations.length) * 100) + "%)",
);