1
+ import * as fs from "fs" ;
2
+ import { createServer } from "net" ;
1
3
import { Project , tempdirProject } from "@typestrong/fs-fixture-builder" ;
2
- import { deepStrictEqual as equal } from "assert" ;
3
- import { basename } from "path" ;
4
+ import { AssertionError , deepStrictEqual as equal } from "assert" ;
5
+ import { basename , dirname , resolve , normalize } from "path" ;
4
6
import { glob } from "../../lib/utils/fs" ;
5
7
6
8
describe ( "fs.ts" , ( ) => {
@@ -18,7 +20,7 @@ describe("fs.ts", () => {
18
20
fix . write ( ) ;
19
21
20
22
const result = glob ( fix . cwd , fix . cwd , { includeDirectories : true } ) ;
21
- equal ( result , [ fix . cwd ] ) ;
23
+ equal ( result . map ( normalize ) , [ fix . cwd ] . map ( normalize ) ) ;
22
24
} ) ;
23
25
24
26
it ( "Handles basic globbing" , ( ) => {
@@ -37,5 +39,87 @@ describe("fs.ts", () => {
37
39
[ "test.ts" , "test2.ts" ]
38
40
) ;
39
41
} ) ;
42
+
43
+ describe ( "when 'followSymlinks' option is true" , ( ) => {
44
+ it ( "should navigate symlinked directories" , ( ) => {
45
+ const target = dirname ( fix . dir ( "a" ) . addFile ( "test.ts" ) . path ) ;
46
+ fix . write ( ) ;
47
+ fs . symlinkSync ( target , resolve ( fix . cwd , "b" ) , "junction" ) ;
48
+ equal (
49
+ glob ( `${ fix . cwd } /b/*.ts` , fix . cwd , {
50
+ followSymlinks : true ,
51
+ } ) . map ( ( f ) => basename ( f ) ) ,
52
+ [ "test.ts" ]
53
+ ) ;
54
+ } ) ;
55
+
56
+ it ( "should navigate recursive symlinked directories only once" , ( ) => {
57
+ fix . addFile ( "test.ts" ) ;
58
+ fix . write ( ) ;
59
+ fs . symlinkSync (
60
+ fix . cwd ,
61
+ resolve ( fix . cwd , "recursive" ) ,
62
+ "junction"
63
+ ) ;
64
+ equal (
65
+ glob ( `${ fix . cwd } /**/*.ts` , fix . cwd , {
66
+ followSymlinks : true ,
67
+ } ) . map ( ( f ) => basename ( f ) ) ,
68
+ [ "test.ts" , "test.ts" ]
69
+ ) ;
70
+ } ) ;
71
+
72
+ it ( "should handle symlinked files" , function ( ) {
73
+ const { path } = fix . addFile ( "test.ts" ) ;
74
+ fix . write ( ) ;
75
+ try {
76
+ fs . symlinkSync (
77
+ path ,
78
+ resolve ( dirname ( path ) , "test-2.ts" ) ,
79
+ "file"
80
+ ) ;
81
+ } catch ( err ) {
82
+ // on windows, you need elevated permissions to create a file symlink.
83
+ // maybe we have them! maybe we don't!
84
+ if (
85
+ ( err as NodeJS . ErrnoException ) . code === "EPERM" &&
86
+ process . platform === "win32"
87
+ ) {
88
+ return this . skip ( ) ;
89
+ }
90
+ }
91
+ equal (
92
+ glob ( `${ fix . cwd } /**/*.ts` , fix . cwd , {
93
+ followSymlinks : true ,
94
+ } ) . map ( ( f ) => basename ( f ) ) ,
95
+ [ "test-2.ts" , "test.ts" ]
96
+ ) ;
97
+ } ) ;
98
+ } ) ;
99
+
100
+ it ( "should ignore anything that is not a file, symbolic link, or directory" , function ( done ) {
101
+ // Use unix socket for example, because that's easiest to create.
102
+ // Skip on Windows because it doesn't support unix sockets
103
+ if ( process . platform === "win32" ) {
104
+ return this . skip ( ) ;
105
+ }
106
+ fix . write ( ) ;
107
+
108
+ const sockServer = createServer ( )
109
+ . unref ( )
110
+ . listen ( resolve ( fix . cwd , "socket.sock" ) )
111
+ . once ( "listening" , ( ) => {
112
+ let err : AssertionError | null = null ;
113
+ try {
114
+ equal ( glob ( `${ fix . cwd } /*.sock` , fix . cwd ) , [ ] ) ;
115
+ } catch ( e ) {
116
+ err = e as AssertionError ;
117
+ } finally {
118
+ sockServer . close ( ( ) => {
119
+ done ( err ) ;
120
+ } ) ;
121
+ }
122
+ } ) ;
123
+ } ) ;
40
124
} ) ;
41
125
} ) ;
0 commit comments