github.com/ladydascalie/elvish@v0.0.0-20170703214355-2964dd3ece7f/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 if gotwd := Getwd(); gotwd != tmpdir { 19 t.Errorf("Getwd() -> %v, want %v", gotwd, tmpdir) 20 } 21 22 // Override $HOME to trick GetHome. 23 os.Setenv("HOME", tmpdir) 24 25 if gotwd := Getwd(); gotwd != "~" { 26 t.Errorf("Getwd() -> %v, want ~", gotwd) 27 } 28 29 mustOK(os.Mkdir("a", 0700)) 30 mustOK(os.Chdir("a")) 31 if gotwd := Getwd(); gotwd != "~/a" { 32 t.Errorf("Getwd() -> %v, want ~/a", gotwd) 33 } 34 35 // On macOS os.Getwd will still return the old path name in face of 36 // directory being removed. Hence we only test this on Linux. 37 // TODO(xiaq): Check the behavior on other BSDs and relax this condition 38 // if possible. 39 if runtime.GOOS == "linux" { 40 mustOK(os.Remove(path.Join(tmpdir, "a"))) 41 if gotwd := Getwd(); gotwd != "?" { 42 t.Errorf("Getwd() -> %v, want ?", gotwd) 43 } 44 } 45 }) 46 } 47 48 func mustOK(err error) { 49 if err != nil { 50 panic(err) 51 } 52 }