This repository was archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
125 lines (102 loc) · 2.24 KB
/
Copy pathmain_test.go
File metadata and controls
125 lines (102 loc) · 2.24 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
package main
import (
"bytes"
"io/ioutil"
"os"
"testing"
)
func init() {
exit = panicExit
stderr = ioutil.Discard
}
func panicExit(code int) {
panic(code)
}
func expectExit(t *testing.T, code int) {
if code < 0 {
got := recover()
if got != nil {
t.Fatalf("exit unexpectedly called with value %v", got)
}
return
}
got := recover()
if got != code {
t.Fatalf("exited with wrong error %d, expected %d", got, code)
}
}
func TestBrokenFlag(t *testing.T) {
_, err := parseArguments([]string{"./test", "-broken"})
if err == nil {
t.Errorf("parseArguments() did not err on broken arguments")
}
}
func TestParseArguments(t *testing.T) {
cases := []struct {
in []string
expected string
}{
{[]string{"./test", "-yes", "path1"}, "path1"},
{[]string{"./test", "path2"}, "path2"},
{[]string{"./test", "path3", "-yes"}, "path3"},
}
for i, c := range cases {
result, err := parseArguments(c.in)
if err != nil {
t.Errorf("%d: %s", i, err.Error())
}
if result != c.expected {
t.Errorf("%d: parseArguments() did not return expected path for %+v. Got %s, expected %s", i, c.in, result, c.expected)
}
}
}
func TestMissingKey(t *testing.T) {
defer expectExit(t, 1)
apiKey = ""
apiEmail = ""
os.Args = []string{"./test", "zone"}
main()
}
func TestMissingArgument(t *testing.T) {
_, err := parseArguments([]string{"./test"})
if err == nil {
t.Errorf("parseArguments() did not err on missing argument")
}
}
func TestNonexisting(t *testing.T) {
defer expectExit(t, 1)
apiKey = "nonempty"
apiEmail = "nonempty"
os.Args = []string{"./test", "/non/existing/zone"}
main()
}
func TestBrokenZone(t *testing.T) {
defer expectExit(t, 1)
apiKey = "nonempty"
apiEmail = "nonempty"
os.Args = []string{"./test", "/dev/null"}
main()
}
func TestYesNo(t *testing.T) {
cases := []struct {
line string
expected bool
}{
{"y\n", true},
{"Y\n", true},
{"n\n", false},
{"N\n", false},
{"\n", false},
{"-\n", false},
{"\ny\n", false},
{"", false},
{" ", false},
}
for i, in := range cases {
b := bytes.NewBufferString(in.line)
result := yesNo(b)
if result != in.expected {
t.Errorf("%d: yesNo() returned wrong result for '%s', got %v, expected %v", i, in.line, result, in.expected)
}
}
}