github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/pkg/cataloger/generic/cataloger_test.go (about)

     1  package generic
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/nextlinux/gosbom/gosbom/artifact"
     9  	"github.com/nextlinux/gosbom/gosbom/file"
    10  	"github.com/nextlinux/gosbom/gosbom/pkg"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func Test_Cataloger(t *testing.T) {
    16  	allParsedPaths := make(map[string]bool)
    17  	parser := func(resolver file.Resolver, env *Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    18  		allParsedPaths[reader.AccessPath()] = true
    19  		contents, err := io.ReadAll(reader)
    20  		require.NoError(t, err)
    21  
    22  		if len(contents) == 0 {
    23  			return nil, nil, nil
    24  		}
    25  
    26  		p := pkg.Package{
    27  			Name:      string(contents),
    28  			Locations: file.NewLocationSet(reader.Location),
    29  		}
    30  		r := artifact.Relationship{
    31  			From: p,
    32  			To:   p,
    33  			Type: artifact.ContainsRelationship,
    34  		}
    35  
    36  		return []pkg.Package{p}, []artifact.Relationship{r}, nil
    37  	}
    38  
    39  	upstream := "some-other-cataloger"
    40  
    41  	expectedSelection := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt", "test-fixtures/empty.txt"}
    42  	resolver := file.NewMockResolverForPaths(expectedSelection...)
    43  	cataloger := NewCataloger(upstream).
    44  		WithParserByPath(parser, "test-fixtures/another-path.txt", "test-fixtures/last/path.txt").
    45  		WithParserByGlobs(parser, "**/a-path.txt", "**/empty.txt")
    46  
    47  	actualPkgs, relationships, err := cataloger.Catalog(resolver)
    48  	assert.NoError(t, err)
    49  
    50  	expectedPkgs := make(map[string]pkg.Package)
    51  	for _, path := range expectedSelection {
    52  		require.True(t, allParsedPaths[path])
    53  		if path == "test-fixtures/empty.txt" {
    54  			continue // note: empty.txt won't become a package
    55  		}
    56  		expectedPkgs[path] = pkg.Package{
    57  			FoundBy: upstream,
    58  			Name:    fmt.Sprintf("%s file contents!", path),
    59  		}
    60  	}
    61  
    62  	assert.Len(t, allParsedPaths, len(expectedSelection))
    63  	assert.Len(t, actualPkgs, len(expectedPkgs))
    64  	assert.Len(t, relationships, len(actualPkgs))
    65  
    66  	for _, p := range actualPkgs {
    67  		ls := p.Locations.ToSlice()
    68  		require.NotEmpty(t, ls)
    69  		ref := ls[0]
    70  		exP, ok := expectedPkgs[ref.RealPath]
    71  		if !ok {
    72  			t.Errorf("missing expected pkg: ref=%+v", ref)
    73  			continue
    74  		}
    75  
    76  		// assigned by the generic cataloger
    77  		if p.FoundBy != exP.FoundBy {
    78  			t.Errorf("bad upstream: %s", p.FoundBy)
    79  		}
    80  
    81  		// assigned by the parser
    82  		if exP.Name != p.Name {
    83  			t.Errorf("bad contents mapping: %+v", p.Locations)
    84  		}
    85  	}
    86  }