forked from actions/toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal-globber.test.ts
917 lines (838 loc) · 28.8 KB
/
internal-globber.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
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
import * as child from 'child_process'
import * as io from '../../io/src/io'
import * as os from 'os'
import * as path from 'path'
import {Globber, DefaultGlobber} from '../src/internal-globber'
import {GlobOptions} from '../src/internal-glob-options'
import {promises as fs} from 'fs'
const IS_WINDOWS = process.platform === 'win32'
/**
* These test focus on the ability of globber to find files
* and not on the pattern matching aspect
*/
describe('globber', () => {
beforeAll(async () => {
await io.rmRF(getTestTemp())
})
it('captures cwd', async () => {
// Create the following layout:
// first-cwd
// first-cwd/the-correct-file
// second-cwd
// second-cwd/the-wrong-file
const root = path.join(getTestTemp(), 'preserves-cwd')
await fs.mkdir(path.join(root, 'first-cwd'), {recursive: true})
await fs.writeFile(
path.join(root, 'first-cwd', 'the-correct-file.txt'),
'test file content'
)
await fs.mkdir(path.join(root, 'second-cwd'), {recursive: true})
await fs.writeFile(
path.join(root, 'second-cwd', 'the-wrong-file.txt'),
'test file content'
)
const originalCwd = process.cwd()
try {
process.chdir(path.join(root, 'first-cwd'))
const globber = await DefaultGlobber.create('*')
process.chdir(path.join(root, 'second-cwd'))
expect(globber.getSearchPaths()).toEqual([path.join(root, 'first-cwd')])
const itemPaths = await globber.glob()
expect(itemPaths).toEqual([
path.join(root, 'first-cwd', 'the-correct-file.txt')
])
} finally {
process.chdir(originalCwd)
}
})
it('defaults to followSymbolicLinks=true', async () => {
// Create the following layout:
// <root>
// <root>/folder-a
// <root>/folder-a/file
// <root>/symDir -> <root>/folder-a
const root = path.join(
getTestTemp(),
'defaults-to-follow-symbolic-links-true'
)
await fs.mkdir(path.join(root, 'folder-a'), {recursive: true})
await fs.writeFile(path.join(root, 'folder-a', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'folder-a'),
path.join(root, 'symDir')
)
const itemPaths = await glob(root, {})
expect(itemPaths).toEqual([
root,
path.join(root, 'folder-a'),
path.join(root, 'folder-a', 'file'),
path.join(root, 'symDir'),
path.join(root, 'symDir', 'file')
])
})
it('defaults to implicitDescendants=true', async () => {
// Create the following layout:
// <root>
// <root>/folder-a
// <root>/folder-a/file
const root = path.join(
getTestTemp(),
'defaults-to-implicit-descendants-true'
)
await fs.mkdir(path.join(root, 'folder-a'), {recursive: true})
await fs.writeFile(path.join(root, 'folder-a', 'file'), 'test file content')
const itemPaths = await glob(root, {})
expect(itemPaths).toEqual([
root,
path.join(root, 'folder-a'),
path.join(root, 'folder-a', 'file')
])
})
it('defaults to matchDirectories=true', async () => {
// Create the following layout:
// <root>
// <root>/folder-a
// <root>/folder-a/file
const root = path.join(getTestTemp(), 'defaults-to-match-directories-true')
await fs.mkdir(path.join(root, 'folder-a'), {recursive: true})
await fs.writeFile(path.join(root, 'folder-a', 'file'), 'test file content')
const itemPaths = await glob(root, {})
expect(itemPaths).toEqual([
root,
path.join(root, 'folder-a'),
path.join(root, 'folder-a', 'file')
])
})
it('does not match file with trailing slash when implicitDescendants=true', async () => {
// Create the following layout:
// <root>
// <root>/file
const root = path.join(
getTestTemp(),
'defaults-to-implicit-descendants-true'
)
const filePath = path.join(root, 'file')
await fs.mkdir(root, {recursive: true})
await fs.writeFile(filePath, 'test file content')
const itemPaths = await glob(`${filePath}/`, {})
expect(itemPaths).toEqual([])
})
it('defaults to omitBrokenSymbolicLinks=true', async () => {
// Create the following layout:
// <root>
// <root>/folder-a
// <root>/folder-a/file
// <root>/symDir -> <root>/no-such
const root = path.join(
getTestTemp(),
'defaults-to-omit-broken-symbolic-links-true'
)
await fs.mkdir(path.join(root, 'folder-a'), {recursive: true})
await fs.writeFile(path.join(root, 'folder-a', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'no-such'),
path.join(root, 'symDir')
)
const itemPaths = await glob(root, {})
expect(itemPaths).toEqual([
root,
path.join(root, 'folder-a'),
path.join(root, 'folder-a', 'file')
])
})
it('detects cycle when followSymbolicLinks=true', async () => {
// Create the following layout:
// <root>
// <root>/file
// <root>/symDir -> <root>
const root = path.join(getTestTemp(), 'detects-cycle-when-follow-true')
await fs.mkdir(root, {recursive: true})
await fs.writeFile(path.join(root, 'file'), 'test file content')
await createSymlinkDir(root, path.join(root, 'symDir'))
const itemPaths = await glob(root, {followSymbolicLinks: true})
expect(itemPaths).toEqual([root, path.join(root, 'file')])
})
it('detects deep cycle starting from middle when followSymbolicLinks=true', async () => {
// Create the following layout:
// <root>
// <root>/file-under-root
// <root>/folder-a
// <root>/folder-a/file-under-a
// <root>/folder-a/folder-b
// <root>/folder-a/folder-b/file-under-b
// <root>/folder-a/folder-b/folder-c
// <root>/folder-a/folder-b/folder-c/file-under-c
// <root>/folder-a/folder-b/folder-c/sym-folder -> <root>
const root = path.join(
getTestTemp(),
'detects-deep-cycle-starting-from-middle-when-follow-true'
)
await fs.mkdir(path.join(root, 'folder-a', 'folder-b', 'folder-c'), {
recursive: true
})
await fs.writeFile(
path.join(root, 'file-under-root'),
'test file under root contents'
)
await fs.writeFile(
path.join(root, 'folder-a', 'file-under-a'),
'test file under a contents'
)
await fs.writeFile(
path.join(root, 'folder-a', 'folder-b', 'file-under-b'),
'test file under b contents'
)
await fs.writeFile(
path.join(root, 'folder-a', 'folder-b', 'folder-c', 'file-under-c'),
'test file under c contents'
)
await createSymlinkDir(
root,
path.join(root, 'folder-a', 'folder-b', 'folder-c', 'sym-folder')
)
await fs.stat(
path.join(
root,
'folder-a',
'folder-b',
'folder-c',
'sym-folder',
'file-under-root'
)
)
const itemPaths = await glob(path.join(root, 'folder-a', 'folder-b'), {
followSymbolicLinks: true
})
expect(itemPaths).toEqual([
path.join(root, 'folder-a', 'folder-b'),
path.join(root, 'folder-a', 'folder-b', 'file-under-b'),
path.join(root, 'folder-a', 'folder-b', 'folder-c'),
path.join(root, 'folder-a', 'folder-b', 'folder-c', 'file-under-c'),
path.join(root, 'folder-a', 'folder-b', 'folder-c', 'sym-folder'),
path.join(
root,
'folder-a',
'folder-b',
'folder-c',
'sym-folder',
'file-under-root'
),
path.join(
root,
'folder-a',
'folder-b',
'folder-c',
'sym-folder',
'folder-a'
),
path.join(
root,
'folder-a',
'folder-b',
'folder-c',
'sym-folder',
'folder-a',
'file-under-a'
)
])
await unlinkSymlinkDir(itemPaths)
})
it('detects cycle starting from symlink when followSymbolicLinks=true', async () => {
// Create the following layout:
// <root>
// <root>/file
// <root>/symDir -> <root>
const root: string = path.join(
getTestTemp(),
'detects-cycle-starting-from-symlink-when-follow-true'
)
await fs.mkdir(root, {recursive: true})
await fs.writeFile(path.join(root, 'file'), 'test file content')
await createSymlinkDir(root, path.join(root, 'symDir'))
await fs.stat(path.join(root, 'symDir'))
const itemPaths = await glob(path.join(root, 'symDir'), {
followSymbolicLinks: true
})
expect(itemPaths).toEqual([
path.join(root, 'symDir'),
path.join(root, 'symDir', 'file')
])
})
it('does not follow symlink when followSymbolicLinks=false', async () => {
// Create the following layout:
// <root>
// <root>/realDir
// <root>/realDir/file
// <root>/symDir -> <root>/realDir
const root = path.join(
getTestTemp(),
'does-not-follow-symlink-when-follow-false'
)
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'realDir'),
path.join(root, 'symDir')
)
const itemPaths = await glob(root, {followSymbolicLinks: false})
expect(itemPaths).toEqual([
root,
path.join(root, 'realDir'),
path.join(root, 'realDir', 'file'),
path.join(root, 'symDir')
])
})
it('does not follow symlink when search path is symlink and followSymbolicLinks=false', async () => {
// Create the following layout:
// realDir
// realDir/file
// symDir -> realDir
const root = path.join(
getTestTemp(),
'does-not-follow-symlink-when-search-path-is-symlink-and-follow-false'
)
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'realDir'),
path.join(root, 'symDir')
)
const itemPaths = await glob(path.join(root, 'symDir'), {
followSymbolicLinks: false
})
expect(itemPaths).toEqual([path.join(root, 'symDir')])
})
it('does not return broken symlink when follow-true and omit-true', async () => {
// Create the following layout:
// <root>
// <root>/brokenSym -> <root>/noSuch
// <root>/realDir
// <root>/realDir/file
// <root>/symDir -> <root>/realDir
const root = path.join(
getTestTemp(),
'does-not-return-broken-symlink-when-follow-true-and-omit-true'
)
await fs.mkdir(root, {recursive: true})
await createSymlinkDir(
path.join(root, 'noSuch'),
path.join(root, 'brokenSym')
)
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'realDir'),
path.join(root, 'symDir')
)
const itemPaths = await glob(root, {followSymbolicLinks: true})
expect(itemPaths).toEqual([
root,
path.join(root, 'realDir'),
path.join(root, 'realDir', 'file'),
path.join(root, 'symDir'),
path.join(root, 'symDir', 'file')
])
})
it('does not return broken symlink when search path is broken symlink and followSymbolicLinks=true', async () => {
// Create the following layout:
// <root>
// <root>/brokenSym -> <root>/noSuch
const root = path.join(
getTestTemp(),
'does-not-return-broken-symlink-when-search-path-is-broken-symlink-and-follow-true'
)
await fs.mkdir(root, {recursive: true})
const brokenSymPath = path.join(root, 'brokenSym')
await createSymlinkDir(path.join(root, 'noSuch'), brokenSymPath)
await fs.lstat(brokenSymPath)
const itemPaths = await glob(brokenSymPath, {followSymbolicLinks: true})
expect(itemPaths).toEqual([])
})
it('does not return directories when match directories false', async () => {
// Create the following layout:
// <root>/file-1
// <root>/dir-1
// <root>/dir-1/file-2
// <root>/dir-1/dir-2
// <root>/dir-1/dir-2/file-3
const root = path.join(
getTestTemp(),
'does-not-return-directories-when-match-directories-false'
)
await fs.mkdir(path.join(root, 'dir-1', 'dir-2'), {recursive: true})
await fs.writeFile(path.join(root, 'file-1'), '')
await fs.writeFile(path.join(root, 'dir-1', 'file-2'), '')
await fs.writeFile(path.join(root, 'dir-1', 'dir-2', 'file-3'), '')
const pattern = `${root}${path.sep}**`
expect(
await glob(pattern, {
matchDirectories: false
})
).toEqual([
path.join(root, 'dir-1', 'dir-2', 'file-3'),
path.join(root, 'dir-1', 'file-2'),
path.join(root, 'file-1')
])
})
it('does not search paths that are not partial matches', async () => {
// Create the following layout:
// <root>
// <root>/realDir
// <root>/realDir/nested
// <root>/realDir/nested/file
// <root>/realDir2
// <root>/realDir2/nested2
// <root>/realDir2/nested2/symDir -> <root>/noSuch
const root = path.join(
getTestTemp(),
'does-not-search-paths-that-are-not-partial-matches'
)
await fs.mkdir(path.join(root, 'realDir', 'nested'), {recursive: true})
await fs.writeFile(
path.join(root, 'realDir', 'nested', 'file'),
'test file content'
)
await fs.mkdir(path.join(root, 'realDir2', 'nested2'), {recursive: true})
await createSymlinkDir(
path.join(root, 'noSuch'),
path.join(root, 'realDir2', 'nested2', 'symDir')
)
const options: GlobOptions = {
followSymbolicLinks: true,
omitBrokenSymbolicLinks: false
}
// Should throw
try {
await glob(`${root}/*Dir*/*nested*/*`, options)
throw new Error('should not reach here')
} catch (err) {
expect(err.message).toMatch(/broken symbolic link/i)
}
// Not partial match
let itemPaths = await glob(`${root}/*Dir/*nested*/*`, options)
expect(itemPaths).toEqual([path.join(root, 'realDir', 'nested', 'file')])
// Not partial match
itemPaths = await glob(`${root}/*Dir*/*nested/*`, options)
expect(itemPaths).toEqual([path.join(root, 'realDir', 'nested', 'file')])
})
it('does not throw for broken symlinks that are not matches or partial matches when followSymbolicLinks=true and omitBrokenSymbolicLinks=false', async () => {
// Create the following layout:
// <root>
// <root>/realDir
// <root>/realDir/file
// <root>/symDir -> <root>/noSuch
const root = path.join(
getTestTemp(),
'does-not-throw-for-broken-symlinks-that-are-not-matches-or-partial-matches-when-follow-true-and-omit-false'
)
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(path.join(root, 'noSuch'), path.join(root, 'symDir'))
const options: GlobOptions = {
followSymbolicLinks: true,
omitBrokenSymbolicLinks: false
}
// Match should throw
try {
await glob(`${root}/*`, options)
throw new Error('should not reach here')
} catch (err) {
expect(err.message).toMatch(/broken symbolic link/i)
}
// Partial match should throw
try {
await glob(`${root}/*/*`, options)
throw new Error('should not reach here')
} catch (err) {
expect(err.message).toMatch(/broken symbolic link/i)
}
// Not match or partial match
const itemPaths = await glob(`${root}/*eal*/*`, options)
expect(itemPaths).toEqual([path.join(root, 'realDir', 'file')])
})
it('follows symlink when follow-symbolic-links=true', async () => {
// Create the following layout:
// <root>
// <root>/realDir
// <root>/realDir/file
// <root>/symDir -> <root>/realDir
const root = path.join(getTestTemp(), 'follows-symlink')
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'realDir'),
path.join(root, 'symDir')
)
const itemPaths = await glob(root, {followSymbolicLinks: true})
expect(itemPaths).toEqual([
root,
path.join(root, 'realDir'),
path.join(root, 'realDir', 'file'),
path.join(root, 'symDir'),
path.join(root, 'symDir', 'file')
])
})
it('follows symlink when search path is symlink and follow-symbolic-links=true', async () => {
// Create the following layout:
// realDir
// realDir/file
// symDir -> realDir
const root = path.join(
getTestTemp(),
'follows-symlink-when-search-path-is-symlink-and-follow-true'
)
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'realDir'),
path.join(root, 'symDir')
)
const itemPaths = await glob(path.join(root, 'symDir'), {
followSymbolicLinks: true
})
expect(itemPaths).toEqual([
path.join(root, 'symDir'),
path.join(root, 'symDir', 'file')
])
})
it('returns broken symlink when followSymbolicLinks=false', async () => {
// Create the following layout:
// <root>
// <root>/brokenSym -> <root>/noSuch
// <root>/realDir
// <root>/realDir/file
// <root>/symDir -> <root>/realDir
const root = path.join(
getTestTemp(),
'returns-broken-symlink-when-follow-false'
)
await fs.mkdir(root, {recursive: true})
await createSymlinkDir(
path.join(root, 'noSuch'),
path.join(root, 'brokenSym')
)
await fs.mkdir(path.join(root, 'realDir'), {recursive: true})
await fs.writeFile(path.join(root, 'realDir', 'file'), 'test file content')
await createSymlinkDir(
path.join(root, 'realDir'),
path.join(root, 'symDir')
)
const itemPaths = await glob(root, {followSymbolicLinks: false})
expect(itemPaths).toEqual([
root,
path.join(root, 'brokenSym'),
path.join(root, 'realDir'),
path.join(root, 'realDir', 'file'),
path.join(root, 'symDir')
])
})
it('returns broken symlink when search path is broken symlink and followSymbolicLinks=false', async () => {
// Create the following layout:
// <root>
// <root>/brokenSym -> <root>/noSuch
const root = path.join(
getTestTemp(),
'returns-broken-symlink-when-search-path-is-broken-symlink-and-follow-false'
)
await fs.mkdir(root, {recursive: true})
const brokenSymPath = path.join(root, 'brokenSym')
await createSymlinkDir(path.join(root, 'noSuch'), brokenSymPath)
const itemPaths = await glob(brokenSymPath, {followSymbolicLinks: false})
expect(itemPaths).toEqual([brokenSymPath])
})
it('returns depth first', async () => {
// Create the following layout:
// <root>/a-file
// <root>/b-folder
// <root>/b-folder/a-file
// <root>/b-folder/b-folder
// <root>/b-folder/b-folder/file
// <root>/b-folder/c-file
// <root>/c-file
const root = path.join(getTestTemp(), 'returns-depth-first')
await fs.mkdir(path.join(root, 'b-folder', 'b-folder'), {recursive: true})
await fs.writeFile(path.join(root, 'a-file'), 'test a-file content')
await fs.writeFile(
path.join(root, 'b-folder', 'a-file'),
'test b-folder/a-file content'
)
await fs.writeFile(
path.join(root, 'b-folder', 'b-folder', 'file'),
'test b-folder/b-folder/file content'
)
await fs.writeFile(
path.join(root, 'b-folder', 'c-file'),
'test b-folder/c-file content'
)
await fs.writeFile(path.join(root, 'c-file'), 'test c-file content')
const itemPaths = await glob(root)
expect(itemPaths).toEqual([
root,
path.join(root, 'a-file'),
path.join(root, 'b-folder'),
path.join(root, 'b-folder', 'a-file'),
path.join(root, 'b-folder', 'b-folder'),
path.join(root, 'b-folder', 'b-folder', 'file'),
path.join(root, 'b-folder', 'c-file'),
path.join(root, 'c-file')
])
})
it('returns descendants', async () => {
// Create the following layout:
// <root>/file-1
// <root>/dir-1
// <root>/dir-1/file-2
// <root>/dir-1/dir-2
// <root>/dir-1/dir-2/file-3
const root = path.join(getTestTemp(), 'returns-descendants')
await fs.mkdir(path.join(root, 'dir-1', 'dir-2'), {recursive: true})
await fs.writeFile(path.join(root, 'file-1'), '')
await fs.writeFile(path.join(root, 'dir-1', 'file-2'), '')
await fs.writeFile(path.join(root, 'dir-1', 'dir-2', 'file-3'), '')
// When pattern ends with `/**/`
let pattern = `${root}${path.sep}**${path.sep}`
expect(
await glob(pattern, {
implicitDescendants: false
})
).toHaveLength(3) // sanity check
expect(await glob(pattern)).toEqual([
root,
path.join(root, 'dir-1'),
path.join(root, 'dir-1', 'dir-2'),
path.join(root, 'dir-1', 'dir-2', 'file-3'),
path.join(root, 'dir-1', 'file-2'),
path.join(root, 'file-1')
])
// When pattern ends with something other than `/**/`
pattern = `${root}${path.sep}**${path.sep}dir-?`
expect(
await glob(pattern, {
implicitDescendants: false
})
).toHaveLength(2) // sanity check
expect(await glob(pattern)).toEqual([
path.join(root, 'dir-1'),
path.join(root, 'dir-1', 'dir-2'),
path.join(root, 'dir-1', 'dir-2', 'file-3'),
path.join(root, 'dir-1', 'file-2')
])
})
it('returns directories only when trailing slash and implicit descendants false', async () => {
// Create the following layout:
// <root>/file-1
// <root>/dir-1
// <root>/dir-1/file-2
// <root>/dir-1/dir-2
// <root>/dir-1/dir-2/file-3
const root = path.join(
getTestTemp(),
'returns-directories-only-when-trailing-slash-and-implicit-descendants-false'
)
await fs.mkdir(path.join(root, 'dir-1', 'dir-2'), {recursive: true})
await fs.writeFile(path.join(root, 'file-1'), '')
await fs.writeFile(path.join(root, 'dir-1', 'file-2'), '')
await fs.writeFile(path.join(root, 'dir-1', 'dir-2', 'file-3'), '')
const pattern = `${root}${path.sep}**${path.sep}`
expect(await glob(pattern)).toHaveLength(6) // sanity check
expect(
await glob(pattern, {
implicitDescendants: false
})
).toEqual([
root,
path.join(root, 'dir-1'),
path.join(root, 'dir-1', 'dir-2')
])
})
it('returns empty when search path does not exist', async () => {
const itemPaths = await glob(path.join(getTestTemp(), 'nosuch'))
expect(itemPaths).toEqual([])
})
it('returns hidden files by default', async () => {
// Create the following layout:
// <root>
// <root>/.emptyFolder
// <root>/.file
// <root>/.folder
// <root>/.folder/file
const root = path.join(getTestTemp(), 'returns-hidden-files')
await createHiddenDirectory(path.join(root, '.emptyFolder'))
await createHiddenDirectory(path.join(root, '.folder'))
await createHiddenFile(path.join(root, '.file'), 'test .file content')
await fs.writeFile(
path.join(root, '.folder', 'file'),
'test .folder/file content'
)
const itemPaths = await glob(root)
expect(itemPaths).toEqual([
root,
path.join(root, '.emptyFolder'),
path.join(root, '.file'),
path.join(root, '.folder'),
path.join(root, '.folder', 'file')
])
})
it('ignores hidden files when excludeHiddenFiles is set', async () => {
// Create the following layout:
// <root>
// <root>/.emptyFolder
// <root>/.file
// <root>/.folder
// <root>/.folder/file
const root = path.join(getTestTemp(), 'ignores-hidden-files')
await createHiddenDirectory(path.join(root, '.emptyFolder'))
await createHiddenDirectory(path.join(root, '.folder'))
await createHiddenFile(path.join(root, '.file'), 'test .file content')
await fs.writeFile(
path.join(root, '.folder', 'file'),
'test .folder/file content'
)
const itemPaths = await glob(root, {excludeHiddenFiles: true})
expect(itemPaths).toEqual([root])
})
it('returns normalized paths', async () => {
// Create the following layout:
// <root>/hello/world.txt
const root: string = path.join(getTestTemp(), 'returns-normalized-paths')
await fs.mkdir(path.join(root, 'hello'), {recursive: true})
await fs.writeFile(path.join(root, 'hello', 'world.txt'), '')
const itemPaths = await glob(
`${root}${path.sep}${path.sep}${path.sep}hello`
)
expect(itemPaths).toEqual([
path.join(root, 'hello'),
path.join(root, 'hello', 'world.txt')
])
})
it('skips comments', async () => {
const searchPaths = await getSearchPaths(
`#aaa/*${os.EOL}/foo/*${os.EOL}#bbb/*${os.EOL}/bar/*`
)
const drive = IS_WINDOWS ? process.cwd().substr(0, 2) : ''
expect(searchPaths).toEqual([
IS_WINDOWS ? `${drive}\\foo` : '/foo',
IS_WINDOWS ? `${drive}\\bar` : '/bar'
])
})
it('skips empty lines', async () => {
const searchPaths = await getSearchPaths(
`${os.EOL}${os.EOL}/foo/*${os.EOL}${os.EOL}/bar/*${os.EOL}/baz/**${os.EOL}`
)
const drive = IS_WINDOWS ? process.cwd().substr(0, 2) : ''
expect(searchPaths).toEqual([
IS_WINDOWS ? `${drive}\\foo` : '/foo',
IS_WINDOWS ? `${drive}\\bar` : '/bar',
IS_WINDOWS ? `${drive}\\baz` : '/baz'
])
})
it('throws when match broken symlink and followSymbolicLinks=true and omitBrokenSymbolicLinks=false', async () => {
// Create the following layout:
// <root>
// <root>/brokenSym -> <root>/noSuch
const root = path.join(
getTestTemp(),
'throws-when-match-broken-symlink-and-follow-true-and-omit-false'
)
await fs.mkdir(root, {recursive: true})
await createSymlinkDir(
path.join(root, 'noSuch'),
path.join(root, 'brokenSym')
)
try {
await glob(root, {
followSymbolicLinks: true,
omitBrokenSymbolicLinks: false
})
throw new Error('Expected tl.find to throw')
} catch (err) {
expect(err.message).toMatch(/broken symbolic link/)
}
})
it('throws when search path is broken symlink and followSymbolicLinks=true and omitBrokenSymbolicLinks=false', async () => {
// Create the following layout:
// <root>
// <root>/brokenSym -> <root>/noSuch
const root = path.join(
getTestTemp(),
'throws-when-search-path-is-broken-symlink-and-follow-true-and-omit-false'
)
await fs.mkdir(root, {recursive: true})
const brokenSymPath = path.join(root, 'brokenSym')
await createSymlinkDir(path.join(root, 'noSuch'), brokenSymPath)
await fs.lstat(brokenSymPath)
try {
await glob(brokenSymPath, {
followSymbolicLinks: true,
omitBrokenSymbolicLinks: false
})
throw new Error('Expected tl.find to throw')
} catch (err) {
expect(err.message).toMatch(/broken symbolic link/)
}
})
})
async function createHiddenDirectory(dir: string): Promise<void> {
if (!path.basename(dir).match(/^\./)) {
throw new Error(`Expected dir '${dir}' to start with '.'.`)
}
await fs.mkdir(dir, {recursive: true})
if (IS_WINDOWS) {
const result = child.spawnSync('attrib.exe', ['+H', dir])
if (result.status !== 0) {
const message: string = (result.output || []).join(' ').trim()
throw new Error(
`Failed to set hidden attribute for directory '${dir}'. ${message}`
)
}
}
}
async function createHiddenFile(file: string, content: string): Promise<void> {
if (!path.basename(file).match(/^\./)) {
throw new Error(`Expected dir '${file}' to start with '.'.`)
}
await fs.mkdir(path.dirname(file), {recursive: true})
await fs.writeFile(file, content)
if (IS_WINDOWS) {
const result = child.spawnSync('attrib.exe', ['+H', file])
if (result.status !== 0) {
const message: string = (result.output || []).join(' ').trim()
throw new Error(
`Failed to set hidden attribute for file '${file}'. ${message}`
)
}
}
}
function getTestTemp(): string {
return path.join(__dirname, '_temp', 'glob')
}
/**
* Deletes a symlink directory
*/
async function unlinkSymlinkDir(links: string[]): Promise<void> {
for (const link of links) {
await fs.rm(link, {recursive: true, force: true})
}
}
/**
* Creates a symlink directory on OSX/Linux, and a junction point directory on Windows.
* A symlink directory is not created on Windows since it requires an elevated context.
*/
async function createSymlinkDir(real: string, link: string): Promise<void> {
if (IS_WINDOWS) {
await fs.symlink(real, link, 'junction')
} else {
await fs.symlink(real, link)
}
}
async function getSearchPaths(patterns: string): Promise<string[]> {
const globber: Globber = await DefaultGlobber.create(patterns)
return globber.getSearchPaths()
}
async function glob(
patterns: string,
options?: GlobOptions
): Promise<string[]> {
const globber: Globber = await DefaultGlobber.create(patterns, options)
return await globber.glob()
}