-
Notifications
You must be signed in to change notification settings - Fork 14
/
helper_test.go
181 lines (175 loc) · 5.33 KB
/
helper_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
package main
import (
"reflect"
"testing"
)
func Test_resolveInputs(t *testing.T) {
cases := []struct {
github GitHub
inputs *Inputs
hasError bool
tags []string
repository string
}{
{
github: GitHub{
Repository: "username/repo",
Commit: "45ba489c4f97b5f854ebaba6454b51fa",
Ref: "refs/heads/master",
},
inputs: &Inputs{
Tags: []string{"my_tag"},
},
hasError: false,
tags: []string{"username/repo:my_tag"},
repository: "username/repo",
},
{
github: GitHub{
Repository: "username/repo",
Commit: "45ba489c4f97b5f854ebaba6454b51fa",
Ref: "refs/heads/master",
},
inputs: &Inputs{
Repository: "someone/repo",
Tags: []string{"my_tag"},
},
hasError: false,
tags: []string{"someone/repo:my_tag"},
repository: "someone/repo",
},
{
github: GitHub{
Repository: "username/repo",
Commit: "45ba489c4f97b5f854ebaba6454b51fa",
Ref: "refs/heads/master",
},
inputs: &Inputs{
Registry: "other.docker.io",
Tags: []string{"my_tag"},
},
hasError: false,
tags: []string{"other.docker.io/username/repo:my_tag"},
repository: "other.docker.io/username/repo",
},
{
github: GitHub{
Repository: "username/repo",
Commit: "45ba489c4f97b5f854ebaba6454b51fa",
Ref: "refs/heads/master",
},
inputs: &Inputs{
Registry: "other.docker.io",
Repository: "other.docker.io/my/repo",
Tags: []string{"my_tag"},
},
hasError: false,
tags: []string{"other.docker.io/my/repo:my_tag"},
repository: "other.docker.io/my/repo",
},
{
github: GitHub{
Repository: "username/repo",
Commit: "45ba489c4f97b5f854ebaba6454b51fa",
Ref: "refs/pull/master",
},
inputs: &Inputs{
Tags: []string{},
AllowPullRequest: false,
},
hasError: true,
tags: []string{},
repository: "username/repo",
},
}
for _, c := range cases {
err := resolveInputs(c.github, c.inputs)
if c.repository != c.inputs.Repository {
t.Errorf("expect repository is %s, actual is %s", c.repository, c.inputs.Repository)
}
if c.hasError && err == nil {
t.Errorf("expect error but it never occurs")
}
if !c.hasError && err != nil {
t.Errorf("unexpected error occurs: %v", err)
}
if !reflect.DeepEqual(c.tags, c.inputs.Tags) {
t.Errorf("expect tag list is %v, actual is %s", c.tags, c.inputs.Tags)
}
}
}
func Test_resolveRef(t *testing.T) {
cases := []struct {
ref string
expectType string
expectName string
}{
{"refs/heads/master", RefTypeBranch, "master"},
{"refs/heads/20190927/tests", RefTypeBranch, "20190927/tests"},
{"refs/tags/v1.0.0", RefTypeTag, "v1.0.0"},
{"refs/tags/v2.0.0-rc1", RefTypeTag, "v2.0.0-rc1"},
{"refs/pull/master", RefTypePull, "master"},
{"refs/pull/2019/09/27/pull", RefTypePull, "2019/09/27/pull"},
{"refs/unknown/master", "", "master"},
{"", "", ""},
}
for _, c := range cases {
typ, name := resolveRef(GitHub{Ref: c.ref})
if typ != c.expectType {
t.Errorf("expect ref type is %s, actual is %s", c.expectType, typ)
}
if name != c.expectName {
t.Errorf("expect ref name is %s, actual is %s", c.expectName, name)
}
}
}
func Test_resolveAutoTag(t *testing.T) {
cases := []struct {
typ string
name string
inputs *Inputs
expects []string
}{
{RefTypeBranch, "master", &Inputs{AutoTag: false, Tags: []string{}}, []string{}},
{RefTypeBranch, "master", &Inputs{AutoTag: false, Tags: []string{"master"}}, []string{"master"}},
{RefTypeBranch, "master", &Inputs{AutoTag: false, Tags: []string{"a", "b", "c"}}, []string{"a", "b", "c"}},
{RefTypeBranch, "master", &Inputs{AutoTag: true, Tags: []string{"master"}}, []string{"latest"}},
{RefTypeBranch, "master", &Inputs{AutoTag: true, Tags: []string{"feature", "develop"}}, []string{"latest"}},
{RefTypeBranch, "master", &Inputs{AutoTag: true, Tags: []string{}}, []string{"latest"}},
{RefTypeBranch, "develop", &Inputs{AutoTag: true, Tags: []string{}}, []string{"develop"}},
{RefTypeTag, "v1.0.0", &Inputs{AutoTag: true, Tags: []string{}}, []string{"1", "1.0", "1.0.0"}},
{RefTypePull, "master", &Inputs{AutoTag: true, Tags: []string{}}, []string{"pr-master"}},
}
for _, c := range cases {
resolveAutoTag(c.typ, c.name, c.inputs)
if !reflect.DeepEqual(c.expects, c.inputs.Tags) {
t.Errorf("expect tag list is %v, actual is %v", c.expects, c.inputs.Tags)
}
}
}
func Test_resolveSemanticVersionTag(t *testing.T) {
cases := []struct {
input string
expect []string
}{
// invalid semantic version
{"1", []string{"1"}},
{"master", []string{"master"}},
{"20190921-actions", []string{"20190921-actions"}},
// valid semantic version
{"0.0.0", []string{"0", "0.0", "0.0.0"}},
{"1.0.0", []string{"1", "1.0", "1.0.0"}},
{"2.7.10", []string{"2", "2.7", "2.7.10"}},
{"v1.0.0", []string{"1", "1.0", "1.0.0"}},
{"v2.3.6", []string{"2", "2.3", "2.3.6"}},
{"3.0.0-rc1", []string{"3-rc1", "3.0-rc1", "3.0.0-rc1"}},
{"v3.0.0-alpha.1", []string{"3-alpha.1", "3.0-alpha.1", "3.0.0-alpha.1"}},
{"4.0.0-alpha.1.2", []string{"4-alpha.1.2", "4.0-alpha.1.2", "4.0.0-alpha.1.2"}},
}
for _, c := range cases {
actual := resolveSemanticVersionTag(c.input)
if !reflect.DeepEqual(c.expect, actual) {
t.Errorf("expect tag list is %v, actual is %s", c.expect, actual)
}
}
}