test(state-mgmt): add missing coverage for EnsureStatePath, relocate misplaced Python version listing func

This commit is contained in:
Marc 2024-01-31 19:54:57 -05:00
parent a0345a1606
commit 906cf9a4b0
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 33 additions and 13 deletions

View file

@ -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 {

View file

@ -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
}

View file

@ -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
}

View file

@ -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)
}
}