mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
add distro version tests
This commit is contained in:
parent
f0b8aaacda
commit
b6122a413b
@ -45,7 +45,7 @@ func runCmdWrapper(cmd *cobra.Command, args []string) {
|
|||||||
os.Exit(doRunCmd(cmd, args))
|
os.Exit(doRunCmd(cmd, args))
|
||||||
}
|
}
|
||||||
|
|
||||||
func doRunCmd(cmd *cobra.Command, args []string) int {
|
func doRunCmd(_ *cobra.Command, args []string) int {
|
||||||
userImageStr := args[0]
|
userImageStr := args[0]
|
||||||
log.Infof("Fetching image '%s'", userImageStr)
|
log.Infof("Fetching image '%s'", userImageStr)
|
||||||
img, err := stereoscope.GetImage(userImageStr)
|
img, err := stereoscope.GetImage(userImageStr)
|
||||||
|
|||||||
@ -9,6 +9,7 @@ import (
|
|||||||
type Distro struct {
|
type Distro struct {
|
||||||
Type Type
|
Type Type
|
||||||
Version *hashiVer.Version
|
Version *hashiVer.Version
|
||||||
|
RawVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDistro(t Type, ver string) (Distro, error) {
|
func NewDistro(t Type, ver string) (Distro, error) {
|
||||||
@ -19,11 +20,16 @@ func NewDistro(t Type, ver string) (Distro, error) {
|
|||||||
return Distro{
|
return Distro{
|
||||||
Type: t,
|
Type: t,
|
||||||
Version: verObj,
|
Version: verObj,
|
||||||
|
RawVersion: ver,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Distro) MajorVersion() int {
|
func (d Distro) MajorVersion() string {
|
||||||
return d.Version.Segments()[0]
|
return fmt.Sprintf("%d", d.Version.Segments()[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Distro) FullVersion() string {
|
||||||
|
return d.RawVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Distro) String() string {
|
func (d Distro) String() string {
|
||||||
|
|||||||
90
imgbom/distro/distro_test.go
Normal file
90
imgbom/distro/distro_test.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package distro
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDistro_FullVersion(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
dist Type
|
||||||
|
version string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
version: "8",
|
||||||
|
expected: "8",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: "18.04",
|
||||||
|
expected: "18.04",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: "0",
|
||||||
|
expected: "0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: "18.1.2",
|
||||||
|
expected: "18.1.2",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
name := fmt.Sprintf("%s:%s", test.dist, test.version)
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
d, err := NewDistro(test.dist, test.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := d.FullVersion()
|
||||||
|
if actual != test.expected {
|
||||||
|
t.Errorf("mismatched distro raw version: '%s'!='%s'", actual, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDistro_MajorVersion(t *testing.T) {
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
dist Type
|
||||||
|
version string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
version: "8",
|
||||||
|
expected: "8",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: "18.04",
|
||||||
|
expected: "18",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: "0",
|
||||||
|
expected: "0",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
version: "18.1.2",
|
||||||
|
expected: "18",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
name := fmt.Sprintf("%s:%s", test.dist, test.version)
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
d, err := NewDistro(test.dist, test.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not create distro='%+v:%+v': %+v", test.dist, test.version, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual := d.MajorVersion()
|
||||||
|
if actual != test.expected {
|
||||||
|
t.Errorf("mismatched major version: '%s'!='%s'", actual, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user