more testing, refactoring

This commit is contained in:
Tyrel Souza 2019-05-15 13:12:36 -04:00 committed by Tyrel Souza
parent ccd5d5f0cb
commit 610febd055
No known key found for this signature in database
GPG Key ID: 5A9394D4C30AEAC0
3 changed files with 34 additions and 8 deletions

View File

@ -85,7 +85,8 @@ func verify(directory string) error {
if !verifyTideliftYamlExists(directory) {
return cli.NewExitError("no .tidelift.yml at supplied directory path", 6)
}
missingKey := passesMinimumRequirements(directory)
missingKey := passesMinimumRequirements(readTideliftYamlFile(directory))
if missingKey != "" {
errorMsg := fmt.Sprintf("Missing key '%s:' in .tidelift.yml", missingKey)
return cli.NewExitError(errorMsg, 7)

View File

@ -9,6 +9,11 @@ import (
"gopkg.in/yaml.v2"
)
type TideliftYML struct {
TeamName string `yaml:"team-name"`
RepositoryName string `yaml:"repository-name"`
}
func verifyTideliftYamlExists(directory string) bool {
filepath := fmt.Sprintf("%s/.tidelift.yml", directory)
@ -19,25 +24,29 @@ func verifyTideliftYamlExists(directory string) bool {
return false
}
func passesMinimumRequirements(directory string) string {
func readTideliftYamlFile(directory string) []byte {
filename, _ := filepath.Abs(fmt.Sprintf("%s/.tidelift.yml", directory))
yamlFile, err := ioutil.ReadFile(filename)
check(err)
type TideliftYML struct {
TeamName string `yaml:"team-name"`
RepositoryName string `yaml:"repository-name"`
}
return yamlFile
}
func getKeysFromYaml(yamlText []byte) TideliftYML {
var yml TideliftYML
err = yaml.Unmarshal(yamlFile, &yml)
err := yaml.Unmarshal(yamlText, &yml)
check(err)
return yml
}
func passesMinimumRequirements(yamlText []byte) string {
yml := getKeysFromYaml(yamlText)
// Check for Team name
if len(yml.TeamName) <= 0 {
return "team-name"

View File

@ -14,3 +14,19 @@ func TestVerifyTideliftYamlExists(t *testing.T) {
assert.True(t, verifiedTrue, "should be a .tidelift.yml in test/bad_config")
}
func TestPassesMinimumRequirements(t *testing.T) {
missingTeam := []byte("")
assert.Equal(t, "team-name", passesMinimumRequirements(missingTeam), "should be `team-name`")
missingRepo := []byte("team-name: teamname")
assert.Equal(t, "repository-name", passesMinimumRequirements(missingRepo), "should be `repository-name`")
ok := []byte("team-name: teamname\nrepository-name: repo")
assert.Equal(t, "", passesMinimumRequirements(ok), "should be ``")
}
func TestReadTideliftYamlFile(t *testing.T) {
yamlFile := string(readTideliftYamlFile("test/good_config"))
assert.Equal(t, "team-name: tidelift\nrepository-name: fake\n", yamlFile, "should have parsed")
}