github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/scanner/utils/utils_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/devseccon/trivy/pkg/fanal/types"
     9  )
    10  
    11  func TestFormatSrcVersion(t *testing.T) {
    12  	tests := []struct {
    13  		name string
    14  		pkg  types.Package
    15  		want string
    16  	}{
    17  		{
    18  			name: "happy path",
    19  			pkg: types.Package{
    20  				SrcVersion: "1.2.3",
    21  				SrcRelease: "1",
    22  			},
    23  			want: "1.2.3-1",
    24  		},
    25  		{
    26  			name: "with epoch",
    27  			pkg: types.Package{
    28  				SrcEpoch:   2,
    29  				SrcVersion: "1.2.3",
    30  				SrcRelease: "alpha",
    31  			},
    32  			want: "2:1.2.3-alpha",
    33  		},
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			got := FormatSrcVersion(tt.pkg)
    38  			assert.Equal(t, tt.want, got)
    39  		})
    40  	}
    41  }
    42  
    43  func TestFormatVersion(t *testing.T) {
    44  	tests := []struct {
    45  		name string
    46  		pkg  types.Package
    47  		want string
    48  	}{
    49  		{
    50  			name: "happy path",
    51  			pkg: types.Package{
    52  				Version: "1.2.3",
    53  				Release: "1",
    54  			},
    55  			want: "1.2.3-1",
    56  		},
    57  		{
    58  			name: "with epoch",
    59  			pkg: types.Package{
    60  				Epoch:   2,
    61  				Version: "1.2.3",
    62  				Release: "alpha",
    63  			},
    64  			want: "2:1.2.3-alpha",
    65  		},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			got := FormatVersion(tt.pkg)
    70  			assert.Equal(t, tt.want, got)
    71  		})
    72  	}
    73  }