Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move BuildCLI function to testutil package #2211

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 3 additions & 43 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/env"
Expand Down Expand Up @@ -89,8 +88,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)
Expand Down Expand Up @@ -334,47 +335,6 @@ func readMergedScriptContents(t *testing.T, dir string) string {
return strings.Join(prepares, "\n")
}

func BuildCLI(t *testing.T, cwd, coverDir string) string {
execPath := filepath.Join(cwd, "build", "databricks")
if runtime.GOOS == "windows" {
execPath += ".exe"
}

start := time.Now()
args := []string{
"go", "build",
"-mod", "vendor",
"-o", execPath,
}

if coverDir != "" {
args = append(args, "-cover")
}

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 = ".."
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
}

func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
Expand Down
File renamed without changes.
55 changes: 55 additions & 0 deletions internal/testutil/build_cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package testutil

import (
"os/exec"
"path/filepath"
"runtime"
"time"

"github.com/databricks/cli/libs/folders"
"github.com/stretchr/testify/require"
)

func BuildCLI(t TestingT, flags ...string) string {
repoRoot, err := folders.FindDirWithLeaf(".", ".git")
require.NoError(t, err)

// Stable path for the CLI binary. This ensures fast builds and cache reuse.
execPath := filepath.Join(repoRoot, "internal", "testutil", "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 = repoRoot
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
}
Loading