forked from github/vscode-codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles.test.ts
More file actions
654 lines (573 loc) · 19.9 KB
/
files.test.ts
File metadata and controls
654 lines (573 loc) · 19.9 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import { join, parse } from "path";
import {
containsPath,
findCommonParentDir,
findDirWithFile,
gatherQlFiles,
getDirectoryNamesInsidePath,
pathsEqual,
readDirFullPaths,
walkDirectory,
} from "../../../src/common/files";
import type { DirResult } from "tmp";
import { dirSync } from "tmp";
import {
createFileSync,
ensureDirSync,
mkdirSync,
symlinkSync,
writeFileSync,
} from "fs-extra";
import "../../matchers/toEqualPath";
describe("files", () => {
const dataDir = join(__dirname, "../../data");
const data2Dir = join(__dirname, "../../data2");
describe("gatherQlFiles", () => {
it("should find one file", async () => {
const singleFile = join(dataDir, "query.ql");
const result = await gatherQlFiles([singleFile]);
expect(result).toEqual([[singleFile], false]);
});
it("should find no files", async () => {
const result = await gatherQlFiles([]);
expect(result).toEqual([[], false]);
});
it("should find no files", async () => {
const singleFile = join(dataDir, "library.qll");
const result = await gatherQlFiles([singleFile]);
expect(result).toEqual([[], false]);
});
it("should handle invalid file", async () => {
const singleFile = join(dataDir, "xxx");
const result = await gatherQlFiles([singleFile]);
expect(result).toEqual([[], false]);
});
it("should find two files", async () => {
const singleFile = join(dataDir, "query.ql");
const otherFile = join(dataDir, "multiple-result-sets.ql");
const notFile = join(dataDir, "library.qll");
const invalidFile = join(dataDir, "xxx");
const result = await gatherQlFiles([
singleFile,
otherFile,
notFile,
invalidFile,
]);
expect(result.sort()).toEqual([[singleFile, otherFile], false]);
});
it("should scan a directory", async () => {
const file1 = join(dataDir, "compute-default-strings.ql");
const file2 = join(dataDir, "debugger", "QuickEvalBigIntQuery.ql");
const file3 = join(dataDir, "debugger", "QuickEvalQuery.ql");
const file4 = join(dataDir, "debugger", "simple-query.ql");
const file5 = join(dataDir, "multiple-result-sets.ql");
const file6 = join(dataDir, "query.ql");
const vaDir = join(dataDir, "variant-analysis-query-packs");
const file7 = join(vaDir, "workspace1", "dir1", "query1.ql");
const file8 = join(vaDir, "workspace1", "pack1", "query1.ql");
const file9 = join(vaDir, "workspace1", "pack1", "query2.ql");
const file10 = join(vaDir, "workspace1", "pack2", "query1.ql");
const file11 = join(vaDir, "workspace1", "query1.ql");
const file12 = join(vaDir, "workspace2", "query1.ql");
const result = await gatherQlFiles([dataDir]);
expect(result.sort()).toEqual([
[
file1,
file2,
file3,
file4,
file5,
file6,
file7,
file8,
file9,
file10,
file11,
file12,
],
true,
]);
});
it("should scan a directory and some files", async () => {
const singleFile = join(dataDir, "query.ql");
const empty1File = join(data2Dir, "empty1.ql");
const empty2File = join(data2Dir, "sub-folder", "empty2.ql");
const result = await gatherQlFiles([singleFile, data2Dir]);
expect(result.sort()).toEqual([
[singleFile, empty1File, empty2File],
true,
]);
});
it("should avoid duplicates", async () => {
const file1 = join(dataDir, "compute-default-strings.ql");
const file2 = join(dataDir, "debugger", "QuickEvalBigIntQuery.ql");
const file3 = join(dataDir, "debugger", "QuickEvalQuery.ql");
const file4 = join(dataDir, "debugger", "simple-query.ql");
const file5 = join(dataDir, "multiple-result-sets.ql");
const file6 = join(dataDir, "query.ql");
const vaDir = join(dataDir, "variant-analysis-query-packs");
const file7 = join(vaDir, "workspace1", "dir1", "query1.ql");
const file8 = join(vaDir, "workspace1", "pack1", "query1.ql");
const file9 = join(vaDir, "workspace1", "pack1", "query2.ql");
const file10 = join(vaDir, "workspace1", "pack2", "query1.ql");
const file11 = join(vaDir, "workspace1", "query1.ql");
const file12 = join(vaDir, "workspace2", "query1.ql");
const result = await gatherQlFiles([file1, dataDir, file3, file4, file5]);
result[0].sort();
expect(result.sort()).toEqual([
[
file1,
file2,
file3,
file4,
file5,
file6,
file7,
file8,
file9,
file10,
file11,
file12,
],
true,
]);
});
});
describe("getDirectoryNamesInsidePath", () => {
it("should fail if path does not exist", async () => {
await expect(getDirectoryNamesInsidePath("xxx")).rejects.toThrow(
"Path does not exist: xxx",
);
});
it("should fail if path is not a directory", async () => {
const filePath = join(data2Dir, "empty1.ql");
await expect(getDirectoryNamesInsidePath(filePath)).rejects.toThrow(
`Path is not a directory: ${filePath}`,
);
});
it("should find sub-folders", async () => {
const result = await getDirectoryNamesInsidePath(data2Dir);
expect(result).toEqual(["sub-folder"]);
});
});
describe("readDirFullPaths", () => {
it("should return all files with full path", async () => {
const file1 = join(dataDir, "compute-default-strings.ql");
const file2 = join(dataDir, "multiple-result-sets.ql");
const file3 = join(dataDir, "query.ql");
const paths = await readDirFullPaths(dataDir);
expect(paths.some((path) => path === file1)).toBe(true);
expect(paths.some((path) => path === file2)).toBe(true);
expect(paths.some((path) => path === file3)).toBe(true);
});
});
});
describe("pathsEqual", () => {
const testCases: Array<{
path1: string;
path2: string;
platform: NodeJS.Platform;
expected: boolean;
}> = [
{
path1:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: true,
},
{
path1:
"/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: false,
},
{
path1:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"\\home\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "linux",
expected: false,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: false,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: true,
},
{
path1:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
path2:
"D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: false,
},
];
test.each(testCases)(
"$path1 and $path2 are equal on $platform = $expected",
({ path1, path2, platform, expected }) => {
if (platform !== process.platform) {
// We're using the platform-specific path.resolve, so we can't really run
// these tests on all platforms.
return;
}
expect(pathsEqual(path1, path2)).toEqual(expected);
},
);
});
describe("containsPath", () => {
const testCases: Array<{
parent: string;
child: string;
platform: NodeJS.Platform;
expected: boolean;
}> = [
{
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: true,
},
{
parent:
"/HOME/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: false,
},
{
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "linux",
expected: false,
},
{
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "linux",
expected: false,
},
{
parent:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"/home/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "linux",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"c:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: true,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"C:/USERS/GITHUB/PROJECTS/VSCODE-CODEQL-STARTER/CODEQL-CUSTOM-QUERIES-JAVASCRIPT/EXAMPLE.QL",
platform: "win32",
expected: true,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"D:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "win32",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"C:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: true,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
child:
"D:\\Users\\github\\projects\\vscode-codeql-starter\\codeql-custom-queries-javascript\\example.ql",
platform: "win32",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript",
platform: "win32",
expected: false,
},
{
parent:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-java",
child:
"C:/Users/github/projects/vscode-codeql-starter/codeql-custom-queries-javascript/example.ql",
platform: "win32",
expected: false,
},
];
test.each(testCases)(
"$parent contains $child on $platform = $expected",
({ parent, child, platform, expected }) => {
if (platform !== process.platform) {
// We're using the platform-specific path.resolve, so we can't really run
// these tests on all platforms.
return;
}
expect(containsPath(parent, child)).toEqual(expected);
},
);
});
describe("walkDirectory", () => {
let tmpDir: DirResult;
let dir: string;
let dir2: string;
beforeEach(() => {
tmpDir = dirSync({ unsafeCleanup: true });
dir = join(tmpDir.name, "dir");
ensureDirSync(dir);
dir2 = join(tmpDir.name, "dir2");
});
afterEach(() => {
tmpDir.removeCallback();
});
it("should walk a directory", async () => {
const file1 = join(dir, "file1");
const file2 = join(dir, "file2");
const file3 = join(dir, "file3");
const dir3 = join(dir, "dir3");
const file4 = join(dir, "file4");
const file5 = join(dir, "file5");
const file6 = join(dir, "file6");
// These symlinks link back to paths that are already existing, so ignore.
const symLinkFile7 = join(dir, "symlink0");
const symlinkDir = join(dir2, "symlink1");
// some symlinks that point outside of the base dir.
const file8 = join(tmpDir.name, "file8");
const file9 = join(dir2, "file8");
const symlinkDir2 = join(dir2, "symlink2");
const symlinkFile2 = join(dir2, "symlinkFile3");
ensureDirSync(dir2);
ensureDirSync(dir3);
writeFileSync(file1, "file1");
writeFileSync(file2, "file2");
writeFileSync(file3, "file3");
writeFileSync(file4, "file4");
writeFileSync(file5, "file5");
writeFileSync(file6, "file6");
writeFileSync(file8, "file8");
writeFileSync(file9, "file9");
// We don't really need to be testing all of these variants of symlinks,
// but it doesn't hurt, and will help us if we ever do decide to support them.
symlinkSync(file6, symLinkFile7, "file");
symlinkSync(dir3, symlinkDir, "dir");
symlinkSync(file8, symlinkFile2, "file");
symlinkSync(dir2, symlinkDir2, "dir");
const files = [];
for await (const file of walkDirectory(dir)) {
files.push(file);
}
// Only real files should be returned.
expect(files.sort()).toEqual([file1, file2, file3, file4, file5, file6]);
});
});
describe("findCommonParentDir", () => {
const rootDir = parse(process.cwd()).root;
it("should fail if not all paths are not absolute", async () => {
const paths = [
join("foo", "bar", "baz"),
join("/foo", "bar", "qux"),
join("/foo", "bar", "quux"),
];
expect(() => findCommonParentDir(...paths)).toThrow(
"All paths must be absolute",
);
});
it("should fail if no path are provided", async () => {
expect(() => findCommonParentDir()).toThrow(
"At least one path must be provided",
);
});
it("should find the common parent dir for multiple paths with common parent", () => {
const paths = [
join("/foo", "bar", "baz"),
join("/foo", "bar", "qux"),
join("/foo", "bar", "quux"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(join("/foo", "bar"));
});
it("should return empty path if paths have no common parent", () => {
const paths = [
join("/foo", "bar", "baz"),
join("/qux", "quux", "corge"),
join("/grault", "garply"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(rootDir);
});
it("should handle a mix of dirs and files", async () => {
const paths = [
join("/foo", "bar", "baz"),
join("/foo", "bar", "qux.ql"),
join("/foo", "bar", "quux"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(join("/foo", "bar"));
});
it("should handle dirs that have the same name", async () => {
const paths = [
join("/foo", "foo", "bar"),
join("/foo", "foo", "baz"),
join("/foo", "foo"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(join("/foo", "foo"));
});
it("should handle dirs that have the same subdir structure but different base path", async () => {
const paths = [
join("/foo", "bar"),
join("/bar", "foo", "bar"),
join("/foo", "foo", "bar"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(rootDir);
});
it("should return the same path if all paths are identical", () => {
const paths = [
join("/foo", "bar", "baz"),
join("/foo", "bar", "baz"),
join("/foo", "bar", "baz"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(join("/foo", "bar", "baz"));
});
it("should return the directory path if paths only differ by the file extension", () => {
const paths = [
join("/foo", "bar", "baz.txt"),
join("/foo", "bar", "baz.jpg"),
join("/foo", "bar", "baz.pdf"),
];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(join("/foo", "bar"));
});
it("should handle empty paths", () => {
const paths = ["/", "/", "/"];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(rootDir);
});
it("should return the parent dir of a single file", async () => {
const dataDir = join(__dirname, "../../data");
const paths = [dataDir];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(dataDir);
});
it("should return the dir if a single dir is provided", async () => {
const dataDir = join(__dirname, "../../data");
const filePath = join(dataDir, "query.ql");
const paths = [filePath];
const commonDir = findCommonParentDir(...paths);
expect(commonDir).toEqualPath(dataDir);
});
});
describe("findDirWithFile", () => {
let dir: DirResult;
beforeEach(() => {
dir = dirSync({ unsafeCleanup: true });
createFile("a");
createFile("b");
createFile("c");
createDir("dir1");
createFile("dir1", "d");
createFile("dir1", "e");
createFile("dir1", "f");
createDir("dir2");
createFile("dir2", "g");
createFile("dir2", "h");
createFile("dir2", "i");
createDir("dir2", "dir3");
createFile("dir2", "dir3", "j");
createFile("dir2", "dir3", "k");
createFile("dir2", "dir3", "l");
});
it("should find files", async () => {
expect(await findDirWithFile(dir.name, "k")).toBe(
join(dir.name, "dir2", "dir3"),
);
expect(await findDirWithFile(dir.name, "h")).toBe(join(dir.name, "dir2"));
expect(await findDirWithFile(dir.name, "z", "a")).toBe(dir.name);
// there's some slight indeterminism when more than one name exists
// but in general, this will find files in the current directory before
// finding files in sub-dirs
expect(await findDirWithFile(dir.name, "k", "a")).toBe(dir.name);
});
it("should not find files", async () => {
expect(await findDirWithFile(dir.name, "x", "y", "z")).toBeUndefined();
});
function createFile(...segments: string[]) {
createFileSync(join(dir.name, ...segments));
}
function createDir(...segments: string[]) {
mkdirSync(join(dir.name, ...segments));
}
});