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
137 lines (115 loc) · 3.78 KB
/
schema-test-coverage.mjs
File metadata and controls
137 lines (115 loc) · 3.78 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
import { readdir, readFile } from "node:fs/promises";
import YAML from "yaml";
import { join } from "node:path";
import { argv } from "node:process";
import { validate } from "@hyperjump/json-schema/draft-2020-12";
import "@hyperjump/json-schema/draft-04";
import { BASIC } 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") {
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 { 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);
}
console.log(
"Covered:",
visitedLocations.size,
"of",
allLocations.length,
"(" + Math.floor((visitedLocations.size / allLocations.length) * 100) + "%)",
);
if (visitedLocations.size != allLocations.length) {
process.exitCode = 1;
}