|
| 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