Repository navigation
Expand file tree
/
Copy pathcli_test.go
More file actions
359 lines (346 loc) · 12.5 KB
/
Copy pathcli_test.go
File metadata and controls
359 lines (346 loc) · 12.5 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"testing"
"time"
"github.com/xo/usql/internal/capture"
)
// update rewrites the golden files instead of comparing against them.
//
// Read the diff before committing a rewrite. A golden here records what usql
// does, so rewriting one accepts whatever it does now as correct.
var update = flag.Bool("update", false, "rewrite the golden transcripts")
// usqlPath is the binary the sessions drive. TestMain builds it.
var usqlPath string
// buildTags are the tags the recorded binary is built with. Only the drivers
// the sessions use are needed, and a smaller set builds much faster than the
// full one.
const buildTags = "sqlite3 sqlite_app_armor sqlite_fts5 sqlite_introspect sqlite_json1 sqlite_math_functions sqlite_stat4 sqlite_vtable no_drivers"
func TestMain(m *testing.M) {
flag.Parse()
code, err := runMain(m)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
if code == 0 {
code = 1
}
}
os.Exit(code)
}
// runMain builds usql into a temporary directory and runs the tests.
func runMain(m *testing.M) (int, error) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
// Nothing to build, because Record cannot run here. The tests
// themselves report the skip, so that the reason is visible.
return m.Run(), nil
}
dir, err := os.MkdirTemp("", "usql-cli-")
if err != nil {
return 0, fmt.Errorf("creating build directory: %w", err)
}
defer func() { _ = os.RemoveAll(dir) }()
usqlPath = filepath.Join(dir, "usql")
cmd := exec.Command("go", "build", "-tags", buildTags, "-o", usqlPath, ".")
if out, err := cmd.CombinedOutput(); err != nil {
return 0, fmt.Errorf("building usql: %w\n%s", err, out)
}
return m.Run(), nil
}
// scrubbers replace the parts of the output that change between runs. Without
// them a golden churns on every release and on every machine.
var scrubbers = []capture.Scrubber{
// The banner carries the version of usql itself.
{Match: regexp.MustCompile(`usql, the universal command-line interface for SQL databases, version [^\r\n]+`), With: `usql, the universal command-line interface for SQL databases, version VERSION`},
// The variable VERSION carries it too, with the version of Go.
{Match: regexp.MustCompile(`VERSION = 'usql \d+\.\d+\.\d+[^'\r\n]*'`), With: `VERSION = 'usql VERSION'`},
{Match: regexp.MustCompile(`usql \d+\.\d+\.\d+[^\r\n]*`), With: `usql VERSION`},
// The variable SERVER_VERSION_NAME carries the SQLite version.
{Match: regexp.MustCompile(`SERVER_VERSION_NAME = '[^'\r\n]*'`), With: `SERVER_VERSION_NAME = 'VERSION'`},
// The connection banner carries the SQLite version.
{Match: regexp.MustCompile(`\([^)\r\n]*\d+\.\d+\.\d+[^)\r\n]*\)`), With: `(VERSION)`},
// Query timings.
{Match: regexp.MustCompile(`\d+\.\d+ ?ms`), With: `TIME`},
// The temporary home directory appears in error messages and in \conninfo.
{Match: regexp.MustCompile(`/tmp/usql-capture-[A-Za-z0-9]+`), With: `HOME`},
{Match: regexp.MustCompile(`/var/folders/[^\s"']+`), With: `HOME`},
}
// sessions are the recorded terminal sessions.
//
// Every one of these uses SQLite, so they need no container and run wherever
// usql builds. A session that needed a server would make the whole file depend
// on one.
func sessions() []capture.Session {
// memory is an in-memory SQLite database, so nothing is written to disk
// and each session starts from the same empty state.
const memory = "sqlite3://:memory:"
// noPager keeps the output in the terminal. Without it usql hands long
// output to a pager and the recording captures the pager instead.
noPager := []string{"--pset=pager=off"}
args := func(extra ...string) []string {
return append(append([]string{}, noPager...), extra...)
}
return []capture.Session{
{
Name: "banner-and-quit",
About: "usql prints its banner and a prompt, and \\q leaves",
Args: args(memory),
Steps: []capture.Step{
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "query",
About: "a query prints a result table and a row count",
Args: args(memory),
Steps: []capture.Step{
{Send: `select 1 as n, 'two' as s;` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "multiline-continuation",
About: "an unterminated statement prompts for more input",
Args: args(memory),
Steps: []capture.Step{
{Send: `select` + capture.KeyEnter},
{Send: ` 1 as n` + capture.KeyEnter},
{Send: `;` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "variables",
About: `\set defines a variable, :name interpolates it, \unset removes it`,
Args: args(memory),
Steps: []capture.Step{
{Send: `\set n 42` + capture.KeyEnter},
{Send: `\set` + capture.KeyEnter},
{Send: `select :n as answer;` + capture.KeyEnter},
{Send: `\unset n` + capture.KeyEnter},
{Send: `\set` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "conditionals",
About: `\if runs one branch, a branch that does not run ignores its statements, and %R in the prompt is @ there`,
Args: args(memory),
Steps: []capture.Step{
{Send: `\set x on` + capture.KeyEnter},
{Send: `\if :x` + capture.KeyEnter},
{Send: `\echo yes` + capture.KeyEnter},
{Send: `\else` + capture.KeyEnter},
{Send: `select 1;` + capture.KeyEnter},
{Send: `\endif` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "settings",
About: "\\uset shows the settings of usql as \\pset does, sets one, and \\set refuses a setting",
Args: args(memory),
Steps: []capture.Step{
{Send: `\uset` + capture.KeyEnter},
{Send: `\uset pager "less -R"` + capture.KeyEnter},
{Send: `\uset on_error_stop on` + capture.KeyEnter},
{Send: `\uset on_error_stop` + capture.KeyEnter},
{Send: `\set pager x` + capture.KeyEnter},
{Send: `\uset NOPE` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "highlighting",
About: "the line editor draws the input in the colors of the style, and editor.color turns the colors off",
Args: args(memory),
Steps: []capture.Step{
{Send: `select 1 as n, 'two' as s; ` + capture.KeyEnter},
{Send: `\config editor.color off` + capture.KeyEnter},
{Send: `select 1 as n; ` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "editor",
About: "a line of editor.bindings that rline cannot read is a warning, and \\config editor.bindings binds a key at once",
Args: args(memory, `--option=editor.bindings=set editing-mode nothing`),
Steps: []capture.Step{
{Send: `\config editor.bindings '"\\C-o": "select 42 as bound;"'` + capture.KeyEnter},
{Send: "\x0f" + capture.KeyEnter},
{Send: `\config editor.bindings` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "trailing-backslash",
About: "a \\ at the end of a line is an invalid command, as in psql, and the statement stays in the buffer",
Args: args(memory),
Steps: []capture.Step{
{Send: `select 1 as a \` + capture.KeyEnter},
{Send: `;` + capture.KeyEnter},
{Send: `select 2 as b \\` + capture.KeyEnter},
{Send: `;` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "history",
About: "\\config changes a key of history in a session, and refuses a value that is not a number",
Args: args(memory),
Steps: []capture.Step{
{Send: `select 1 as a;` + capture.KeyEnter},
{Send: `\config history.control ignorespace` + capture.KeyEnter},
{Send: ` select 2 as b;` + capture.KeyEnter},
// Up shows the \config line, because the line with a space
// before it stays out of the history
{Send: capture.KeyUp},
{Send: capture.CtrlU},
{Send: `\config history.size nope` + capture.KeyEnter},
{Send: `\config history.size` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "quoted-interpolation",
About: `:'name' quotes as a literal and :"name" quotes as an identifier`,
Args: args(memory),
Steps: []capture.Step{
{Send: `create table t (a int);` + capture.KeyEnter},
{Send: `\set tbl t` + capture.KeyEnter},
{Send: `\set word hello` + capture.KeyEnter},
{Send: `select :'word' as literal;` + capture.KeyEnter},
{Send: `select count(*) as n from :"tbl";` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "error-recovery",
About: "a failed statement reports an error and the prompt returns",
Args: args(memory),
Steps: []capture.Step{
{Send: `select * from nonexistent;` + capture.KeyEnter},
{Send: `select 1 as still_works;` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "describe",
About: `\d lists relations and \d name describes one`,
Args: args(memory),
Steps: []capture.Step{
{Send: `create table films (id int primary key, title text);` + capture.KeyEnter},
{Send: `\dt` + capture.KeyEnter},
{Send: `\d films` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "line-editing",
About: "the line editor handles backspace and start of line, and Enter runs a finished entry wherever the cursor is",
Args: args(memory),
Steps: []capture.Step{
{Send: `select 11 as nn;`},
{Send: capture.KeyBackspace + capture.KeyBackspace},
{Send: `x;`},
{Send: capture.CtrlA},
{Send: `select 2 as n; `},
{Send: capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
{
Name: "no-connection",
About: "usql starts without a database and reports the lack of one",
Args: args(),
Steps: []capture.Step{
{Send: `select 1;` + capture.KeyEnter},
{Send: `\q` + capture.KeyEnter},
},
},
}
}
func TestCLI(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("recording a terminal session needs a pseudo-terminal, which %s does not provide", runtime.GOOS)
}
for _, s := range sessions() {
t.Run(s.Name, func(t *testing.T) {
tr, err := capture.Record(context.Background(), usqlPath, s)
if err != nil {
t.Fatalf("recording %s: %v", s.Name, err)
}
tr.Apply(scrubbers)
got := tr.Encode()
golden := filepath.Join("testdata", "cli", s.Name+".txt")
if *update {
if err := os.MkdirAll(filepath.Dir(golden), 0o755); err != nil {
t.Fatalf("creating %s: %v", filepath.Dir(golden), err)
}
if err := os.WriteFile(golden, []byte(got), 0o644); err != nil {
t.Fatalf("writing %s: %v", golden, err)
}
return
}
want, err := os.ReadFile(golden)
switch {
case errors.Is(err, os.ErrNotExist):
t.Fatalf("%s does not exist. Record it with: go test -run TestCLI -update .", golden)
case err != nil:
t.Fatalf("reading %s: %v", golden, err)
}
if got != string(want) {
t.Errorf("%s does not match the recording.\nRe-record with: go test -run TestCLI -update .\n%s", golden, firstDifference(string(want), got))
}
})
}
}
// firstDifference describes where two transcripts stop matching, because a
// whole transcript is too long to read in a failure message.
func firstDifference(want, got string) string {
wantLines, gotLines := splitLines(want), splitLines(got)
for i := 0; i < len(wantLines) && i < len(gotLines); i++ {
if wantLines[i] != gotLines[i] {
return fmt.Sprintf("line %d:\n want: %q\n got: %q", i+1, wantLines[i], gotLines[i])
}
}
return fmt.Sprintf("the recordings agree for %d lines, then the lengths differ: want %d lines, got %d",
min(len(wantLines), len(gotLines)), len(wantLines), len(gotLines))
}
// splitLines splits s on newlines without dropping a trailing empty line.
func splitLines(s string) []string {
var out []string
start := 0
for i := 0; i < len(s); i++ {
if s[i] == '\n' {
out = append(out, s[start:i])
start = i + 1
}
}
return append(out, s[start:])
}
// TestCLIRecordsSomething checks that the harness itself works, so that an
// empty recording cannot pass every golden comparison quietly.
func TestCLIRecordsSomething(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skipf("recording a terminal session needs a pseudo-terminal, which %s does not provide", runtime.GOOS)
}
tr, err := capture.Record(context.Background(), usqlPath, capture.Session{
Name: "smoke",
Args: []string{"--pset=pager=off", "sqlite3://:memory:"},
Steps: []capture.Step{{Send: `\q` + capture.KeyEnter, Wait: 2 * time.Second}},
})
if err != nil {
t.Fatalf("recording: %v", err)
}
if len(tr.Bytes()) == 0 {
t.Fatal("the recording is empty, so every golden comparison would be meaningless")
}
}