Compare commits

...

11 Commits

Author SHA1 Message Date
Denis Bilenko 3bf36aaedd fix test in libs/python 2024-12-19 15:48:47 +01:00
Denis Bilenko 094530af05 fix python mutator test 2024-12-19 15:41:44 +01:00
Denis Bilenko 5a931dd513 update DetectVEnvExecutable to use python.exe 2024-12-19 15:26:32 +01:00
Denis Bilenko 1357f4a78b fix handling of Dir option; use TempDir in test 2024-12-19 15:25:45 +01:00
Denis Bilenko 56785cf555 remove test for DetectExecutable(); add test for LookPath(GetExecutable()) 2024-12-19 14:29:53 +01:00
Denis Bilenko 21a4cd1128 clean up 2024-12-19 14:24:48 +01:00
Denis Bilenko b50227afbe clean up 2024-12-19 14:24:07 +01:00
Denis Bilenko 2b89625728 add GetExecutable(); don't use DetectExecutable() in whl/infer 2024-12-19 14:16:34 +01:00
Denis Bilenko 640de90ba2 use python instead of python.exe 2024-12-19 10:29:35 +01:00
Denis Bilenko 5b4c7624fe rm InsertPathEntry; use os.PathListSeparator 2024-12-19 10:14:06 +01:00
Denis Bilenko dcfeeacea2 refactor: split venv creation into a separate function
this new function does not change chdir or sets environment
2024-12-19 10:06:17 +01:00
8 changed files with 140 additions and 92 deletions

View File

@ -16,12 +16,6 @@ type infer struct {
func (m *infer) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { func (m *infer) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
artifact := b.Config.Artifacts[m.name] artifact := b.Config.Artifacts[m.name]
// TODO use python.DetectVEnvExecutable once bundle has a way to specify venv path
py, err := python.DetectExecutable(ctx)
if err != nil {
return diag.FromErr(err)
}
// Note: using --build-number (build tag) flag does not help with re-installing // Note: using --build-number (build tag) flag does not help with re-installing
// libraries on all-purpose clusters. The reason is that `pip` ignoring build tag // libraries on all-purpose clusters. The reason is that `pip` ignoring build tag
// when upgrading the library and only look at wheel version. // when upgrading the library and only look at wheel version.
@ -36,7 +30,9 @@ func (m *infer) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
// version=datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S"), // version=datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S"),
// ... // ...
//) //)
artifact.BuildCommand = fmt.Sprintf(`"%s" setup.py bdist_wheel`, py)
py := python.GetExecutable()
artifact.BuildCommand = fmt.Sprintf(`%s setup.py bdist_wheel`, py)
return nil return nil
} }

View File

@ -541,7 +541,7 @@ func TestLoadDiagnosticsFile_nonExistent(t *testing.T) {
func TestInterpreterPath(t *testing.T) { func TestInterpreterPath(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
assert.Equal(t, "venv\\Scripts\\python3.exe", interpreterPath("venv")) assert.Equal(t, "venv\\Scripts\\python.exe", interpreterPath("venv"))
} else { } else {
assert.Equal(t, "venv/bin/python3", interpreterPath("venv")) assert.Equal(t, "venv/bin/python3", interpreterPath("venv"))
} }
@ -673,7 +673,7 @@ func withFakeVEnv(t *testing.T, venvPath string) {
func interpreterPath(venvPath string) string { func interpreterPath(venvPath string) string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return filepath.Join(venvPath, "Scripts", "python3.exe") return filepath.Join(venvPath, "Scripts", "python.exe")
} else { } else {
return filepath.Join(venvPath, "bin", "python3") return filepath.Join(venvPath, "bin", "python3")
} }

View File

@ -1,26 +0,0 @@
package testutil
import (
"bytes"
"os"
"os/exec"
"github.com/stretchr/testify/require"
)
func RunCommand(t TestingT, name string, args ...string) {
cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
}
func CaptureCommandOutput(t TestingT, name string, args ...string) string {
cmd := exec.Command(name, args...)
var stdout bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
require.NoError(t, err)
return stdout.String()
}

View File

@ -61,25 +61,3 @@ func Chdir(t TestingT, dir string) string {
return wd return wd
} }
func InsertPathEntry(t TestingT, path string) {
var separator string
if runtime.GOOS == "windows" {
separator = ";"
} else {
separator = ":"
}
t.Setenv("PATH", path+separator+os.Getenv("PATH"))
}
func InsertVirtualenvInPath(t TestingT, venvPath string) {
if runtime.GOOS == "windows" {
// https://github.com/pypa/virtualenv/commit/993ba1316a83b760370f5a3872b3f5ef4dd904c1
venvPath = filepath.Join(venvPath, "Scripts")
} else {
venvPath = filepath.Join(venvPath, "bin")
}
InsertPathEntry(t, venvPath)
}

View File

@ -11,6 +11,19 @@ import (
"runtime" "runtime"
) )
// GetExecutable gets appropriate python binary name for the platform
func GetExecutable() string {
// On Windows when virtualenv is created, the <env>/Scripts directory
// contains python.exe but no python3.exe.
// Most installers (e.g. the ones from python.org) only install python.exe and not python3.exe
if runtime.GOOS == "windows" {
return "python"
} else {
return "python3"
}
}
// DetectExecutable looks up the path to the python3 executable from the PATH // DetectExecutable looks up the path to the python3 executable from the PATH
// environment variable. // environment variable.
// //
@ -26,31 +39,16 @@ func DetectExecutable(ctx context.Context) (string, error) {
// //
// See https://github.com/pyenv/pyenv#understanding-python-version-selection // See https://github.com/pyenv/pyenv#understanding-python-version-selection
// On Windows when virtualenv is created, the <env>/Scripts directory out, err := exec.LookPath(GetExecutable())
// contains python.exe but no python3.exe. However, system python does have python3 entry
// and it is also added to PATH, so it is found first.
if runtime.GOOS == "windows" {
out, err := exec.LookPath("python.exe")
if err == nil && out != "" {
return out, nil
}
if err != nil && !errors.Is(err, exec.ErrNotFound) {
return "", err
}
}
out, err := exec.LookPath("python3")
// most of the OS'es have python3 in $PATH, but for those which don't, // most of the OS'es have python3 in $PATH, but for those which don't,
// we perform the latest version lookup // we perform the latest version lookup
if err != nil && !errors.Is(err, exec.ErrNotFound) { if err != nil && !errors.Is(err, exec.ErrNotFound) {
return "", err return "", err
} }
if out != "" { if out != "" {
return out, nil return out, nil
} }
// otherwise, detect all interpreters and pick the least that satisfies // otherwise, detect all interpreters and pick the least that satisfies
// minimal version requirements // minimal version requirements
all, err := DetectInterpreters(ctx) all, err := DetectInterpreters(ctx)
@ -71,7 +69,7 @@ func DetectExecutable(ctx context.Context) (string, error) {
func DetectVEnvExecutable(venvPath string) (string, error) { func DetectVEnvExecutable(venvPath string) (string, error) {
interpreterPath := filepath.Join(venvPath, "bin", "python3") interpreterPath := filepath.Join(venvPath, "bin", "python3")
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
interpreterPath = filepath.Join(venvPath, "Scripts", "python3.exe") interpreterPath = filepath.Join(venvPath, "Scripts", "python.exe")
} }
if _, err := os.Stat(interpreterPath); err != nil { if _, err := os.Stat(interpreterPath); err != nil {

View File

@ -39,7 +39,7 @@ func TestDetectVEnvExecutable_badLayout(t *testing.T) {
func interpreterPath(venvPath string) string { func interpreterPath(venvPath string) string {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
return filepath.Join(venvPath, "Scripts", "python3.exe") return filepath.Join(venvPath, "Scripts", "python.exe")
} else { } else {
return filepath.Join(venvPath, "bin", "python3") return filepath.Join(venvPath, "bin", "python3")
} }

View File

@ -2,31 +2,106 @@ package pythontest
import ( import (
"context" "context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing"
"github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/internal/testutil"
"github.com/databricks/cli/libs/python"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func RequirePythonVENV(t testutil.TestingT, ctx context.Context, pythonVersion string, checkVersion bool) string { type VenvOpts struct {
tmpDir := t.TempDir() // input
testutil.Chdir(t, tmpDir) PythonVersion string
skipVersionCheck bool
venvName := testutil.RandomName("test-venv-") // input/output
testutil.RunCommand(t, "uv", "venv", venvName, "--python", pythonVersion, "--seed") Dir string
testutil.InsertVirtualenvInPath(t, filepath.Join(tmpDir, venvName)) Name string
pythonExe, err := python.DetectExecutable(ctx) // output:
require.NoError(t, err) // Absolute path to venv
require.Contains(t, pythonExe, venvName) EnvPath string
if checkVersion { // Absolute path to venv/bin or venv/Scripts, depending on OS
actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version") BinPath string
expectVersion := "Python " + pythonVersion
require.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion) // Absolute path to python binary
PythonExe string
}
func CreatePythonEnv(opts *VenvOpts) error {
if opts == nil || opts.PythonVersion == "" {
return errors.New("PythonVersion must be provided")
}
if opts.Name == "" {
opts.Name = testutil.RandomName("test-venv-")
} }
return tmpDir cmd := exec.Command("uv", "venv", opts.Name, "--python", opts.PythonVersion, "--seed", "-q")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = opts.Dir
err := cmd.Run()
if err != nil {
return err
}
opts.EnvPath, err = filepath.Abs(filepath.Join(opts.Dir, opts.Name))
if err != nil {
return err
}
_, err = os.Stat(opts.EnvPath)
if err != nil {
return fmt.Errorf("cannot stat EnvPath %s: %s", opts.EnvPath, err)
}
if runtime.GOOS == "windows" {
// https://github.com/pypa/virtualenv/commit/993ba1316a83b760370f5a3872b3f5ef4dd904c1
opts.BinPath = filepath.Join(opts.EnvPath, "Scripts")
opts.PythonExe = filepath.Join(opts.BinPath, "python.exe")
} else {
opts.BinPath = filepath.Join(opts.EnvPath, "bin")
opts.PythonExe = filepath.Join(opts.BinPath, "python3")
}
_, err = os.Stat(opts.BinPath)
if err != nil {
return fmt.Errorf("cannot stat BinPath %s: %s", opts.BinPath, err)
}
_, err = os.Stat(opts.PythonExe)
if err != nil {
return fmt.Errorf("cannot stat PythonExe %s: %s", opts.PythonExe, err)
}
if !opts.skipVersionCheck {
cmd := exec.Command(opts.PythonExe, "--version")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to run %s --version: %s", opts.PythonExe, err)
}
outString := string(out)
expectVersion := "Python " + opts.PythonVersion
if !strings.HasPrefix(outString, expectVersion) {
return fmt.Errorf("Unexpected output from %s --version: %v (expected %v)", opts.PythonExe, outString, expectVersion)
}
}
return nil
}
func RequireActivatedPythonEnv(t *testing.T, ctx context.Context, opts *VenvOpts) {
err := CreatePythonEnv(opts)
require.NoError(t, err)
require.DirExists(t, opts.BinPath)
newPath := fmt.Sprintf("%s%c%s", opts.BinPath, os.PathListSeparator, os.Getenv("PATH"))
t.Setenv("PATH", newPath)
} }

View File

@ -2,15 +2,42 @@ package pythontest
import ( import (
"context" "context"
"os/exec"
"path/filepath"
"testing" "testing"
"github.com/databricks/cli/libs/python"
"github.com/stretchr/testify/require"
) )
func TestVenv(t *testing.T) { func TestVenvSuccess(t *testing.T) {
// Test at least two version to ensure we capture a case where venv version does not match system one // Test at least two version to ensure we capture a case where venv version does not match system one
for _, pythonVersion := range []string{"3.11", "3.12"} { for _, pythonVersion := range []string{"3.11", "3.12"} {
t.Run(pythonVersion, func(t *testing.T) { t.Run(pythonVersion, func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
RequirePythonVENV(t, ctx, pythonVersion, true) dir := t.TempDir()
opts := VenvOpts{
PythonVersion: pythonVersion,
Dir: dir,
}
RequireActivatedPythonEnv(t, ctx, &opts)
require.DirExists(t, opts.EnvPath)
require.DirExists(t, opts.BinPath)
require.FileExists(t, opts.PythonExe)
pythonExe, err := exec.LookPath(python.GetExecutable())
require.NoError(t, err)
require.Equal(t, filepath.Dir(pythonExe), filepath.Dir(opts.PythonExe))
require.FileExists(t, pythonExe)
}) })
} }
} }
func TestWrongVersion(t *testing.T) {
require.Error(t, CreatePythonEnv(&VenvOpts{PythonVersion: "4.0"}))
}
func TestMissingVersion(t *testing.T) {
require.Error(t, CreatePythonEnv(nil))
require.Error(t, CreatePythonEnv(&VenvOpts{}))
}