From c442e2c98dfb701bfe182eb5c17af37b27e8e2e2 Mon Sep 17 00:00:00 2001 From: Mike Pountney Date: Sun, 15 Sep 2019 19:55:16 -0700 Subject: [PATCH] Add test for findClosestParentPath to check for figtree regression I inadvertently 'upgraded' to coryb/figtree v1.0.0, and it caused an error in findCLosestParentPath, due to figtree.FindParentPaths having a different function signature in v1.0.0 No idea why a regular compile isn't picking this up - but when explicitly testing like this, we get the error detailed in #277: ``` Mikes-MBP:jira mike$ go test ./... ? github.com/go-jira/jira [no test files] jiracli/util.go:29:34: too many arguments in call to figtree.FindParentPaths have (string, string, string) want (string) jiracli/util.go:29:34: too many arguments in call to figtree.FindParentPaths have (string, string, string) want (string) FAIL github.com/go-jira/jira/jiracli [build failed] ok github.com/go-jira/jira/jiradata (cached) Mikes-MBP:jira mike$ ``` --- jiracli/util_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 jiracli/util_test.go diff --git a/jiracli/util_test.go b/jiracli/util_test.go new file mode 100644 index 00000000..8785d03d --- /dev/null +++ b/jiracli/util_test.go @@ -0,0 +1,71 @@ +package jiracli + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func realPath(path string) string { + cpath, err := filepath.EvalSymlinks(path) + if err != nil { + log.Fatal(err) + } + return cpath +} + +func comparePaths(p1 string, p2 string) bool { + if realPath(p1) == realPath(p2) { + return true + } + return false +} + +func TestFindClosestParentPath(t *testing.T) { + dir, err := ioutil.TempDir("", "testFindParentPath") + if err != nil { + log.Fatal(err) + } + defer os.RemoveAll(dir) + + origDir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + defer os.Chdir(origDir) + + t1 := filepath.Join(dir, "/.test1") + t2 := filepath.Join(t1, "/.test2") + t3 := filepath.Join(t2, "/.test1") + err = os.MkdirAll(t3, os.ModePerm) + if err != nil { + log.Fatal(err) + } + os.Chdir(t3) + + path1, err := findClosestParentPath(".test1") + if err != nil { + t.Errorf("findClosestParentPath should not have errored: %s", err) + } + if ok := comparePaths(path1, t3); !ok { + t.Errorf("%s != %s", path1, t3) + } + + path2, err := findClosestParentPath(".test2") + if err != nil { + t.Errorf("findClosestParentPath should not have errored: %s", err) + } + if ok := comparePaths(path2, t2); !ok { + t.Errorf("%s != %s", path2, t2) + } + + path3, err := findClosestParentPath(".test3") + if err.Error() != ".test3 not found in parent directory hierarchy" { + t.Errorf("incorrect error from findClosestParentPath: %s", err) + } + if path3 != "" { + t.Errorf("path3 should be empty, but is not: %s", path3) + } + +}