From 0d6b3e4a96890dfec41d4887f8052729b8ac76f5 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Wed, 22 Jan 2025 21:36:01 +0100 Subject: [PATCH] Move BuildCLI function to testutil package --- acceptance/acceptance_test.go | 4 +- internal/testutil/build_cli.go | 68 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 internal/testutil/build_cli.go diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go index e611f4e50..cb6079f42 100644 --- a/acceptance/acceptance_test.go +++ b/acceptance/acceptance_test.go @@ -89,8 +89,10 @@ func testAccept(t *testing.T, InprocessMode bool, singleTest string) int { cmdServer := StartCmdServer(t) t.Setenv("CMD_SERVER_URL", cmdServer.URL) execPath = filepath.Join(cwd, "bin", "callserver.py") + } else if coverDir != "" { + execPath = testutil.BuildCLI(t, "-cover") } else { - execPath = BuildCLI(t, cwd, coverDir) + execPath = testutil.BuildCLI(t) } t.Setenv("CLI", execPath) diff --git a/internal/testutil/build_cli.go b/internal/testutil/build_cli.go new file mode 100644 index 000000000..ee0ea3b7d --- /dev/null +++ b/internal/testutil/build_cli.go @@ -0,0 +1,68 @@ +package testutil + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func findRoot(t *testing.T) string { + curr, err := os.Getwd() + require.NoError(t, err) + + for curr != filepath.Dir(curr) { + if _, err := os.Stat(filepath.Join(curr, "go.mod")); err == nil { + return curr + } + curr = filepath.Dir(curr) + } + require.Fail(t, "could not find root directory") + return "" +} + +func BuildCLI(t *testing.T, flags ...string) string { + tmpDir := t.TempDir() + + execPath := filepath.Join(tmpDir, "build", "databricks") + if runtime.GOOS == "windows" { + execPath += ".exe" + } + + start := time.Now() + args := []string{ + "go", "build", + "-mod", "vendor", + "-o", execPath, + } + if len(flags) > 0 { + args = append(args, flags...) + } + + if runtime.GOOS == "windows" { + // Get this error on my local Windows: + // error obtaining VCS status: exit status 128 + // Use -buildvcs=false to disable VCS stamping. + args = append(args, "-buildvcs=false") + } + + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = findRoot(t) + out, err := cmd.CombinedOutput() + elapsed := time.Since(start) + t.Logf("%s took %s", args, elapsed) + require.NoError(t, err, "go build failed: %s: %s\n%s", args, err, out) + if len(out) > 0 { + t.Logf("go build output: %s: %s", args, out) + } + + // Quick check + warm up cache: + cmd = exec.Command(execPath, "--version") + out, err = cmd.CombinedOutput() + require.NoError(t, err, "%s --version failed: %s\n%s", execPath, err, out) + return execPath +}