Skip to content

Commit 2efd6ce

Browse files
committed
Unit test
1 parent e5f3b94 commit 2efd6ce

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

scripts/schema-convert.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ const fs = require('fs');
66
const yaml = require('yaml');
77

88
function convert(filename,date) {
9-
const s = fs.readFileSync(filename,'utf8');
10-
let obj;
119
try {
12-
obj = yaml.parse(s, {prettyErrors: true});
10+
const s = fs.readFileSync(filename,'utf8');
11+
const obj = yaml.parse(s, {prettyErrors: true});
1312
// replace last segment of id, $id, and $ref value with date
1413
for (const p of ["id","$id","$ref"]) {
1514
if (obj[p]) {

tests/md2html/md2html.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { readdirSync, readFileSync } from "node:fs";
22
import { exec } from "node:child_process";
33
import { resolve } from "node:path";
44
import { describe, test, expect } from "vitest";
5-
import assert from "node:assert";
65

76
const folder = "./tests/md2html/fixtures/";
8-
describe("v3.0 - Validate examples", async () => {
7+
describe("Markdown to HTML", async () => {
98
readdirSync(folder, { withFileTypes: true })
109
.filter((entry) => entry.isFile() && /\.md$/.test(entry.name))
1110
.forEach((entry) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "https://spec.openapis.org/oas/3.0/schema/87654321",
3+
"$id": "https://spec.openapis.org/oas/3.0/schema/87654321",
4+
"$ref": "path/to/somewhere/87654321",
5+
"other": {
6+
"foo": "bar"
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
id: https://spec.openapis.org/oas/3.0/schema/20241017
2+
$id: https://spec.openapis.org/oas/3.0/schema/some-iteration
3+
$ref: path/to/somewhere/replace-last-segment
4+
other:
5+
foo: bar
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { readdirSync, readFileSync } from "node:fs";
2+
import { exec } from "node:child_process";
3+
import { resolve } from "node:path";
4+
import { describe, test, expect } from "vitest";
5+
import assert from "node:assert";
6+
7+
const folder = "./tests/schema-convert/fixtures/";
8+
describe("Convert Schemas", async () => {
9+
test("YAML with id, $id, $ref", async () => {
10+
const expected = readFileSync(folder + "schema.json", "utf8");
11+
const output = await convert(
12+
[
13+
"schema.yaml",
14+
"87654321",
15+
],
16+
folder,
17+
);
18+
expect(output.stdout).to.equal(expected);
19+
});
20+
21+
test("non-existing input", async () => {
22+
const output = await convert(
23+
[
24+
"does-not-exist",
25+
"YYYYMMDD",
26+
],
27+
folder,
28+
);
29+
expect(output.stderr).to.equal(" ENOENT: no such file or directory, open 'does-not-exist'\n");
30+
});
31+
32+
test("invalid parameters", async () => {
33+
const output = await convert(
34+
[
35+
"schema.yaml",
36+
],
37+
folder,
38+
);
39+
expect(output.stderr).to.equal("Usage: convert-schema.js file.yaml YYYYMMDD\n");
40+
});
41+
});
42+
43+
function convert(args, cwd) {
44+
return new Promise((res) => {
45+
exec(
46+
`node ${resolve("./scripts/schema-convert.js")} ${args.join(" ")}`,
47+
{ cwd },
48+
(error, stdout, stderr) => {
49+
res({
50+
code: error?.code || 0,
51+
error,
52+
stdout,
53+
stderr,
54+
});
55+
},
56+
);
57+
});
58+
}

0 commit comments

Comments
 (0)