Skip to content

Commit

Permalink
Exclude bad versions like hello 2.1 from the IsPartialVersion test
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Apr 26, 2019
1 parent 652481c commit da8ba98
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (config *TGFConfig) SetDefaultValues(ssmParameterFolder, location, files st
}

var reVersion = regexp.MustCompile(`(?P<version>\d+\.\d+(?:\.\d+){0,1})`)
var reVersionWithEndMarkers = regexp.MustCompile(`^` + reVersion.String() + `$`)

// https://regex101.com/r/ZKt4OP/5
var reImage = regexp.MustCompile(`^(?P<image>.*?)(?::(?:` + reVersion.String() + `(?:(?P<sep>[\.-])(?P<spec>.+))?|(?P<fix>.+)))?$`)
Expand Down Expand Up @@ -286,7 +287,9 @@ func (config *TGFConfig) Validate() (errors []error) {

// IsPartialVersion returns true if the given version is partial (x.x instead of semver's x.x.x)
func (config *TGFConfig) IsPartialVersion() bool {
return config.ImageVersion != nil && reVersion.MatchString(*config.ImageVersion) && strings.Count(*config.ImageVersion, ".") == 1
return config.ImageVersion != nil &&
reVersionWithEndMarkers.MatchString(*config.ImageVersion) &&
strings.Count(*config.ImageVersion, ".") == 1
}

// GetImageName returns the actual image name
Expand Down
50 changes: 50 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,56 @@ func TestParseAliases(t *testing.T) {
}
}

func TestIsPartialVersion(t *testing.T) {
tests := []struct {
name string
version *string
isPartial bool
}{
{
"nil version",
nil,
false,
},
{
"partial",
aws.String("2.1"),
true,
},
{
"full",
aws.String("2.1.2"),
false,
},
{
"non-semver",
aws.String("stuff"),
false,
},
{
"partial-letters",
aws.String("a.b"),
false,
},
{
"partial with tag (this is not a real version, TGF would give a warning)",
aws.String("2.1-k8s"),
false,
},
{
"partial with non-semver word",
aws.String("hello 2.1"),
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := &TGFConfig{ImageVersion: tt.version}
assert.Equal(t, tt.isPartial, config.IsPartialVersion())
})
}
}

func writeSSMConfig(parameterFolder, parameterKey, parameterValue string) {
fullParameterKey := fmt.Sprintf("%s/%s", parameterFolder, parameterKey)
client := getSSMClient()
Expand Down

0 comments on commit da8ba98

Please sign in to comment.