github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/redhat/package_test.go (about) 1 package redhat 2 3 import ( 4 "testing" 5 6 "github.com/sergi/go-diff/diffmatchpatch" 7 8 "github.com/anchore/syft/syft/linux" 9 "github.com/anchore/syft/syft/pkg" 10 ) 11 12 func Test_packageURL(t *testing.T) { 13 tests := []struct { 14 name string 15 distro *linux.Release 16 metadata pkg.RpmDBEntry 17 expected string 18 }{ 19 { 20 name: "go case", 21 distro: &linux.Release{ 22 ID: "rhel", 23 VersionID: "8.4", 24 }, 25 metadata: pkg.RpmDBEntry{ 26 Name: "p", 27 Version: "v", 28 Release: "r", 29 Epoch: nil, 30 }, 31 expected: "pkg:rpm/rhel/p@v-r?distro=rhel-8.4", 32 }, 33 { 34 name: "with arch and epoch", 35 distro: &linux.Release{ 36 ID: "centos", 37 VersionID: "7", 38 }, 39 metadata: pkg.RpmDBEntry{ 40 Name: "p", 41 Version: "v", 42 Arch: "a", 43 Release: "r", 44 Epoch: intRef(1), 45 }, 46 expected: "pkg:rpm/centos/p@v-r?arch=a&epoch=1&distro=centos-7", 47 }, 48 { 49 name: "missing distro", 50 metadata: pkg.RpmDBEntry{ 51 Name: "p", 52 Version: "v", 53 Release: "r", 54 Epoch: nil, 55 }, 56 expected: "pkg:rpm/p@v-r", 57 }, 58 { 59 name: "with upstream source rpm info", 60 distro: &linux.Release{ 61 ID: "rhel", 62 VersionID: "8.4", 63 }, 64 metadata: pkg.RpmDBEntry{ 65 Name: "p", 66 Version: "v", 67 Release: "r", 68 SourceRpm: "sourcerpm", 69 }, 70 expected: "pkg:rpm/rhel/p@v-r?upstream=sourcerpm&distro=rhel-8.4", 71 }, 72 } 73 74 for _, test := range tests { 75 t.Run(test.name, func(t *testing.T) { 76 actual := packageURL( 77 test.metadata.Name, 78 test.metadata.Arch, 79 test.metadata.Epoch, 80 test.metadata.SourceRpm, 81 test.metadata.Version, 82 test.metadata.Release, 83 test.distro, 84 ) 85 if actual != test.expected { 86 dmp := diffmatchpatch.New() 87 diffs := dmp.DiffMain(test.expected, actual, true) 88 t.Errorf("diff: %s", dmp.DiffPrettyText(diffs)) 89 } 90 }) 91 } 92 }