forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput-spec-versions.test.ts
61 lines (53 loc) · 2.09 KB
/
output-spec-versions.test.ts
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
import { resolvePath } from "@typespec/compiler";
import {
BasicTestRunner,
expectDiagnosticEmpty,
resolveVirtualPath,
} from "@typespec/compiler/testing";
import { ok } from "assert";
import { beforeEach, expect, it } from "vitest";
import { OpenAPI3EmitterOptions } from "../src/lib.js";
import { createOpenAPITestRunner } from "./test-host.js";
const outputDir = resolveVirtualPath("test-output");
let runner: BasicTestRunner;
beforeEach(async () => {
runner = await createOpenAPITestRunner();
});
async function compileOpenAPI(options: OpenAPI3EmitterOptions, code: string = ""): Promise<void> {
const diagnostics = await runner.diagnose(code, {
noEmit: false,
emit: ["@typespec/openapi3"],
options: { "@typespec/openapi3": { ...options, "emitter-output-dir": outputDir } },
});
expectDiagnosticEmpty(diagnostics);
}
function expectHasOutput(filename: string) {
const outPath = resolvePath(outputDir, filename);
const content = runner.fs.get(outPath);
ok(content, `Expected ${outPath} to exist.`);
}
it("creates nested directory if multiple spec versions are specified", async () => {
await compileOpenAPI({ "openapi-versions": ["3.0.0", "3.1.0"] });
expectHasOutput("3.0.0/openapi.yaml");
expectHasOutput("3.1.0/openapi.yaml");
});
it("does not create nested directory if only 1 spec version is specified", async () => {
await compileOpenAPI({ "openapi-versions": ["3.0.0"] });
expectHasOutput("openapi.yaml");
});
it("defaults to 3.0.0 if not specified", async () => {
await compileOpenAPI({ "file-type": "json" });
const outPath = resolvePath(outputDir, "openapi.json");
const content = runner.fs.get(outPath);
ok(content, `Expected ${outPath} to exist.`);
const doc = JSON.parse(content);
expect(doc.openapi).toBe("3.0.0");
});
it("supports 3.1.0", async () => {
await compileOpenAPI({ "openapi-versions": ["3.1.0"], "file-type": "json" });
const outPath = resolvePath(outputDir, "openapi.json");
const content = runner.fs.get(outPath);
ok(content, `Expected ${outPath} to exist.`);
const doc = JSON.parse(content);
expect(doc.openapi).toBe("3.1.0");
});