diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-10-07 15:07:07 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2021-10-08 15:56:14 +0300 |
commit | b0b41211ca6362e53e69afb334e0102f9770597f (patch) | |
tree | 5db04395e8be27ba1e1dd276924d5afc78979bc8 | |
parent | 09a250169a343bb8632f6897bde7d97117768eed (diff) |
testhelper: Expose helper to build Go executables
The `buildBinary()` helper handles building of Go executables, where it
makes sure to build each executable once only. This function is used to
build a set of Gitaly executables which we expect to be present. But in
fact, it's more generally useful to also build for example Go test
servers.
Make the function public such that it can be reused in other packages.
-rw-r--r-- | internal/testhelper/build.go | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/internal/testhelper/build.go b/internal/testhelper/build.go index 2ef0b8cf7..873e3a91f 100644 --- a/internal/testhelper/build.go +++ b/internal/testhelper/build.go @@ -17,7 +17,7 @@ var buildOnceByName sync.Map // BuildGitalyGit2Go builds the gitaly-git2go command and installs it into the binary directory. func BuildGitalyGit2Go(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-git2go") + BuildBinary(t, cfg.BinDir, gitalyCommandPath("gitaly-git2go")) // The link is needed because gitaly uses version-named binary. // Please check out https://gitlab.com/gitlab-org/gitaly/-/issues/3647 for more info. if err := os.Link(filepath.Join(cfg.BinDir, "gitaly-git2go"), filepath.Join(cfg.BinDir, "gitaly-git2go-"+version.GetModuleVersion())); err != nil { @@ -31,28 +31,33 @@ func BuildGitalyGit2Go(t testing.TB, cfg config.Cfg) { // BuildGitalyLFSSmudge builds the gitaly-lfs-smudge command and installs it into the binary // directory. func BuildGitalyLFSSmudge(t *testing.T, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-lfs-smudge") + BuildBinary(t, cfg.BinDir, gitalyCommandPath("gitaly-lfs-smudge")) } // BuildGitalyHooks builds the gitaly-hooks command and installs it into the binary directory. func BuildGitalyHooks(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-hooks") + BuildBinary(t, cfg.BinDir, gitalyCommandPath("gitaly-hooks")) } // BuildGitalySSH builds the gitaly-ssh command and installs it into the binary directory. func BuildGitalySSH(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "gitaly-ssh") + BuildBinary(t, cfg.BinDir, gitalyCommandPath("gitaly-ssh")) } // BuildPraefect builds the praefect command and installs it into the binary directory. func BuildPraefect(t testing.TB, cfg config.Cfg) { - buildBinary(t, cfg.BinDir, "praefect") + BuildBinary(t, cfg.BinDir, gitalyCommandPath("praefect")) } -func buildBinary(t testing.TB, targetDir, executableName string) { +// BuildBinary builds a Go binary once and copies it into the target directory. The source path can +// either be a ".go" file or a directory containing Go files. Returns the path to the executable in +// the destination directory. +func BuildBinary(t testing.TB, targetDir, sourcePath string) string { require.NotEmpty(t, testDirectory, "you must call testhelper.Configure() first") var ( + // executableName is the name of the executable. + executableName = filepath.Base(sourcePath) // sharedBinariesDir is where all binaries will be compiled into. This directory is // shared between all tests. sharedBinariesDir = filepath.Join(testDirectory, "bins") @@ -75,7 +80,7 @@ func buildBinary(t testing.TB, targetDir, executableName string) { "build", "-tags", "static,system_libgit2", "-o", sharedBinaryPath, - fmt.Sprintf("gitlab.com/gitlab-org/gitaly/v14/cmd/%s", executableName), + sourcePath, ) }) @@ -85,4 +90,10 @@ func buildBinary(t testing.TB, targetDir, executableName string) { require.NoError(t, os.MkdirAll(targetDir, os.ModePerm)) CopyFile(t, sharedBinaryPath, targetPath) require.NoError(t, os.Chmod(targetPath, 0o755)) + + return targetPath +} + +func gitalyCommandPath(command string) string { + return fmt.Sprintf("gitlab.com/gitlab-org/gitaly/v14/cmd/%s", command) } |