Skip to content

Commit

Permalink
Pull out function so that we can test it
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomorain authored and johnswanson committed Jul 23, 2021
1 parent 2540675 commit b57cba6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
29 changes: 22 additions & 7 deletions update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ func checkFromSource(check *Options) error {
return err
}

// Homebrew revisions get added to the version with an underscore.
// So `1.2.3 revision 4` becomes `1.2.3_4`. This fails to parse as valid semver
// version. We can work around this by replacing underscores with `-` to convert
// the revision to a semver tag.
// https://github.com/CircleCI-Public/circleci-cli/issues/610
func ParseHomebrewVersion(homebrewVesion string) (semver.Version, error) {

withRevisionAsTag := strings.Replace(homebrewVesion, "_", "-", 10)

version, err := semver.Parse(withRevisionAsTag)

if err != nil {
return semver.Version{}, fmt.Errorf("failed to parse current version from %s: %w", homebrewVesion, err)
}

return version, nil
}

func checkFromHomebrew(check *Options) error {
brew, err := exec.LookPath("brew")
if err != nil {
Expand All @@ -91,20 +109,17 @@ func checkFromHomebrew(check *Options) error {
for _, o := range outdated.Formulae {
if o.Name == "circleci" {
if len(o.InstalledVersions) > 0 {
// homebrew versions may have a revision number appended, like
// `1.2.3_<revision_number>`. This is not valid semver, but we
// can make it valid by replacing the underscore with a hyphen.
current, err := semver.Parse(strings.Replace(o.InstalledVersions[0], "_", "-"))
current, err := ParseHomebrewVersion(o.InstalledVersions[0])
if err != nil {
return errors.Wrap(err, "failed to parse current version from `brew outdated --json=v2`")
return err
}
check.Current = current
}

// see above regarding homebrew / revision numbers
latest, err := semver.Parse(strings.Replace(o.CurrentVersion, "_", "-"))
latest, err := ParseHomebrewVersion(o.CurrentVersion)
if err != nil {
return errors.Wrap(err, "failed to parse latest version from `brew outdated --json=v2`")
return err
}
check.Latest = &selfupdate.Release{
Version: latest,
Expand Down
13 changes: 13 additions & 0 deletions update/update_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package update_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestUpdate(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Update Suite")
}
31 changes: 31 additions & 0 deletions update/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package update_test

import (
"github.com/CircleCI-Public/circleci-cli/update"
"github.com/blang/semver"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Homebrew Version Parsing", func() {

It("Should parse simple versions", func() {
version, err := update.ParseHomebrewVersion("1.0.0")
Expect(err).ShouldNot(HaveOccurred())
Expect(version.String()).To(Equal("1.0.0"))
})

It("Should parse strings with revisions", func() {
version, err := update.ParseHomebrewVersion("0.1.15410_1")
Expect(err).ShouldNot(HaveOccurred())
Expect(version.String()).To(Equal("0.1.15410-1"))
Expect(version.Pre).To(HaveLen(1))
Expect(version.Pre[0]).To(Equal(semver.PRVersion{VersionNum: 1, IsNum: true}))
})

It("Should can deal with garbage", func() {
_, err := update.ParseHomebrewVersion("asdad.1231.-_")
Expect(err).To(MatchError(MatchRegexp("failed to parse current version")))
Expect(err).To(MatchError(MatchRegexp("asdad.1231.-_")))
})
})

0 comments on commit b57cba6

Please sign in to comment.