Debian Codegraph @

generators/test/filepaths.test.ts

raw symbols SCIP
1 // import { filepaths, FilepathsOptions, folders } from "..";
2 import { describe, expect, it } from "vitest";
3 import { getCurrentInsertedDirectory, sortFilesAlphabetically } from "../src/filepaths";
4 // import { testQueryTerm, testTrigger } from "./keyvalue.test";
5
6 // function toName(suggestion: Fig.Suggestion): string {
7 // return suggestion.name as string;
8 // }
9
10 const defaultHome = "/home/user";
11 const defaultCwd = `${defaultHome}/current_cwd`;
12
13 const defaultContext: Fig.GeneratorContext = {
14 searchTerm: "",
15 currentWorkingDirectory: defaultCwd,
16 currentProcess: "zsh",
17 sshPrefix: "",
18 environmentVariables: {
19 HOME: defaultHome,
20 },
21 };
22
23 const defaultContextWithEnv = (env: Record<string, string>) => ({
24 ...defaultContext,
25 environmentVariables: {
26 ...defaultContext.environmentVariables,
27 ...env,
28 },
29 });
30
31 describe("Test getCurrentInsertedDirectory", () => {
32 it("returns root for null cwd", () => {
33 expect(getCurrentInsertedDirectory(null, "foo/", defaultContext)).to.equal("/");
34 });
35
36 it("returns merged path when both cwd and search term are specified", () => {
37 expect(getCurrentInsertedDirectory(defaultCwd, "test/", defaultContext)).to.equal(
38 `${defaultCwd}/test/`
39 );
40 });
41
42 it("returns partial path when trailing slash is missing (1)", () => {
43 expect(getCurrentInsertedDirectory(defaultCwd, "src/packages", defaultContext)).to.equal(
44 `${defaultCwd}/src/`
45 );
46 });
47
48 it("returns partial path when trailing slash is missing (2)", () => {
49 expect(getCurrentInsertedDirectory(defaultCwd, "src", defaultContext)).to.equal(
50 `${defaultCwd}/`
51 );
52 });
53
54 it("returns the entire search term if it is an absolute path relative to ~", () => {
55 expect(
56 getCurrentInsertedDirectory(defaultCwd, "~/some_dir/src/test/", defaultContext)
57 ).to.equal(`${defaultHome}/some_dir/src/test/`);
58 });
59
60 it("returns the entire search term if it is an absolute path relative to /", () => {
61 expect(getCurrentInsertedDirectory(defaultCwd, "/etc/bin/tool", defaultContext)).to.equal(
62 "/etc/bin/"
63 );
64 });
65
66 it("returns the path with $HOME resolved", () => {
67 expect(getCurrentInsertedDirectory(defaultCwd, "$HOME/src/test/", defaultContext)).to.equal(
68 `${defaultHome}/src/test/`
69 );
70 });
71
72 it("returns the path with $DIR resolved", () => {
73 expect(
74 getCurrentInsertedDirectory(
75 defaultCwd,
76 "$DIR/src/test/",
77 defaultContextWithEnv({
78 DIR: "/tmp/folder",
79 })
80 )
81 ).to.equal("/tmp/folder/src/test/");
82 });
83
84 it("returns the path with $DIR and $HOME resolved", () => {
85 expect(
86 getCurrentInsertedDirectory(
87 defaultCwd,
88 "$HOME/src/$DIR/test/",
89 defaultContextWithEnv({
90 DIR: "tmp/folder",
91 })
92 )
93 ).to.equal(`${defaultHome}/src/tmp/folder/test/`);
94 });
95
96 it("returns the path when just $HOME is specified", () => {
97 expect(getCurrentInsertedDirectory(defaultHome, "$HOME", defaultContext)).to.equal("/home/");
98 });
99
100 it("returns the path when $DIR is not specified", () => {
101 expect(getCurrentInsertedDirectory(defaultCwd, "$DIR", defaultContext)).to.equal(
102 `${defaultCwd}/`
103 );
104 });
105
106 it("returns the path with the complex $DIR and $HOME resolved", () => {
107 expect(
108 getCurrentInsertedDirectory(
109 defaultCwd,
110 // eslint-disable-next-line no-template-curly-in-string
111 "${HOME}/src/${DIR}/test/",
112 defaultContextWithEnv({
113 DIR: "tmp/folder",
114 })
115 )
116 ).to.equal(`${defaultHome}/src/tmp/folder/test/`);
117 });
118
119 it("returns the path with the complex $DIR and $HOME resolved with default", () => {
120 expect(
121 getCurrentInsertedDirectory(
122 defaultCwd,
123 // eslint-disable-next-line no-template-curly-in-string
124 "${HOME:-/fallback}/src/${DIR:-fallback}/test/",
125 defaultContext
126 )
127 ).to.equal(`${defaultHome}/src/fallback/test/`);
128 });
129 });
130
131 describe("Test sortFilesAlphabetically", () => {
132 const files = ["a", "e", "z", "t", "m", "b", "x"];
133
134 it("should sort files alphabetically", () => {
135 expect(sortFilesAlphabetically(files)).to.eql(["a", "b", "e", "m", "t", "x", "z", "../"]);
136 });
137
138 it("should sort files alphabetically and put dotfiles/folders at the end", () => {
139 expect(sortFilesAlphabetically([...files, ".g", ".l"])).to.eql([
140 "a",
141 "b",
142 "e",
143 "m",
144 "t",
145 "x",
146 "z",
147 ".g",
148 ".l",
149 "../",
150 ]);
151 });
152
153 it("should sort files alphabetically and skip files in the skip array", () => {
154 expect(sortFilesAlphabetically(files, ["b", "t"])).to.eql(["a", "e", "m", "x", "z", "../"]);
155 });
156 });
157
158 // describe("Test filepaths generators", () => {
159 // let currentCWD: string;
160 // let executeCommandStub: sinon.SinonStub;
161
162 // async function executeCommand(args: Fig.ExecuteCommandInput): Promise<Fig.ExecuteCommandOutput> {
163 // try {
164 // return executeCommandStub(args);
165 // } catch (err) {
166 // return new Promise((resolve) => {
167 // resolve({
168 // stdout: "",
169 // stderr: "",
170 // status: 1,
171 // });
172 // });
173 // }
174 // }
175
176 // async function runFilepaths(
177 // options: FilepathsOptions,
178 // mockLSResults: string[],
179 // context = defaultContext
180 // ): Promise<string[]> {
181 // executeCommandStub.resolves(mockLSResults.join("\n"));
182 // return (await filepaths(options).custom!([], executeCommand, context)).map(toName);
183 // }
184
185 // beforeEach(() => {
186 // executeCommandStub = sinon.stub();
187 // // these steps are approximately the ones performed by the engine before running a generator
188 // currentCWD = "~/current_cwd/";
189 // defaultContext.currentWorkingDirectory = currentCWD;
190 // });
191
192 // afterEach(() => {
193 // executeCommandStub.resetHistory();
194 // });
195
196 // after(() => {
197 // sinon.reset();
198 // });
199
200 // describe("filepaths generator", () => {
201 // describe("should trigger correctly", () => {
202 // const test = testTrigger(filepaths);
203 // test("", "", false);
204 // test("", "a", false);
205 // test("a", "aa", false);
206 // test("aa", "aa", false);
207 // test("a", "a/", true);
208 // test("a/a", "a/ab", false);
209 // test("a/ab", "a/abc", false);
210 // test("a/ab", "/ab", true);
211 // test("bar/ab", "foo/ab", true);
212 // });
213
214 // describe("should return the correct query term", () => {
215 // const test = testQueryTerm(filepaths);
216 // test("", "");
217 // test("a", "a");
218 // test("abc", "abc");
219 // test("abc/", "");
220 // test("abc/d", "d");
221 // test("~/abc", "abc");
222 // test("/abc", "abc");
223 // });
224
225 // describe("should return filepaths suggestions", () => {
226 // const suggestions = ["a/", "c/", "l", "x"];
227 // it("should show all suggestions when no options or search term is specified", async () => {
228 // executeCommandStub.resolves(suggestions.join("\n"));
229 // expect(await filepaths.custom!([], executeCommand, defaultContext)).to.eql(
230 // [
231 // { insertValue: "a/", name: "a/", type: "folder", context: { templateType: "folders" } },
232 // { insertValue: "c/", name: "c/", type: "folder", context: { templateType: "folders" } },
233 // { insertValue: "l", name: "l", type: "file", context: { templateType: "filepaths" } },
234 // { insertValue: "x", name: "x", type: "file", context: { templateType: "filepaths" } },
235 // {
236 // insertValue: "../",
237 // name: "../",
238 // type: "folder",
239 // context: { templateType: "folders" },
240 // },
241 // ].map((x) => ({ ...x, isDangerous: undefined }))
242 // );
243 // });
244 // it("should call executeCommand with specified user input dir", async () => {
245 // const results = await runFilepaths({ rootDirectory: "/etc/" }, suggestions);
246 // expect(executeCommand).to.have.been.calledWith(
247 // "cd /etc/ && command ls -1ApL | cat",
248 // undefined
249 // );
250 // expect(results).to.eql(suggestions.concat("../"));
251 // });
252
253 // it("should call executeCommand with specified user input dir and updated relative search term", async () => {
254 // const results = await runFilepaths({ rootDirectory: "/etc/" }, suggestions, {
255 // ...defaultContext,
256 // searchTerm: "bin/n",
257 // });
258 // expect(executeCommand).to.have.been.calledWith(
259 // "cd /etc/bin/ && command ls -1ApL | cat",
260 // undefined
261 // );
262 // expect(results).to.eql(suggestions.concat("../"));
263 // });
264
265 // it("should call executeCommand with specified user input dir and updated absolute search term", async () => {
266 // const results = await runFilepaths({ rootDirectory: "/etc/" }, suggestions, {
267 // ...defaultContext,
268 // searchTerm: "/etc/bin/pin",
269 // });
270
271 // expect(executeCommand).to.have.been.calledWith(
272 // "cd /etc/bin/ && command ls -1ApL | cat",
273 // undefined
274 // );
275 // expect(results).to.eql(suggestions.concat("../"));
276 // });
277 // });
278
279 // describe("should apply options correctly when they are specified", () => {
280 // it("should suggest files containing a single-dotted extension", async () => {
281 // const options: FilepathsOptions = {
282 // extensions: ["js", "mjs"],
283 // };
284 // const passing: string[] = [
285 // "file1.test.js",
286 // "file2.js",
287 // "file3.mjs",
288 // "folder1.txt/",
289 // "folder2.txt/",
290 // "folder3/",
291 // ];
292 // const failing: string[] = ["file4.ts"];
293 // const results = await runFilepaths(options, passing.concat(failing));
294 // expect(executeCommand).to.have.been.calledWith(
295 // "cd ~/current_cwd/ && command ls -1ApL | cat",
296 // undefined
297 // );
298 // expect(results).to.eql(passing.concat("../"));
299 // });
300
301 // it("should suggest files containing extensions with multiple dots", async () => {
302 // const options: FilepathsOptions = {
303 // extensions: ["test.js", "test.ts"],
304 // };
305 // const passing: string[] = [
306 // "file.detail.test.js",
307 // "file1.test.js",
308 // "file2.test.ts",
309 // "folder1.txt/",
310 // "folder2.txt/",
311 // "folder3/",
312 // ];
313 // const failing: string[] = ["file.js", "file3.test.mjs", "file4.test.mts"];
314 // const results = await runFilepaths(options, passing.concat(failing));
315 // expect(results).to.eql(passing.concat("../"));
316 // });
317
318 // describe("showFolders option", () => {
319 // it("it should always suggest folders when no `showFolders` option is provided", async () => {
320 // const options: FilepathsOptions = {};
321 // const passing: string[] = [
322 // "file1.test.js",
323 // "file2.js",
324 // "file3.mjs",
325 // "file4.ts",
326 // "folder1.txt/",
327 // "folder2.txt/",
328 // "folder3/",
329 // ];
330 // const results = await runFilepaths(options, passing);
331 // expect(results).to.eql(passing.concat("../"));
332 // });
333
334 // it("it should not suggest folders when `showFolders` is 'never'", async () => {
335 // const options: FilepathsOptions = {
336 // showFolders: "never",
337 // };
338 // const passing: string[] = ["file1.test.js", "file2.js", "file3.mjs", "file4.ts"];
339 // const failing: string[] = ["folder1.txt/", "folder2.txt/", "folder3/"];
340 // const results = await runFilepaths(options, passing.concat(failing));
341 // // NOTE: results won't have `../`
342 // expect(results).to.eql(passing);
343 // });
344
345 // it("it should suggest only folders when `showFolders` is 'only'", async () => {
346 // const options: FilepathsOptions = {
347 // showFolders: "only",
348 // };
349 // const passing: string[] = ["folder1.txt/", "folder2.txt/", "folder3/"];
350 // const failing: string[] = ["file1.test.js", "file2.js", "file3.mjs", "file4.ts"];
351 // const results = await runFilepaths(options, passing.concat(failing));
352 // expect(results).to.eql(passing.concat("../"));
353 // });
354 // });
355
356 // it("it should filter folders as if they were custom files when `filterFolders` is `true`", async () => {
357 // const options: FilepathsOptions = {
358 // filterFolders: true,
359 // equals: ["package.json", "a-folder/"],
360 // matches: /foo/g,
361 // };
362 // const passing: string[] = ["a-folder/", "foo.js", "foo/", "package.json"];
363 // const failing: string[] = ["bar.js", "a-folder.js", "some-other-folder/"];
364 // const results = await runFilepaths(options, passing.concat(failing));
365 // expect(results).to.eql(passing);
366 // });
367
368 // it("it should work correctly with 'matches'", async () => {
369 // const options: FilepathsOptions = {
370 // matches: /(file){1,2}\..*/g,
371 // };
372 // const passing: string[] = ["file.mjs", "file1/", "filefile.js", "filefile.txt/"];
373 // const failing: string[] = ["file1.js"];
374 // const results = await runFilepaths(options, passing.concat(failing));
375 // expect(results).to.eql(passing.concat("../"));
376 // });
377
378 // it("it should work correctly with 'equals'", async () => {
379 // const options: FilepathsOptions = {
380 // equals: ["foo.js", "bar.ts"],
381 // };
382 // const passing: string[] = ["bar.ts", "bar.ts/", "folder/", "foo.js", "foo.js/", "foo.ts/"];
383 // const failing: string[] = ["foo.ts", "foo.js.ts"];
384 // const results = await runFilepaths(options, passing.concat(failing));
385 // expect(results).to.eql(passing.concat("../"));
386 // });
387 // });
388
389 // it("it should sort suggestions returned form the `ls` command executed", async () => {
390 // const options: FilepathsOptions = {};
391 // const passing: string[] = ["c/", "b/", "a/"];
392 // const expected: string[] = ["a/", "b/", "c/"];
393 // const results = await runFilepaths(options, passing);
394 // expect(results).to.eql(expected.concat("../"));
395 // });
396
397 // describe("deprecated sshPrefix", () => {
398 // it("should call executeCommand with default user input dir ignoring ssh", async () => {
399 // await filepaths.custom!([], executeCommand, {
400 // ...defaultContext,
401 // sshPrefix: "ssh -i blabla",
402 // });
403
404 // expect(executeCommand).to.have.been.calledWith(
405 // "cd ~/current_cwd/ && command ls -1ApL | cat",
406 // undefined
407 // );
408 // });
409 // });
410 // });
411
412 // describe("folders generator", () => {
413 // it("should suggest only folders and no filepaths", async () => {
414 // const passing: string[] = ["folder1.txt/", "folder2.txt/", "folder3/"];
415 // const failing: string[] = ["file1.test.js", "file2.js", "file3.mjs", "file4.ts"];
416
417 // executeCommandStub.resolves(passing.concat(failing).join("\n"));
418 // const results = (await folders().custom!([], executeCommand, defaultContext)).map(toName);
419
420 // expect(results).to.eql(passing.concat("../"));
421 // });
422 // });
423 // });