[command.package] Add tests

Signed-off-by: Debdut Chakraborty <debdut.chakraborty@rocket.chat>
pull/11220/head
Debdut Chakraborty 3 years ago
parent c8311a0342
commit 5aa65a8c8b

@ -62,8 +62,8 @@ func (p *Package) Run(path string, vals map[string]interface{}) (string, error)
return "", err
}
if matched, _ := regexp.MatchString("^[[:lower:][:digit:]-]+$", ch.Name()); !matched {
return "", errors.New(fmt.Sprintf("Invalid chart name \"%s\". Chart names must be lower case letters and numbers with optionally dashes (-) for separator\n", ch.Name()))
if err := validateName(ch.Name()); err != nil {
return "", err
}
// If version is set, modify the version.
@ -109,6 +109,14 @@ func (p *Package) Run(path string, vals map[string]interface{}) (string, error)
return name, err
}
// validateName verifies if the chart name has any illegal characters or not
func validateName(name string) error {
if matched, _ := regexp.MatchString("^[[:lower:][:digit:]-]+$", name); !matched {
return errors.New(fmt.Sprintf("Invalid chart name \"%s\". Chart names must be lower case letters and numbers with optionally dashes (-) for separator\n", name))
}
return nil
}
// validateVersion Verify that version is a Version, and error out if it is not.
func validateVersion(ver string) error {
if _, err := semver.NewVersion(ver); err != nil {

@ -17,6 +17,7 @@ limitations under the License.
package action
import (
"fmt"
"io/ioutil"
"os"
"path"
@ -123,3 +124,50 @@ func TestValidateVersion(t *testing.T) {
})
}
}
func TestValidateName(t *testing.T) {
type args struct {
name string
}
expectedError := "Invalid chart name \"%s\". Chart names must be lower case letters and numbers with optionally dashes (-) for separator\n"
tests := []struct {
name string
args
}{
{
"Uppercase letter in name",
args{
name: "TestChart",
},
},
{
"Whitespace ( ) in name",
args{
name: "test chart",
},
},
{
"Underscore (_) in name",
args{
name: "test_chart",
},
},
{
"dot (.) in name",
args{
name: "TestChart",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := validateName(test.args.name); err != nil {
er := fmt.Sprintf(expectedError, test.args.name)
if err.Error() != er {
t.Errorf("Expected {%v}, got {%v}", er, err)
}
}
})
}
}

Loading…
Cancel
Save