Skip to content

Commit

Permalink
cmd/status: fix error message if tag does not exist (test: status_err_3)
Browse files Browse the repository at this point in the history
  • Loading branch information
irkode committed Dec 5, 2024
1 parent 0e71a65 commit 903dc96
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 145 deletions.
3 changes: 2 additions & 1 deletion cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func status(cmd *cobra.Command) error {
if len(r) == 0 || strings.ToLower(string(r[0])) == "y" {
err = use(version)
if err != nil {
return err
theFix := fmt.Sprintf("run \"%[1]s use\" to select a version, or \"%[1]s disable\" to remove the file", App.Name)
return fmt.Errorf("the version specified in the %s file (%s) is not available in the repository: %s", App.DotFileName, version, theFix)
}
fmt.Println()
break
Expand Down
101 changes: 0 additions & 101 deletions cmd/use-getTagFromString_test.go

This file was deleted.

50 changes: 7 additions & 43 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ To bypass the selection screen provide a version argument to the command.`,

if useVersionInDotFile {
version, err = getVersionFromDotFile()
cobra.CheckErr(err)
cobra.CheckErr(err)
if version == "" {
cobra.CheckErr(fmt.Errorf("the current directory does not contain an .hvm file"))
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func use(version string) error {
asset := newAsset()
repo := newRepository()

if version == "" {
if version == "" {
msg := "Select a version to use in the current directory"
err := repo.selectTag(asset, msg)
if err != nil {
Expand Down Expand Up @@ -254,7 +254,7 @@ func (r *repository) fetchTags() error {

// getLatestTag returns the most recent tag from repository.
func (r *repository) getLatestTag(a *asset) error {
if "" == r.latestTag {
if r.latestTag == "" {
return fmt.Errorf("no latest release found")
}
a.tag = r.latestTag
Expand All @@ -264,53 +264,19 @@ func (r *repository) getLatestTag(a *asset) error {
func (r *repository) getTagFromString(a *asset, version string) error {
inputVersion := version
// fast return for simple cases
switch version {
case "latest", "v":
if version == "latest" {
return r.getLatestTag(a)
}
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
if strings.HasPrefix(version, "v.") { // empty major
version = strings.Replace(version, "v.", semver.Major(r.latestTag)+".", 1)
}
if semver.Compare(version, "") == 0 {
return fmt.Errorf("invalid tag: %s", inputVersion)
}
if version == semver.Canonical(version) { // major.minor.patch
a.tag = version
} else {
type CompareSemVer func(semver string) bool
var compareSemVer CompareSemVer
if version == semver.Major(version) { // major only
compareSemVer = func(t string) bool {
return semver.Compare(semver.Major(version), semver.Major(t)) >= 0
}
} else if version == semver.MajorMinor(version) { // major.minor
compareSemVer = func(t string) bool {
return semver.Major(version) == semver.Major(t) && semver.Compare(semver.MajorMinor(version), semver.MajorMinor(t)) >= 0
}
}
if Config.SortAscending {
for i := len(r.tags) - 1; i >= 0; i-- {
tag := r.tags[i]
if compareSemVer(tag) {
a.tag = tag
return nil
}
}
} else {
for _, tag := range r.tags {
if compareSemVer(tag) {
a.tag = tag
return nil
}
}
}
}
if !slices.Contains(r.tags, a.tag) {
if !slices.Contains(r.tags, version) {
return fmt.Errorf("tag \"%s\" not found in repository", inputVersion)
}
a.tag = version
return nil
}

Expand Down Expand Up @@ -460,9 +426,7 @@ func (a *asset) downloadAsset() error {

// Check server response.
if resp.StatusCode != http.StatusOK {
if err != nil {
return fmt.Errorf("bad status: %s", resp.Status)
}
return fmt.Errorf("bad status: %s", resp.Status)
}

// Write the body to file.
Expand Down
72 changes: 72 additions & 0 deletions cmd/use_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright © 2023 Joe Mooring <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"fmt"
"testing"
)

func newMockRepo() *repository {
r := repository{
tags: []string{"v1.2.3", "v1.2.2", "v1.2.1"},
latestTag: "v1.2.3",
}
return &r
}

var testCases = []struct {
given string
want string
}{
// valid existing version
{"latest", "v1.2.3"},
{"v1.2.3", "v1.2.3"},
{"1.2.3", "v1.2.3"},
{"v1.2.2", "v1.2.2"},
{"1.2.2", "v1.2.2"},
{"v1.2.1", "v1.2.1"},
{"1.2.1", "v1.2.1"},
// valid missing version
{"v1.2.0", ""},
{"1.2.0", ""},
// invalid strings
{"", ""},
{"late", ""},
{"a.b.c", ""},
{"1", ""},
{"1.2.", ""},
{"1.2.", ""},
{"1.2.3.", ""},
{"1.2.3.4", ""},
}

func TestGetTagFromString(t *testing.T) {
// mock needed elements and sort config
Config.SortAscending = false
repo := newMockRepo()
asset := newAsset()
for _, tc := range testCases {
t.Run(fmt.Sprintf("[%s] ShouldBe [%s]", tc.given, tc.want), func(t *testing.T) {
asset.tag = ""
err := repo.getTagFromString(asset, tc.given)
if asset.tag != tc.want {
t.Fatalf("given(%s) -> calculated(%s) -> want(%s) : %s", tc.given, asset.tag, tc.want, err)
}
})
}
}

0 comments on commit 903dc96

Please sign in to comment.