-
Notifications
You must be signed in to change notification settings - Fork 89
/
tag_test.go
145 lines (123 loc) · 4.9 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
package main
import (
"testing"
)
func TestGetLatestAcceptableTag(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.3", "2.3"}, "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.3", "2.3"}, "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", "1.5"}, "1.5"},
{"~> 1.3", []string{"1.2", "1.3", "1.3.1"}, "1.3.1"},
{">= 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"},
{">= 1.5", []string{"1.1", "1.2", "1.3", "1.4", "2.0", "2.1"}, "2.1"},
{"v1.0.7", []string{"v1.0.7"}, "v1.0.7"},
{"v1.0.7", []string{}, ""},
}
for _, tc := range cases {
tag, err := getLatestAcceptableTag(tc.tagConstraint, tc.tags)
if err != nil {
t.Fatalf("Failed on call to getLatestAcceptableTag: %s", err.details)
}
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 TestIsTagConstraintSpecificTag(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
desiredTag string
specific 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 {
specific, desiredTag := isTagConstraintSpecificTag(tc.tagConstraint)
if specific != tc.specific {
t.Fatalf("Given constraint: \"%s\", expected %t, but received %t", tc.tagConstraint, tc.specific, specific)
}
if desiredTag != tc.desiredTag {
t.Fatalf("Given constraint: \"%s\", expected result tag: \"%s\", but received \"%s\"", tc.tagConstraint, tc.desiredTag, desiredTag)
}
}
}
func TestGetLatestAcceptableTagOnEmptyConstraint(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{"v0.5", "2.6", "1.4", "v1.0.8", "v3.1.4", "1.2.3"}, "v3.1.4"},
{"", []string{"v0.5", "2.6", "1.4", "v1.0.8", "v3.1.4", "1.2.3", "4.0"}, "4.0"},
{"", []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 := getLatestAcceptableTag(tc.tagConstraint, tc.tags)
if err != nil {
t.Fatalf("Failed on call to getLatestAcceptableTag: %s", err.details)
}
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 TestGetLatestAcceptableTagOnMalformedConstraint(t *testing.T) {
t.Parallel()
cases := []struct {
tagConstraint string
}{
{"josh"},
{"plump elephants dancing in the night"},
}
for _, tc := range cases {
_, err := getLatestAcceptableTag(tc.tagConstraint, []string{"v0.0.1"})
if err == nil {
t.Fatalf("Expected malformed constraint error, but received nothing.")
}
}
}
func TestGetLatestAcceptableTagNoSuchTag(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 := getLatestAcceptableTag(tc.tagConstraint, tc.tags)
if err == nil {
t.Fatalf("Expected 'Tag does not exist' but received nothing")
}
}
}