-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix finding Python within virtualenv on Windows
- Loading branch information
Showing
5 changed files
with
110 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
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,42 @@ | ||
package python | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/databricks/cli/internal/testutil" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func setupVenvWithUV(t testutil.TestingT, ctx context.Context, directory, pythonVersion string) { | ||
venvName := testutil.RandomName("test-venv-") | ||
testutil.RunCommand(t, "uv", "venv", venvName, "--python", pythonVersion, "--seed") | ||
|
||
testutil.InsertVirtualenvInPath(t, filepath.Join(directory, venvName)) | ||
|
||
pythonExe, err := DetectExecutable(ctx) | ||
assert.NoError(t, err) | ||
assert.Contains(t, pythonExe, venvName) | ||
|
||
actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version") | ||
expectVersion := "Python " + pythonVersion | ||
assert.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion) | ||
} | ||
|
||
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) { | ||
testVenv(t, pythonVersion) | ||
}) | ||
} | ||
} | ||
|
||
func testVenv(t *testing.T, pythonVersion string) { | ||
ctx := context.Background() | ||
tmpDir := t.TempDir() | ||
testutil.Chdir(t, tmpDir) | ||
setupVenvWithUV(t, ctx, tmpDir, pythonVersion) | ||
} |