github.com/searchspring/haus@v0.1.8-0.20200414161854-a7ca8bb9ea93/fileutils/dir_test.go (about) 1 package fileutils 2 3 import( 4 "testing" 5 "os" 6 "io/ioutil" 7 8 ) 9 10 func TestCreatePath(t *testing.T) { 11 testpath, err := ioutil.TempDir("", "RepoTsar") 12 if err != nil { 13 t.Error(err) 14 } 15 defer os.RemoveAll(testpath) 16 17 // Test path already exists 18 path,err := CreatePath(testpath) 19 if err != nil { 20 t.Error(err) 21 } 22 if path != testpath { 23 t.Errorf("Expected %#v, got %#v", testpath, path) 24 } 25 // Test path doesn't exist 26 path,err = CreatePath(testpath+"/Testdir") 27 if err != nil { 28 t.Error(err) 29 } 30 if path != testpath+"/Testdir" { 31 t.Errorf("Expected %#v, got %#v", testpath+"/Testdir", path) 32 } 33 34 // Test expanding ~ 35 homedir := os.Getenv("HOME") 36 os.Setenv("HOME",testpath) 37 defer os.Setenv("HOME",homedir) 38 path,err = CreatePath("~/Testdir2") 39 if err != nil { 40 t.Error(err) 41 } 42 if path != testpath+"/Testdir2" { 43 t.Errorf("Expected %#v, got %#v", testpath+"/Testdir2", path) 44 } 45 }