github.com/mithrandie/csvq@v1.18.1/lib/terminal/main_test.go (about) 1 package terminal 2 3 import ( 4 "context" 5 "io" 6 "os" 7 "path/filepath" 8 "runtime" 9 "testing" 10 11 "github.com/mithrandie/csvq/lib/file" 12 "github.com/mithrandie/csvq/lib/option" 13 "github.com/mithrandie/csvq/lib/query" 14 15 "github.com/mitchellh/go-homedir" 16 ) 17 18 var tempdir, _ = filepath.Abs(os.TempDir()) 19 var TestDir = filepath.Join(tempdir, "csvq_terminal_test") 20 var CompletionTestDir = filepath.Join(TestDir, "completion") 21 var CompletionTestSubDir = filepath.Join(TestDir, "completion", "sub") 22 var TestLocation = "UTC" 23 var TestTx, _ = query.NewTransaction(context.Background(), file.DefaultWaitTimeout, file.DefaultRetryDelay, query.NewSession()) 24 var TestDataDir string 25 var HomeDir string 26 27 func GetWD() string { 28 wdir, _ := os.Getwd() 29 return wdir 30 } 31 32 func TestMain(m *testing.M) { 33 os.Exit(run(m)) 34 } 35 36 func run(m *testing.M) int { 37 defer teardown() 38 39 setup() 40 return m.Run() 41 } 42 43 func initFlag(flags *option.Flags) { 44 cpu := runtime.NumCPU() / 2 45 if cpu < 1 { 46 cpu = 1 47 } 48 49 flags.Repository = "." 50 flags.DatetimeFormat = []string{} 51 flags.AnsiQuotes = false 52 flags.StrictEqual = false 53 flags.WaitTimeout = 15 54 flags.ImportOptions = option.NewImportOptions() 55 flags.ExportOptions = option.NewExportOptions() 56 flags.Quiet = false 57 flags.LimitRecursion = 5 58 flags.CPU = cpu 59 flags.Stats = false 60 flags.SetColor(false) 61 _ = flags.SetLocation(TestLocation) 62 } 63 64 func copyfile(dstfile string, srcfile string) error { 65 src, err := os.Open(srcfile) 66 if err != nil { 67 return err 68 } 69 defer func() { _ = src.Close() }() 70 71 dst, err := os.Create(dstfile) 72 if err != nil { 73 return err 74 } 75 defer func() { _ = dst.Close() }() 76 77 _, err = io.Copy(dst, src) 78 if err != nil { 79 return err 80 } 81 82 return nil 83 } 84 85 func setup() { 86 if _, err := os.Stat(TestDir); err == nil { 87 _ = os.RemoveAll(TestDir) 88 } 89 if _, err := os.Stat(TestDir); os.IsNotExist(err) { 90 _ = os.Mkdir(TestDir, 0755) 91 } 92 if _, err := os.Stat(CompletionTestDir); os.IsNotExist(err) { 93 _ = os.Mkdir(CompletionTestDir, 0755) 94 } 95 if _, err := os.Stat(CompletionTestSubDir); os.IsNotExist(err) { 96 _ = os.Mkdir(CompletionTestSubDir, 0755) 97 } 98 99 HomeDir, _ = homedir.Dir() 100 TestDataDir = filepath.Join(GetWD(), "..", "..", "testdata", "csv") 101 102 _ = copyfile(filepath.Join(CompletionTestDir, "table1.csv"), filepath.Join(TestDataDir, "table1.csv")) 103 _ = copyfile(filepath.Join(CompletionTestDir, ".table1.csv"), filepath.Join(TestDataDir, "table1.csv")) 104 _ = copyfile(filepath.Join(CompletionTestDir, "source.sql"), filepath.Join(filepath.Join(GetWD(), "..", "..", "testdata"), "source.sql")) 105 _ = copyfile(filepath.Join(CompletionTestSubDir, "table2.csv"), filepath.Join(TestDataDir, "table2.csv")) 106 107 _ = os.Setenv("CSVQ_TEST_ENV", "foo") 108 query.Version = "v1.0.0" 109 TestTx.Session.SetStdout(query.NewDiscard()) 110 TestTx.Session.SetStderr(query.NewDiscard()) 111 initFlag(TestTx.Flags) 112 } 113 114 func teardown() { 115 if _, err := os.Stat(TestDir); err == nil { 116 _ = os.RemoveAll(TestDir) 117 } 118 }