From 6f6b353c25b777caae4844db836f6546b02cc5eb Mon Sep 17 00:00:00 2001 From: Chris Darroch Date: Fri, 27 Mar 2020 17:28:26 -0700 Subject: [PATCH] t: ensure local binary always used in tests When running our test suite, including the Go tests, we want to ensure that our locally compiled "git-lfs" binary is the one which is used whenever "git-lfs" is invoked, for instance as a Git filter command or as the transfer queue's default custom adapter. Otherwise we may see unexpected test failures or successes, if another "git-lfs" binary installed elsewhere on the system is used instead. Therefore we define an init() function in the common test utilities package which prepends to the PATH environment variable an absolute path to the working tree's "bin/" directory, to ensure our "git-lfs" binary is always found in preference to any other installed versions. --- t/cmd/util/testutils.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/t/cmd/util/testutils.go b/t/cmd/util/testutils.go index 43574b1a..8759be0e 100644 --- a/t/cmd/util/testutils.go +++ b/t/cmd/util/testutils.go @@ -16,6 +16,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" "sync" "time" @@ -27,6 +28,34 @@ import ( "github.com/git-lfs/git-lfs/lfs" ) +func init() { + path := os.Getenv("PATH") + sep := "" + if path != "" { + if runtime.GOOS == "windows" { + sep = ";" + } else { + sep = ":" + } + } + + // Strip the trailing "t/cmd/util/testutils.go" from the path to this + // source file to create a path to the working tree's "bin" directory, + // then prepend that to the PATH environment variable to ensure our + // "git-lfs" binary is used in preference to any installed versions when + // executing the Go tests. + _, srcdir, _, _ := runtime.Caller(0) + for i := 0; i < 4; i++ { + srcdir = filepath.Dir(srcdir) + } + var err error + srcdir, err = filepath.Abs(srcdir) + if err != nil { + panic(err) + } + os.Setenv("PATH", filepath.Join(srcdir, "bin")+sep+path) +} + type RepoType int const (