github.com/omoiti/u-root@v7.0.0+incompatible/cmds/core/elvish/util/getwd_test.go (about) 1 package util 2 3 import ( 4 "os" 5 "path" 6 "path/filepath" 7 "runtime" 8 "testing" 9 ) 10 11 func TestGetwd(t *testing.T) { 12 InTempDir(func(tmpdir string) { 13 // On some systems /tmp is a symlink. 14 tmpdir, err := filepath.EvalSymlinks(tmpdir) 15 if err != nil { 16 panic(err) 17 } 18 // Override $HOME to make sure that tmpdir is not abbreviatable. 19 os.Setenv("HOME", "/does/not/exist") 20 if gotwd := Getwd(); gotwd != tmpdir { 21 t.Errorf("Getwd() -> %v, want %v", gotwd, tmpdir) 22 } 23 24 // Override $HOME to trick GetHome. 25 os.Setenv("HOME", tmpdir) 26 27 if gotwd := Getwd(); gotwd != "~" { 28 t.Errorf("Getwd() -> %v, want ~", gotwd) 29 } 30 31 mustOK(os.Mkdir("a", 0700)) 32 mustOK(os.Chdir("a")) 33 if gotwd := Getwd(); gotwd != filepath.Join("~", "a") { 34 t.Errorf("Getwd() -> %v, want ~/a", gotwd) 35 } 36 37 // On macOS os.Getwd will still return the old path name in face of 38 // directory being removed. Hence we only test this on Linux. 39 // TODO(xiaq): Check the behavior on other BSDs and relax this condition 40 // if possible. 41 if runtime.GOOS == "linux" { 42 mustOK(os.Remove(path.Join(tmpdir, "a"))) 43 if gotwd := Getwd(); gotwd != "?" { 44 t.Errorf("Getwd() -> %v, want ?", gotwd) 45 } 46 } 47 }) 48 }