From 610febd0555e340d6f261970f95e36b3e59a8642 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Wed, 15 May 2019 13:12:36 -0400 Subject: [PATCH] more testing, refactoring --- main.go | 3 ++- tidelift_yml.go | 23 ++++++++++++++++------- tidelift_yml_test.go | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index f27e34d..0bce5d5 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/tidelift_yml.go b/tidelift_yml.go index 5a0c2e0..d7464d7 100644 --- a/tidelift_yml.go +++ b/tidelift_yml.go @@ -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" diff --git a/tidelift_yml_test.go b/tidelift_yml_test.go index a5a7475..4df7a54 100644 --- a/tidelift_yml_test.go +++ b/tidelift_yml_test.go @@ -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") +}