return error instead of handling it inside getLatestVersion

This commit is contained in:
2025-12-19 06:50:35 -08:00
parent 9e7bc31d4d
commit 42f9e6d458
2 changed files with 16 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package recipe package recipe
import ( import (
"errors"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
@ -19,6 +20,9 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
// Errors
var emptyVersionsInCatalogue = errors.New("Catalogue versions list is empty (unexpectedly!)")
// translators: `abra recipe reset` aliases. use a comma separated list of // translators: `abra recipe reset` aliases. use a comma separated list of
// aliases with no spaces in between // aliases with no spaces in between
var recipeSyncAliases = i18n.G("s") var recipeSyncAliases = i18n.G("s")
@ -127,7 +131,10 @@ likely to change.
} }
latestRelease := tags[len(tags)-1] latestRelease := tags[len(tags)-1]
latestRecipeVersion := getLatestVersion(recipe, catl) latestRecipeVersion, err := getLatestVersion(recipe, catl)
if err != nil && err != emptyVersionsInCatalogue {
log.Fatal(err)
}
changesTable.Headers(i18n.G("SERVICE"), latestRelease, i18n.G("PROPOSED CHANGES")) changesTable.Headers(i18n.G("SERVICE"), latestRelease, i18n.G("PROPOSED CHANGES"))
allRecipeVersions := catl[recipe.Name].Versions allRecipeVersions := catl[recipe.Name].Versions
@ -294,14 +301,13 @@ func init() {
) )
} }
func getLatestVersion(recipe recipePkg.Recipe, catl recipePkg.RecipeCatalogue) string { func getLatestVersion(recipe recipePkg.Recipe, catl recipePkg.RecipeCatalogue) (string, error) {
versions, err := recipePkg.GetRecipeCatalogueVersions(recipe.Name, catl) versions, err := recipePkg.GetRecipeCatalogueVersions(recipe.Name, catl)
if err != nil { if err != nil {
log.Fatal(err) return "", err
} }
latestRecipeVersion := "0.0.0"
if len(versions) > 0 { if len(versions) > 0 {
latestRecipeVersion = versions[len(versions)-1] return versions[len(versions)-1], nil
} }
return latestRecipeVersion return "", emptyVersionsInCatalogue
} }

View File

@ -7,11 +7,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestGetLatestVersionReturnsDefault(t *testing.T) { func TestGetLatestVersionReturnsErrorWhenVersionsIsEmpty(t *testing.T) {
recipe := recipePkg.Recipe{} recipe := recipePkg.Recipe{}
catalogue := recipePkg.RecipeCatalogue{} catalogue := recipePkg.RecipeCatalogue{}
version := getLatestVersion(recipe, catalogue) _, err := getLatestVersion(recipe, catalogue)
assert.Equal(t, version, "0.0.0") assert.Equal(t, err, emptyVersionsInCatalogue)
} }
func TestGetLatestVersionReturnsLastVersion(t *testing.T) { func TestGetLatestVersionReturnsLastVersion(t *testing.T) {
@ -28,6 +28,6 @@ func TestGetLatestVersionReturnsLastVersion(t *testing.T) {
catalogue["test"] = recipePkg.RecipeMeta{ catalogue["test"] = recipePkg.RecipeMeta{
Versions: versions, Versions: versions,
} }
version := getLatestVersion(recipe, catalogue) version, _ := getLatestVersion(recipe, catalogue)
assert.Equal(t, version, "0.0.3") assert.Equal(t, version, "0.0.3")
} }