Tweak validation messages

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-03-19 03:17:02 +01:00
parent b5d0d179e7
commit e5702e000c
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 11 additions and 11 deletions

View File

@ -100,7 +100,7 @@ func TestParseEnvFileBadlyFormattedFile(t *testing.T) {
if _, ok := err.(ErrBadKey); !ok { if _, ok := err.(ErrBadKey); !ok {
t.Fatalf("Expected an ErrBadKey, got [%v]", err) t.Fatalf("Expected an ErrBadKey, got [%v]", err)
} }
expectedMessage := "poorly formatted environment: variable 'f ' has white spaces" expectedMessage := "poorly formatted environment: variable 'f ' contains whitespaces"
if err.Error() != expectedMessage { if err.Error() != expectedMessage {
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error()) t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
} }
@ -134,7 +134,7 @@ another invalid line`
if _, ok := err.(ErrBadKey); !ok { if _, ok := err.(ErrBadKey); !ok {
t.Fatalf("Expected an ErrBadKey, got [%v]", err) t.Fatalf("Expected an ErrBadKey, got [%v]", err)
} }
expectedMessage := "poorly formatted environment: variable 'first line' has white spaces" expectedMessage := "poorly formatted environment: variable 'first line' contains whitespaces"
if err.Error() != expectedMessage { if err.Error() != expectedMessage {
t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error()) t.Fatalf("Expected [%v], got [%v]", expectedMessage, err.Error())
} }

View File

@ -51,7 +51,7 @@ func parseKeyValueFile(filename string, emptyFn func(string) (string, bool)) ([]
// trim the front of a variable, but nothing else // trim the front of a variable, but nothing else
variable := strings.TrimLeft(data[0], whiteSpaces) variable := strings.TrimLeft(data[0], whiteSpaces)
if strings.ContainsAny(variable, whiteSpaces) { if strings.ContainsAny(variable, whiteSpaces) {
return []string{}, ErrBadKey{fmt.Sprintf("variable '%s' has white spaces", variable)} return []string{}, ErrBadKey{fmt.Sprintf("variable '%s' contains whitespaces", variable)}
} }
if len(variable) == 0 { if len(variable) == 0 {
return []string{}, ErrBadKey{fmt.Sprintf("no variable name on line '%s'", line)} return []string{}, ErrBadKey{fmt.Sprintf("no variable name on line '%s'", line)}

View File

@ -281,10 +281,10 @@ func ValidateLabel(val string) (string, error) {
arr := strings.SplitN(val, "=", 2) arr := strings.SplitN(val, "=", 2)
key := strings.TrimLeft(arr[0], whiteSpaces) key := strings.TrimLeft(arr[0], whiteSpaces)
if key == "" { if key == "" {
return "", fmt.Errorf("empty label name: '%s'", val) return "", fmt.Errorf("invalid label '%s': empty name", val)
} }
if strings.ContainsAny(key, whiteSpaces) { if strings.ContainsAny(key, whiteSpaces) {
return "", fmt.Errorf("label '%s' has white spaces", key) return "", fmt.Errorf("label '%s' contains whitespaces", key)
} }
return val, nil return val, nil
} }

View File

@ -184,17 +184,17 @@ func TestValidateLabel(t *testing.T) {
}{ }{
{ {
name: "empty", name: "empty",
expectedErr: `empty label name: ''`, expectedErr: `invalid label '': empty name`,
}, },
{ {
name: "whitespace only ", name: "whitespace only ",
value: " ", value: " ",
expectedErr: `empty label name: ' '`, expectedErr: `invalid label ' ': empty name`,
}, },
{ {
name: "whitespace around equal-sign", name: "whitespace around equal-sign",
value: " = ", value: " = ",
expectedErr: `empty label name: ' = '`, expectedErr: `invalid label ' = ': empty name`,
}, },
{ {
name: "leading whitespace", name: "leading whitespace",
@ -203,12 +203,12 @@ func TestValidateLabel(t *testing.T) {
{ {
name: "whitespaces in key without value", name: "whitespaces in key without value",
value: "this is a label without value", value: "this is a label without value",
expectedErr: `label 'this is a label without value' has white spaces`, expectedErr: `label 'this is a label without value' contains whitespaces`,
}, },
{ {
name: "whitespaces in key", name: "whitespaces in key",
value: "this is a label=value", value: "this is a label=value",
expectedErr: `label 'this is a label' has white spaces`, expectedErr: `label 'this is a label' contains whitespaces`,
}, },
{ {
name: "whitespaces in value", name: "whitespaces in value",
@ -229,7 +229,7 @@ func TestValidateLabel(t *testing.T) {
{ {
name: "no key", name: "no key",
value: "=label", value: "=label",
expectedErr: `empty label name: '=label'`, expectedErr: `invalid label '=label': empty name`,
}, },
{ {
name: "empty value", name: "empty value",