diff --git a/pkg/shell/paths_unix.go b/pkg/shell/paths_unix.go index f3ac8fdb..303fa1d6 100644 --- a/pkg/shell/paths_unix.go +++ b/pkg/shell/paths_unix.go @@ -7,6 +7,8 @@ import ( "os" "path/filepath" "syscall" + + "github.com/elves/elvish/pkg/util" ) // Returns a "run directory" for storing ephemeral files, which is guaranteed @@ -38,8 +40,8 @@ func getSecureRunDir() (string, error) { // preference. func getRunDirCandidates() []string { tmpDirPath := filepath.Join(os.TempDir(), fmt.Sprintf("elvish-%d", os.Getuid())) - if os.Getenv("XDG_RUNTIME_DIR") != "" { - xdgDirPath := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "elvish") + if os.Getenv(util.EnvXDG_RUNTIME_DIR) != "" { + xdgDirPath := filepath.Join(os.Getenv(util.EnvXDG_RUNTIME_DIR), "elvish") return []string{xdgDirPath, tmpDirPath} } return []string{tmpDirPath} diff --git a/pkg/shell/paths_unix_test.go b/pkg/shell/paths_unix_test.go index 2c4fe64a..1194478c 100644 --- a/pkg/shell/paths_unix_test.go +++ b/pkg/shell/paths_unix_test.go @@ -45,7 +45,7 @@ func TestGetSecureRunDir_PrefersTmpWhenOnlyItExists(t *testing.T) { func TestGetSecureRunDir_PrefersTmpWhenXdgEnvIsEmpty(t *testing.T) { _, tmp, cleanup := setupForSecureRunDir() defer cleanup() - os.Setenv("XDG_RUNTIME_DIR", "") + os.Setenv(util.EnvXDG_RUNTIME_DIR, "") testSecureRunDir(t, filepath.Join(tmp, elvishDashUid), false) } @@ -60,8 +60,8 @@ func setupForSecureRunDir() (xdgRuntimeDir, tmpDir string, cleanup func()) { xdgRuntimeDir, xdgCleanup := util.TestDir() tmpDir, tmpCleanup := util.TestDir() envCleanup := withTempEnvs(map[string]string{ - "XDG_RUNTIME_DIR": xdgRuntimeDir, - "TMPDIR": tmpDir, + util.EnvXDG_RUNTIME_DIR: xdgRuntimeDir, + "TMPDIR": tmpDir, }) return xdgRuntimeDir, tmpDir, func() { envCleanup() diff --git a/pkg/util/util.go b/pkg/util/util.go index 9d8b3da6..e91a10e9 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -6,10 +6,11 @@ package util // Note that some of these env vars may be significant only in special // circumstances; such as when running unit tests. const ( - EnvHOME = "HOME" - EnvPATH = "PATH" - EnvPATHEXT = "PATHEXT" - EnvPWD = "PWD" - EnvSHLVL = "SHLVL" - EnvLS_COLORS = "LS_COLORS" + EnvHOME = "HOME" + EnvPATH = "PATH" + EnvPATHEXT = "PATHEXT" + EnvPWD = "PWD" + EnvSHLVL = "SHLVL" + EnvLS_COLORS = "LS_COLORS" + EnvXDG_RUNTIME_DIR = "XDG_RUNTIME_DIR" )