@@ -229,6 +229,93 @@ describe('klaw-sync', () => {
229229 testDepthLimit ( 3 , expected )
230230 } )
231231
232+ it ( 'should honor depthLimit option -1 with nodir = true' , ( ) => {
233+ const expected = [ 'a/b/c/d.txt' , 'a/e.jpg' , 'h/i/j/k.txt' , 'h/i/l.txt' , 'h/i/m.jpg' , 't.txt' ]
234+ testDepthLimitNoDir ( - 1 , expected )
235+ } )
236+
237+ it ( 'should honor depthLimit option 0 with nodir = true' , ( ) => {
238+ const expected = [ 't.txt' ]
239+ testDepthLimitNoDir ( 0 , expected )
240+ } )
241+
242+ it ( 'should honor depthLimit option 1 with nodir = true' , ( ) => {
243+ const expected = [ 'a/e.jpg' , 't.txt' ]
244+ testDepthLimitNoDir ( 1 , expected )
245+ } )
246+
247+ it ( 'should honor depthLimit option -1 with nodir = true and with a filter to search for a specific file' , ( ) => {
248+ let expected = [ 'h/i/j/k.txt' ]
249+ const fixtures = [
250+ 'a/b/c/d.txt' ,
251+ 'a/e.jpg' ,
252+ 'h/i/j/k.txt' ,
253+ 'h/i/l.txt' ,
254+ 'h/i/m.jpg' ,
255+ 't.txt'
256+ ]
257+
258+ const filterFunction = function ( item ) {
259+ return item . stats . isDirectory ( ) || path . basename ( item . path ) === 'k.txt'
260+ }
261+
262+ fixtures . forEach ( f => {
263+ f = path . join ( TEST_DIR , f )
264+ fs . outputFileSync ( f , path . basename ( f , path . extname ( f ) ) )
265+ } )
266+
267+ const items = klawSync ( TEST_DIR , { depthLimit : - 1 , nodir : true , filter : filterFunction } ) . map ( i => i . path )
268+ items . sort ( )
269+ expected = expected . map ( item => path . join ( path . join ( TEST_DIR , item ) ) )
270+ assert . deepStrictEqual ( items , expected )
271+ } )
272+
273+ it ( 'should return all files except under filtered out directory' , ( ) => {
274+ let expected = [ 'a/b/c/d.txt' , 'a/e.jpg' , 't.txt' ]
275+ const fixtures = [
276+ 'a/b/c/d.txt' ,
277+ 'a/e.jpg' ,
278+ 'h/i/j/k.txt' ,
279+ 'h/i/l.txt' ,
280+ 'h/i/m.jpg' ,
281+ 't.txt'
282+ ]
283+
284+ const filterFunction = function ( item ) {
285+ return ! item . stats . isDirectory ( ) || ( item . stats . isDirectory ( ) && path . basename ( item . path ) !== 'i' )
286+ }
287+
288+ fixtures . forEach ( f => {
289+ f = path . join ( TEST_DIR , f )
290+ fs . outputFileSync ( f , path . basename ( f , path . extname ( f ) ) )
291+ } )
292+
293+ const items = klawSync ( TEST_DIR , { depthLimit : - 1 , nodir : true , filter : filterFunction } ) . map ( i => i . path )
294+ items . sort ( )
295+ expected = expected . map ( item => path . join ( path . join ( TEST_DIR , item ) ) )
296+ assert . deepStrictEqual ( items , expected )
297+ } )
298+
299+ function testDepthLimitNoDir ( depthLimit , expected ) {
300+ const fixtures = [
301+ 'a/b/c/d.txt' ,
302+ 'a/e.jpg' ,
303+ 'h/i/j/k.txt' ,
304+ 'h/i/l.txt' ,
305+ 'h/i/m.jpg' ,
306+ 't.txt'
307+ ]
308+ fixtures . forEach ( f => {
309+ f = path . join ( TEST_DIR , f )
310+ fs . outputFileSync ( f , path . basename ( f , path . extname ( f ) ) )
311+ } )
312+
313+ const items = klawSync ( TEST_DIR , { depthLimit : depthLimit , nodir : true } ) . map ( i => i . path )
314+ items . sort ( )
315+ expected = expected . map ( item => path . join ( path . join ( TEST_DIR , item ) ) )
316+ assert . deepStrictEqual ( items , expected )
317+ }
318+
232319 function testDepthLimit ( depthLimit , expected ) {
233320 const fixtures = [
234321 'a/b/c/d.txt' ,
0 commit comments