Merge pull request #23 from mcataford/fix/no-python-folder-handling

fix(python): handle cases where the python runtime dir does not exist
This commit is contained in:
Marc 2024-01-28 23:52:49 -05:00 committed by GitHub
commit a0345a1606
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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"))