@@ -10,6 +10,7 @@ import (
10
10
"github.com/go-logr/logr"
11
11
"github.com/kylelemons/godebug/pretty"
12
12
"github.com/stretchr/testify/assert"
13
+ "github.com/stretchr/testify/require"
13
14
"google.golang.org/protobuf/types/known/anypb"
14
15
15
16
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
@@ -300,6 +301,83 @@ func TestChunkUnitReporterErr(t *testing.T) {
300
301
assert .Error (t , err )
301
302
}
302
303
304
+ func TestSkipDir (t * testing.T ) {
305
+ t .Parallel ()
306
+ ctx := context .Background ()
307
+
308
+ // create a temp directory with files
309
+ ignoreDir , cleanupDir , err := createTempDir ("" , "ignore1" , "ignore2" , "ignore3" )
310
+ require .NoError (t , err )
311
+ defer cleanupDir ()
312
+
313
+ // create an ExcludePathsFile that contains the ignoreDir path
314
+ excludeFile , cleanupFile , err := createTempFile ("" , ignoreDir + "\n " )
315
+ require .NoError (t , err )
316
+ defer cleanupFile ()
317
+
318
+ conn , err := anypb .New (& sourcespb.Filesystem {
319
+ ExcludePathsFile : excludeFile .Name (),
320
+ })
321
+ require .NoError (t , err )
322
+
323
+ // initialize the source.
324
+ s := Source {}
325
+ err = s .Init (ctx , "exclude directory" , 0 , 0 , true , conn , 1 )
326
+ require .NoError (t , err )
327
+
328
+ reporter := sourcestest.TestReporter {}
329
+ err = s .ChunkUnit (ctx , sources.CommonSourceUnit {
330
+ ID : ignoreDir ,
331
+ }, & reporter )
332
+ require .NoError (t , err )
333
+
334
+ require .Equal (t , 0 , len (reporter .Chunks ), "Expected no chunks from excluded directory" )
335
+ require .Equal (t , 0 , len (reporter .ChunkErrs ), "Expected no errors for excluded directory" )
336
+ }
337
+
338
+ func TestScanSubDirFile (t * testing.T ) {
339
+ t .Parallel ()
340
+ ctx := context .Background ()
341
+
342
+ // create a temp directory with files
343
+ parentDir , cleanupParentDir , err := createTempDir ("" , "file1" )
344
+ require .NoError (t , err )
345
+ defer cleanupParentDir ()
346
+
347
+ childDir , cleanupChildDir , err := createTempDir (parentDir , "file2" )
348
+ require .NoError (t , err )
349
+ defer cleanupChildDir ()
350
+
351
+ // create a file in child directory
352
+ file , cleanupFile , err := createTempFile (childDir , "should scan this file" )
353
+ require .NoError (t , err )
354
+ defer cleanupFile ()
355
+
356
+ // create an IncludePathsFile that contains the file path
357
+ includeFile , cleanupFile , err := createTempFile ("" , file .Name ()+ "\n " )
358
+ require .NoError (t , err )
359
+ defer cleanupFile ()
360
+
361
+ conn , err := anypb .New (& sourcespb.Filesystem {
362
+ IncludePathsFile : includeFile .Name (),
363
+ })
364
+ require .NoError (t , err )
365
+
366
+ // initialize the source.
367
+ s := Source {}
368
+ err = s .Init (ctx , "include sub directory file" , 0 , 0 , true , conn , 1 )
369
+ require .NoError (t , err )
370
+
371
+ reporter := sourcestest.TestReporter {}
372
+ err = s .ChunkUnit (ctx , sources.CommonSourceUnit {
373
+ ID : parentDir ,
374
+ }, & reporter )
375
+ require .NoError (t , err )
376
+
377
+ require .Equal (t , 1 , len (reporter .Chunks ), "Expected chunks from included file" )
378
+ require .Equal (t , 0 , len (reporter .ChunkErrs ), "Expected no errors" )
379
+ }
380
+
303
381
// createTempFile is a helper function to create a temporary file in the given
304
382
// directory with the provided contents. If dir is "", the operating system's
305
383
// temp directory is used.
0 commit comments