github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/rpm/package_test.go (about)

     1  package rpm
     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.RpmMetadata
    17  		expected string
    18  	}{
    19  		{
    20  			name: "go case",
    21  			distro: &linux.Release{
    22  				ID:        "rhel",
    23  				VersionID: "8.4",
    24  			},
    25  			metadata: pkg.RpmMetadata{
    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.RpmMetadata{
    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.RpmMetadata{
    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.RpmMetadata{
    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(test.metadata, test.distro)
    77  			if actual != test.expected {
    78  				dmp := diffmatchpatch.New()
    79  				diffs := dmp.DiffMain(test.expected, actual, true)
    80  				t.Errorf("diff: %s", dmp.DiffPrettyText(diffs))
    81  			}
    82  		})
    83  	}
    84  }