@@ -132,6 +132,9 @@ describe('File Service', () => {
132132 fileService . isSafeToDelete ( '/one/node_/ro/modules' , target ) ,
133133 ) . toBeFalsy ( ) ;
134134 expect ( fileService . isSafeToDelete ( 'nodemodules' , target ) ) . toBeFalsy ( ) ;
135+ expect ( fileService . isSafeToDelete ( '/' , target ) ) . toBeFalsy ( ) ;
136+ expect ( fileService . isSafeToDelete ( '/home' , target ) ) . toBeFalsy ( ) ;
137+ expect ( fileService . isSafeToDelete ( '/home/user' , target ) ) . toBeFalsy ( ) ;
135138 } ) ;
136139
137140 it ( 'should get true if is safe to delete ' , ( ) => {
@@ -144,39 +147,112 @@ describe('File Service', () => {
144147 } ) ;
145148 } ) ;
146149
147- describe ( '#isDangerous' , ( ) => {
148- it ( 'should return false for paths that are not considered dangerous' , ( ) => {
149- expect (
150- fileService . isDangerous ( '/home/apps/myapp/node_modules' ) ,
151- ) . toBeFalsy ( ) ;
152- expect ( fileService . isDangerous ( 'node_modules' ) ) . toBeFalsy ( ) ;
153- expect (
154- fileService . isDangerous ( '/home/user/projects/a/node_modules' ) ,
155- ) . toBeFalsy ( ) ;
156- expect (
157- fileService . isDangerous ( '/Applications/NotAnApp/node_modules' ) ,
158- ) . toBeFalsy ( ) ;
159- expect (
160- fileService . isDangerous ( 'C:\\Users\\User\\Documents\\node_modules' ) ,
161- ) . toBeFalsy ( ) ;
150+ describe ( 'isDangerous' , ( ) => {
151+ const originalEnv = { ...process . env } ;
152+ const originalCwd = process . cwd ( ) ;
153+
154+ const mockCwd = ( cwd : string ) => {
155+ jest . spyOn ( process , 'cwd' ) . mockReturnValue ( cwd ) ;
156+ } ;
157+
158+ afterAll ( ( ) => {
159+ process . env = originalEnv ;
162160 } ) ;
163161
164- it ( 'should return true for paths that are considered dangerous' , ( ) => {
165- expect (
166- fileService . isDangerous ( '/home/.config/myapp/node_modules' ) ,
167- ) . toBeTruthy ( ) ;
168- expect ( fileService . isDangerous ( '.apps/node_modules' ) ) . toBeTruthy ( ) ;
169- expect (
170- fileService . isDangerous ( '.apps/.sample/node_modules' ) ,
171- ) . toBeTruthy ( ) ;
172- expect (
173- fileService . isDangerous ( '/Applications/MyApp.app/node_modules' ) ,
174- ) . toBeTruthy ( ) ;
175- expect (
176- fileService . isDangerous (
177- 'C:\\Users\\User\\AppData\\Local\\node_modules' ,
178- ) ,
179- ) . toBeTruthy ( ) ;
162+ describe ( 'POSIX paths' , ( ) => {
163+ beforeAll ( ( ) => {
164+ process . env . HOME = '/home/user' ;
165+ delete process . env . USERPROFILE ;
166+ } ) ;
167+
168+ test ( 'safe relative path' , ( ) => {
169+ mockCwd ( '/safe/path' ) ;
170+ expect ( fileService . isDangerous ( '../project/node_modules' ) ) . toBe ( false ) ;
171+ } ) ;
172+
173+ test ( 'hidden relative path' , ( ) => {
174+ mockCwd ( '/home/user/projects' ) ;
175+ expect ( fileService . isDangerous ( '../.config/secret' ) ) . toBe ( true ) ;
176+ } ) ;
177+
178+ test ( 'node_modules in ~/.cache' , ( ) => {
179+ expect (
180+ fileService . isDangerous ( '/home/user/.cache/project/node_modules' ) ,
181+ ) . toBe ( false ) ;
182+ } ) ;
183+
184+ test ( 'parent relative path (..)' , ( ) => {
185+ mockCwd ( '/home/user' ) ;
186+ expect ( fileService . isDangerous ( '..' ) ) . toBe ( false ) ;
187+ } ) ;
188+
189+ test ( 'current relative path (.)' , ( ) => {
190+ mockCwd ( '/home/user' ) ;
191+ expect ( fileService . isDangerous ( '.' ) ) . toBe ( false ) ;
192+ } ) ;
193+ } ) ;
194+
195+ describe ( 'Windows paths' , ( ) => {
196+ beforeAll ( ( ) => {
197+ process . env . USERPROFILE = 'C:\\Users\\user' ;
198+ process . env . HOME = '' ;
199+ } ) ;
200+
201+ test ( 'safe relative path' , ( ) => {
202+ mockCwd ( 'D:\\safe\\path' ) ;
203+ expect ( fileService . isDangerous ( '..\\project\\node_modules' ) ) . toBe (
204+ false ,
205+ ) ;
206+ } ) ;
207+
208+ test ( 'AppData relative path' , ( ) => {
209+ mockCwd ( 'C:\\Users\\user\\Documents' ) ;
210+ expect ( fileService . isDangerous ( '..\\AppData\\Roaming\\app' ) ) . toBe ( true ) ;
211+ } ) ;
212+
213+ test ( 'Program Files (x86)' , ( ) => {
214+ expect (
215+ fileService . isDangerous ( 'C:\\Program Files (x86)\\app\\node_modules' ) ,
216+ ) . toBe ( true ) ;
217+ } ) ;
218+
219+ test ( 'network paths' , ( ) => {
220+ expect (
221+ fileService . isDangerous ( '\\\\network\\share\\.hidden\\node_modules' ) ,
222+ ) . toBe ( true ) ;
223+ } ) ;
224+ } ) ;
225+
226+ describe ( 'Edge cases' , ( ) => {
227+ test ( 'no home directory' , ( ) => {
228+ delete process . env . HOME ;
229+ delete process . env . USERPROFILE ;
230+ expect ( fileService . isDangerous ( '/some/path' ) ) . toBe ( false ) ;
231+ } ) ;
232+
233+ test ( 'whitelisted hidden segments' , ( ) => {
234+ expect (
235+ fileService . isDangerous ( '/tmp/.cache/project/node_modules' ) ,
236+ ) . toBe ( false ) ;
237+ expect ( fileService . isDangerous ( '/tmp/.npm/project/node_modules' ) ) . toBe (
238+ false ,
239+ ) ;
240+ } ) ;
241+
242+ test ( 'macOS application bundle' , ( ) => {
243+ expect (
244+ fileService . isDangerous (
245+ '/Applications/MyApp.app/Contents/node_modules' ,
246+ ) ,
247+ ) . toBe ( true ) ;
248+ } ) ;
249+
250+ test ( 'Windows-style path on POSIX' , ( ) => {
251+ process . env . HOME = '/home/user' ;
252+ expect ( fileService . isDangerous ( '/home/user/AppData/Local/.cache' ) ) . toBe (
253+ false ,
254+ ) ;
255+ } ) ;
180256 } ) ;
181257 } ) ;
182258
0 commit comments