-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: user strict semver parsing for image and commit selection, but…
… allow opt-out (#2441) Signed-off-by: Kent Rancourt <[email protected]>
- Loading branch information
Showing
22 changed files
with
668 additions
and
452 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package semver | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
func Parse(s string, strict bool) *semver.Version { | ||
// We do the non-strict parsing first, because it will ensure that | ||
// sv.Original() returns the original string from which we have not | ||
// potentially stripped a leading "v". | ||
sv, err := semver.NewVersion(s) | ||
if err != nil { | ||
return nil // tag wasn't a semantic version | ||
} | ||
if strict { | ||
if _, err = semver.StrictNewVersion(strings.TrimPrefix(s, "v")); err != nil { | ||
return nil // tag wasn't a strict semantic version | ||
} | ||
} | ||
return sv | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package semver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
tag string | ||
strict bool | ||
assertions func(t *testing.T, sv *semver.Version) | ||
}{ | ||
{ | ||
name: "invalid with strict parsing", | ||
strict: true, | ||
tag: "invalid", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.Nil(t, sv) | ||
}, | ||
}, | ||
{ | ||
name: "invalid without strict parsing", | ||
strict: false, | ||
tag: "invalid", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.Nil(t, sv) | ||
}, | ||
}, | ||
{ | ||
name: "valid, but not strictly so, with strict parsing", | ||
strict: true, | ||
tag: "1", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.Nil(t, sv) | ||
}, | ||
}, | ||
{ | ||
name: "valid, but not strictly so, without strict parsing", | ||
strict: false, | ||
tag: "1", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.NotNil(t, sv) | ||
require.Equal(t, "1.0.0", sv.String()) | ||
require.Equal(t, "1", sv.Original()) | ||
}, | ||
}, | ||
{ | ||
name: "strictly valid, with strict parsing", | ||
strict: true, | ||
tag: "1.0.0", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.NotNil(t, sv) | ||
require.Equal(t, "1.0.0", sv.String()) | ||
require.Equal(t, "1.0.0", sv.Original()) | ||
}, | ||
}, | ||
{ | ||
name: "strictly valid, without strict parsing", | ||
strict: false, | ||
tag: "1.0.0", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.NotNil(t, sv) | ||
require.Equal(t, "1.0.0", sv.String()) | ||
require.Equal(t, "1.0.0", sv.Original()) | ||
}, | ||
}, | ||
{ | ||
name: "strictly valid, with leading v and strict parsing", | ||
strict: true, | ||
tag: "v1.0.0", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.NotNil(t, sv) | ||
require.Equal(t, "1.0.0", sv.String()) | ||
require.Equal(t, "v1.0.0", sv.Original()) | ||
}, | ||
}, | ||
{ | ||
name: "strictly valid, with leading v, but without strict parsing", | ||
strict: false, | ||
tag: "v1.0.0", | ||
assertions: func(t *testing.T, sv *semver.Version) { | ||
require.NotNil(t, sv) | ||
require.Equal(t, "1.0.0", sv.String()) | ||
require.Equal(t, "v1.0.0", sv.Original()) | ||
}, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
testCase.assertions(t, Parse(testCase.tag, testCase.strict)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.