@@ -25,6 +25,7 @@ import (
2525 "io"
2626 "os"
2727 "path/filepath"
28+ "strings"
2829 "testing"
2930
3031 "github.com/stretchr/testify/require"
@@ -416,6 +417,90 @@ func TestIgnorePatterns(t *testing.T) {
416417 require .Len (t , p , 4 )
417418}
418419
420+ func TestRecursiveNameFilter (t * testing.T ) {
421+ /*
422+ create the starting package structure
423+
424+ root-p
425+ |
426+ |-target-p-0
427+ | |-sub-p-0
428+ |
429+ |-sub-p-1
430+ | |-target-p-1
431+ | |-sub-p-2
432+ |
433+ |-sub-p-3
434+ */
435+ packageIDs := []string {"root-p" }
436+ for i := range 3 {
437+ packageIDs = append (packageIDs , fmt .Sprintf ("target-p-%d" , i ))
438+ }
439+ for i := range 7 {
440+ packageIDs = append (packageIDs , fmt .Sprintf ("sub-p-%d" , i ))
441+ }
442+ edges := []struct {
443+ p string
444+ c string
445+ }{
446+ {"root-p" , "target-p-0" },
447+ {"root-p" , "sub-p-1" },
448+ {"root-p" , "sub-p-3" },
449+ {"target-p-0" , "sub-p-0" },
450+ {"sub-p-1" , "target-p-1" },
451+ {"sub-p-1" , "sub-p-2" },
452+ }
453+ packages := map [string ]* Package {}
454+ for _ , id := range packageIDs {
455+ p := NewPackage ()
456+ p .SetSPDXID (id )
457+ p .Name = strings .Join (strings .Split (id , "-" )[:2 ], "-" )
458+ packages [id ] = p
459+ }
460+
461+ for _ , edge := range edges {
462+ require .NoError (t , packages [edge .p ].AddPackage (packages [edge .c ]))
463+ }
464+
465+ /*
466+ create the expected package structure
467+
468+ root-p
469+ |
470+ |-target-p-0
471+ */
472+ ePackageIDs := []string {"root-p" , "target-p-0" }
473+ eEdges := []struct {
474+ p string
475+ c string
476+ }{
477+ {"root-p" , "target-p-0" },
478+ }
479+ ePackages := map [string ]* Package {}
480+ for _ , id := range ePackageIDs {
481+ p := NewPackage ()
482+ p .SetSPDXID (id )
483+ p .Name = strings .Join (strings .Split (id , "-" )[:2 ], "-" )
484+ ePackages [id ] = p
485+ }
486+
487+ for _ , edge := range eEdges {
488+ require .NoError (t , ePackages [edge .p ].AddPackage (ePackages [edge .c ]))
489+ }
490+
491+ // filter the starting packages
492+ ok := recursiveNameFilter (
493+ "target-p" ,
494+ packages ["root-p" ],
495+ 2 ,
496+ & map [string ]bool {},
497+ )
498+ require .True (t , ok )
499+
500+ // check filtered == expected
501+ require .Equal (t , ePackages ["root-p" ], packages ["root-p" ])
502+ }
503+
419504func TestRecursiveSearch (t * testing.T ) {
420505 p := NewPackage ()
421506 p .SetSPDXID ("p-top" )
0 commit comments