dont change dir, add more tests

This commit is contained in:
Tyrel Souza 2019-05-14 23:54:04 -04:00 committed by Tyrel Souza
parent efc5a3267f
commit f3e356f314
No known key found for this signature in database
GPG Key ID: 5A9394D4C30AEAC0
6 changed files with 58 additions and 17 deletions

14
helpers.go Normal file
View File

@ -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)
}

View File

@ -57,7 +57,9 @@ func main() {
func getDirectory(c *cli.Context) string { func getDirectory(c *cli.Context) string {
// Return a directory if it's in the arguments // Return a directory if it's in the arguments
if len(c.Args()) > 0 { if len(c.Args()) > 0 {
return c.Args().First() path := c.Args().First()
path = strings.TrimRight(path, "/")
return path
} }
return "." return "."
} }

View File

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
) )
@ -115,11 +114,10 @@ var (
// //
// returns string[] of filenames relative to the directory passed in. // returns string[] of filenames relative to the directory passed in.
func getListOfManifestFilenames(directory string) []string { func getListOfManifestFilenames(directory string) []string {
os.Chdir(directory)
matchingFiles := make([]string, 0) matchingFiles := make([]string, 0)
for _, glob := range manifestGlobs { for _, glob := range manifestGlobs {
files := getListOfGlobMatches(glob) files := getListOfGlobMatches(directory, glob)
if len(files) > 0 { if len(files) > 0 {
matchingFiles = append(matchingFiles, files...) matchingFiles = append(matchingFiles, files...)
@ -132,8 +130,8 @@ func getListOfManifestFilenames(directory string) []string {
// all instances of a manifest file pattern, in your given directory // all instances of a manifest file pattern, in your given directory
// //
// returns string[] of all matching file patterns // returns string[] of all matching file patterns
func getListOfGlobMatches(glob string) []string { func getListOfGlobMatches(directory string, glob string) []string {
pathGlob := fmt.Sprintf("**/%s", glob) pathGlob := fmt.Sprintf("%s/**/%s", directory, glob)
files, err := doublestar.Glob(pathGlob) files, err := doublestar.Glob(pathGlob)
if err != nil { if err != nil {

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"os"
"sort" "sort"
"testing" "testing"
@ -10,12 +9,12 @@ import (
func TestGetListOfGlobMatches(t *testing.T) { func TestGetListOfGlobMatches(t *testing.T) {
//assert getManifestMatches gets all requirements.txt //assert getManifestMatches gets all requirements.txt
os.Chdir("test/good_config")
expected := []string{ expected := []string{
"subdir/requirements.txt", "test/good_config/subdir/requirements.txt",
"a/b/c/d/e/f/g/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(expected)
sort.Strings(got) sort.Strings(got)
@ -24,10 +23,10 @@ func TestGetListOfGlobMatches(t *testing.T) {
func TestGetListOfManifestFilenames(t *testing.T) { func TestGetListOfManifestFilenames(t *testing.T) {
expected := []string{ expected := []string{
"yarn.lock", "test/good_config/yarn.lock",
"Gemfile", "test/good_config/Gemfile",
"a/b/c/d/e/f/g/requirements.txt", "test/good_config/a/b/c/d/e/f/g/requirements.txt",
"subdir/requirements.txt", "test/good_config/subdir/requirements.txt",
} }
got := getListOfManifestFilenames("test/good_config") got := getListOfManifestFilenames("test/good_config")

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -9,8 +10,9 @@ import (
) )
func verifyTideliftYamlExists(directory string) bool { func verifyTideliftYamlExists(directory string) bool {
os.Chdir(directory) filepath := fmt.Sprintf("%s/.tidelift.yml", directory)
if _, err := os.Stat(".tidelift.yml"); err == nil {
if fileExists(filepath) {
return true return true
} }
@ -52,3 +54,13 @@ func check(e error) {
panic(e) panic(e)
} }
} }
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

16
tidelift_yml_test.go Normal file
View File

@ -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")
}