diff --git a/python/commands.go b/python/commands.go index 330ed38..8ca0a2f 100644 --- a/python/commands.go +++ b/python/commands.go @@ -26,7 +26,7 @@ func use(args []string, flags cli.Flags, currentState state.State) error { return err } - availableVersions := state.GetAvailableVersions() + availableVersions := GetAvailableVersions() found := false for _, v := range availableVersions { diff --git a/python/version.go b/python/version.go index 888c588..b04d47a 100644 --- a/python/version.go +++ b/python/version.go @@ -112,3 +112,16 @@ func DetermineSystemPython() (string, string) { detectedVersion, _ := strings.CutPrefix(versionOut, "Python") return strings.TrimSpace(detectedVersion), "/bin/python" } + +// Gets all install versions. +func GetAvailableVersions() []string { + entries, _ := os.ReadDir(state.GetStatePath("runtimes", "python")) + + versions := []string{} + + for _, d := range entries { + versions = append(versions, d.Name()) + } + + return versions +} diff --git a/state/state.go b/state/state.go index 58afebc..d09b222 100644 --- a/state/state.go +++ b/state/state.go @@ -51,15 +51,3 @@ func WriteState(version string) { d, _ := json.Marshal(state) ioutil.WriteFile(GetStatePath("state.json"), d, 0750) } - -func GetAvailableVersions() []string { - entries, _ := os.ReadDir(GetStatePath("runtimes", "python")) - - versions := []string{} - - for _, d := range entries { - versions = append(versions, d.Name()) - } - - return versions -} diff --git a/state/state_test.go b/state/state_test.go index c907e62..b5a3914 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -6,6 +6,7 @@ import ( "os" "path" "testing" + testutils "v/testutils" ) // If an override is provided via V_ROOT, it's used as state path. @@ -65,3 +66,21 @@ func TestWriteStateWritesAtPath(t *testing.T) { t.Errorf("Did not find expected state. %v != %v", mockState, readState) } } + +func TestEnsureStatePath(t *testing.T) { + // EnsureStatePath returns an error if the given path doesn't exist. + defer testutils.SetupAndCleanupEnvironment(t)() + err := EnsureStatePath("nonfile") + + if err == nil || !os.IsNotExist(err) { + t.Errorf("Expected NotExist error, got %v.", err) + } + + path, _ := os.MkdirTemp("", GetStatePath("testfolder")) + + err = EnsureStatePath(path) + + if err != nil { + t.Errorf("Expected no error, got %v", err) + } +}