From f3e356f3149da8d62d7ee77f7c1f79c43a4b819c Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Tue, 14 May 2019 23:54:04 -0400 Subject: [PATCH] dont change dir, add more tests --- helpers.go | 14 ++++++++++++++ main.go | 4 +++- manifests.go | 8 +++----- manifests_test.go | 17 ++++++++--------- tidelift_yml.go | 16 ++++++++++++++-- tidelift_yml_test.go | 16 ++++++++++++++++ 6 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 helpers.go create mode 100644 tidelift_yml_test.go diff --git a/helpers.go b/helpers.go new file mode 100644 index 0000000..f1babb5 --- /dev/null +++ b/helpers.go @@ -0,0 +1,14 @@ +package main + +import "os" + +func tempChdir(directory string) string { + wd, _ := os.Getwd() + os.Chdir(directory) + return wd + +} + +func fixWD(directory string) { + os.Chdir(directory) +} diff --git a/main.go b/main.go index d4b5500..f27e34d 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,9 @@ func main() { func getDirectory(c *cli.Context) string { // Return a directory if it's in the arguments if len(c.Args()) > 0 { - return c.Args().First() + path := c.Args().First() + path = strings.TrimRight(path, "/") + return path } return "." } diff --git a/manifests.go b/manifests.go index 3a05c5a..3cfe100 100644 --- a/manifests.go +++ b/manifests.go @@ -3,7 +3,6 @@ package main import ( "fmt" "log" - "os" "github.com/bmatcuk/doublestar" ) @@ -115,11 +114,10 @@ var ( // // returns string[] of filenames relative to the directory passed in. func getListOfManifestFilenames(directory string) []string { - os.Chdir(directory) matchingFiles := make([]string, 0) for _, glob := range manifestGlobs { - files := getListOfGlobMatches(glob) + files := getListOfGlobMatches(directory, glob) if len(files) > 0 { matchingFiles = append(matchingFiles, files...) @@ -132,8 +130,8 @@ func getListOfManifestFilenames(directory string) []string { // all instances of a manifest file pattern, in your given directory // // returns string[] of all matching file patterns -func getListOfGlobMatches(glob string) []string { - pathGlob := fmt.Sprintf("**/%s", glob) +func getListOfGlobMatches(directory string, glob string) []string { + pathGlob := fmt.Sprintf("%s/**/%s", directory, glob) files, err := doublestar.Glob(pathGlob) if err != nil { diff --git a/manifests_test.go b/manifests_test.go index 4a3bf13..f8890ff 100644 --- a/manifests_test.go +++ b/manifests_test.go @@ -1,7 +1,6 @@ package main import ( - "os" "sort" "testing" @@ -10,12 +9,12 @@ import ( func TestGetListOfGlobMatches(t *testing.T) { //assert getManifestMatches gets all requirements.txt - os.Chdir("test/good_config") expected := []string{ - "subdir/requirements.txt", - "a/b/c/d/e/f/g/requirements.txt", + "test/good_config/subdir/requirements.txt", + "test/good_config/a/b/c/d/e/f/g/requirements.txt", } - got := getListOfGlobMatches("**/requirements.txt") + got := getListOfGlobMatches("test/good_config", "**/requirements.txt") + sort.Strings(expected) sort.Strings(got) @@ -24,10 +23,10 @@ func TestGetListOfGlobMatches(t *testing.T) { func TestGetListOfManifestFilenames(t *testing.T) { expected := []string{ - "yarn.lock", - "Gemfile", - "a/b/c/d/e/f/g/requirements.txt", - "subdir/requirements.txt", + "test/good_config/yarn.lock", + "test/good_config/Gemfile", + "test/good_config/a/b/c/d/e/f/g/requirements.txt", + "test/good_config/subdir/requirements.txt", } got := getListOfManifestFilenames("test/good_config") diff --git a/tidelift_yml.go b/tidelift_yml.go index 462e871..c973ce4 100644 --- a/tidelift_yml.go +++ b/tidelift_yml.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "io/ioutil" "os" "path/filepath" @@ -9,8 +10,9 @@ import ( ) func verifyTideliftYamlExists(directory string) bool { - os.Chdir(directory) - if _, err := os.Stat(".tidelift.yml"); err == nil { + filepath := fmt.Sprintf("%s/.tidelift.yml", directory) + + if fileExists(filepath) { return true } @@ -52,3 +54,13 @@ func check(e error) { panic(e) } } + +func fileExists(filename string) bool { + + info, err := os.Stat(filename) + + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/tidelift_yml_test.go b/tidelift_yml_test.go new file mode 100644 index 0000000..a5a7475 --- /dev/null +++ b/tidelift_yml_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestVerifyTideliftYamlExists(t *testing.T) { + verifiedFalse := verifyTideliftYamlExists("test/no_config") + assert.False(t, verifiedFalse, "should not be a .tidelift.yml in test/no_config") + + verifiedTrue := verifyTideliftYamlExists("test/bad_config") + assert.True(t, verifiedTrue, "should be a .tidelift.yml in test/bad_config") + +}