-
Notifications
You must be signed in to change notification settings - Fork 69
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
Fix finding Python within virtualenv on Windows #2034
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3c3c172
Fix finding Python within virtualenv on Windows; new pythontest package
denik dcfeeac
refactor: split venv creation into a separate function
denik 5b4c762
rm InsertPathEntry; use os.PathListSeparator
denik 640de90
use python instead of python.exe
denik 2b89625
add GetExecutable(); don't use DetectExecutable() in whl/infer
denik b50227a
clean up
denik 21a4cd1
clean up
denik 56785cf
remove test for DetectExecutable(); add test for LookPath(GetExecutab…
denik 1357f4a
fix handling of Dir option; use TempDir in test
denik 5a931dd
update DetectVEnvExecutable to use python.exe
denik 094530a
fix python mutator test
denik 3bf36aa
fix test in libs/python
denik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
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" | ||
) | ||
|
||
type VenvOpts struct { | ||
// input | ||
PythonVersion string | ||
skipVersionCheck bool | ||
|
||
// input/output | ||
Dir string | ||
Name string | ||
|
||
// 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 | ||
denik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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")) | ||
denik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Setenv("PATH", newPath) | ||
denik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pythonExe, err := python.DetectExecutable(ctx) | ||
require.NoError(t, err) | ||
require.Equal(t, filepath.Dir(pythonExe), filepath.Dir(opts.PythonExe)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package pythontest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestVenvSuccess(t *testing.T) { | ||
// Test at least two version to ensure we capture a case where venv version does not match system one | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Smart :) |
||
for _, pythonVersion := range []string{"3.11", "3.12"} { | ||
t.Run(pythonVersion, func(t *testing.T) { | ||
ctx := context.Background() | ||
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{})) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this regress the non-venv cases where
python.exe
resolves to a system-wide Python 2 installation?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, if they somehow managed to have Python2 installed in the first place, but why would they have that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see python3 mentioned in another place in this module - it also won't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it matter which Windows terminal is being used? Some users might use Git Bash for example and maybe the behaviour is different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that should not matter