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

     1  package golang
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/anchore/syft/syft/file"
     7  	"github.com/anchore/syft/syft/pkg"
     8  	"github.com/anchore/syft/syft/pkg/cataloger/internal/pkgtest"
     9  )
    10  
    11  func TestParseGoMod(t *testing.T) {
    12  	tests := []struct {
    13  		fixture  string
    14  		expected []pkg.Package
    15  	}{
    16  		{
    17  			fixture: "test-fixtures/one-package",
    18  			expected: []pkg.Package{
    19  				{
    20  					Name:      "github.com/bmatcuk/doublestar",
    21  					Version:   "v1.3.1",
    22  					PURL:      "pkg:golang/github.com/bmatcuk/doublestar@v1.3.1",
    23  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/one-package")),
    24  					Language:  pkg.Go,
    25  					Type:      pkg.GoModulePkg,
    26  					Metadata:  pkg.GolangModuleEntry{},
    27  				},
    28  			},
    29  		},
    30  		{
    31  
    32  			fixture: "test-fixtures/many-packages",
    33  			expected: []pkg.Package{
    34  				{
    35  					Name:      "github.com/anchore/go-testutils",
    36  					Version:   "v0.0.0-20200624184116-66aa578126db",
    37  					PURL:      "pkg:golang/github.com/anchore/go-testutils@v0.0.0-20200624184116-66aa578126db",
    38  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/many-packages")),
    39  					Language:  pkg.Go,
    40  					Type:      pkg.GoModulePkg,
    41  					Metadata:  pkg.GolangModuleEntry{},
    42  				},
    43  				{
    44  					Name:      "github.com/anchore/go-version",
    45  					Version:   "v1.2.2-0.20200701162849-18adb9c92b9b",
    46  					PURL:      "pkg:golang/github.com/anchore/go-version@v1.2.2-0.20200701162849-18adb9c92b9b",
    47  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/many-packages")),
    48  					Language:  pkg.Go,
    49  					Type:      pkg.GoModulePkg,
    50  					Metadata:  pkg.GolangModuleEntry{},
    51  				},
    52  				{
    53  					Name:      "github.com/anchore/stereoscope",
    54  					Version:   "v0.0.0-20200706164556-7cf39d7f4639",
    55  					PURL:      "pkg:golang/github.com/anchore/stereoscope@v0.0.0-20200706164556-7cf39d7f4639",
    56  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/many-packages")),
    57  					Language:  pkg.Go,
    58  					Type:      pkg.GoModulePkg,
    59  					Metadata:  pkg.GolangModuleEntry{},
    60  				},
    61  				{
    62  					Name:      "github.com/bmatcuk/doublestar",
    63  					Version:   "v8.8.8",
    64  					PURL:      "pkg:golang/github.com/bmatcuk/doublestar@v8.8.8",
    65  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/many-packages")),
    66  					Language:  pkg.Go,
    67  					Type:      pkg.GoModulePkg,
    68  					Metadata:  pkg.GolangModuleEntry{},
    69  				},
    70  				{
    71  					Name:      "github.com/go-test/deep",
    72  					Version:   "v1.0.6",
    73  					PURL:      "pkg:golang/github.com/go-test/deep@v1.0.6",
    74  					Locations: file.NewLocationSet(file.NewLocation("test-fixtures/many-packages")),
    75  					Language:  pkg.Go,
    76  					Type:      pkg.GoModulePkg,
    77  					Metadata:  pkg.GolangModuleEntry{},
    78  				},
    79  			},
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		t.Run(test.fixture, func(t *testing.T) {
    85  			c := goModCataloger{}
    86  			pkgtest.NewCatalogTester().
    87  				FromFile(t, test.fixture).
    88  				Expects(test.expected, nil).
    89  				TestParser(t, c.parseGoModFile)
    90  		})
    91  	}
    92  }
    93  
    94  func Test_GoSumHashes(t *testing.T) {
    95  	tests := []struct {
    96  		fixture  string
    97  		expected []pkg.Package
    98  	}{
    99  		{
   100  			fixture: "test-fixtures/go-sum-hashes",
   101  			expected: []pkg.Package{
   102  				{
   103  					Name:      "github.com/CycloneDX/cyclonedx-go",
   104  					Version:   "v0.6.0",
   105  					PURL:      "pkg:golang/github.com/CycloneDX/cyclonedx-go@v0.6.0",
   106  					Locations: file.NewLocationSet(file.NewLocation("go.mod")),
   107  					FoundBy:   "go-module-file-cataloger",
   108  					Language:  pkg.Go,
   109  					Type:      pkg.GoModulePkg,
   110  					Metadata:  pkg.GolangModuleEntry{},
   111  				},
   112  				{
   113  					Name:      "github.com/acarl005/stripansi",
   114  					Version:   "v0.0.0-20180116102854-5a71ef0e047d",
   115  					PURL:      "pkg:golang/github.com/acarl005/stripansi@v0.0.0-20180116102854-5a71ef0e047d",
   116  					Locations: file.NewLocationSet(file.NewLocation("go.mod")),
   117  					FoundBy:   "go-module-file-cataloger",
   118  					Language:  pkg.Go,
   119  					Type:      pkg.GoModulePkg,
   120  					Metadata: pkg.GolangModuleEntry{
   121  						H1Digest: "h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=",
   122  					},
   123  				},
   124  				{
   125  					Name:      "github.com/mgutz/ansi",
   126  					Version:   "v0.0.0-20200706080929-d51e80ef957d",
   127  					PURL:      "pkg:golang/github.com/mgutz/ansi@v0.0.0-20200706080929-d51e80ef957d",
   128  					Locations: file.NewLocationSet(file.NewLocation("go.mod")),
   129  					FoundBy:   "go-module-file-cataloger",
   130  					Language:  pkg.Go,
   131  					Type:      pkg.GoModulePkg,
   132  					Metadata: pkg.GolangModuleEntry{
   133  						H1Digest: "h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=",
   134  					},
   135  				},
   136  			},
   137  		},
   138  	}
   139  
   140  	for _, test := range tests {
   141  		t.Run(test.fixture, func(t *testing.T) {
   142  			pkgtest.NewCatalogTester().
   143  				FromDirectory(t, test.fixture).
   144  				Expects(test.expected, nil).
   145  				TestCataloger(t, NewGoModuleFileCataloger(CatalogerConfig{}))
   146  		})
   147  	}
   148  }