Skip to content

Commit 634f2c8

Browse files
committed
Allow printing of nil Version/RelaxedVersion without panic
1 parent 6e1bf36 commit 634f2c8

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

relaxed_version.go

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ func ParseRelaxed(in string) *RelaxedVersion {
3333
}
3434

3535
func (v *RelaxedVersion) String() string {
36+
if v == nil {
37+
return ""
38+
}
3639
if v.version != nil {
3740
return v.version.String()
3841
}

relaxed_version_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ func TestRelaxedVersionComparator(t *testing.T) {
9191
ParseRelaxed("0.0.0+aaa.bbb"),
9292
)
9393
}
94+
95+
func TestNilRelaxedVersionString(t *testing.T) {
96+
var nilVersion *RelaxedVersion
97+
require.Equal(t, "", nilVersion.String())
98+
}

version.go

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ type Version struct {
1717
}
1818

1919
func (v *Version) String() string {
20+
if v == nil {
21+
return ""
22+
}
2023
res := string(v.major)
2124
if len(v.minor) > 0 {
2225
res += "." + string(v.minor)

version_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,8 @@ func TestVersionComparator(t *testing.T) {
9898
MustParse("0.0.0-ab+aaa.bbb"),
9999
)
100100
}
101+
102+
func TestNilVersionString(t *testing.T) {
103+
var nilVersion *Version
104+
require.Equal(t, "", nilVersion.String())
105+
}

0 commit comments

Comments
 (0)