/
cli_test.go
69 lines (66 loc) · 1.53 KB
/
cli_test.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
package main
import (
"os"
"strings"
"testing"
)
func Test_run(t *testing.T) {
t.Parallel()
cli := &cli{}
t.Run("invalid arg case", func(t *testing.T) {
testCases := []struct {
name string
argStr string
}{
{
name: "no args",
argStr: "",
},
{
name: "no option",
argStr: "schema.sql",
},
}
for _, tc := range testCases {
args := strings.Split(tc.argStr, " ")
if got, want := cli.run(args), exitCodeArgsError; got != want {
t.Fatalf("%s exits %d, want %d\n", tc.name, got, want)
}
}
})
t.Run("error case", func(t *testing.T) {
testCases := []struct {
name string
argStr string
}{
{
name: "file does not exist",
argStr: "-s not_exist.sql",
},
}
for _, tc := range testCases {
args := strings.Split(tc.argStr, " ")
if got, want := cli.run(args), exitCodeError; got != want {
t.Fatalf("%s exits %d, want %d\n", tc.name, got, want)
}
}
})
t.Run("help case", func(t *testing.T) {
if got, want := cli.run([]string{"-h"}), exitCodeOK; got != want {
t.Fatalf("help case exits %d, want %d\n", got, want)
}
})
t.Run("valid case", func(t *testing.T) {
output := "testdata/sample.png"
if got, want := cli.run([]string{"-s", "testdata/sample.sql", "-o", output}), exitCodeOK; got != want {
t.Fatalf("valid case exits %d, want %d\n", got, want)
}
_, err := os.Stat(output)
if err != nil {
t.Fatalf("cant create output file. %s", err)
}
if err := os.Remove(output); err != nil {
t.Fatalf("remove test file failed. %s", err)
}
})
}