Skip to content
This repository has been archived by the owner on May 5, 2024. It is now read-only.

Commit

Permalink
refactor: pass venv name instead of dir path
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed May 6, 2023
1 parent b3f7763 commit f071e1d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
5 changes: 2 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ func printVenvs() {
line += bold.Sprint(" " + venvName)
}
if verbose {
venvDir := filepath.Join(xdg.DataDir, venvName)
projectPath, err := venv.ProjectPath(venvDir)
projectPath, err := venv.ProjectPath(venvName)
if err != nil {
log.Fatal(err)
}
pythonVersion, err := venv.PythonVersion(venvDir)
pythonVersion, err := venv.PythonVersion(venvName)
if err != nil {
log.Fatal(err)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/venv/venv.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func Names() ([]string, error) {
// ProjectPath returns the absolute path to the project this virtual
// environment belongs to. This information is extracted from the
// `.project` file present in the virtual environment directory.
func ProjectPath(venvDir string) (string, error) {
content, err := os.ReadFile(filepath.Join(venvDir, ".project"))
func ProjectPath(venvName string) (string, error) {
content, err := os.ReadFile(filepath.Join(xdg.DataDir, venvName, ".project"))
if err != nil {
return "", err
}
Expand All @@ -45,8 +45,8 @@ func ProjectPath(venvDir string) (string, error) {
// PythonVersion returns the Python version this environment was created from.
// This information is extracted from the config file present in the virtual
// environment directory.
func PythonVersion(venvDir string) (string, error) {
file, err := os.Open(filepath.Join(venvDir, "pyvenv.cfg"))
func PythonVersion(venvName string) (string, error) {
file, err := os.Open(filepath.Join(xdg.DataDir, venvName, "pyvenv.cfg"))
if err != nil {
return "", err
}
Expand Down
16 changes: 12 additions & 4 deletions internal/venv/venv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ func TestNames(t *testing.T) {
}

func TestProjectPath(t *testing.T) {
venvDir := filepath.Join(testdataDir, "venv1")
originalDataDir := xdg.DataDir
xdg.DataDir = testdataDir
t.Cleanup(func() {
xdg.DataDir = originalDataDir
})

got, err := ProjectPath(venvDir)
got, err := ProjectPath("venv1")
if err != nil {
t.Fatalf("ProjectPath() error = %v, want nil", err)
}
Expand All @@ -54,9 +58,13 @@ func TestProjectPath(t *testing.T) {
}

func TestPythonVersion(t *testing.T) {
venvDir := filepath.Join(testdataDir, "venv1")
originalDataDir := xdg.DataDir
xdg.DataDir = testdataDir
t.Cleanup(func() {
xdg.DataDir = originalDataDir
})

got, err := PythonVersion(venvDir)
got, err := PythonVersion("venv1")
if err != nil {
t.Fatalf("PythonVersion() error = %v, want nil", err)
}
Expand Down

0 comments on commit f071e1d

Please sign in to comment.