From 3c3c172622b22c7868cf1b82bb02caeb748e2875 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Wed, 18 Dec 2024 20:20:30 +0100 Subject: [PATCH 01/12] Fix finding Python within virtualenv on Windows; new pythontest package --- .github/workflows/push.yml | 3 +++ internal/testutil/cmd.go | 26 ++++++++++++++++++ internal/testutil/env.go | 22 ++++++++++++++++ libs/python/detect.go | 17 ++++++++++++ libs/python/pythontest/pythontest.go | 32 +++++++++++++++++++++++ libs/python/pythontest/pythontest_test.go | 16 ++++++++++++ 6 files changed, 116 insertions(+) create mode 100644 internal/testutil/cmd.go create mode 100644 libs/python/pythontest/pythontest.go create mode 100644 libs/python/pythontest/pythontest_test.go diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index ea561805bf..e5bc6b1928 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -40,6 +40,9 @@ jobs: with: python-version: '3.9' + - name: Install uv + uses: astral-sh/setup-uv@v4 + - name: Set go env run: | echo "GOPATH=$(go env GOPATH)" >> $GITHUB_ENV diff --git a/internal/testutil/cmd.go b/internal/testutil/cmd.go new file mode 100644 index 0000000000..4045c606c8 --- /dev/null +++ b/internal/testutil/cmd.go @@ -0,0 +1,26 @@ +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() +} diff --git a/internal/testutil/env.go b/internal/testutil/env.go index 10557c4e63..0ac3fbea83 100644 --- a/internal/testutil/env.go +++ b/internal/testutil/env.go @@ -61,3 +61,25 @@ func Chdir(t TestingT, dir string) string { 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) +} diff --git a/libs/python/detect.go b/libs/python/detect.go index 8fcc7cd9ca..247548a715 100644 --- a/libs/python/detect.go +++ b/libs/python/detect.go @@ -25,15 +25,32 @@ func DetectExecutable(ctx context.Context) (string, error) { // the parent directory tree. // // See https://github.com/pyenv/pyenv#understanding-python-version-selection + + // On Windows when virtualenv is created, the /Scripts directory + // 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, // we perform the latest version lookup if err != nil && !errors.Is(err, exec.ErrNotFound) { return "", err } + if out != "" { return out, nil } + // otherwise, detect all interpreters and pick the least that satisfies // minimal version requirements all, err := DetectInterpreters(ctx) diff --git a/libs/python/pythontest/pythontest.go b/libs/python/pythontest/pythontest.go new file mode 100644 index 0000000000..5afea6366a --- /dev/null +++ b/libs/python/pythontest/pythontest.go @@ -0,0 +1,32 @@ +package pythontest + +import ( + "context" + "path/filepath" + "strings" + + "github.com/databricks/cli/internal/testutil" + "github.com/databricks/cli/libs/python" + "github.com/stretchr/testify/require" +) + +func RequirePythonVENV(t testutil.TestingT, ctx context.Context, pythonVersion string, checkVersion bool) string { + tmpDir := t.TempDir() + testutil.Chdir(t, tmpDir) + + venvName := testutil.RandomName("test-venv-") + testutil.RunCommand(t, "uv", "venv", venvName, "--python", pythonVersion, "--seed") + testutil.InsertVirtualenvInPath(t, filepath.Join(tmpDir, venvName)) + + pythonExe, err := python.DetectExecutable(ctx) + require.NoError(t, err) + require.Contains(t, pythonExe, venvName) + + if checkVersion { + actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version") + expectVersion := "Python " + pythonVersion + require.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion) + } + + return tmpDir +} diff --git a/libs/python/pythontest/pythontest_test.go b/libs/python/pythontest/pythontest_test.go new file mode 100644 index 0000000000..3aba780c59 --- /dev/null +++ b/libs/python/pythontest/pythontest_test.go @@ -0,0 +1,16 @@ +package pythontest + +import ( + "context" + "testing" +) + +func TestVenv(t *testing.T) { + // 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"} { + t.Run(pythonVersion, func(t *testing.T) { + ctx := context.Background() + RequirePythonVENV(t, ctx, pythonVersion, true) + }) + } +} From dcfeeacea2a1610f43c3d73acf80596f2d18a9ac Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 10:06:17 +0100 Subject: [PATCH 02/12] refactor: split venv creation into a separate function this new function does not change chdir or sets environment --- internal/testutil/cmd.go | 26 ----- libs/python/pythontest/pythontest.go | 110 +++++++++++++++++++--- libs/python/pythontest/pythontest_test.go | 19 +++- 3 files changed, 113 insertions(+), 42 deletions(-) delete mode 100644 internal/testutil/cmd.go diff --git a/internal/testutil/cmd.go b/internal/testutil/cmd.go deleted file mode 100644 index 4045c606c8..0000000000 --- a/internal/testutil/cmd.go +++ /dev/null @@ -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() -} diff --git a/libs/python/pythontest/pythontest.go b/libs/python/pythontest/pythontest.go index 5afea6366a..7e5e2eb280 100644 --- a/libs/python/pythontest/pythontest.go +++ b/libs/python/pythontest/pythontest.go @@ -2,31 +2,113 @@ package pythontest import ( "context" + "errors" + "fmt" + "os" + "os/exec" "path/filepath" + "runtime" "strings" + "testing" "github.com/databricks/cli/internal/testutil" "github.com/databricks/cli/libs/python" "github.com/stretchr/testify/require" ) -func RequirePythonVENV(t testutil.TestingT, ctx context.Context, pythonVersion string, checkVersion bool) string { - tmpDir := t.TempDir() - testutil.Chdir(t, tmpDir) +type VenvOpts struct { + // input + PythonVersion string + skipVersionCheck bool - venvName := testutil.RandomName("test-venv-") - testutil.RunCommand(t, "uv", "venv", venvName, "--python", pythonVersion, "--seed") - testutil.InsertVirtualenvInPath(t, filepath.Join(tmpDir, venvName)) + // input/output + Dir string + Name string - pythonExe, err := python.DetectExecutable(ctx) - require.NoError(t, err) - require.Contains(t, pythonExe, venvName) + // output: + // Absolute path to venv + EnvPath string + + // Absolute path to venv/bin or venv/Scripts, depending on OS + BinPath string + + // 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-") + } + if opts.Dir != "" { + opts.Dir = "." + } + + 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 checkVersion { - actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version") - expectVersion := "Python " + pythonVersion - require.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion) + 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 tmpDir + return nil +} + +func RequireActivatedPythonEnv(t *testing.T, ctx context.Context, opts *VenvOpts) { + err := CreatePythonEnv(opts) + require.NoError(t, err) + require.DirExists(t, opts.BinPath) + + testutil.InsertPathEntry(t, opts.BinPath) + + pythonExe, err := python.DetectExecutable(ctx) + require.NoError(t, err) + require.Equal(t, filepath.Dir(pythonExe), filepath.Dir(opts.PythonExe)) } diff --git a/libs/python/pythontest/pythontest_test.go b/libs/python/pythontest/pythontest_test.go index 3aba780c59..d8eeca64b3 100644 --- a/libs/python/pythontest/pythontest_test.go +++ b/libs/python/pythontest/pythontest_test.go @@ -3,14 +3,29 @@ package pythontest import ( "context" "testing" + + "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 for _, pythonVersion := range []string{"3.11", "3.12"} { t.Run(pythonVersion, func(t *testing.T) { ctx := context.Background() - RequirePythonVENV(t, ctx, pythonVersion, true) + opts := VenvOpts{PythonVersion: pythonVersion} + RequireActivatedPythonEnv(t, ctx, &opts) + require.DirExists(t, opts.EnvPath) + require.DirExists(t, opts.BinPath) + require.FileExists(t, opts.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{})) +} From 5b4c7624fed913513ba061a7e7b8f097d2f6b43e Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 10:14:06 +0100 Subject: [PATCH 03/12] rm InsertPathEntry; use os.PathListSeparator --- internal/testutil/env.go | 22 ---------------------- libs/python/pythontest/pythontest.go | 3 ++- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/internal/testutil/env.go b/internal/testutil/env.go index 0ac3fbea83..10557c4e63 100644 --- a/internal/testutil/env.go +++ b/internal/testutil/env.go @@ -61,25 +61,3 @@ func Chdir(t TestingT, dir string) string { 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) -} diff --git a/libs/python/pythontest/pythontest.go b/libs/python/pythontest/pythontest.go index 7e5e2eb280..8500c0f246 100644 --- a/libs/python/pythontest/pythontest.go +++ b/libs/python/pythontest/pythontest.go @@ -106,7 +106,8 @@ func RequireActivatedPythonEnv(t *testing.T, ctx context.Context, opts *VenvOpts require.NoError(t, err) require.DirExists(t, opts.BinPath) - testutil.InsertPathEntry(t, opts.BinPath) + newPath := fmt.Sprintf("%s%c%s", opts.BinPath, os.PathListSeparator, os.Getenv("PATH")) + t.Setenv("PATH", newPath) pythonExe, err := python.DetectExecutable(ctx) require.NoError(t, err) From 640de90ba2e26faa2bcc139b56fb97c8c9c1bad7 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 10:29:35 +0100 Subject: [PATCH 04/12] use python instead of python.exe --- libs/python/detect.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/python/detect.go b/libs/python/detect.go index 247548a715..ce46d64465 100644 --- a/libs/python/detect.go +++ b/libs/python/detect.go @@ -30,7 +30,7 @@ func DetectExecutable(ctx context.Context) (string, error) { // 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") + out, err := exec.LookPath("python") if err == nil && out != "" { return out, nil } From 2b89625728b3d67050d62bfd3dca63266c579437 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 14:15:12 +0100 Subject: [PATCH 05/12] add GetExecutable(); don't use DetectExecutable() in whl/infer --- bundle/artifacts/whl/infer.go | 10 +++------- libs/python/detect.go | 30 ++++++++++++++++-------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/bundle/artifacts/whl/infer.go b/bundle/artifacts/whl/infer.go index cb727de0ee..604bfc4497 100644 --- a/bundle/artifacts/whl/infer.go +++ b/bundle/artifacts/whl/infer.go @@ -16,12 +16,6 @@ type infer struct { func (m *infer) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics { 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 // libraries on all-purpose clusters. The reason is that `pip` ignoring build tag // 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"), // ... //) - 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 } diff --git a/libs/python/detect.go b/libs/python/detect.go index ce46d64465..6343b726e7 100644 --- a/libs/python/detect.go +++ b/libs/python/detect.go @@ -11,6 +11,21 @@ import ( "runtime" ) +// GetExecutable gets appropriate python binary name for the platform +func GetExecutable() string { + // On Windows when virtualenv is created, the /Scripts directory + // 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. + + // 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 // environment variable. // @@ -26,20 +41,7 @@ func DetectExecutable(ctx context.Context) (string, error) { // // See https://github.com/pyenv/pyenv#understanding-python-version-selection - // On Windows when virtualenv is created, the /Scripts directory - // 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") - if err == nil && out != "" { - return out, nil - } - if err != nil && !errors.Is(err, exec.ErrNotFound) { - return "", err - } - } - - out, err := exec.LookPath("python3") + out, err := exec.LookPath(GetExecutable()) // most of the OS'es have python3 in $PATH, but for those which don't, // we perform the latest version lookup From b50227afbe3d128c1419819f98352f8c1c6b6380 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 14:24:07 +0100 Subject: [PATCH 06/12] clean up --- libs/python/detect.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libs/python/detect.go b/libs/python/detect.go index 6343b726e7..f92ea5660c 100644 --- a/libs/python/detect.go +++ b/libs/python/detect.go @@ -14,9 +14,7 @@ import ( // GetExecutable gets appropriate python binary name for the platform func GetExecutable() string { // On Windows when virtualenv is created, the /Scripts directory - // 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. - + // 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" { From 21a4cd1128ed780f06c19defe409cbf443ffa1e3 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 14:24:48 +0100 Subject: [PATCH 07/12] clean up --- libs/python/detect.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/libs/python/detect.go b/libs/python/detect.go index f92ea5660c..f6dcd008d1 100644 --- a/libs/python/detect.go +++ b/libs/python/detect.go @@ -46,11 +46,9 @@ func DetectExecutable(ctx context.Context) (string, error) { if err != nil && !errors.Is(err, exec.ErrNotFound) { return "", err } - if out != "" { return out, nil } - // otherwise, detect all interpreters and pick the least that satisfies // minimal version requirements all, err := DetectInterpreters(ctx) From 56785cf555201229f87b13d3e25dc137ff2fb45c Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 14:29:24 +0100 Subject: [PATCH 08/12] remove test for DetectExecutable(); add test for LookPath(GetExecutable()) --- libs/python/pythontest/pythontest.go | 5 ----- libs/python/pythontest/pythontest_test.go | 8 ++++++++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/python/pythontest/pythontest.go b/libs/python/pythontest/pythontest.go index 8500c0f246..e43a1974b4 100644 --- a/libs/python/pythontest/pythontest.go +++ b/libs/python/pythontest/pythontest.go @@ -12,7 +12,6 @@ import ( "testing" "github.com/databricks/cli/internal/testutil" - "github.com/databricks/cli/libs/python" "github.com/stretchr/testify/require" ) @@ -108,8 +107,4 @@ func RequireActivatedPythonEnv(t *testing.T, ctx context.Context, opts *VenvOpts newPath := fmt.Sprintf("%s%c%s", opts.BinPath, os.PathListSeparator, os.Getenv("PATH")) t.Setenv("PATH", newPath) - - pythonExe, err := python.DetectExecutable(ctx) - require.NoError(t, err) - require.Equal(t, filepath.Dir(pythonExe), filepath.Dir(opts.PythonExe)) } diff --git a/libs/python/pythontest/pythontest_test.go b/libs/python/pythontest/pythontest_test.go index d8eeca64b3..8af24d7189 100644 --- a/libs/python/pythontest/pythontest_test.go +++ b/libs/python/pythontest/pythontest_test.go @@ -2,8 +2,11 @@ package pythontest import ( "context" + "os/exec" + "path/filepath" "testing" + "github.com/databricks/cli/libs/python" "github.com/stretchr/testify/require" ) @@ -17,6 +20,11 @@ func TestVenvSuccess(t *testing.T) { 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) }) } } From 1357f4a78b5ecf386e10270a995fb3f225d394ec Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 15:25:45 +0100 Subject: [PATCH 09/12] fix handling of Dir option; use TempDir in test --- libs/python/pythontest/pythontest.go | 3 --- libs/python/pythontest/pythontest_test.go | 6 +++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/libs/python/pythontest/pythontest.go b/libs/python/pythontest/pythontest.go index e43a1974b4..9a2dec0eec 100644 --- a/libs/python/pythontest/pythontest.go +++ b/libs/python/pythontest/pythontest.go @@ -42,9 +42,6 @@ func CreatePythonEnv(opts *VenvOpts) error { if opts.Name == "" { opts.Name = testutil.RandomName("test-venv-") } - if opts.Dir != "" { - opts.Dir = "." - } cmd := exec.Command("uv", "venv", opts.Name, "--python", opts.PythonVersion, "--seed", "-q") cmd.Stdout = os.Stdout diff --git a/libs/python/pythontest/pythontest_test.go b/libs/python/pythontest/pythontest_test.go index 8af24d7189..3161092d32 100644 --- a/libs/python/pythontest/pythontest_test.go +++ b/libs/python/pythontest/pythontest_test.go @@ -15,7 +15,11 @@ func TestVenvSuccess(t *testing.T) { for _, pythonVersion := range []string{"3.11", "3.12"} { t.Run(pythonVersion, func(t *testing.T) { ctx := context.Background() - opts := VenvOpts{PythonVersion: pythonVersion} + dir := t.TempDir() + opts := VenvOpts{ + PythonVersion: pythonVersion, + Dir: dir, + } RequireActivatedPythonEnv(t, ctx, &opts) require.DirExists(t, opts.EnvPath) require.DirExists(t, opts.BinPath) From 5a931dd5131afd56b12e16239e9303aec1e0895d Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 15:26:32 +0100 Subject: [PATCH 10/12] update DetectVEnvExecutable to use python.exe --- libs/python/detect.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/python/detect.go b/libs/python/detect.go index f6dcd008d1..e86d9d6210 100644 --- a/libs/python/detect.go +++ b/libs/python/detect.go @@ -69,7 +69,7 @@ func DetectExecutable(ctx context.Context) (string, error) { func DetectVEnvExecutable(venvPath string) (string, error) { interpreterPath := filepath.Join(venvPath, "bin", "python3") 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 { From 094530af054036cc3fc1e66bb601c30d44792ea3 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 15:41:44 +0100 Subject: [PATCH 11/12] fix python mutator test --- bundle/config/mutator/python/python_mutator_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bundle/config/mutator/python/python_mutator_test.go b/bundle/config/mutator/python/python_mutator_test.go index 0c6df98334..8bdf91d03e 100644 --- a/bundle/config/mutator/python/python_mutator_test.go +++ b/bundle/config/mutator/python/python_mutator_test.go @@ -541,7 +541,7 @@ func TestLoadDiagnosticsFile_nonExistent(t *testing.T) { func TestInterpreterPath(t *testing.T) { if runtime.GOOS == "windows" { - assert.Equal(t, "venv\\Scripts\\python3.exe", interpreterPath("venv")) + assert.Equal(t, "venv\\Scripts\\python.exe", interpreterPath("venv")) } else { assert.Equal(t, "venv/bin/python3", interpreterPath("venv")) } @@ -673,7 +673,7 @@ func withFakeVEnv(t *testing.T, venvPath string) { func interpreterPath(venvPath string) string { if runtime.GOOS == "windows" { - return filepath.Join(venvPath, "Scripts", "python3.exe") + return filepath.Join(venvPath, "Scripts", "python.exe") } else { return filepath.Join(venvPath, "bin", "python3") } From 3bf36aaedd4a4f6eb166adc300a1f11ed6c65638 Mon Sep 17 00:00:00 2001 From: Denis Bilenko Date: Thu, 19 Dec 2024 15:48:47 +0100 Subject: [PATCH 12/12] fix test in libs/python --- libs/python/detect_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/python/detect_test.go b/libs/python/detect_test.go index 485aa1875c..0aeedb776e 100644 --- a/libs/python/detect_test.go +++ b/libs/python/detect_test.go @@ -39,7 +39,7 @@ func TestDetectVEnvExecutable_badLayout(t *testing.T) { func interpreterPath(venvPath string) string { if runtime.GOOS == "windows" { - return filepath.Join(venvPath, "Scripts", "python3.exe") + return filepath.Join(venvPath, "Scripts", "python.exe") } else { return filepath.Join(venvPath, "bin", "python3") }