github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/internal/cmptest/common_options.go (about)

     1  package cmptest
     2  
     3  import (
     4  	"github.com/google/go-cmp/cmp"
     5  	"github.com/google/go-cmp/cmp/cmpopts"
     6  
     7  	"github.com/anchore/syft/syft/file"
     8  	"github.com/anchore/syft/syft/pkg"
     9  )
    10  
    11  func DefaultCommonOptions() []cmp.Option {
    12  	return CommonOptions(nil, nil)
    13  }
    14  
    15  func CommonOptions(licenseCmp LicenseComparer, locationCmp LocationComparer) []cmp.Option {
    16  	if licenseCmp == nil {
    17  		licenseCmp = DefaultLicenseComparer
    18  	}
    19  
    20  	if locationCmp == nil {
    21  		locationCmp = DefaultLocationComparer
    22  	}
    23  
    24  	return []cmp.Option{
    25  		cmpopts.IgnoreFields(pkg.Package{}, "id"), // note: ID is not deterministic for test purposes
    26  		cmpopts.SortSlices(pkg.Less),
    27  		cmpopts.SortSlices(DefaultRelationshipComparer),
    28  		cmp.Comparer(
    29  			func(x, y file.LocationSet) bool {
    30  				xs := x.ToSlice()
    31  				ys := y.ToSlice()
    32  
    33  				if len(xs) != len(ys) {
    34  					return false
    35  				}
    36  				for i, xe := range xs {
    37  					ye := ys[i]
    38  					if !locationCmp(xe, ye) {
    39  						return false
    40  					}
    41  				}
    42  
    43  				return true
    44  			},
    45  		),
    46  		cmp.Comparer(
    47  			func(x, y pkg.LicenseSet) bool {
    48  				xs := x.ToSlice()
    49  				ys := y.ToSlice()
    50  
    51  				if len(xs) != len(ys) {
    52  					return false
    53  				}
    54  				for i, xe := range xs {
    55  					ye := ys[i]
    56  					if !licenseCmp(xe, ye) {
    57  						return false
    58  					}
    59  				}
    60  
    61  				return true
    62  			},
    63  		),
    64  		cmp.Comparer(
    65  			locationCmp,
    66  		),
    67  		cmp.Comparer(
    68  			licenseCmp,
    69  		),
    70  	}
    71  }