github.com/mithrandie/csvq@v1.18.1/lib/option/main_test.go (about) 1 package option 2 3 import ( 4 "io" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 var TestDir = filepath.Join(os.TempDir(), "csvq_cmd_test") 11 var TestDataDir string 12 13 func TestMain(m *testing.M) { 14 os.Exit(run(m)) 15 } 16 17 func run(m *testing.M) int { 18 defer teardown() 19 20 setup() 21 return m.Run() 22 } 23 24 func setup() { 25 if _, err := os.Stat(TestDir); err == nil { 26 _ = os.RemoveAll(TestDir) 27 } 28 29 wdir, _ := os.Getwd() 30 TestDataDir = filepath.Join(wdir, "..", "..", "testdata", "csv") 31 32 if _, err := os.Stat(TestDir); os.IsNotExist(err) { 33 _ = os.Mkdir(TestDir, 0755) 34 } 35 36 _ = copyfile(filepath.Join(TestDir, "table1.csv"), filepath.Join(TestDataDir, "table1.csv")) 37 } 38 39 func teardown() { 40 if _, err := os.Stat(TestDir); err == nil { 41 _ = os.RemoveAll(TestDir) 42 } 43 } 44 45 func copyfile(dstfile string, srcfile string) error { 46 src, err := os.Open(srcfile) 47 if err != nil { 48 return err 49 } 50 defer func() { _ = src.Close() }() 51 52 dst, err := os.Create(dstfile) 53 if err != nil { 54 return err 55 } 56 defer func() { _ = dst.Close() }() 57 58 _, err = io.Copy(dst, src) 59 if err != nil { 60 return err 61 } 62 63 return nil 64 }