-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcompletions.mjs
215 lines (188 loc) · 6.56 KB
/
completions.mjs
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
//@ts-check
import { expect } from "chai";
import { match, stub } from "sinon";
import { run } from "../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../src/config/setup-test-container.mjs";
import { validDefaultConfigNames } from "../src/lib/config/config.mjs";
import { eventually, mockAccessKeysFile } from "./helpers.mjs";
const realLogger = console.log; // eslint-disable-line no-console
/**
* @param {object} args
* @prop {string} [configPath]
* @prop {string} [matchFlag = ""] - the option/argument/flag before the substring to generate completions for. in `fauna query --profile hello`, the matchFlag is `profile`; do not include the leading `--`.
* @prop {string} [matchSubstring = ""] - the substring generate completions for. in `fauna query --profile hello`, the matchSubstring is `hello`.
* @prop {string} [command]
* @prop {Record<string, string>} [env]
* @prop {any} container
*/
async function complete({
configPath,
matchFlag = "",
matchSubstring = "",
container,
command,
env,
}) {
// to test these manually in zsh/bash, do:
// `fauna --get-yargs-completions fauna query --database "us-std/stringToComplete"`
let commandString = `fauna --get-yargs-completions fauna`;
if (command) commandString += ` ${command}`;
if (configPath) commandString += ` --config ${configPath}`;
commandString += ` --${matchFlag} ${matchSubstring}`;
process.argv = commandString.split(" ");
if (env) {
for (const [key, value] of Object.entries(env)) {
process.env[key] = value;
}
}
await run(commandString.split(" "), container);
}
const basicConfig = {
default: {},
dev: {},
prod: {},
};
const advancedConfig = {
development: {},
production: {},
};
const defaultNameConfig = {
plain: {},
boring: {},
};
describe("shell completion", () => {
let container, fs, fakeLogger;
beforeEach(() => {
// reset the container before each test
container = setupContainer();
fakeLogger = stub();
console.log = fakeLogger; // eslint-disable-line no-console
fs = container.resolve("fs");
});
after(() => {
console.log = realLogger; // eslint-disable-line no-console
});
describe("for profiles", () => {
beforeEach(() => {
fs.readdirSync.withArgs(match.any).returns([
{
isFile: () => true,
name: validDefaultConfigNames[0],
path: process.cwd(),
parentParth: process.cwd(),
},
]);
fs.readFileSync
.withArgs("/config/basic.yaml")
.returns(JSON.stringify(basicConfig));
fs.readFileSync
.withArgs("/config/advanced.yaml")
.returns(JSON.stringify(advancedConfig));
fs.readFileSync
.withArgs(validDefaultConfigNames[0])
.returns(JSON.stringify(defaultNameConfig));
});
it("works with config files in the same directory with default names", async () => {
await complete({
container,
matchFlag: "profile",
});
expect(fakeLogger).to.have.been.calledWith("plain");
expect(fakeLogger).to.have.been.calledWith("boring");
});
it("works with config files chosen by flag or env var", async () => {
await complete({
container,
matchFlag: "profile",
env: {
FAUNA_CONFIG: "/config/basic.yaml",
},
});
expect(fakeLogger).to.have.been.calledWith("default");
expect(fakeLogger).to.have.been.calledWith("dev");
expect(fakeLogger).to.have.been.calledWith("prod");
});
it("prioritizes config file paths provided by flag over env vars", async () => {
await complete({
container,
matchFlag: "profile",
env: {
FAUNA_CONFIG: "/config/basic.yaml",
},
configPath: "/config/advanced.yaml",
});
expect(fakeLogger).to.have.been.calledWith("development");
expect(fakeLogger).to.have.been.calledWith("production");
});
it.skip("is resilient against wrapping quotes", async () => {});
});
describe("for databases", () => {
beforeEach(() => {
// reset the container before each test
container = setupContainer();
fs = container.resolve("fs");
fs.readdirSync.withArgs(match.any).returns([
{
isFile: () => true,
name: validDefaultConfigNames[0],
path: process.cwd(),
parentParth: process.cwd(),
},
]);
fs.readFileSync
.withArgs("/config/basic.yaml")
.returns(JSON.stringify(basicConfig));
mockAccessKeysFile({ fs });
const { listDatabases } = container.resolve("accountAPI");
const stubbedResponse = { results: [{ name: "americacentric" }] };
listDatabases
.withArgs(match({ path: "us-std" }))
.resolves(stubbedResponse);
});
it("suggests a region group if the current word doesn't start with a region group", async () => {
await complete({
container,
matchFlag: "database",
command: "query",
});
expect(fakeLogger).to.have.been.calledWith("eu-std");
expect(fakeLogger).to.have.been.calledWith("us-std");
expect(fakeLogger).to.have.been.calledWith("global");
});
it("suggests a top level database in the selected region group", async () => {
const { listDatabases } = container.resolve("accountAPI");
listDatabases.withArgs(match({ path: "eu-std" })).resolves({
results: [{ name: "eurocentric" }],
});
await complete({
container,
matchFlag: "database",
matchSubstring: "eu-std/",
command: "query",
});
await eventually(() => {
expect(fakeLogger).to.have.been.calledWith("eu-std/eurocentric");
});
});
it("suggests a nested level database in the selected region group", async () => {
const { listDatabases } = container.resolve("accountAPI");
listDatabases.withArgs(match({ path: "eu-std/a/b/c/d" })).resolves({
results: [{ name: "1" }, { name: "2" }, { name: "3" }, { name: "4" }],
});
await complete({
container,
matchFlag: "database",
matchSubstring: "eu-std/a/b/c/d",
command: "query",
});
await eventually(() => {
expect(fakeLogger).to.have.been.calledWith("eu-std/a/b/c/d/1");
expect(fakeLogger).to.have.been.calledWith("eu-std/a/b/c/d/2");
expect(fakeLogger).to.have.been.calledWith("eu-std/a/b/c/d/3");
expect(fakeLogger).to.have.been.calledWith("eu-std/a/b/c/d/4");
});
});
it.skip("is resilient against trailing slashes", async () => {});
it.skip("is resilient against wrapping quotes", async () => {});
});
});