forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.test.ts
258 lines (225 loc) · 8.08 KB
/
cli.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { deepStrictEqual, ok, strictEqual } from "assert";
import { beforeEach, describe, it } from "vitest";
import { stringify } from "yaml";
import { TypeSpecRawConfig } from "../src/config/types.js";
import { CompileCliArgs, getCompilerOptions } from "../src/core/cli/actions/compile/args.js";
import { CompilerOptions } from "../src/core/options.js";
import {
TestHost,
createTestHost,
expectDiagnosticEmpty,
expectDiagnostics,
resolveVirtualPath,
} from "../src/testing/index.js";
describe("compiler: cli", () => {
let host: TestHost;
const cwd = resolveVirtualPath("ws");
beforeEach(async () => {
host = await createTestHost();
host.addTypeSpecFile("ws/main.tsp", "");
});
describe("resolving compiler options", () => {
async function resolveCompilerOptions(args: CompileCliArgs, env: Record<string, string> = {}) {
const [options, diagnostics] = await getCompilerOptions(
host.compilerHost,
"ws/main.tsp",
cwd,
args,
env
);
expectDiagnosticEmpty(diagnostics);
ok(options, "Options should have been set.");
const { configFile: config, ...rest } = options;
return rest;
}
it("no args and config: return empty options with output-dir at {cwd}/tsp-output", async () => {
const options = await resolveCompilerOptions({});
deepStrictEqual(options, {
outputDir: `${cwd}/tsp-output`,
options: {},
});
});
it("--option without an emitter are moved to miscOptions", async () => {
const options = await resolveCompilerOptions({
options: [`test-debug=true`],
});
deepStrictEqual(options?.miscOptions, { "test-debug": "true" });
deepStrictEqual(options?.options, {});
});
describe("config file with emitters", () => {
beforeEach(() => {
host.addTypeSpecFile(
"ws/tspconfig.yaml",
stringify({
parameters: {
"custom-arg": {
default: "/default-arg-value",
},
},
emit: ["@typespec/openapi3", "@typespec/with-args"],
options: {
"@typespec/openapi3": {
"emitter-output-dir": "{output-dir}/custom",
},
"@typespec/with-args": {
"emitter-output-dir": "{custom-arg}/custom",
},
},
})
);
});
it("interpolate default output-dir in emitter output-dir", async () => {
const options = await resolveCompilerOptions({});
strictEqual(
options?.options?.["@typespec/openapi3"]?.["emitter-output-dir"],
`${cwd}/tsp-output/custom`
);
});
it("override output-dir from cli args", async () => {
const options = await resolveCompilerOptions({ "output-dir": `${cwd}/my-output-dir` });
strictEqual(
options?.options?.["@typespec/openapi3"]?.["emitter-output-dir"],
`${cwd}/my-output-dir/custom`
);
});
it("override emitter-output-dir from cli args", async () => {
const options = await resolveCompilerOptions({
options: [`@typespec/openapi3.emitter-output-dir={cwd}/relative-to-cwd`],
});
strictEqual(
options?.options?.["@typespec/openapi3"]?.["emitter-output-dir"],
`${cwd}/relative-to-cwd`
);
});
it("override emitter-output-dir from cli args using path to emitter with extension", async () => {
const options = await resolveCompilerOptions({
options: [`path/to/emitter.js.emitter-output-dir={cwd}/relative-to-cwd`],
});
strictEqual(
options?.options?.["path/to/emitter.js"]?.["emitter-output-dir"],
`${cwd}/relative-to-cwd`
);
});
describe("arg interpolation", () => {
it("use default arg value", async () => {
const options = await resolveCompilerOptions({});
strictEqual(
options?.options?.["@typespec/with-args"]?.["emitter-output-dir"],
`/default-arg-value/custom`
);
});
it("passing --arg interpolate args in the cli", async () => {
const options = await resolveCompilerOptions({
args: [`custom-arg=/my-updated-arg-value`],
});
strictEqual(
options?.options?.["@typespec/with-args"]?.["emitter-output-dir"],
`/my-updated-arg-value/custom`
);
});
});
it("emit diagnostic if passing unknown parameter", async () => {
const [_, diagnostics] = await getCompilerOptions(
host.compilerHost,
"ws/main.tsp",
cwd,
{
args: ["not-defined-arg=my-value"],
},
{}
);
expectDiagnostics(diagnostics, {
code: "config-invalid-argument",
message: `Argument "not-defined-arg" is not defined as a parameter in the config.`,
});
});
it("emit diagnostic if using relative path in config paths", async () => {
host.addTypeSpecFile(
"ws/tspconfig.yaml",
stringify({
"output-dir": "./my-output",
})
);
const [_, diagnostics] = await getCompilerOptions(
host.compilerHost,
"ws/main.tsp",
cwd,
{},
{}
);
expectDiagnostics(diagnostics, {
code: "config-path-absolute",
message: `Path "./my-output" cannot be relative. Use {cwd} or {project-root} to specify what the path should be relative to.`,
});
});
});
async function resolveCompilerOptionsFor({
args,
config,
}: {
args?: CompileCliArgs;
config?: TypeSpecRawConfig;
}) {
host.addTypeSpecFile("ws/tspconfig.yaml", stringify(config ?? {}));
return (await resolveCompilerOptions(args ?? {})) ?? {};
}
interface TestUnifiedOptions<
K extends keyof CompileCliArgs & keyof TypeSpecRawConfig,
T extends Exclude<keyof CompilerOptions, "configFile">,
> {
default: CompileCliArgs[K];
set: { in: CompileCliArgs[K]; alt: CompileCliArgs[K]; expected: CompilerOptions[T] }[];
}
function testUnifiedOptions<
K extends keyof CompileCliArgs & keyof TypeSpecRawConfig,
T extends Exclude<keyof CompilerOptions, "configFile">,
>(name: K, resolvedName: T, data: TestUnifiedOptions<K, T>) {
describe(name, () => {
it("default", async () => {
const options = await resolveCompilerOptionsFor({});
strictEqual(options[resolvedName], data.default);
});
for (const { in: input, alt, expected } of data.set) {
describe(`input: ${input}`, () => {
it("set from the cli args", async () => {
const options = await resolveCompilerOptionsFor({ args: { [name]: input } });
deepStrictEqual(options[resolvedName], expected);
});
it("set from the config", async () => {
const options = await resolveCompilerOptionsFor({ config: { [name]: input } });
deepStrictEqual(options[resolvedName], expected);
});
it("both cli and config (cli wins)", async () => {
const options = await resolveCompilerOptionsFor({
args: { [name]: input },
config: { [name]: alt },
});
deepStrictEqual(options[resolvedName], expected);
});
});
}
});
}
testUnifiedOptions("warn-as-error", "warningAsError", {
default: undefined,
set: [
{ in: true, expected: true, alt: false },
{ in: false, expected: false, alt: true },
],
});
testUnifiedOptions("output-dir", "outputDir", {
default: resolveVirtualPath("ws/tsp-output"),
set: [
{
in: "{cwd}/override",
expected: resolveVirtualPath("ws/override"),
alt: "{cwd}/alt-in-config",
},
],
});
testUnifiedOptions("trace", "trace", {
default: undefined,
set: [{ in: ["one", "two"], expected: ["one", "two"], alt: ["three"] }],
});
});
});