-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
136 lines (122 loc) · 3.45 KB
/
config_test.go
File metadata and controls
136 lines (122 loc) · 3.45 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
package main
import (
"bytes"
"reflect"
"testing"
)
var repositoryConfig1 = RepositoryConfig{
URL: "https://bitbucket.org/user/repo1",
Branch: "master",
}
var repositoryConfig2 = RepositoryConfig{
URL: "https://bitbucket.org/user/repo2",
}
type mockNotification struct {
repositoryURL string
branches []string
}
func (n mockNotification) RepositoryURL() string {
return n.repositoryURL
}
func (n mockNotification) Branches() map[string]bool {
branchMap := make(map[string]bool)
for _, branch := range n.branches {
branchMap[branch] = true
}
return branchMap
}
type findRepositoryConfigTest struct {
config Config
notification Notification
expectMatch bool
expected RepositoryConfig
}
var config1 = Config{Repositories: []RepositoryConfig{repositoryConfig1, repositoryConfig2}}
var findRepositoryConfigTests = []findRepositoryConfigTest{
{config1, mockNotification{}, false, RepositoryConfig{}},
{config1, mockNotification{"https://bitbucket.org/user/repo1", []string{}}, false, RepositoryConfig{}},
{config1, mockNotification{"https://bitbucket.org/user/repo1", []string{"dev"}}, false, RepositoryConfig{}},
{config1, mockNotification{"https://bitbucket.org/user/repo1", []string{"dev", "master"}}, true, repositoryConfig1},
{config1, mockNotification{"https://bitbucket.org/user/repo2", []string{}}, true, repositoryConfig2},
{config1, mockNotification{"https://bitbucket.org/user/repo2", []string{"dev", "master"}}, true, repositoryConfig2},
}
func TestFindRepositoryConfig(t *testing.T) {
for i, test := range findRepositoryConfigTests {
actual, found := test.config.FindRepositoryConfig("", test.notification)
if found != test.expectMatch {
t.Errorf("%d. got %#v, want %#v", i, found, test.expectMatch)
}
if actual.Branch != test.expected.Branch {
t.Errorf("%d. got %#v, want %#v", i, actual.Branch, test.expected.Branch)
}
if actual.Name != test.expected.Name {
t.Errorf("%d. got %#v, want %#v", i, actual.Name, test.expected.Name)
}
if actual.Dir != test.expected.Dir {
t.Errorf("%d. got %#v, want %#v", i, actual.Dir, test.expected.Dir)
}
if actual.URL != test.expected.URL {
t.Errorf("%d. got %#v, want %#v", i, actual.URL, test.expected.URL)
}
if !reflect.DeepEqual(actual.Command, test.expected.Command) {
t.Errorf("%d. got %#v, want %#v", i, actual.Command, test.expected.Command)
}
}
}
type appendConfigTest struct {
input string
expected Config
}
var appendConfigTests = []appendConfigTest{
{`{
"Addr": ":8081",
"Secret": "t0ps3cr3t",
"Repositories": [
{
"URL": "https://bitbucket.org/srt/foo",
"Command": [
"git pull"
],
"Dir": "/var/www/foo"
},
{
"Name": "bar",
"Command": [
"git pull"
],
"Dir": "/var/www/bar"
}
]
}`, Config{
Addr: ":8081",
Secret: "t0ps3cr3t",
Repositories: []RepositoryConfig{
RepositoryConfig{
URL: "https://bitbucket.org/srt/foo",
Command: []string{
"git pull",
},
Dir: "/var/www/foo",
},
RepositoryConfig{
Name: "bar",
Command: []string{
"git pull",
},
Dir: "/var/www/bar",
},
}},
}}
func TestAppendConfig(t *testing.T) {
actual := Config{}
actual.Addr = ":8080"
for _, test := range appendConfigTests {
err := appendConfig(&actual, bytes.NewReader([]byte(test.input)))
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(actual, test.expected) {
t.Errorf("got %#v, want %#v", actual, test.expected)
}
}
}