Replace "XDG_RUNTIME_DIR" literals with a constant

This commit is contained in:
Kurtis Rader 2020-08-14 22:08:32 -07:00 committed by Qi Xiao
parent e04c503431
commit 947ac39876
3 changed files with 14 additions and 11 deletions

View File

@ -7,6 +7,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
"github.com/elves/elvish/pkg/util"
) )
// Returns a "run directory" for storing ephemeral files, which is guaranteed // Returns a "run directory" for storing ephemeral files, which is guaranteed
@ -38,8 +40,8 @@ func getSecureRunDir() (string, error) {
// preference. // preference.
func getRunDirCandidates() []string { func getRunDirCandidates() []string {
tmpDirPath := filepath.Join(os.TempDir(), fmt.Sprintf("elvish-%d", os.Getuid())) tmpDirPath := filepath.Join(os.TempDir(), fmt.Sprintf("elvish-%d", os.Getuid()))
if os.Getenv("XDG_RUNTIME_DIR") != "" { if os.Getenv(util.EnvXDG_RUNTIME_DIR) != "" {
xdgDirPath := filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "elvish") xdgDirPath := filepath.Join(os.Getenv(util.EnvXDG_RUNTIME_DIR), "elvish")
return []string{xdgDirPath, tmpDirPath} return []string{xdgDirPath, tmpDirPath}
} }
return []string{tmpDirPath} return []string{tmpDirPath}

View File

@ -45,7 +45,7 @@ func TestGetSecureRunDir_PrefersTmpWhenOnlyItExists(t *testing.T) {
func TestGetSecureRunDir_PrefersTmpWhenXdgEnvIsEmpty(t *testing.T) { func TestGetSecureRunDir_PrefersTmpWhenXdgEnvIsEmpty(t *testing.T) {
_, tmp, cleanup := setupForSecureRunDir() _, tmp, cleanup := setupForSecureRunDir()
defer cleanup() defer cleanup()
os.Setenv("XDG_RUNTIME_DIR", "") os.Setenv(util.EnvXDG_RUNTIME_DIR, "")
testSecureRunDir(t, filepath.Join(tmp, elvishDashUid), false) testSecureRunDir(t, filepath.Join(tmp, elvishDashUid), false)
} }
@ -60,8 +60,8 @@ func setupForSecureRunDir() (xdgRuntimeDir, tmpDir string, cleanup func()) {
xdgRuntimeDir, xdgCleanup := util.TestDir() xdgRuntimeDir, xdgCleanup := util.TestDir()
tmpDir, tmpCleanup := util.TestDir() tmpDir, tmpCleanup := util.TestDir()
envCleanup := withTempEnvs(map[string]string{ envCleanup := withTempEnvs(map[string]string{
"XDG_RUNTIME_DIR": xdgRuntimeDir, util.EnvXDG_RUNTIME_DIR: xdgRuntimeDir,
"TMPDIR": tmpDir, "TMPDIR": tmpDir,
}) })
return xdgRuntimeDir, tmpDir, func() { return xdgRuntimeDir, tmpDir, func() {
envCleanup() envCleanup()

View File

@ -6,10 +6,11 @@ package util
// Note that some of these env vars may be significant only in special // Note that some of these env vars may be significant only in special
// circumstances; such as when running unit tests. // circumstances; such as when running unit tests.
const ( const (
EnvHOME = "HOME" EnvHOME = "HOME"
EnvPATH = "PATH" EnvPATH = "PATH"
EnvPATHEXT = "PATHEXT" EnvPATHEXT = "PATHEXT"
EnvPWD = "PWD" EnvPWD = "PWD"
EnvSHLVL = "SHLVL" EnvSHLVL = "SHLVL"
EnvLS_COLORS = "LS_COLORS" EnvLS_COLORS = "LS_COLORS"
EnvXDG_RUNTIME_DIR = "XDG_RUNTIME_DIR"
) )