fix(python): handle cases where the python runtime dir does not exist

This commit is contained in:
Marc 2024-01-28 23:50:17 -05:00
parent ecb808e16c
commit 3d71de6533
Signed by: marc
GPG key ID: 048E042F22B5DC79
3 changed files with 28 additions and 0 deletions

View file

@ -30,9 +30,17 @@ type SelectedVersion struct {
} }
func ListInstalledVersions() ([]string, error) { func ListInstalledVersions() ([]string, error) {
if ensureErr := state.EnsureStatePath("runtimes"); ensureErr != nil {
return []string{}, ensureErr
}
runtimesDir := state.GetStatePath("runtimes", "python") runtimesDir := state.GetStatePath("runtimes", "python")
entries, err := os.ReadDir(runtimesDir) entries, err := os.ReadDir(runtimesDir)
if os.IsNotExist(err) {
return []string{}, nil
}
if err != nil { if err != nil {
return []string{}, err return []string{}, err
} }

View file

@ -149,6 +149,18 @@ func TestListInstalledVersionNoVersionsInstalled(t *testing.T) {
} }
} }
func TestListInstalledVersionNoVersionsInstalledNoPythonDir(t *testing.T) {
defer testutils.SetupAndCleanupEnvironment(t)()
os.MkdirAll(state.GetStatePath("runtimes"), 0750)
installedVersions, _ := ListInstalledVersions()
if len(installedVersions) != 0 {
t.Errorf("Expected 0 elements, got %d (%s).", len(installedVersions), installedVersions)
}
}
func TestListInstalledVersionNoRuntimesDir(t *testing.T) { func TestListInstalledVersionNoRuntimesDir(t *testing.T) {
defer testutils.SetupAndCleanupEnvironment(t)() defer testutils.SetupAndCleanupEnvironment(t)()

View file

@ -27,6 +27,14 @@ func GetStatePath(pathSegments ...string) string {
return path.Join(allSegments...) return path.Join(allSegments...)
} }
func EnsureStatePath(pathSegments ...string) error {
path := GetStatePath(pathSegments...)
_, err := os.Stat(path)
return err
}
func ReadState() State { func ReadState() State {
c, _ := ioutil.ReadFile(GetStatePath("state.json")) c, _ := ioutil.ReadFile(GetStatePath("state.json"))