2024-01-26 04:20:26 +00:00
|
|
|
package python
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2024-01-23 17:55:06 +00:00
|
|
|
cli "v/cli"
|
|
|
|
logger "v/logger"
|
2024-01-23 17:45:08 +00:00
|
|
|
state "v/state"
|
2024-01-26 04:09:52 +00:00
|
|
|
testutils "v/testutils"
|
2023-12-04 04:54:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestListVersionOutputsNoticeIfNoVersionsInstalled(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-23 17:45:08 +00:00
|
|
|
os.Mkdir(state.GetStatePath("runtimes"), 0750)
|
2023-12-04 04:54:02 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-26 05:40:16 +00:00
|
|
|
listVersions([]string{}, cli.Flags{}, state.State{})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := out.String()
|
|
|
|
if captured != "No versions installed!\n" {
|
|
|
|
t.Errorf("Unexpected message: %s", captured)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListVersionOutputsVersionsInstalled(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-23 17:45:08 +00:00
|
|
|
os.MkdirAll(state.GetStatePath("runtimes", "py-1.2.3"), 0750)
|
2023-12-04 04:54:02 +00:00
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-26 05:40:16 +00:00
|
|
|
listVersions([]string{}, cli.Flags{}, state.State{})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := out.String()
|
|
|
|
if captured != "1.2.3\n" {
|
|
|
|
t.Errorf("Unexpected message: %s", captured)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListVersionReturnsErrorOnFailure(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-26 05:40:16 +00:00
|
|
|
err := listVersions([]string{}, cli.Flags{}, state.State{})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := out.String()
|
|
|
|
if captured != "" {
|
|
|
|
t.Errorf("Captured unexpected message: %s", captured)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error returned, did not get one.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListVersionOutputsVersionSelectedAndWarnsNotInstalled(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-26 05:40:16 +00:00
|
|
|
which([]string{}, cli.Flags{}, state.State{GlobalVersion: "1.2.3"})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := out.String()
|
|
|
|
if captured != "The desired version (1.2.3) is not installed.\n" {
|
|
|
|
t.Errorf("Unexpected message: %s", captured)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhichOutputsVersionSelectedIfInstalled(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-23 17:45:08 +00:00
|
|
|
os.MkdirAll(state.GetStatePath("runtimes", "py-1.2.3"), 0750)
|
2024-01-26 05:40:16 +00:00
|
|
|
which([]string{}, cli.Flags{}, state.State{GlobalVersion: "1.2.3"})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := strings.TrimSpace(out.String())
|
2024-01-23 17:45:08 +00:00
|
|
|
expected := state.GetStatePath("runtimes", "py-1.2.3", "bin", "python1.2")
|
2023-12-04 04:54:02 +00:00
|
|
|
if !strings.Contains(captured, expected) {
|
|
|
|
t.Errorf("Unexpected message: %s, not %s", captured, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhichOutputsSystemVersionIfNoneSelected(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-26 05:40:16 +00:00
|
|
|
which([]string{}, cli.Flags{RawOutput: true}, state.State{})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := strings.TrimSpace(out.String())
|
|
|
|
|
|
|
|
if captured != "/bin/python (system)" {
|
|
|
|
t.Errorf("%s != %s", captured, "/bin/python (system)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWhichOutputsVersionWithoutPrefixesIfRawOutput(t *testing.T) {
|
2024-01-26 04:09:52 +00:00
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2024-01-23 17:55:06 +00:00
|
|
|
logger.InfoLogger.SetOutput(&out)
|
|
|
|
defer logger.InfoLogger.SetOutput(os.Stdout)
|
2023-12-04 04:54:02 +00:00
|
|
|
|
2024-01-23 17:45:08 +00:00
|
|
|
os.MkdirAll(state.GetStatePath("runtimes", "py-1.2.3"), 0750)
|
2024-01-26 05:40:16 +00:00
|
|
|
which([]string{}, cli.Flags{RawOutput: true}, state.State{GlobalVersion: "1.2.3"})
|
2023-12-04 04:54:02 +00:00
|
|
|
|
|
|
|
captured := strings.TrimSpace(out.String())
|
2024-01-23 17:45:08 +00:00
|
|
|
expected := state.GetStatePath("runtimes", "py-1.2.3", "bin", "python1.2")
|
2023-12-04 04:54:02 +00:00
|
|
|
if captured != expected {
|
|
|
|
t.Errorf("Unexpected message: %s, not %s", captured, expected)
|
|
|
|
}
|
|
|
|
}
|