forked from gruntwork-io/fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_test.go
179 lines (150 loc) · 5.22 KB
/
tag_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
package main
import (
"testing"
)
func TestTagToGet(t *testing.T) {
t.Parallel()
// specific - should work regardless of meeting semver format
// specific - includes when val prefixed with =
// if no tag provided - should provide latest
// if constraint can not be met, err
tags := []string{"foo", "v1.0.0", "10.0.20-some-txt", "bar", "10.0.10", "10.1.9", "100.10.1"}
o := fetchOpts{}
cases := []struct {
constraint string
tag string
err bool
}{
{"~>10.0", "10.1.9", false},
{"bar", "bar", false},
{"=bar", "bar", false},
{"", "100.10.1", false},
{"<1.0.0", "", true},
}
for _, tc := range cases {
o.tagConstraint = tc.constraint
if tag, err := o.tagToGet(tags); err != nil {
if tc.err != true {
t.Fatalf("Did not expect the error we received: %s\nconstraint:[%s]", err, tc.constraint)
}
} else {
if tag != tc.tag {
t.Fatalf("Did not expect the tag we received: %s\nconstraint:[%s]", tag, tc.constraint)
}
}
}
}
func TestBestFitTag(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
tags []string
expectedTag string
}{
{"1.0.7", []string{"1.0.7"}, "1.0.7"},
{"~> 1.0.0", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.0.9"},
{"~> 1.0.7", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.0.9"},
{"~> 1.1.0", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.1.0"},
{"~> 1.1.1", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.1.3"},
{"~> 1.2.1", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.2.3"},
{"~> 1.1", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.4.0"},
{"~> 1.2", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.4.0"},
{"~> 1.3", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "1.4.0"},
{">= 1.3", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "2.1.0"},
{"v1.0.7", []string{"v1.0.7"}, "v1.0.7"},
{"v1.0.7", []string{}, ""},
}
for _, tc := range cases {
tag, err := bestFitTag(tc.tagConstraint, tc.tags)
if err != nil {
t.Fatalf("Failed on call to bestFitTag: %s", err)
}
if tag != tc.expectedTag {
t.Fatalf("Given constraint %s and tag list %v, expected %s, but received: %s", tc.tagConstraint, tc.tags, tc.expectedTag, tag)
}
}
}
func TestIsTagConstraintOrExactTag(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
desiredTag string
exact bool
}{
{"1.0.7", "1.0.7", true},
{" 1.0.7 ", "1.0.7", true},
{"=1.0.7", "1.0.7", true},
{"= 1.0.7", "1.0.7", true},
{"~> 1.0.0", "~> 1.0.0", false},
{"> 1.3", "> 1.3", false},
{">= 1.3", ">= 1.3", false},
{"<= 1.3", "<= 1.3", false},
{"< 1.3", "< 1.3", false},
{"v1.0.7", "v1.0.7", true},
{" v1.0.7 ", "v1.0.7", true},
{"=v1.0.7", "v1.0.7", true},
{"= v1.0.7", "v1.0.7", true},
}
for _, tc := range cases {
exact, desiredTag := isTagConstraintOrExactTag(tc.tagConstraint)
if exact != tc.exact {
t.Fatalf("Given constraint: \"%s\", expected %t, but received %t", tc.tagConstraint, tc.exact, exact)
}
if desiredTag != tc.desiredTag {
t.Fatalf("Given constraint: \"%s\", expected result tag: \"%s\", but received \"%s\"", tc.tagConstraint, tc.desiredTag, desiredTag)
}
}
}
func TestBestFitTagOnEmptyConstraint(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
tags []string
expectedTag string
}{
{"", []string{"v0.0.1", "v0.0.2", "v0.0.3"}, "v0.0.3"},
{"", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.2.3"}, "1.2.3"},
{"", []string{"1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9", "1.1.0", "1.1.1", "1.1.2", "1.1.3", "1.2.3", "1.4.0", "2.0.0", "2.1.0"}, "2.1.0"},
{"", []string{}, ""},
}
for _, tc := range cases {
tag, err := bestFitTag(tc.tagConstraint, tc.tags)
if err != nil {
t.Fatalf("Failed on call to bestFitTag: %s", err)
}
if tag != tc.expectedTag {
t.Fatalf("Given constraint %s and tag list %v, expected %s, but received: %s", tc.tagConstraint, tc.tags, tc.expectedTag, tag)
}
}
}
func TestBestFitTagOnMalformedConstraint(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
}{
{"josh"},
{"plump elephants dancing in the night"},
}
for _, tc := range cases {
_, err := bestFitTag(tc.tagConstraint, []string{"v0.0.1"})
if err == nil {
t.Fatalf("Expected malformed constraint error, but received nothing.")
}
}
}
func TestBestFitTagNoSuchTag(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
tags []string
}{
{"~> 0.0.4", []string{"0.0.1", "0.0.2", "0.0.3"}},
{"> 0.0.4", []string{"0.0.1", "0.0.2", "0.0.3"}},
}
for _, tc := range cases {
_, err := bestFitTag(tc.tagConstraint, tc.tags)
if err == nil {
t.Fatalf("Expected 'Tag does not exist' but received nothing")
}
}
}