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"
"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}

View File

@ -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()

View File

@ -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"
)