Files
Stefan Haller f0ccb937d3 Use lo.Map instead of manual append loops
Not only is this nicer code (and more idiomatic at least in this code
base), but it also avoids linter warnings about missing preallocations
(lo.Map does preallocate the result array).
2026-08-16 16:35:11 +02:00

71 lines
2.3 KiB
Go

package components
import (
"fmt"
"os"
"github.com/samber/lo"
)
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 integration 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 {
return lo.Map(hostEnvironmentAllowlist[:], func(envVar string, _ int) string {
return fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar))
})
}
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)))
// Disable gh telemetry. It was enabled by default in gh 2.91.0, and
// this would cause gh config files to be left in the working tree
// (e.g. `test/.local/state/gh/device-id`).
env = append(env, "GH_TELEMETRY=disabled")
return env
}