Skip to content

Commit

Permalink
feat: adding code coverage (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlaricWhitney authored Aug 8, 2023
1 parent 7cea75b commit a87217d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 15 deletions.
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-vela/types v0.14.0 h1:m75BdRfQm9PC4l/oHSgeplt8mqgau3JmqD3DN+KdePk=
github.com/go-vela/types v0.14.0/go.mod h1:Z/94BulwLbd+bSiPVJEUNdQxB1EP2JCYWaBsv/d65vs=
github.com/go-vela/types v0.20.1 h1:hHAX0Iij2J7UZ9f3SlXbwNy481CjKzU9CBfkiLuysVE=
github.com/go-vela/types v0.20.1/go.mod h1:AXO4oQSygOBQ02fPapsKjQHkx2aQO3zTu7clpvVbXBY=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
19 changes: 15 additions & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,28 @@ func New() *version.Version {
Tag = "v0.0.0"
}

var major, minor, patch uint64

var prerelease string

v, err := semver.NewVersion(Tag)
if err != nil {
fmt.Println(fmt.Errorf("unable to parse semantic version for %s: %w", Tag, err))
}

if v != nil {
major = v.Major()
minor = v.Minor()
patch = v.Patch()
prerelease = v.Prerelease()
}

return &version.Version{
Canonical: Tag,
Major: v.Major(),
Minor: v.Minor(),
Patch: v.Patch(),
PreRelease: v.Prerelease(),
Major: major,
Minor: minor,
Patch: patch,
PreRelease: prerelease,
Metadata: version.Metadata{
Architecture: Arch,
BuildDate: Date,
Expand Down
83 changes: 83 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package version

import (
"testing"

"github.com/go-vela/types/version"
"github.com/stretchr/testify/assert"
)

func TestNew(t *testing.T) {
t.Run("Valid Version Output", func(t *testing.T) {
// to avoid flaky tests, set the values
Go = "go1.20.7"
OS = "darwin"
Compiler = "gc"
Tag = "v1.1.1"
Commit = "000"
Date = "111"
Arch = "arm64"

expected := version.Version{
Canonical: "v1.1.1",
Major: 1,
Minor: 1,
Patch: 1,
Metadata: version.Metadata{
Architecture: "arm64",
BuildDate: "111",
Compiler: "gc",
GitCommit: "000",
GoVersion: "go1.20.7",
OperatingSystem: "darwin",
},
}
assert.Equal(t, expected, *New())
})
t.Run("no tag", func(t *testing.T) {
// to avoid flaky tests, set the values
Go = "go1.20.7"
OS = "darwin"
Compiler = "gc"
Tag = ""
Commit = "000"
Date = "111"
Arch = "arm64"

expected := version.Version{
Canonical: "v0.0.0",
Metadata: version.Metadata{
Architecture: "arm64",
BuildDate: "111",
Compiler: "gc",
GitCommit: "000",
GoVersion: "go1.20.7",
OperatingSystem: "darwin",
},
}
assert.Equal(t, expected, *New())
})
t.Run("invalid tag", func(t *testing.T) {
// to avoid flaky tests, set the values
Go = "go1.20.7"
OS = "darwin"
Compiler = "gc"
Tag = "something"
Commit = "000"
Date = "111"
Arch = "arm64"

expected := version.Version{
Canonical: "something",
Metadata: version.Metadata{
Architecture: "arm64",
BuildDate: "111",
Compiler: "gc",
GitCommit: "000",
GoVersion: "go1.20.7",
OperatingSystem: "darwin",
},
}
assert.Equal(t, expected, *New())
})
}

0 comments on commit a87217d

Please sign in to comment.