github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/testhelpers/e2e/env.go (about) 1 package e2e 2 3 import ( 4 "fmt" 5 "os" 6 "os/exec" 7 "path/filepath" 8 "runtime" 9 "strings" 10 "testing" 11 12 "github.com/ActiveState/cli/internal/constants" 13 "github.com/ActiveState/cli/internal/errs" 14 "github.com/ActiveState/cli/internal/fileutils" 15 "github.com/stretchr/testify/require" 16 ) 17 18 func sandboxedTestEnvironment(t *testing.T, dirs *Dirs, updatePath bool, extraEnv ...string) []string { 19 var env []string 20 basePath := platformPath() 21 if os.Getenv(constants.OverrideSandbox) != "" { 22 basePath = os.Getenv("PATH") 23 env = append(env, os.Environ()...) 24 } 25 if value := os.Getenv(constants.ActiveStateCIEnvVarName); value != "" { 26 env = append(env, fmt.Sprintf("%s=%s", constants.ActiveStateCIEnvVarName, value)) 27 } 28 29 // add go binary to PATH 30 goBinary := goBinaryPath(t) 31 basePath = fmt.Sprintf("%s%s%s", basePath, string(os.PathListSeparator), filepath.Dir(goBinary)) 32 33 env = append(env, []string{ 34 constants.ConfigEnvVarName + "=" + dirs.Config, 35 constants.CacheEnvVarName + "=" + dirs.Cache, 36 constants.DisableRuntime + "=true", 37 constants.ProjectEnvVarName + "=", 38 constants.E2ETestEnvVarName + "=true", 39 constants.DisableUpdates + "=true", 40 constants.OptinUnstableEnvVarName + "=true", 41 constants.ServiceSockDir + "=" + dirs.SockRoot, 42 constants.HomeEnvVarName + "=" + dirs.HomeDir, 43 systemHomeEnvVarName + "=" + dirs.HomeDir, 44 "NO_COLOR=true", 45 "CI=true", 46 }...) 47 48 if updatePath { 49 // add bin path 50 oldPath := basePath 51 newPath := fmt.Sprintf( 52 "PATH=%s%s%s", 53 dirs.Bin, string(os.PathListSeparator), oldPath, 54 ) 55 env = append(env, newPath) 56 } else { 57 env = append(env, "PATH="+basePath) 58 } 59 60 // append platform specific environment variables 61 env = append(env, platformSpecificEnv(dirs)...) 62 63 // Prepare sandboxed home directory 64 err := prepareHomeDir(dirs.HomeDir) 65 require.NoError(t, err) 66 67 // add session environment variables 68 env = append(env, extraEnv...) 69 70 return env 71 } 72 73 func prepareHomeDir(dir string) error { 74 if runtime.GOOS == "windows" { 75 return nil 76 } 77 78 if !fileutils.DirExists(dir) { 79 err := fileutils.Mkdir(dir) 80 if err != nil { 81 return errs.Wrap(err, "Could not create home dir") 82 } 83 } 84 85 var filename string 86 switch runtime.GOOS { 87 case "linux": 88 filename = ".bashrc" 89 case "darwin": 90 filename = ".zshrc" 91 } 92 93 rcFile := filepath.Join(dir, filename) 94 err := fileutils.Touch(rcFile) 95 if err != nil { 96 return errs.Wrap(err, "Could not create rc file") 97 } 98 99 return nil 100 } 101 102 func goBinaryPath(t *testing.T) string { 103 locator := "which" 104 if runtime.GOOS == "windows" { 105 locator = "where" 106 } 107 cmd := exec.Command(locator, "go") 108 output, err := cmd.Output() 109 if err != nil { 110 t.Log("Could not find go binary") 111 return "" 112 } 113 goBinary := string(output) 114 goBinary = strings.TrimSpace(string(goBinary)) 115 return goBinary 116 }