forked from xo/usql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcli.go
152 lines (142 loc) · 3.28 KB
/
testcli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//go:build ignore
// Command testcli runs goexpect tests against a built usql binary.
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"time"
gexpect "github.com/google/goexpect"
)
func main() {
binpath := flag.String("binpath", "./usql", "bin path")
deadline := flag.Duration("deadline", 5*time.Minute, "total execution deadline")
timeout := flag.Duration("timeout", 2*time.Minute, "individual test timeout")
re := flag.String("run", "", "test name regexp to run")
flag.Parse()
if err := run(context.Background(), *binpath, *deadline, *timeout, *re); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(ctx context.Context, binpath string, deadline, timeout time.Duration, re string) error {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(deadline))
defer cancel()
tests, err := cliTests()
if err != nil {
return err
}
var nameRE *regexp.Regexp
if re != "" {
nameRE, err = regexp.Compile(re)
if err != nil {
return err
}
}
for _, test := range tests {
if nameRE != nil && !nameRE.MatchString(test.name) {
log.Printf(">>> SKIPPING: %s", test.name)
continue
}
log.Printf(">>> RUNNING: %s", test.name)
if err := test.do(ctx, binpath, timeout); err != nil {
return fmt.Errorf("test %s: %v", test.name, err)
}
log.Printf(">>> COMPLETED: %s", test.name)
}
return nil
}
type Test struct {
name string
script string
args []string
env []string
}
func cliTests() ([]Test, error) {
env := append(os.Environ(), "TERM=xterm-256color")
return []Test{
{
"complex/postgres",
"./contrib/postgres/test.sql",
[]string{"pgsql://postgres:P4ssw0rd@localhost", "--pset=pager=off"},
env,
},
{
"complex/mysql",
"./contrib/mysql/test.sql",
[]string{"my://root:P4ssw0rd@localhost", "--pset=pager=off"},
env,
},
{
"complex/sqlite3",
"./contrib/sqlite3/test.sql",
[]string{"sqlite:./testdata/sqlite3_test.db", "--pset=pager=off"},
env,
},
{
"complex/moderncsqlite",
"./contrib/sqlite3/test.sql",
[]string{"mq:./testdata/moderncsqlite_test.db", "--pset=pager=off"},
env,
},
{
"complex/sqlserver",
"./contrib/sqlserver/test.sql",
[]string{"sqlserver://sa:Adm1nP@ssw0rd@localhost/", "--pset=pager=off"},
env,
},
{
"complex/cassandra",
"./contrib/cassandra/test.sql",
[]string{"ca://cassandra:cassandra@localhost", "--pset=pager=off"},
env,
},
{
"copy/a_bit_of_everything",
"./testdata/copy.sql",
[]string{"--pset=pager=off"},
env,
},
}, nil
}
func (test Test) do(ctx context.Context, binpath string, timeout time.Duration) error {
exp, errch, err := gexpect.SpawnWithArgs(
append([]string{binpath}, test.args...),
timeout,
gexpect.SetEnv(test.env),
gexpect.Tee(&noopWriteCloser{os.Stdout}),
)
if err != nil {
return err
}
buf, err := ioutil.ReadFile(test.script)
if err != nil {
return err
}
for _, line := range bytes.Split(buf, []byte{'\n'}) {
if err := exp.Send(string(line) + "\n"); err != nil {
return err
}
}
select {
case <-ctx.Done():
defer exp.Close()
return ctx.Err()
case err := <-errch:
defer exp.Close()
return err
}
return exp.Close()
}
type noopWriteCloser struct {
io.Writer
}
func (*noopWriteCloser) Close() error {
return nil
}