From 00a6a2928e86bf2861f1d76978b2a4206deac8b2 Mon Sep 17 00:00:00 2001 From: Keegan Carruthers-Smith Date: Fri, 23 Sep 2022 09:52:19 +0200 Subject: [PATCH] check for .git in all tests --- cmd/git-sg/catfile_test.go | 4 ++++ cmd/git-sg/main_test.go | 26 ++++++++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cmd/git-sg/catfile_test.go b/cmd/git-sg/catfile_test.go index b6682da0d..cbe9b4b98 100644 --- a/cmd/git-sg/catfile_test.go +++ b/cmd/git-sg/catfile_test.go @@ -9,6 +9,8 @@ import ( ) func TestInfo(t *testing.T) { + setGitDir(t) + p, err := startGitCatFileBatch("") if err != nil { t.Fatal(err) @@ -43,6 +45,8 @@ func TestInfo(t *testing.T) { } func TestContents(t *testing.T) { + setGitDir(t) + p, err := startGitCatFileBatch("") if err != nil { t.Fatal(err) diff --git a/cmd/git-sg/main_test.go b/cmd/git-sg/main_test.go index 9289e7ef6..d933807c3 100644 --- a/cmd/git-sg/main_test.go +++ b/cmd/git-sg/main_test.go @@ -7,15 +7,7 @@ import ( ) func TestDo(t *testing.T) { - dir, err := filepath.Abs("../../.git") - if err != nil { - t.Fatal(err) - } - t.Setenv("GIT_DIR", dir) - - if _, err := os.Stat(dir); os.Getenv("CI") != "" && os.IsNotExist(err) { - t.Skipf("skipping since on CI and this is not a git checkout: %v", err) - } + setGitDir(t) for _, envvar := range []string{"", "GIT_SG_BUFFER", "GIT_SG_FILTER", "GIT_SG_CATFILE", "GIT_SG_LSTREE"} { name := envvar @@ -27,7 +19,7 @@ func TestDo(t *testing.T) { t.Setenv(envvar, "1") } var w countingWriter - err = do(&w) + err := do(&w) if err != nil { t.Fatal(err) } @@ -47,3 +39,17 @@ func (w *countingWriter) Write(b []byte) (int, error) { w.N += len(b) return len(b), nil } + +func setGitDir(t *testing.T) { + t.Helper() + + dir, err := filepath.Abs("../../.git") + if err != nil { + t.Fatal(err) + } + t.Setenv("GIT_DIR", dir) + + if _, err := os.Stat(dir); os.Getenv("CI") != "" && os.IsNotExist(err) { + t.Skipf("skipping since on CI and this is not a git checkout: %v", err) + } +}