-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconf_test.ts
129 lines (117 loc) · 4.12 KB
/
conf_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
import {
assert,
assertEquals,
assertObjectMatch,
assertThrows,
} from "jsr:@std/assert@^1.0.0";
import { stub } from "jsr:@std/testing@^1.0.0/mock";
import { basename, isAbsolute } from "jsr:@std/path@^1.0.1";
import { _internal, getConfig } from "./conf.ts";
const ENV_VARS: Readonly<Record<string, string | undefined>> = {
DENOPS_TEST_DENOPS_PATH: "denops.vim",
DENOPS_TEST_VIM_EXECUTABLE: undefined,
DENOPS_TEST_NVIM_EXECUTABLE: undefined,
DENOPS_TEST_VERBOSE: undefined,
DENOPS_TEST_CONNECT_TIMEOUT: undefined,
};
function stubEnvVars(envVars: Readonly<Record<string, string | undefined>>) {
return stub(Deno.env, "get", (name) => envVars[name]);
}
function stubConfModule(): Disposable {
const savedConf = getConfig();
_internal.resetConfig(undefined);
return {
[Symbol.dispose]() {
_internal.resetConfig(savedConf);
},
};
}
Deno.test("getConfig() throws if DENOPS_TEST_DENOPS_PATH env var is not set", () => {
using _module = stubConfModule();
using _env = stubEnvVars({ ...ENV_VARS, DENOPS_TEST_DENOPS_PATH: undefined });
assertThrows(
() => {
getConfig();
},
Error,
"'DENOPS_TEST_DENOPS_PATH' is required",
);
});
Deno.test("getConfig() returns `{ denopsPath: ... }` with resolved DENOPS_TEST_DENOPS_PATH env var", () => {
using _module = stubConfModule();
using _env = stubEnvVars({ ...ENV_VARS, DENOPS_TEST_DENOPS_PATH: "foo" });
const actual = getConfig();
assert(isAbsolute(actual.denopsPath), "`denopsPath` should be absolute path");
assertEquals(basename(actual.denopsPath), "foo");
});
Deno.test("getConfig() returns `{ vimExecutable: 'vim' }` if DENOPS_TEST_VIM_EXECUTABLE env var is not set", () => {
using _module = stubConfModule();
using _env = stubEnvVars({
...ENV_VARS,
DENOPS_TEST_VIM_EXECUTABLE: undefined,
});
const actual = getConfig();
assertObjectMatch(actual, { vimExecutable: "vim" });
});
Deno.test("getConfig() returns `{ vimExecutable: ... }` with DENOPS_TEST_VIM_EXECUTABLE env var", () => {
using _module = stubConfModule();
using _env = stubEnvVars({ ...ENV_VARS, DENOPS_TEST_VIM_EXECUTABLE: "foo" });
const actual = getConfig();
assertObjectMatch(actual, { vimExecutable: "foo" });
});
Deno.test("getConfig() returns `{ nvimExecutable: 'nvim' }` if DENOPS_TEST_NVIM_EXECUTABLE env var is not set", () => {
using _module = stubConfModule();
using _env = stubEnvVars({
...ENV_VARS,
DENOPS_TEST_NVIM_EXECUTABLE: undefined,
});
const actual = getConfig();
assertObjectMatch(actual, { nvimExecutable: "nvim" });
});
Deno.test("getConfig() returns `{ nvimExecutable: ... }` with DENOPS_TEST_NVIM_EXECUTABLE env var", () => {
using _module = stubConfModule();
using _env = stubEnvVars({ ...ENV_VARS, DENOPS_TEST_NVIM_EXECUTABLE: "foo" });
const actual = getConfig();
assertObjectMatch(actual, { nvimExecutable: "foo" });
});
Deno.test("getConfig() returns `{ verbose: false }` if DENOPS_TEST_VERBOSE env var is not set", () => {
using _module = stubConfModule();
using _env = stubEnvVars({ ...ENV_VARS, DENOPS_TEST_VERBOSE: undefined });
const actual = getConfig();
assertObjectMatch(actual, { verbose: false });
});
for (
const [input, expected] of [
["false", false],
["0", false],
["invalid", false],
["true", true],
["1", true],
] as const
) {
Deno.test(`getConfig() returns \`{ verbose: ${expected} }\` if DENOPS_TEST_VERBOSE env var is '${input}'`, () => {
using _module = stubConfModule();
using _env = stubEnvVars({ ...ENV_VARS, DENOPS_TEST_VERBOSE: input });
const actual = getConfig();
assertObjectMatch(actual, { verbose: expected });
});
}
for (
const [input, expected] of [
["123", 123],
["123.456", 123],
["0", undefined],
["-123", undefined],
["string", undefined],
] as const
) {
Deno.test(`getConfig() returns \`{ connectTimeout: ${expected} }\` if DENOPS_TEST_CONNECT_TIMEOUT env var is '${input}'`, () => {
using _module = stubConfModule();
using _env = stubEnvVars({
...ENV_VARS,
DENOPS_TEST_CONNECT_TIMEOUT: input,
});
const actual = getConfig();
assertObjectMatch(actual, { connectTimeout: expected });
});
}