test(state-mgmt): add missing coverage for EnsureStatePath, relocate misplaced Python version listing func
This commit is contained in:
parent
a0345a1606
commit
906cf9a4b0
4 changed files with 33 additions and 13 deletions
|
@ -26,7 +26,7 @@ func use(args []string, flags cli.Flags, currentState state.State) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
availableVersions := state.GetAvailableVersions()
|
availableVersions := GetAvailableVersions()
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for _, v := range availableVersions {
|
for _, v := range availableVersions {
|
||||||
|
|
|
@ -112,3 +112,16 @@ func DetermineSystemPython() (string, string) {
|
||||||
detectedVersion, _ := strings.CutPrefix(versionOut, "Python")
|
detectedVersion, _ := strings.CutPrefix(versionOut, "Python")
|
||||||
return strings.TrimSpace(detectedVersion), "/bin/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
|
||||||
|
}
|
||||||
|
|
|
@ -51,15 +51,3 @@ func WriteState(version string) {
|
||||||
d, _ := json.Marshal(state)
|
d, _ := json.Marshal(state)
|
||||||
ioutil.WriteFile(GetStatePath("state.json"), d, 0750)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
testutils "v/testutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// If an override is provided via V_ROOT, it's used as state path.
|
// 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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue