Repository navigation
Expand file tree
/
Copy pathdump_cli_test.go
More file actions
166 lines (161 loc) · 6.91 KB
/
Copy pathdump_cli_test.go
File metadata and controls
166 lines (161 loc) · 6.91 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
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// TestDumpCLI runs usql --dump as a script runs it: it dumps a SQLite
// database to a file with -o, loads the file into a new database with -f,
// and dumps the copy to standard output. The two dumps are the same text.
func TestDumpCLI(t *testing.T) {
if usqlPath == "" {
t.Skip("TestMain builds usql on Linux and macOS only")
}
dir := t.TempDir()
src, dst := "sqlite3:"+filepath.Join(dir, "src.db"), "sqlite3:"+filepath.Join(dir, "dst.db")
fixture, err := filepath.Abs(filepath.Join("drivers", "testdata", "dump", "sqlite3.sql"))
if err != nil {
t.Fatal(err)
}
usql := func(t *testing.T, args ...string) (string, string, error) {
t.Helper()
cmd := exec.Command(usqlPath, args...)
// no configuration, history or variable of the user applies
cmd.Env = []string{"HOME=" + dir, "PATH=" + os.Getenv("PATH"), "XDG_CONFIG_HOME=" + filepath.Join(dir, "config")}
cmd.Dir = dir
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
cmd.Stdout, cmd.Stderr = stdout, stderr
err := cmd.Run()
return stdout.String(), stderr.String(), err
}
if _, stderr, err := usql(t, "-X", "-q", src, "-f", fixture); err != nil {
t.Fatalf("loading the fixture: %v\n%s", err, stderr)
}
out := filepath.Join(dir, "dump.sql")
if _, stderr, err := usql(t, src, "--dump", "--restrict-key", "k", "-o", out); err != nil {
t.Fatalf("usql --dump -o: %v\n%s", err, stderr)
}
want, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
for _, line := range []string{"-- usql dump", `\uset on_error_stop on`, `\restrict k`, `\unrestrict k`, `CREATE TABLE authors`, `INSERT INTO "books"`} {
if !strings.Contains(string(want), line+"\n") && !strings.Contains(string(want), line+" ") {
t.Errorf("the dump has no line %q", line)
}
}
if _, stderr, err := usql(t, "-X", "-q", dst, "-f", out); err != nil {
t.Fatalf("loading the dump: %v\n%s", err, stderr)
}
got, stderr, err := usql(t, dst, "-D", "--restrict-key", "k")
if err != nil {
t.Fatalf("usql -D: %v\n%s", err, stderr)
}
if got != string(want) {
t.Errorf("the dump of the copy differs from the dump of the source:\n--- source\n%s\n--- copy\n%s", want, got)
}
// the options of a dump
if got, _, err := usql(t, src, "--dump", "--schema-only", "--restrict-key", "k"); err != nil || strings.Contains(got, "INSERT INTO") {
t.Errorf("--schema-only returned %v, and wrote rows: %t", err, strings.Contains(got, "INSERT INTO"))
}
if got, _, err := usql(t, src, "--dump", "--data-only", "--restrict-key", "k"); err != nil || strings.Contains(got, "CREATE TABLE") {
t.Errorf("--data-only returned %v, and wrote tables: %t", err, strings.Contains(got, "CREATE TABLE"))
}
if got, _, err := usql(t, src, "--dump", "--table", "books", "--restrict-key", "k"); err != nil || strings.Contains(got, `"authors"`) || !strings.Contains(got, `"books"`) {
t.Errorf("--table books returned %v, and wrote:\n%s", err, got)
}
// \dump takes the options of --dump, as -NAME or --NAME, and writes the
// same script to the output, to a file and to a command
cli, _, err := usql(t, src, "--dump", "--table", "books", "--restrict-key", "k")
if err != nil {
t.Fatal(err)
}
metaFile, pipeFile := filepath.Join(dir, "meta.sql"), filepath.Join(dir, "pipe.sql")
for _, cmd := range []string{
`\dump -table books -restrict-key k`,
`\dump --table books --restrict-key k -`,
`\dump -table books -restrict-key k ` + metaFile,
`\dump -table books -restrict-key k |cat > ` + pipeFile,
} {
stdout, stderr, err := usql(t, "-X", src, "-c", cmd)
if err != nil {
t.Fatalf("%s: %v\n%s", cmd, err, stderr)
}
got := stdout
switch {
case strings.Contains(cmd, metaFile):
got = readFile(t, metaFile)
case strings.Contains(cmd, pipeFile):
got = readFile(t, pipeFile)
}
if got != cli {
t.Errorf("%s wrote a script that differs from --dump:\n--- --dump\n%s\n--- \\dump\n%s", cmd, cli, got)
}
}
if fi, err := os.Stat(metaFile); err != nil || fi.Mode().Perm() != 0o600 {
t.Errorf("the file of \\dump has mode %v, %v, want 0600", fi.Mode().Perm(), err)
}
// the schema as dbmeta's snapshot, in json and yaml, the same from
// --dump and from \dump, and with no rows
for _, format := range []string{"json", "yaml"} {
cli, stderr, err := usql(t, src, "--dump", "--format", format)
if err != nil {
t.Fatalf("--format %s: %v\n%s", format, err, stderr)
}
for _, s := range []string{"version", "authors", "books"} {
if !strings.Contains(cli, s) {
t.Errorf("--format %s does not name %s:\n%s", format, s, cli)
}
}
if strings.Contains(cli, "INSERT INTO") || strings.Contains(cli, `\restrict`) {
t.Errorf("--format %s wrote a script:\n%s", format, cli)
}
if got, stderr, err := usql(t, "-X", src, "-c", `\dump -format `+format); err != nil || got != cli {
t.Errorf("\\dump -format %s returned %v, %s, and wrote another snapshot than --dump:\n--- --dump\n%s\n--- \\dump\n%s", format, err, stderr, cli, got)
}
}
for cmd, want := range map[string]string{
`\dump -format bogus`: "unknown format: bogus (use sql, json or yaml)",
`\dump -format json -data-only`: "data-only cannot be used with format json or yaml",
`\dump -bogus`: `invalid option "bogus"`,
`\dump -table`: "-table needs a value: PATTERN",
`\dump -schema-only -data-only ` + out: "schema-only and data-only cannot be used together",
} {
if _, stderr, _ := usql(t, "-X", src, "-c", cmd); !strings.Contains(stderr, want) {
t.Errorf("%s wrote %q, want an error with %q", cmd, stderr, want)
}
}
// what usql refuses, and a failed dump leaves the file as it was
for _, test := range []struct {
args []string
want string
}{
{[]string{src, "--schema-only"}, "the options of a dump need --dump"},
{[]string{src, "--dump", "-c", "select 1"}, "--dump cannot be used with -c or -f"},
{[]string{src, "--dump", "-1"}, "--dump cannot be used with --single-transaction"},
{[]string{src, "--dump", "--schema-only", "--data-only", "-o", out}, "schema-only and data-only cannot be used together"},
{[]string{src, "--dump", "--target", "nosuchdb", "-o", out}, "unknown target: nosuchdb"},
{[]string{src, "--dump", "--format", "bogus", "-o", out}, "unknown format: bogus"},
{[]string{src, "--dump", "--format", "yaml", "--restrict-key", "k", "-o", out}, "restrict-key cannot be used with format json or yaml"},
} {
_, stderr, err := usql(t, test.args...)
if err == nil || !strings.Contains(stderr, test.want) {
t.Errorf("usql %s returned %v and wrote %q, want an error with %q", strings.Join(test.args[1:], " "), err, stderr, test.want)
}
}
if b, err := os.ReadFile(out); err != nil || !bytes.Equal(b, want) {
t.Errorf("a failed dump changed the file of -o")
}
entries, err := os.ReadDir(dir)
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
if strings.HasPrefix(e.Name(), ".dump.sql.") {
t.Errorf("a failed dump left the temporary file %s", e.Name())
}
}
}