Skip to content

Commit

Permalink
Merge pull request #160 from stefafafan/fix-default-vprefix
Browse files Browse the repository at this point in the history
Fix latest semver tag retrieval for first time setup
  • Loading branch information
Songmu committed Oct 31, 2023
2 parents 299e7ea + 76a6214 commit 52a0b9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
14 changes: 10 additions & 4 deletions tagpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ type tagpr struct {

func (tp *tagpr) latestSemverTag() string {
vers := (&gitsemvers.Semvers{GitPath: tp.gitPath}).VersionStrings()
vPrefix := (tp.cfg.vPrefix != nil && *tp.cfg.vPrefix)
for _, v := range vers {
if strings.HasPrefix(v, "v") == vPrefix {
return v
if tp.cfg.vPrefix != nil {
for _, v := range vers {
if strings.HasPrefix(v, "v") == *tp.cfg.vPrefix {
return v
}
}
} else {
// When vPrefix is not defined (i.e. first time tagpr setup), just return the first value.
if len(vers) > 0 {
return vers[0]
}
}
return ""
Expand Down
11 changes: 7 additions & 4 deletions tagpr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,16 @@ func TestGeneratenNextLabels(t *testing.T) {
}

func TestLatestSemverTag(t *testing.T) {
vPrefixTrue := true
vPrefixFalse := false
tests := []struct {
name string
vPrefix bool
vPrefix *bool
wantVer bool
}{
{"github.com/Songmu/tagpr has a semver tag with 'v' prefix", true, true},
{"github.com/Songmu/tagpr has no semver tag without 'v' prefix", false, false},
{"github.com/Songmu/tagpr has a semver tag with 'v' prefix", &vPrefixTrue, true},
{"github.com/Songmu/tagpr has no semver tag without 'v' prefix", &vPrefixFalse, false},
{"github.com/Songmu/tagpr returns the first tag when no config exists", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -329,7 +332,7 @@ func TestLatestSemverTag(t *testing.T) {
return
}
tp.cfg = &config{
vPrefix: &tt.vPrefix,
vPrefix: tt.vPrefix,
}
got := tp.latestSemverTag()
if (got != "") != tt.wantVer {
Expand Down

0 comments on commit 52a0b9c

Please sign in to comment.