summaryrefslogtreecommitdiffstats
path: root/pkg/integration/components/env.go
blob: d3cdf467f609bec799de1963a6425d1ae55c0350 (plain)
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
package components

import (
	"fmt"
	"os"
)

const (
	// These values will be passed to lazygit
	LAZYGIT_ROOT_DIR          = "LAZYGIT_ROOT_DIR"
	SANDBOX_ENV_VAR           = "SANDBOX"
	TEST_NAME_ENV_VAR         = "TEST_NAME"
	WAIT_FOR_DEBUGGER_ENV_VAR = "WAIT_FOR_DEBUGGER"

	// These values will be passed to both lazygit and shell commands
	GIT_CONFIG_GLOBAL_ENV_VAR = "GIT_CONFIG_GLOBAL"
	// We pass PWD because if it's defined, Go will use it as the working directory
	// rather than make a syscall to the OS, and that means symlinks won't be resolved,
	// which is good to test for.
	PWD = "PWD"

	// We set $HOME and $GIT_CONFIG_NOGLOBAL during integrationt tests so
	// that older versions of git that don't respect $GIT_CONFIG_GLOBAL
	// will find the correct global config file for testing
	HOME                = "HOME"
	GIT_CONFIG_NOGLOBAL = "GIT_CONFIG_NOGLOBAL"

	// These values will be passed through to lazygit and shell commands, with their
	// values inherited from the host environment
	PATH = "PATH"
	TERM = "TERM"
)

// Tests will inherit these environment variables from the host environment, rather
// than the test runner deciding the values itself.
// All other environment variables present in the host environment will be ignored.
// Having such a minimal list ensures that lazygit behaves the same across different test environments.
var hostEnvironmentAllowlist = [...]string{
	PATH,
	TERM,
}

// Returns a copy of the environment filtered by
// hostEnvironmentAllowlist
func allowedHostEnvironment() []string {
	env := []string{}
	for _, envVar := range hostEnvironmentAllowlist {
		env = append(env, fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar)))
	}
	return env
}

func NewTestEnvironment(rootDir string) []string {
	env := allowedHostEnvironment()

	// Set $HOME to control the global git config location for git
	// versions <= 2.31.8
	env = append(env, fmt.Sprintf("%s=%s", HOME, testPath(rootDir)))

	// $GIT_CONFIG_GLOBAL controls global git config location for git
	// versions >= 2.32.0
	env = append(env, fmt.Sprintf("%s=%s", GIT_CONFIG_GLOBAL_ENV_VAR, globalGitConfigPath(rootDir)))

	return env
}