-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_test.go
More file actions
145 lines (139 loc) · 3.1 KB
/
Copy pathrun_test.go
File metadata and controls
145 lines (139 loc) · 3.1 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
package main
import (
"bytes"
"math/rand"
"os"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/kberov/rowx/rx"
)
//nolint:gosec // G404
var tempDBFile = os.TempDir() + `/rowx_test` + strconv.Itoa(rand.Intn(999)) + `.sqlite`
var cases = []struct {
args []string
code int
output string
setup func(t *testing.T)
}{
{
args: []string{},
code: 0,
output: "\nUSAGE:",
},
{
args: []string{`help`},
code: 0,
output: "\nUSAGE:",
},
{
args: []string{`-help`},
code: 0,
output: "\nUSAGE:",
},
{
args: []string{`migrate`},
code: 1,
output: "All flags beside",
},
{
args: []string{`migrate`, `help`},
code: 1,
output: "All flags beside",
},
{
args: []string{`migrate`, `-what`},
code: 1,
output: "flag provided but not defined: -what",
},
{
args: []string{`migrate`, `-log_level`, `UNKNOWN`},
code: 1,
output: "No such log_level: UNKNOWN.\n",
},
{
args: []string{`migrate`, `-sql_file`, `rx/testdata/migrations_01.sql`,
`-dsn`, tempDBFile, `-direction`, `left`},
code: 2,
output: "direction can be only",
},
{
args: []string{`migrate`, `-sql_file`, `rx/testdata/migrations_01.sql`,
`-dsn`, tempDBFile, `-direction`, `up`},
code: 0,
output: "Applying 201804092200 up",
},
{
args: []string{`generate`},
code: 1,
output: "are mandatory!\n",
},
{
args: []string{`generate`, `help`},
code: 1,
output: "are mandatory!\n",
},
{
args: []string{`generate`, `-what`},
code: 1,
output: "flag provided but not defined: -what\n generate",
},
{
args: []string{`generate`, `-log_level`, `UNKNOWN`},
code: 1,
output: "No such log_level: UNKNOWN.\n",
},
{
args: []string{`generate`, `-dsn`, tempDBFile, `-package`, `rx/` + os.Getenv("EXAMPLE_MODEL")},
code: 0,
output: "_tables.go...",
setup: func(t *testing.T) {
err := os.MkdirAll(`rx/`+os.Getenv("EXAMPLE_MODEL"), 0750)
require.NoErrorf(t, err, `Unexpected error: %+v`, err)
},
},
{
args: []string{`generate`, `-dsn`, tempDBFile, `-package`, os.Getenv("EXAMPLE_MODEL")},
code: 2,
output: "The directory must exist already!",
setup: func(t *testing.T) {
err := os.RemoveAll(packagePath)
require.NoErrorf(t, err, `Unexpected error: %+v`, err)
},
},
{
args: []string{`alabalanica`},
code: 1,
output: "\nUknown action ",
},
}
func TestRun(t *testing.T) {
osArgs := os.Args
output = bytes.NewBufferString("")
osStderr := os.Stderr
defer func() {
os.Stderr = osStderr
os.Args = osArgs
}()
for _, tc := range cases {
// reset os.Args
os.Args = []string{`./rowx`}
// Reset flags.
_init()
name := strings.Join(tc.args, `_`)
output.(*bytes.Buffer).Reset()
os.Args = append(os.Args, tc.args...)
rx.Logger.SetOutput(output)
if tc.setup != nil {
tc.setup(t)
}
t.Run(name, func(t *testing.T) {
code := run()
require.Equalf(t, tc.code, code,
`Expected exit code was %d, but we got %d.`, tc.code, code)
require.Containsf(t, output.(*bytes.Buffer).String(), tc.output,
`Expected output to contain [%s], but it is: [%s]`, tc.output, output)
})
}
}