github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/golang/package_test.go (about) 1 package golang 2 3 import ( 4 "runtime/debug" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/anchore/syft/syft/pkg" 10 ) 11 12 func Test_packageURL(t *testing.T) { 13 14 tests := []struct { 15 name string 16 pkg pkg.Package 17 expected string 18 }{ 19 { 20 name: "gocase", 21 pkg: pkg.Package{ 22 Name: "github.com/anchore/syft", 23 Version: "v0.1.0", 24 }, 25 expected: "pkg:golang/github.com/anchore/syft@v0.1.0", 26 }, 27 { 28 name: "golang short name", 29 pkg: pkg.Package{ 30 Name: "go.opencensus.io", 31 Version: "v0.23.0", 32 }, 33 expected: "pkg:golang/go.opencensus.io@v0.23.0", 34 }, 35 { 36 name: "golang with subpath", 37 pkg: pkg.Package{ 38 Name: "github.com/coreos/go-systemd/v22", 39 Version: "v22.1.0", 40 }, 41 expected: "pkg:golang/github.com/coreos/go-systemd@v22.1.0#v22", 42 }, 43 { 44 name: "golang with subpath deep", 45 pkg: pkg.Package{ 46 Name: "google.golang.org/genproto/googleapis/api/annotations", 47 }, 48 expected: "pkg:golang/google.golang.org/genproto/googleapis#api/annotations", 49 }, 50 } 51 52 for _, test := range tests { 53 t.Run(test.name, func(t *testing.T) { 54 assert.Equal(t, test.expected, packageURL(test.pkg.Name, test.pkg.Version)) 55 }) 56 } 57 } 58 59 func Test_newGoBinaryPackage_relativeReplace(t *testing.T) { 60 tests := []struct { 61 name string 62 dep *debug.Module 63 expectedName string 64 }{ 65 { 66 name: "relative replace with ../", 67 dep: &debug.Module{ 68 Path: "github.com/aws/aws-sdk-go-v2", 69 Version: "(devel)", 70 Replace: &debug.Module{ 71 Path: "../../", 72 Version: "(devel)", 73 }, 74 }, 75 expectedName: "github.com/aws/aws-sdk-go-v2", // should use original path, not relative 76 }, 77 { 78 name: "relative replace with ./", 79 dep: &debug.Module{ 80 Path: "github.com/example/module", 81 Version: "v1.0.0", 82 Replace: &debug.Module{ 83 Path: "./local", 84 Version: "v0.0.0", 85 }, 86 }, 87 expectedName: "github.com/example/module", // should use original path 88 }, 89 { 90 name: "absolute replace", 91 dep: &debug.Module{ 92 Path: "github.com/old/module", 93 Version: "v1.0.0", 94 Replace: &debug.Module{ 95 Path: "github.com/new/module", 96 Version: "v2.0.0", 97 }, 98 }, 99 expectedName: "github.com/new/module", // should use replacement path 100 }, 101 { 102 name: "no replace", 103 dep: &debug.Module{ 104 Path: "github.com/normal/module", 105 Version: "v1.0.0", 106 }, 107 expectedName: "github.com/normal/module", // should use original path 108 }, 109 } 110 111 for _, test := range tests { 112 t.Run(test.name, func(t *testing.T) { 113 cataloger := &goBinaryCataloger{} 114 result := cataloger.newGoBinaryPackage(test.dep, pkg.GolangBinaryBuildinfoEntry{}, nil) 115 116 assert.Equal(t, test.expectedName, result.Name) 117 assert.Equal(t, pkg.Go, result.Language) 118 assert.Equal(t, pkg.GoModulePkg, result.Type) 119 }) 120 } 121 }