github.com/hoop33/elvish@v0.0.0-20160801152013-6d25485beab4/util/getwd_test.go (about)

     1  package util
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  )
     9  
    10  func TestGetwd(t *testing.T) {
    11  	tmpdir, error := ioutil.TempDir("", "elvishtest.")
    12  	if error != nil {
    13  		t.Errorf("Got error when creating temp dir: %v", error)
    14  	} else {
    15  		os.Chdir(tmpdir)
    16  		// On some systems /tmp is a symlink.
    17  		tmpdir, error = filepath.EvalSymlinks(tmpdir)
    18  		if gotwd := Getwd(); gotwd != tmpdir || error != nil {
    19  			t.Errorf("Getwd() -> %v, want %v", gotwd, tmpdir)
    20  		}
    21  	}
    22  
    23  	// Override $HOME to trick GetHome.
    24  	os.Setenv("HOME", tmpdir)
    25  
    26  	if gotwd := Getwd(); gotwd != "~" {
    27  		t.Errorf("Getwd() -> %v, want ~", gotwd)
    28  	}
    29  
    30  	mustOK(os.Mkdir("a", 0700))
    31  	mustOK(os.Chdir("a"))
    32  	if gotwd := Getwd(); gotwd != "~/a" {
    33  		t.Errorf("Getwd() -> %v, want ~/a", gotwd)
    34  	}
    35  
    36  	mustOK(os.Remove(tmpdir + "/a"))
    37  	mustOK(os.Remove(tmpdir))
    38  	// XXX On OS X os.Getwd will still return the old path name in face of
    39  	// directory being removed. We disable this test.
    40  	/*
    41  		if gotwd := Getwd(); gotwd != "?" {
    42  			t.Errorf("Getwd() -> %v, want ?", gotwd)
    43  		}
    44  	*/
    45  }
    46  
    47  func mustOK(err error) {
    48  	if err != nil {
    49  		panic(err)
    50  	}
    51  }