-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_test.ts
413 lines (384 loc) · 9.57 KB
/
mod_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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import {
assertEquals,
assertExists,
assertFalse,
assertIsError,
assertNotEquals,
assertRejects,
assertStringIncludes,
} from "@std/assert";
import { ConsoleHandler, FileHandler, jsonFormatter, setup } from "@std/log";
import process from "node:process";
import {
ConfigFileReadError,
ConfigParseError,
EnvNotSetError,
initVariable,
OPTIONAL,
OPTIONAL_NON_EMPTY,
REQUIRED,
REQUIRED_NON_EMPTY,
type ZodSchemaCompat,
} from "./mod.ts";
setup({
handlers: {
console: new ConsoleHandler("DEBUG"),
file: new FileHandler("DEBUG", {
filename: "./log.jsonl",
mode: "a",
formatter: jsonFormatter,
}),
},
loggers: {
default: {
level: "DEBUG",
handlers: ["console", "file"],
},
"@wuespace/envar": {
level: "DEBUG",
handlers: ["file"],
},
},
});
const PARSE_ERROR = new Error("parse error");
const OPTIONAL_PASSING = {
isOptional: () => true,
safeParse: () => ({ error: undefined }),
};
const OPTIONAL_FAILING = {
isOptional: () => true,
safeParse: () => ({ error: PARSE_ERROR }),
};
const REQUIRED_PASSING = {
isOptional: () => false,
safeParse: (val: unknown) => ({
error: typeof val === "string" ? undefined : PARSE_ERROR,
}),
};
const REQUIRED_FAILING = {
isOptional: () => false,
safeParse: () => ({ error: PARSE_ERROR }),
};
Deno.test("EnvNotSetError", async (t) => {
await t.step("without cause", () => {
const err = new EnvNotSetError("TEST");
assertStringIncludes(
err.message,
"TEST",
"Expected the message to contain the env variable name",
);
assertStringIncludes(
err.message,
"TEST_FILE",
"Expected the message to contain a note about setting the variable using TEST_FILE",
);
assertEquals(
err.envVariable,
"TEST",
"Expected the envVariable to be set correctly",
);
assertIsError(
err,
EnvNotSetError,
undefined,
"Expected the error to be an Error instance of EnvNotSetError",
);
});
await t.step("with cause", () => {
const err = new EnvNotSetError("TEST", new Error("cause"));
assertStringIncludes(
err.message,
"TEST",
"Expected the message to contain the env variable name",
);
assertStringIncludes(
err.message,
"TEST_FILE",
"Expected the message to contain a note about setting the variable using TEST_FILE",
);
assertEquals(
err.envVariable,
"TEST",
"Expected the envVariable to be set correctly",
);
assertEquals(
err.cause,
new Error("cause"),
"Expected the cause to be set correctly",
);
assertIsError(
err,
EnvNotSetError,
undefined,
"Expected the error to be an Error instance of EnvNotSetError",
);
});
});
Deno.test("initVariable", async (t) => {
const ENV_VAR = "TEST";
const EXISTING_FILE = import.meta.filename;
const NONEXISTING_FILE = import.meta.filename + ".nonexistent";
const DEFAULT_VALUE = "default";
function prepare(TEST?: string, TEST_FILE?: string) {
Deno.env.delete(ENV_VAR);
Deno.env.delete(ENV_VAR + "_FILE");
TEST && Deno.env.set(ENV_VAR, TEST);
TEST_FILE && Deno.env.set(ENV_VAR + "_FILE", TEST_FILE);
}
await t.step("Value from Environment", async () => {
prepare("value");
await initVariable(ENV_VAR, REQUIRED_PASSING);
assertEquals(
Deno.env.get(ENV_VAR),
"value",
"Expected the value to match the Environment Variable",
);
assertEquals(
process.env[ENV_VAR],
"value",
"Expected the value to match the Environment Variable in process.env",
);
prepare("value2");
await initVariable(ENV_VAR, REQUIRED_PASSING);
assertEquals(
Deno.env.get(ENV_VAR),
"value2",
"Expected the value to match the Environment Variable",
);
assertEquals(
process.env[ENV_VAR],
"value2",
"Expected the value to match the Environment Variable in process.env",
);
prepare("value");
await assertRejects(
() => initVariable(ENV_VAR, REQUIRED_FAILING),
ConfigParseError,
undefined,
"Expected a ConfigParseError when the value cannot be parsed",
);
await assertRejects(
() => initVariable(ENV_VAR, OPTIONAL_FAILING),
ConfigParseError,
undefined,
"Expected a ConfigParseError when the value cannot be parsed",
);
prepare("value", EXISTING_FILE);
await initVariable(ENV_VAR, REQUIRED_PASSING);
assertEquals(
Deno.env.get(ENV_VAR),
"value",
`Expected the Environment Variable to take precedence over the ${ENV_VAR}_FILE file`,
);
assertEquals(
process.env[ENV_VAR],
"value",
`Expected the Environment Variable to take precedence over the ${ENV_VAR}_FILE file in process.env`,
);
prepare("value2");
await initVariable(ENV_VAR, REQUIRED_PASSING, DEFAULT_VALUE);
assertEquals(
Deno.env.get(ENV_VAR),
"value2",
"Expected the Environment Variable to take precedence over the default value",
);
assertEquals(
process.env[ENV_VAR],
"value2",
"Expected the Environment Variable to take precedence over the default value in process.env",
);
});
await t.step(`Value from ${ENV_VAR}_FILE file`, async () => {
prepare(undefined, EXISTING_FILE);
await initVariable(ENV_VAR, REQUIRED_PASSING);
assertExists(
Deno.env.get(ENV_VAR),
`Expected the value to be filled from the ${ENV_VAR}_FILE file`,
);
assertExists(
process.env[ENV_VAR],
`Expected the value to be filled from the ${ENV_VAR}_FILE file in process.env`,
);
prepare(undefined, EXISTING_FILE);
await assertRejects(
() => initVariable(ENV_VAR, REQUIRED_FAILING),
ConfigParseError,
undefined,
`Expected a ConfigParseError when the value cannot be parsed`,
);
prepare(undefined, NONEXISTING_FILE);
await assertRejects(
() => initVariable(ENV_VAR, REQUIRED_PASSING),
ConfigFileReadError,
undefined,
"Expected a ConfigFileReadError when the file does not exist",
);
prepare(undefined, EXISTING_FILE);
await initVariable(ENV_VAR, REQUIRED_PASSING, DEFAULT_VALUE);
assertNotEquals(
Deno.env.get(ENV_VAR),
DEFAULT_VALUE,
`Expected the ${ENV_VAR}_FILE file to take precedence over the default value`,
);
assertNotEquals(
process.env[ENV_VAR],
DEFAULT_VALUE,
`Expected the ${ENV_VAR}_FILE file to take precedence over the default value in process.env`,
);
});
await t.step("value from default", async () => {
prepare();
await initVariable(ENV_VAR, REQUIRED_PASSING, DEFAULT_VALUE);
assertEquals(
Deno.env.get(ENV_VAR),
DEFAULT_VALUE,
"Expected the default value to be used",
);
assertEquals(
process.env[ENV_VAR],
DEFAULT_VALUE,
"Expected the default value to be used in process.env",
);
prepare();
await assertRejects(
() => initVariable(ENV_VAR, REQUIRED_FAILING, DEFAULT_VALUE),
ConfigParseError,
undefined,
"Expected a ConfigParseError when the default value cannot be parsed",
);
prepare();
await initVariable(ENV_VAR, OPTIONAL_PASSING, DEFAULT_VALUE);
assertEquals(
Deno.env.get(ENV_VAR),
DEFAULT_VALUE,
"Expected the default value to be used",
);
assertEquals(
process.env[ENV_VAR],
DEFAULT_VALUE,
"Expected the default value to be used in process.env",
);
});
await t.step("No Value", async () => {
prepare();
await assertRejects(
() => initVariable(ENV_VAR, REQUIRED_PASSING),
ConfigParseError,
undefined,
"Expected a ConfigParseError when no value is set",
);
prepare();
await initVariable(ENV_VAR, OPTIONAL_PASSING);
await assertEquals(
Deno.env.get(ENV_VAR),
undefined,
"Expected the value to be undefined",
);
});
await t.step("Single Argument defaults to OPTIONAL", async () => {
prepare("XXX");
await initVariable(ENV_VAR);
await assertEquals(
Deno.env.get(ENV_VAR),
"XXX",
"Expected the value to be undefined",
);
prepare();
await initVariable(ENV_VAR);
await assertEquals(
Deno.env.get(ENV_VAR),
undefined,
"Expected the value to be undefined",
);
});
});
Deno.test("Built-in ZodSchemaCompat Validators", async (t) => {
await testZodSchemaCompatValidator(t, "REQUIRED", REQUIRED, false, [
"value",
"",
]);
await testZodSchemaCompatValidator(t, "OPTIONAL", OPTIONAL, true, [
"value",
"",
]);
await testZodSchemaCompatValidator(
t,
"REQUIRED_NON_EMPTY",
REQUIRED_NON_EMPTY,
false,
[
"value",
"xxx",
],
[
"",
],
);
await testZodSchemaCompatValidator(
t,
"OPTIONAL_NON_EMPTY",
OPTIONAL_NON_EMPTY,
true,
[
"value",
"xxx",
],
[
"",
],
);
function testZodSchemaCompatValidator(
ctx: Deno.TestContext,
name: string,
validator: ZodSchemaCompat,
isOptional: boolean,
validValues: string[],
invalidValues: string[] = [],
) {
return ctx.step(name, async (t) => {
await t.step("isOptional", () => {
assertEquals(
validator.isOptional(),
isOptional,
`Expected isOptional to return ${isOptional}`,
);
});
await t.step("safeParse", () => {
const NOT_A_STRING = Symbol("not a string");
for (const value of validValues) {
assertFalse(
validator.safeParse(value).error,
`Expected "${value}" to be parsed successfully`,
);
}
for (const value of invalidValues) {
assertIsError(
validator.safeParse(value).error,
Error,
undefined,
`Expected "${value}" to produce an error`,
);
}
assertIsError(
validator.safeParse(NOT_A_STRING as unknown as string)
.error,
Error,
undefined,
"Expected a non-string value to produce an error",
);
if (!isOptional) {
return assertIsError(
validator.safeParse(undefined).error,
Error,
undefined,
"Expected undefined to produce an error for non-optional validator",
);
}
assertFalse(
validator.safeParse(undefined).error,
"Expected undefined to be parsed successfully for optional validator",
);
});
});
}
});