github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/shell/paths_unix_test.go (about) 1 // +build !windows,!plan9 2 3 package shell 4 5 import ( 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "src.elv.sh/pkg/env" 13 "src.elv.sh/pkg/testutil" 14 ) 15 16 // TODO(xiaq): Rewrite these tests to test the exported MakePaths instead of the 17 // unexported getSecureRunDir. 18 19 var elvishDashUID = fmt.Sprintf("elvish-%d", os.Getuid()) 20 21 func TestGetSecureRunDir_PrefersXDGWhenNeitherExists(t *testing.T) { 22 xdg, _, cleanup := setupForSecureRunDir() 23 defer cleanup() 24 testSecureRunDir(t, filepath.Join(xdg, "elvish"), false) 25 } 26 27 func TestGetSecureRunDir_PrefersXDGWhenBothExist(t *testing.T) { 28 xdg, tmp, cleanup := setupForSecureRunDir() 29 defer cleanup() 30 31 os.MkdirAll(filepath.Join(xdg, "elvish"), 0700) 32 os.MkdirAll(filepath.Join(tmp, elvishDashUID), 0700) 33 34 testSecureRunDir(t, filepath.Join(xdg, "elvish"), false) 35 } 36 37 func TestGetSecureRunDir_PrefersTmpWhenOnlyItExists(t *testing.T) { 38 _, tmp, cleanup := setupForSecureRunDir() 39 defer cleanup() 40 41 os.MkdirAll(filepath.Join(tmp, elvishDashUID), 0700) 42 43 testSecureRunDir(t, filepath.Join(tmp, elvishDashUID), false) 44 } 45 46 func TestGetSecureRunDir_PrefersTmpWhenXdgEnvIsEmpty(t *testing.T) { 47 _, tmp, cleanup := setupForSecureRunDir() 48 defer cleanup() 49 os.Setenv(env.XDG_RUNTIME_DIR, "") 50 testSecureRunDir(t, filepath.Join(tmp, elvishDashUID), false) 51 } 52 53 func TestGetSecureRunDir_ReturnsErrorWhenUnableToMkdir(t *testing.T) { 54 xdg, _, cleanup := setupForSecureRunDir() 55 defer cleanup() 56 ioutil.WriteFile(filepath.Join(xdg, "elvish"), nil, 0600) 57 testSecureRunDir(t, "", true) 58 } 59 60 func setupForSecureRunDir() (xdgRuntimeDir, tmpDir string, cleanup func()) { 61 xdgRuntimeDir, xdgCleanup := testutil.TestDir() 62 tmpDir, tmpCleanup := testutil.TestDir() 63 envCleanup := withTempEnvs(map[string]string{ 64 env.XDG_RUNTIME_DIR: xdgRuntimeDir, 65 "TMPDIR": tmpDir, 66 }) 67 return xdgRuntimeDir, tmpDir, func() { 68 envCleanup() 69 tmpCleanup() 70 xdgCleanup() 71 } 72 } 73 74 func testSecureRunDir(t *testing.T, wantRunDir string, wantErr bool) { 75 runDir, err := getSecureRunDir() 76 if runDir != wantRunDir { 77 t.Errorf("got rundir %q, want %q", runDir, wantRunDir) 78 } 79 if wantErr && err == nil { 80 t.Errorf("got nil err, want non-nil") 81 } else if !wantErr && err != nil { 82 t.Errorf("got err %v, want nil err", err) 83 } 84 } 85 86 // TODO(xiaq): Move to the testutil package and add tests. 87 func withTempEnvs(envOverrides map[string]string) func() { 88 valuesToRestore := map[string]string{} 89 90 for key, value := range envOverrides { 91 original, exists := os.LookupEnv(key) 92 93 os.Setenv(key, value) 94 95 if exists { 96 valuesToRestore[key] = original 97 } 98 } 99 100 return func() { 101 for key := range envOverrides { 102 value, exists := valuesToRestore[key] 103 if exists { 104 os.Setenv(key, value) 105 } else { 106 os.Unsetenv(key) 107 } 108 } 109 } 110 }