github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/test/test.go (about) 1 package test 2 3 import ( 4 "io/ioutil" 5 "path" 6 "path/filepath" 7 "runtime" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 // GetRootDir return the root directory of the project 14 func GetRootDir() string { 15 _, mainFile, _, _ := runtime.Caller(2) 16 return filepath.Dir(mainFile) 17 } 18 19 // CreateDir creates a temporary directory for testing 20 func CreateDir(prefix string) string { 21 tmpDir := path.Join(path.Dir(path.Dir(GetRootDir())), "private/tmp") 22 dir, err := ioutil.TempDir(tmpDir, prefix) 23 if err != nil { 24 panic(err) 25 } 26 return dir 27 } 28 29 func RecoverPanic(t *testing.T, expected string, msgs ...string) { 30 msg := "" 31 if len(msgs) > 0 { 32 msg = "should panic on " + msgs[0] 33 } 34 if rs := recover(); rs != nil { 35 if err, ok := rs.(error); ok && err != nil { 36 assert.Equal(t, expected, err.Error(), msg) 37 } else { 38 assert.Equal(t, expected, rs, msg) 39 } 40 } 41 }