add distro version tests

This commit is contained in:
Alex Goodman 2020-06-02 20:48:15 -04:00
parent f0b8aaacda
commit b6122a413b
No known key found for this signature in database
GPG key ID: 86E2870463D5E890
3 changed files with 103 additions and 7 deletions

View file

@ -45,7 +45,7 @@ func runCmdWrapper(cmd *cobra.Command, args []string) {
os.Exit(doRunCmd(cmd, args))
}
func doRunCmd(cmd *cobra.Command, args []string) int {
func doRunCmd(_ *cobra.Command, args []string) int {
userImageStr := args[0]
log.Infof("Fetching image '%s'", userImageStr)
img, err := stereoscope.GetImage(userImageStr)

View file

@ -7,8 +7,9 @@ import (
)
type Distro struct {
Type Type
Version *hashiVer.Version
Type Type
Version *hashiVer.Version
RawVersion string
}
func NewDistro(t Type, ver string) (Distro, error) {
@ -17,13 +18,18 @@ func NewDistro(t Type, ver string) (Distro, error) {
return Distro{}, fmt.Errorf("could not create distro version: %w", err)
}
return Distro{
Type: t,
Version: verObj,
Type: t,
Version: verObj,
RawVersion: ver,
}, nil
}
func (d Distro) MajorVersion() int {
return d.Version.Segments()[0]
func (d Distro) MajorVersion() string {
return fmt.Sprintf("%d", d.Version.Segments()[0])
}
func (d Distro) FullVersion() string {
return d.RawVersion
}
func (d Distro) String() string {

View 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)
}
})
}
}