forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-host.ts
179 lines (164 loc) · 5.48 KB
/
test-host.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Diagnostic, interpolatePath, resolvePath } from "@typespec/compiler";
import {
createTestHost,
createTestWrapper,
expectDiagnosticEmpty,
resolveVirtualPath,
} from "@typespec/compiler/testing";
import { HttpTestLibrary } from "@typespec/http/testing";
import { JsonSchemaTestLibrary } from "@typespec/json-schema/testing";
import { OpenAPITestLibrary } from "@typespec/openapi/testing";
import { RestTestLibrary } from "@typespec/rest/testing";
import { VersioningTestLibrary } from "@typespec/versioning/testing";
import { XmlTestLibrary } from "@typespec/xml/testing";
import { ok } from "assert";
import { parse } from "yaml";
import { OpenAPI3EmitterOptions } from "../src/lib.js";
import { OpenAPI3TestLibrary } from "../src/testing/index.js";
import { OpenAPI3Document } from "../src/types.js";
export async function createOpenAPITestHost() {
return createTestHost({
libraries: [
HttpTestLibrary,
JsonSchemaTestLibrary,
RestTestLibrary,
VersioningTestLibrary,
XmlTestLibrary,
OpenAPITestLibrary,
OpenAPI3TestLibrary,
],
});
}
export async function createOpenAPITestRunner({
emitterOptions,
withVersioning,
}: { withVersioning?: boolean; emitterOptions?: OpenAPI3EmitterOptions } = {}) {
const host = await createOpenAPITestHost();
const importAndUsings = `
import "@typespec/http";
import "@typespec/rest";
import "@typespec/json-schema";
import "@typespec/openapi";
import "@typespec/openapi3";
import "@typespec/xml";
${withVersioning ? `import "@typespec/versioning"` : ""};
using TypeSpec.Rest;
using TypeSpec.Http;
using TypeSpec.OpenAPI;
using TypeSpec.Xml;
${withVersioning ? "using TypeSpec.Versioning;" : ""}
`;
return createTestWrapper(host, {
wrapper: (code) => `${importAndUsings} ${code}`,
compilerOptions: {
emit: ["@typespec/openapi3"],
options: {
"@typespec/openapi3": { ...emitterOptions },
},
},
});
}
export async function emitOpenApiWithDiagnostics(
code: string,
options: OpenAPI3EmitterOptions = {},
): Promise<[OpenAPI3Document, readonly Diagnostic[], string]> {
const runner = await createOpenAPITestRunner();
const fileType = options["file-type"] || "yaml";
const outputFile = resolveVirtualPath("openapi" + fileType === "json" ? ".json" : ".yaml");
const diagnostics = await runner.diagnose(code, {
emit: ["@typespec/openapi3"],
options: {
"@typespec/openapi3": { ...options, "output-file": outputFile },
},
});
const content = runner.fs.get(outputFile);
ok(content, "Expected to have found openapi output");
const doc = fileType === "json" ? JSON.parse(content) : parse(content);
return [doc, diagnostics, content];
}
export async function diagnoseOpenApiFor(code: string, options: OpenAPI3EmitterOptions = {}) {
const runner = await createOpenAPITestRunner();
const diagnostics = await runner.diagnose(code, {
emit: ["@typespec/openapi3"],
options: { "@typespec/openapi3": options as any },
});
return diagnostics;
}
export async function openApiFor(
code: string,
versions?: string[],
options: OpenAPI3EmitterOptions = {},
) {
const host = await createOpenAPITestHost();
const outPath = resolveVirtualPath("{version}.openapi.json");
host.addTypeSpecFile(
"./main.tsp",
`import "@typespec/http"; import "@typespec/json-schema"; import "@typespec/rest"; import "@typespec/openapi"; import "@typespec/openapi3";import "@typespec/xml"; ${
versions ? `import "@typespec/versioning"; using TypeSpec.Versioning;` : ""
}using TypeSpec.Rest;using TypeSpec.Http;using TypeSpec.OpenAPI;using TypeSpec.Xml;${code}`,
);
const diagnostics = await host.diagnose("./main.tsp", {
noEmit: false,
emit: ["@typespec/openapi3"],
options: { "@typespec/openapi3": { ...options, "output-file": outPath } },
});
expectDiagnosticEmpty(diagnostics);
if (!versions) {
return JSON.parse(host.fs.get(resolveVirtualPath("openapi.json"))!);
} else {
const output: any = {};
for (const version of versions) {
output[version] = JSON.parse(host.fs.get(interpolatePath(outPath, { version: version }))!);
}
return output;
}
}
export async function checkFor(code: string, options: OpenAPI3EmitterOptions = {}) {
const host = await createOpenAPITestRunner();
return await host.diagnose(code, {
dryRun: true,
emit: ["@typespec/openapi3"],
options: { "@typespec/openapi3": { ...options } },
});
}
export async function oapiForModel(
name: string,
modelDef: string,
options: OpenAPI3EmitterOptions = {},
) {
const oapi = await openApiFor(
`
${modelDef};
@service(#{title: "Testing model"})
@route("/")
namespace root {
op read(): {
@header contentType: "application/json";
@body body: ${name};
};
}
`,
undefined,
options,
);
const useSchema = oapi.paths["/"].get.responses[200].content["application/json"].schema;
return {
isRef: !!useSchema.$ref,
useSchema,
schemas: oapi.components.schemas || {},
};
}
export async function openapiWithOptions(
code: string,
options: OpenAPI3EmitterOptions,
): Promise<OpenAPI3Document> {
const runner = await createOpenAPITestRunner();
const outPath = resolvePath("/openapi.json");
const diagnostics = await runner.diagnose(code, {
emit: ["@typespec/openapi3"],
options: { "@typespec/openapi3": { ...options, "output-file": outPath } },
});
expectDiagnosticEmpty(diagnostics);
const content = runner.fs.get(outPath)!;
return JSON.parse(content);
}