generated from bitfield/habit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
204 lines (177 loc) · 5.18 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package habit_test
import (
"bytes"
"fmt"
"github.com/crmejia/habit"
"github.com/phayes/freeport"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
)
func TestRunCLIShowsUsageHelpNoArgs(t *testing.T) {
t.Parallel()
buffer := bytes.Buffer{}
habit.RunCLI([]string{}, &buffer)
got := buffer.String()
if !strings.Contains(got, "Usage") {
t.Errorf("no arguments and no previous habits should print usage Message got:\n %s", got)
}
}
func TestRunCLIAllShowsCorrectlyOnChangedDir(t *testing.T) {
t.Parallel()
args := []string{"-d", "testdata/", "-s", "file", "all"}
buffer := bytes.Buffer{}
habit.RunCLI(args, &buffer)
want := "Habits:"
got := buffer.String()
if !strings.Contains(got, want) {
// if habits exist in store, a summary of all habits should be displayed
t.Errorf("habit all should print a summary of all habits got: \n %s", got)
}
}
func TestRunCLIShowsUsageHelpMoreThanOneArg(t *testing.T) {
t.Parallel()
args := []string{"blah", "blah"}
buffer := bytes.Buffer{}
habit.RunCLI(args, &buffer)
want := "Usage"
got := buffer.String()
if !strings.Contains(got, want) {
t.Errorf("too many arguments should print usage Message got: %s", got)
}
}
func TestRunCLIShowsUsageHelpOptionsButNoArgs(t *testing.T) {
t.Parallel()
args := []string{"-f", "daily"}
buffer := bytes.Buffer{}
want := "Usage"
habit.RunCLI(args, &buffer)
got := buffer.String()
if !strings.Contains(got, want) {
t.Errorf("only options and no arguments should print usage Message got: %s", got)
}
}
func TestRunCLIShowsErrorUsageHelpWrongOptions(t *testing.T) {
t.Parallel()
args := []string{"-g", "gibberish"}
buffer := bytes.Buffer{}
habit.RunCLI(args, &buffer)
got := buffer.String()
if !strings.Contains(got, "Usage") {
t.Errorf("only options and no arguments should print usage Message got: %s", got)
}
}
func TestRunClIShowNewHabitMessageNewHabit(t *testing.T) {
t.Parallel()
buffer := bytes.Buffer{}
tmpDir := t.TempDir()
args := []string{"-d", tmpDir, "piano"}
habit.RunCLI(args, &buffer)
want := "Good luck with your new habit"
got := buffer.String()
if !strings.Contains(got, want) {
t.Errorf("new habit should print streak message. Got:\n %s", got)
}
}
func TestRunCLIShowsErrorUsageHelpInvalidFrequency(t *testing.T) {
t.Parallel()
args := []string{"-f", "yellow", "piano"}
buffer := bytes.Buffer{}
habit.RunCLI(args, &buffer)
got := buffer.String()
if !strings.Contains(got, "unknown frequency:") {
t.Errorf("Invalid frecuency should print error message got: %s", got)
}
if !strings.Contains(got, "Usage") {
t.Errorf("Invalid frecuency should print usage Message got: %s", got)
}
}
func TestRunCLIShowsErrorUsageHelpEmptyFrequency(t *testing.T) {
t.Parallel()
args := []string{"-f", "", "piano"}
buffer := bytes.Buffer{}
habit.RunCLI(args, &buffer)
got := buffer.String()
if !strings.Contains(got, "habit frequency cannot be empty") {
t.Errorf("Empty frecuency should print error message got: %s", got)
}
if !strings.Contains(got, "Usage") {
t.Errorf("Empty frecuency should print usage Message got: %s", got)
}
}
func TestRunCLIShowsErrorUsageHelpInvalidStoreType(t *testing.T) {
t.Parallel()
args := []string{"-s", "cloud", "piano"}
buffer := bytes.Buffer{}
habit.RunCLI(args, &buffer)
got := buffer.String()
if !strings.Contains(got, "unknown store type") {
t.Errorf("Invalid frecuency should print error message got: %s", got)
}
if !strings.Contains(got, "Usage") {
t.Errorf("Invalid frecuency should print usage Message got: %s", got)
}
}
func TestRunServerShowsErrorOnWrongArgs(t *testing.T) {
t.Parallel()
const noAddressError = "no address provided"
const tooManyArgsError = "too many args provided"
testCases := []struct {
name string
args []string
want string
}{
{name: "nil args", args: nil, want: noAddressError},
{name: "empty args", args: []string{}, want: noAddressError},
{name: "too many args", args: []string{"blah", "blah"}, want: tooManyArgsError},
}
for _, tc := range testCases {
got := bytes.Buffer{}
habit.RunServer(tc.args, &got)
if !strings.Contains(got.String(), tc.want) {
t.Errorf("%s should fail with %s, got: %s", tc.name, noAddressError, got.String())
}
}
}
func TestRunServerStartsServer(t *testing.T) {
t.Parallel()
freePort, err := freeport.GetFreePort()
if err != nil {
t.Fatal(err)
}
address := fmt.Sprintf("%s:%d", localHostAddress, freePort)
args := []string{address}
output := bytes.Buffer{}
go habit.RunServer(args, &output)
address = "http://" + address + "?habit=piano"
resp, err := retryHttpGet(address)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Want Status %d, got: %d", http.StatusNotFound, resp.StatusCode)
}
want := "piano"
got, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Not able to parse response")
}
if !strings.Contains(string(got), want) {
t.Errorf("want response body to be:\n %s \ngot:\n %s", want, got)
}
}
func retryHttpGet(address string) (*http.Response, error) {
resp, err := http.Get(address)
for err != nil {
switch {
case strings.Contains(err.Error(), "connection refused"):
time.Sleep(5 * time.Millisecond)
resp, err = http.Get(address)
default:
return resp, err
}
}
return resp, nil
}