-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcheck-versions.js
65 lines (55 loc) · 1.97 KB
/
check-versions.js
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
import { strict as assert } from "node:assert/strict";
import { createHash } from "node:crypto";
import { readFileSync } from "node:fs";
import { readdir } from "node:fs/promises";
import { test } from "node:test";
import { URL } from "node:url";
import { Validator } from "../index.js";
import { Snapshot } from "./snapshot.js";
const supportedVersions = Validator.supportedVersions;
function localPath(path) {
return new URL(path, import.meta.url).pathname;
}
const snapShotFile = localPath("snapshots-check-versions.json");
const updateSnapShot = process.argv[2] !== undefined;
const snapshot = new Snapshot(snapShotFile, updateSnapShot);
function matchSnapshot(obj, name) {
const hash = createHash("sha256");
hash.update(JSON.stringify(obj));
const hashValue = hash.digest("hex");
return snapshot.match(hashValue, name);
}
const openApiDir = localPath("../schemas.orig");
function readJSON(file) {
return JSON.parse(readFileSync(file));
}
async function getOpenApiSchemasVersions(oasdir) {
const dirs = (await readdir(oasdir)).filter((d) => !d.endsWith(".html"));
return dirs;
}
async function testVersion(version) {
test(`Check if version ${version} is unchanged`, async (t) => {
const versionDir = `${openApiDir}/${version}/schema/`;
const schemaList = (await readdir(versionDir)).filter(
(f) => !f.endsWith(".md"),
);
const lastSchema = schemaList.pop();
const schema = readJSON(`${openApiDir}/${version}/schema/${lastSchema}`);
assert.equal(
matchSnapshot(schema, `schema v${version} is unchanged`),
true,
);
});
}
test("no new versions should be present", async (t) => {
const versions = await getOpenApiSchemasVersions(openApiDir);
const difference = versions.filter((x) => !supportedVersions.has(x));
assert.equal(difference.length, 0, "all versions are known");
});
async function testAvailableVersions() {
const versions = await getOpenApiSchemasVersions(openApiDir);
for (const version of versions) {
testVersion(version);
}
}
testAvailableVersions();