github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/golang/cataloger_test.go (about)

     1  package golang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
     9  )
    10  
    11  func Test_Mod_Cataloger_Globs(t *testing.T) {
    12  	tests := []struct {
    13  		name     string
    14  		fixture  string
    15  		expected []string
    16  	}{
    17  		{
    18  			name:    "obtain go.mod files",
    19  			fixture: "test-fixtures/glob-paths",
    20  			expected: []string{
    21  				"src/go.mod",
    22  			},
    23  		},
    24  	}
    25  
    26  	for _, test := range tests {
    27  		t.Run(test.name, func(t *testing.T) {
    28  			pkgtest.NewCatalogTester().
    29  				FromDirectory(t, test.fixture).
    30  				ExpectsResolverContentQueries(test.expected).
    31  				IgnoreUnfulfilledPathResponses("src/go.sum").
    32  				TestCataloger(t, NewGoModuleFileCataloger(CatalogerConfig{}))
    33  		})
    34  	}
    35  }
    36  
    37  func Test_Binary_Cataloger_Globs(t *testing.T) {
    38  	tests := []struct {
    39  		name     string
    40  		fixture  string
    41  		expected []string
    42  	}{
    43  		{
    44  			name:    "obtain binary files",
    45  			fixture: "test-fixtures/glob-paths",
    46  			expected: []string{
    47  				"partial-binary",
    48  			},
    49  		},
    50  	}
    51  
    52  	for _, test := range tests {
    53  		t.Run(test.name, func(t *testing.T) {
    54  			pkgtest.NewCatalogTester().
    55  				FromDirectory(t, test.fixture).
    56  				ExpectsResolverContentQueries(test.expected).
    57  				TestCataloger(t, NewGoModuleBinaryCataloger(CatalogerConfig{}))
    58  		})
    59  	}
    60  }
    61  
    62  func Test_Binary_Cataloger_Stdlib_Cpe(t *testing.T) {
    63  	tests := []struct {
    64  		name      string
    65  		candidate string
    66  		want      string
    67  	}{
    68  		{
    69  			name:      "generateStdlibCpe generates a cpe with a - for a major version",
    70  			candidate: "go1.21.0",
    71  			want:      "cpe:2.3:a:golang:go:1.21.0:-:*:*:*:*:*:*",
    72  		},
    73  		{
    74  			name:      "generateStdlibCpe generates a cpe with an rc candidate for a major rc version",
    75  			candidate: "go1.21rc2",
    76  			want:      "cpe:2.3:a:golang:go:1.21:rc2:*:*:*:*:*:*",
    77  		},
    78  	}
    79  
    80  	for _, tc := range tests {
    81  		t.Run(tc.name, func(t *testing.T) {
    82  			got, err := generateStdlibCpe(tc.candidate)
    83  			assert.NoError(t, err, "expected no err; got %v", err)
    84  			assert.Equal(t, got.Attributes.String(), tc.want)
    85  		})
    86  	}
    87  }