|
| 1 | +package python |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/databricks/cli/internal/testutil" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func setupVenvWithUV(t testutil.TestingT, ctx context.Context, directory, pythonVersion string) { |
| 14 | + venvName := testutil.RandomName("test-venv-") |
| 15 | + testutil.RunCommand(t, "uv", "venv", venvName, "--python", pythonVersion, "--seed") |
| 16 | + |
| 17 | + testutil.InsertVirtualenvInPath(t, filepath.Join(directory, venvName)) |
| 18 | + |
| 19 | + pythonExe, err := DetectExecutable(ctx) |
| 20 | + assert.NoError(t, err) |
| 21 | + assert.Contains(t, pythonExe, venvName) |
| 22 | + |
| 23 | + actualVersion := testutil.CaptureCommandOutput(t, pythonExe, "--version") |
| 24 | + expectVersion := "Python " + pythonVersion |
| 25 | + assert.True(t, strings.HasPrefix(actualVersion, expectVersion), "Running %s --version: Expected %v, got %v", pythonExe, expectVersion, actualVersion) |
| 26 | +} |
| 27 | + |
| 28 | +func TestVenv(t *testing.T) { |
| 29 | + // Test at least two version to ensure we capture a case where venv version does not match system one |
| 30 | + for _, pythonVersion := range []string{"3.11", "3.12"} { |
| 31 | + t.Run(pythonVersion, func(t *testing.T) { |
| 32 | + testVenv(t, pythonVersion) |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func testVenv(t *testing.T, pythonVersion string) { |
| 38 | + ctx := context.Background() |
| 39 | + tmpDir := t.TempDir() |
| 40 | + testutil.Chdir(t, tmpDir) |
| 41 | + setupVenvWithUV(t, ctx, tmpDir, pythonVersion) |
| 42 | +} |
0 commit comments