diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-11-18 18:13:30 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-11-24 11:17:07 +0300 |
commit | 2c4d2217f8779344cc360272ef9931f6de392171 (patch) | |
tree | 835442cc3d2dcec030019189cd77586fea251fed | |
parent | 3432d1c3d15817c91cd98b8e7aa72f89765b86e0 (diff) |
testhelper: Stop writing environment into testdata
In order to capture the environment for both hooks and Git itsel, we're
writing a custom scripts which write the environment into `tesdata/`.
This commit converts them to instead use the global temporary test
directory.
-rw-r--r-- | internal/testhelper/git_protocol.go | 25 | ||||
-rw-r--r-- | internal/testhelper/hook_env.go | 30 | ||||
-rw-r--r-- | internal/testhelper/testhelper.go | 3 |
3 files changed, 33 insertions, 25 deletions
diff --git a/internal/testhelper/git_protocol.go b/internal/testhelper/git_protocol.go index 0e0a3690a..ab9e74c6f 100644 --- a/internal/testhelper/git_protocol.go +++ b/internal/testhelper/git_protocol.go @@ -2,12 +2,11 @@ package testhelper import ( "fmt" - "io/ioutil" "os" "path/filepath" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/gitaly/internal/command" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" ) @@ -17,25 +16,29 @@ import ( // restores the given settings as well as an array of environment variables // which need to be set when invoking Git with this setup. func EnableGitProtocolV2Support(t *testing.T) func() { + envPath := filepath.Join(testDirectory, "git-env") + script := fmt.Sprintf(`#!/bin/sh mkdir -p testdata -env | grep ^GIT_PROTOCOL= >>testdata/git-env +env | grep ^GIT_PROTOCOL= >>"%s" exec "%s" "$@" -`, command.GitPath()) +`, envPath, command.GitPath()) - dir, err := ioutil.TempDir("", "gitaly-test-*") - require.NoError(t, err) + dir, cleanupDir := TempDir(t) path := filepath.Join(dir, "git") - - cleanup, err := WriteExecutable(path, []byte(script)) - require.NoError(t, err) + cleanupExe, err := WriteExecutable(path, []byte(script)) + if !assert.NoError(t, err) { + cleanupDir() + t.FailNow() + } oldGitBinPath := config.Config.Git.BinPath config.Config.Git.BinPath = path return func() { - os.Remove("testdata/git-env") + os.Remove(envPath) config.Config.Git.BinPath = oldGitBinPath - cleanup() + cleanupExe() + cleanupDir() } } diff --git a/internal/testhelper/hook_env.go b/internal/testhelper/hook_env.go index 6960c924e..b40991e6e 100644 --- a/internal/testhelper/hook_env.go +++ b/internal/testhelper/hook_env.go @@ -7,30 +7,36 @@ import ( "path/filepath" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/gitaly/internal/git/hooks" "gitlab.com/gitlab-org/gitaly/internal/gitaly/config" ) // CaptureHookEnv creates a bogus 'update' Git hook to sniff out what // environment variables get set for hooks. -func CaptureHookEnv(t testing.TB) (hookPath string, cleanup func()) { - var err error - oldOverride := hooks.Override - hooks.Override, err = filepath.Abs("testdata/scratch/hooks") - require.NoError(t, err) +func CaptureHookEnv(t testing.TB) (string, func()) { + tempDir, cleanup := TempDir(t) - hookOutputFile, err := filepath.Abs("testdata/scratch/hook.env") - require.NoError(t, err) + oldOverride := hooks.Override + hooks.Override = filepath.Join(tempDir, "hooks") + hookOutputFile := filepath.Join(tempDir, "hook.env") - require.NoError(t, os.RemoveAll(hookOutputFile)) + if !assert.NoError(t, os.MkdirAll(hooks.Override, 0755)) { + cleanup() + t.FailNow() + } - require.NoError(t, os.MkdirAll(hooks.Override, 0755)) - require.NoError(t, ioutil.WriteFile(filepath.Join(hooks.Override, "update"), []byte(` + script := []byte(` #!/bin/sh -env | grep -e ^GIT -e ^GL_ > `+hookOutputFile+"\n"), 0755)) +env | grep -e ^GIT -e ^GL_ > ` + hookOutputFile + "\n") + + if !assert.NoError(t, ioutil.WriteFile(filepath.Join(hooks.Override, "update"), script, 0755)) { + cleanup() + t.FailNow() + } return hookOutputFile, func() { + cleanup() hooks.Override = oldOverride } } diff --git a/internal/testhelper/testhelper.go b/internal/testhelper/testhelper.go index 7389ef7ea..41c468283 100644 --- a/internal/testhelper/testhelper.go +++ b/internal/testhelper/testhelper.go @@ -50,7 +50,6 @@ import ( const ( RepositoryAuthToken = "the-secret-token" DefaultStorageName = "default" - testGitEnv = "testdata/git-env" GlRepository = "project-1" GlID = "user-123" GlProjectPath = "gitlab-org/gitlab-test" @@ -390,7 +389,7 @@ func ConfigureRuby(cfg *config.Cfg) error { // GetGitEnvData reads and returns the content of testGitEnv func GetGitEnvData() (string, error) { - gitEnvBytes, err := ioutil.ReadFile(testGitEnv) + gitEnvBytes, err := ioutil.ReadFile(filepath.Join(testDirectory, "git-env")) if err != nil { return "", err |