-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
construct_test.go
184 lines (165 loc) · 4.61 KB
/
construct_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
package labeler
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
type args struct {
owner string
repo string
event string
id int
data *string
}
tests := []struct {
name string
args args
wantErr bool
expectErrMessage string
}{
{
name: "errors on missing data",
args: args{},
wantErr: true,
expectErrMessage: "a JSON string of event data is required",
},
{
name: "constructs as expected",
args: args{
owner: "jimschubert",
repo: "example",
event: "issue",
id: 1,
data: ptr("{}"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := New(tt.args.owner, tt.args.repo, tt.args.event, tt.args.id, tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err != nil && tt.expectErrMessage != "" {
assert.EqualError(t, err, tt.expectErrMessage, "New() error did not match expectation")
}
if err == nil {
assert.NotNil(t, got.Owner)
assert.NotNil(t, got.Repo)
assert.NotNil(t, got.Event)
assert.NotNil(t, got.ID)
assert.NotNil(t, got.context)
assert.NotNil(t, got.client)
assert.Equal(t, tt.args.owner, *got.Owner)
assert.Equal(t, tt.args.repo, *got.Repo)
assert.Equal(t, tt.args.event, *got.Event)
assert.Equal(t, tt.args.id, *got.ID)
assert.Equal(t, ".github/labeler.yml", got.configPath)
}
})
}
}
func TestNewWithOptions(t *testing.T) {
childContext, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
// so tooling doesn't complain about dropping a cancel func
cancel()
})
type args struct {
opts []OptFn
}
tests := []struct {
name string
args args
validate func(l *Labeler)
errorMatch string
}{
// {
// name: "fails if owner and repo are both empty",
// args: args{},
// errorMatch: "both a github and owner are required",
// },
{
name: "fails if repo is in org/repo format",
args: args{
opts: []OptFn{WithRepo("jimschubert/example")},
},
errorMatch: "a repo must be just the repo name. Separate org/repo style into owner and repo options",
},
// {
// name: "fails if repo defined but owner is not",
// args: args{
// opts: []OptFn{WithRepo("example")},
// },
// errorMatch: "a github owner (user or org) is required",
// },
{
name: "fails if owner defined but repo is not",
args: args{
opts: []OptFn{WithOwner("jimschubert")},
},
errorMatch: "a github repo is required",
},
{
name: "fails if owner,repo defined but id is not",
args: args{
opts: []OptFn{WithOwner("jimschubert"), WithRepo("example")},
},
errorMatch: "the integer id of the issue or pull request is required",
},
{
name: "constructs as expected when provided all required fields",
args: args{
opts: []OptFn{WithOwner("jimschubert"), WithRepo("example"), WithID(1000)},
},
validate: func(l *Labeler) {
assert.Equal(t, ptr("jimschubert"), l.Owner)
assert.Equal(t, ptr("example"), l.Repo)
assert.Equal(t, ptr(1000), l.ID)
assert.NotNil(t, l.context, "Should have created a default context")
assert.NotNil(t, l.client, "Should have created a default github client")
},
},
{
name: "constructs as expected when provided all fields",
args: args{
opts: []OptFn{
// required fields
WithOwner("jimschubert"), WithRepo("example"), WithID(1000),
// optional fields
WithContext(childContext), WithConfigPath(".github/labeler-custom.yml"), WithData("{}"), WithToken("irrelevant"),
},
},
validate: func(l *Labeler) {
assert.Equal(t, ptr("jimschubert"), l.Owner)
assert.Equal(t, ptr("example"), l.Repo)
assert.Equal(t, ptr("{}"), l.Data)
assert.Equal(t, ptr(1000), l.ID)
assert.NotNil(t, l.context, "Should have created a default context")
assert.NotNil(t, l.client, "Should have created a default github client")
assert.Equal(t, ".github/labeler-custom.yml", l.configPath)
assert.Equal(t, &childContext, l.context)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewWithOptions(tt.args.opts...)
wantError := tt.errorMatch != ""
if wantError {
assert.EqualError(t, err, tt.errorMatch, fmt.Sprintf("NewWithOptions(%v)", tt.args.opts))
return
}
if err != nil && tt.errorMatch != "" {
t.Errorf("New() error = %v, no error expectation was defined", err)
return
}
if tt.validate != nil {
tt.validate(got)
}
})
}
}