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.
This commit is contained in:
Chris Darroch 2020-03-27 17:28:26 -07:00
parent 351fc228ec
commit 6f6b353c25

@ -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 (