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