github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/test/testinit.go (about) 1 // Package test init test directories 2 package test 3 4 // https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009685279-Go-test-working-directory-keeps-changing-to-dir-of-the-test-file-instead-of-value-in-template 5 import ( 6 "os" 7 "path" 8 "runtime" 9 "testing" 10 11 log "github.com/sirupsen/logrus" 12 ) 13 14 // TestDir working dir for test 15 var TestDir string 16 17 // TestData directory for working Attachments 18 var TestData string 19 20 // Testinit set test directory 21 func Testinit(t *testing.T) { 22 _, filename, _, _ := runtime.Caller(0) 23 dir := path.Dir(filename) 24 err := os.Chdir(dir) 25 if err == nil { 26 TestDir = dir 27 TestData = path.Join(TestDir, "testdata") 28 // create data directory and ignore errors 29 err = os.Mkdir(TestData, 0750) 30 if err != nil && !os.IsExist(err) { 31 t.Fatalf("Init error:%s", err) 32 } 33 t.Logf("Test in %s", dir) 34 } else { 35 t.Fatalf("Init error:%s", err) 36 } 37 } 38 39 // InitTestDirs set test directory 40 func InitTestDirs() { 41 _, filename, _, _ := runtime.Caller(0) 42 dir := path.Dir(filename) 43 err := os.Chdir(dir) 44 if err == nil { 45 TestDir = dir 46 TestData = path.Join(TestDir, "testdata") 47 // create data directory and ignore errors 48 err = os.Mkdir(TestData, 0750) 49 if err != nil && !os.IsExist(err) { 50 log.Fatalf("Init error:%s", err) 51 } 52 } else { 53 log.Fatalf("Init error:%s", err) 54 } 55 }