-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
70 lines (60 loc) · 1.9 KB
/
Copy pathcli_test.go
File metadata and controls
70 lines (60 loc) · 1.9 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
package cli_test
import (
"os"
"testing"
"time"
"github.com/alecthomas/kong"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/block/pg-sprite/internal/cli"
)
func newKong(t *testing.T, c *cli.CLI) *kong.Kong {
t.Helper()
clearFlagEnv(t)
k, err := kong.New(c, kong.Vars{"version": "test"})
require.NoError(t, err, "the command grammar must construct — a bad tag fails here, not in production")
return k
}
// clearFlagEnv isolates parse tests from the caller's shell: flags bound to
// PGSPRITE_* environment variables would otherwise resolve from whatever the
// developer last exported, making required-flag and default-value assertions
// depend on the environment.
func clearFlagEnv(t *testing.T) {
t.Helper()
for _, key := range []string{"PGSPRITE_URL", "PGSPRITE_CA_CERT"} {
t.Setenv(key, "")
require.NoError(t, os.Unsetenv(key))
}
}
func TestGrammarIsValid(t *testing.T) {
newKong(t, cli.New())
}
func TestMigrateFlagsWireIntoDBConfig(t *testing.T) {
c := cli.New()
k := newKong(t, c)
_, err := k.Parse([]string{
"migrate",
"--url", "postgres://user@localhost:5432/app",
"--alter", "ALTER TABLE t ADD COLUMN c int",
"--lock-timeout", "5s",
})
require.NoError(t, err)
cfg := c.Migrate.Config()
assert.Equal(t, "postgres://user@localhost:5432/app", cfg.URL)
assert.Equal(t, 5*time.Second, cfg.LockTimeout)
assert.Equal(t, 30*time.Second, cfg.StatementTimeout, "statement_timeout keeps its default")
assert.Empty(t, cfg.CACertPath)
}
func TestURLIsRequiredForDatabaseCommands(t *testing.T) {
c := cli.New()
k := newKong(t, c)
_, err := k.Parse([]string{"migrate", "--alter", "ALTER TABLE t ADD COLUMN c int"})
require.Error(t, err)
assert.Contains(t, err.Error(), "--url")
}
func TestFmtIsOffline(t *testing.T) {
c := cli.New()
k := newKong(t, c)
_, err := k.Parse([]string{"fmt"})
require.NoError(t, err, "fmt must not require database flags")
}