2024-02-01 01:15:01 +00:00
|
|
|
package commands
|
2024-01-28 19:35:31 +00:00
|
|
|
|
|
|
|
import (
|
2024-02-01 01:05:27 +00:00
|
|
|
"bytes"
|
2024-01-28 19:35:31 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2024-01-29 02:57:22 +00:00
|
|
|
cli "v/cli"
|
2024-02-01 01:05:27 +00:00
|
|
|
logger "v/logger"
|
2024-01-29 02:57:22 +00:00
|
|
|
python "v/python"
|
2024-01-28 19:35:31 +00:00
|
|
|
state "v/state"
|
|
|
|
testutils "v/testutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteShim(t *testing.T) {
|
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
|
|
|
|
|
|
|
os.Mkdir(state.GetStatePath("shims"), 0775)
|
|
|
|
testShimPath := state.GetStatePath("shims", "testshim")
|
2024-01-28 21:54:23 +00:00
|
|
|
e := writeShim(testShimPath, "testcommand")
|
2024-01-28 19:35:31 +00:00
|
|
|
|
|
|
|
shimContent, _ := ioutil.ReadFile(testShimPath)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
t.Errorf("Errored while writing shim")
|
|
|
|
}
|
|
|
|
|
2024-01-28 21:54:23 +00:00
|
|
|
if !strings.Contains(string(shimContent), "testcommand") {
|
2024-01-28 19:35:31 +00:00
|
|
|
t.Errorf("Expected shim to contain pass-through via 'which', got %s", shimContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2024-01-29 02:57:22 +00:00
|
|
|
|
2024-02-01 01:10:11 +00:00
|
|
|
func TestWriteShimBubblesError(t *testing.T) {
|
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
|
|
|
|
|
|
|
testShimPath := state.GetStatePath("shims", "testshim")
|
|
|
|
err := writeShim(testShimPath, "testcommand")
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 02:57:22 +00:00
|
|
|
func TestInitializeCreatesStateDirectories(t *testing.T) {
|
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
|
|
|
|
|
|
|
err := Initialize([]string{}, cli.Flags{}, state.State{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error initializing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(state.GetStatePath()); os.IsNotExist(err) {
|
|
|
|
t.Errorf("Root state directory not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(state.GetStatePath("shims")); os.IsNotExist(err) {
|
|
|
|
t.Errorf("Shims directory not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(state.GetStatePath("cache")); os.IsNotExist(err) {
|
|
|
|
t.Errorf("Cache directory not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = os.Stat(state.GetStatePath("runtimes")); os.IsNotExist(err) {
|
|
|
|
t.Errorf("Runtimes directory not found")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitializeCreatesAllPythonShims(t *testing.T) {
|
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
|
|
|
|
|
|
|
err := Initialize([]string{}, cli.Flags{}, state.State{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error initializing")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedShims := python.Shims
|
|
|
|
|
|
|
|
for shimLabel, shimCall := range expectedShims {
|
|
|
|
shimContent, err := os.ReadFile(state.GetStatePath("shims", shimLabel))
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
t.Errorf("%s shim not created", shimLabel)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(string(shimContent), shimCall) {
|
|
|
|
t.Errorf("%s shim does not contain expected call (%s not in %s)", shimLabel, shimCall, shimContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-01 01:05:27 +00:00
|
|
|
|
|
|
|
func TestInitializeWithAddPathPrintsExportPATH(t *testing.T) {
|
|
|
|
defer testutils.SetupAndCleanupEnvironment(t)()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
logger.InfoLogger.SetOutput(&buf)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
logger.InfoLogger.SetOutput(os.Stdout)
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := Initialize([]string{}, cli.Flags{AddPath: true}, state.State{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := "export PATH=" + state.GetStatePath("shims") + ":$PATH\n"
|
|
|
|
if buf.String() != expected {
|
|
|
|
t.Errorf("Expected PATH export, got %s", buf.String())
|
|
|
|
}
|
|
|
|
}
|