Skip to content

Commit

Permalink
fix: add a routable string helper to verevet.Version
Browse files Browse the repository at this point in the history
- helps with templating version string
  • Loading branch information
tinygrasshopper committed Nov 26, 2024
1 parent 0b5f26b commit 51876ff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
9 changes: 9 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func (v Version) String() string {
return d
}

// RoutableString returns the string representation of the version that can used while setting up the router.
// If the version is before the DefaultPivotDate, it will return the stability in the string.
func (v Version) RoutableString() string {
if v.Date.Before(DefaultPivotDate.Date) {
return v.String()
}
return v.DateString()
}

// AddDays returns the version corresponding to adding the given number of days
// to the version date.
func (v Version) AddDays(days int) Version {
Expand Down
51 changes: 51 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,54 @@ func TestVersionIndex_ResolveGAorBetaStability(t *testing.T) {
})
}
}

func TestVersion_RoutableString(t *testing.T) {
tests := []struct {
name string
v Version
want string
}{
{
name: "ga before pivot date",
v: MustParseVersion("2021-06-07"),
want: "2021-06-07",
},
{
name: "beta before pivot date",
v: MustParseVersion("2021-06-07~beta"),
want: "2021-06-07~beta",
},
{
name: "beta before pivot date",
v: MustParseVersion("2021-06-07~experimental"),
want: "2021-06-07~experimental",
},
{
name: "ga on pivot date",
v: MustParseVersion("2024-10-15"),
want: "2024-10-15",
},
{
name: "beta on pivot date",
v: MustParseVersion("2024-10-15~beta"),
want: "2024-10-15",
},
{
name: "ga after pivot date",
v: MustParseVersion("2024-11-15"),
want: "2024-11-15",
},
{
name: "beta after pivot date",
v: MustParseVersion("2024-11-15~beta"),
want: "2024-11-15",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.v.RoutableString(); got != tt.want {
t.Errorf("RoutableString() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 51876ff

Please sign in to comment.