-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresolver_test.go
189 lines (165 loc) · 4.81 KB
/
resolver_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
185
186
187
188
189
//
// Copyright 2018-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package semver
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
type customDep struct {
name string
cond Constraint
}
// GetName return the name of the dependency (implements the Dependency interface)
func (c *customDep) GetName() string {
return c.name
}
// GetConstraint return the version contraints of the dependency (implements the Dependency interface)
func (c *customDep) GetConstraint() Constraint {
return c.cond
}
func (c *customDep) String() string {
return c.name + c.cond.String()
}
type customRel struct {
name string
vers *Version
deps []*customDep
}
// GetName return the name of the release (implements the Release interface)
func (r *customRel) GetName() string {
return r.name
}
// GetVersion return the version of the release (implements the Release interface)
func (r *customRel) GetVersion() *Version {
return r.vers
}
func (r *customRel) GetDependencies() []*customDep {
return r.deps
}
func (r *customRel) String() string {
return r.name + "@" + r.vers.String()
}
func d(dep string) *customDep {
name := dep[0:1]
cond, err := ParseConstraint(dep[1:])
if err != nil {
panic("invalid operator in dep: " + dep + " (" + err.Error() + ")")
}
return &customDep{name: name, cond: cond}
}
func deps(deps ...string) []*customDep {
var res []*customDep
for _, dep := range deps {
res = append(res, d(dep))
}
return res
}
func rel(name, ver string, deps []*customDep) *customRel {
return &customRel{name: name, vers: v(ver), deps: deps}
}
func TestResolver(t *testing.T) {
a100 := rel("A", "1.0.0", deps("B>=1.2.0", "C>=2.0.0"))
a110 := rel("A", "1.1.0", deps("B=1.2.0", "C>=2.0.0"))
a111 := rel("A", "1.1.1", deps("B", "C=1.1.1"))
a120 := rel("A", "1.2.0", deps("B=1.2.0", "C>2.0.0"))
a121 := rel("A", "1.2.1", deps("B", "C", "G", "H", "I", "E=1.0.1"))
b131 := rel("B", "1.3.1", deps("C<2.0.0"))
b130 := rel("B", "1.3.0", deps())
b121 := rel("B", "1.2.1", deps())
b120 := rel("B", "1.2.0", deps())
b111 := rel("B", "1.1.1", deps())
b110 := rel("B", "1.1.0", deps())
b100 := rel("B", "1.0.0", deps())
c200 := rel("C", "2.0.0", deps())
c120 := rel("C", "1.2.0", deps())
c111 := rel("C", "1.1.1", deps("B=1.1.1"))
c110 := rel("C", "1.1.0", deps())
c102 := rel("C", "1.0.2", deps())
c101 := rel("C", "1.0.1", deps())
c100 := rel("C", "1.0.0", deps())
c021 := rel("C", "0.2.1", deps())
c020 := rel("C", "0.2.0", deps())
c010 := rel("C", "0.1.0", deps("D"))
d100 := rel("D", "1.0.0", deps())
d120 := rel("D", "1.2.0", deps("E"))
e100 := rel("E", "1.0.0", deps())
e101 := rel("E", "1.0.1", deps("F")) // INVALID
g130 := rel("G", "1.3.0", deps())
g140 := rel("G", "1.4.0", deps())
g150 := rel("G", "1.5.0", deps())
g160 := rel("G", "1.6.0", deps())
g170 := rel("G", "1.7.0", deps())
g180 := rel("G", "1.8.0", deps())
h130 := rel("H", "1.3.0", deps())
h140 := rel("H", "1.4.0", deps())
h150 := rel("H", "1.5.0", deps())
h160 := rel("H", "1.6.0", deps())
h170 := rel("H", "1.7.0", deps())
h180 := rel("H", "1.8.0", deps())
i130 := rel("I", "1.3.0", deps())
i140 := rel("I", "1.4.0", deps())
i150 := rel("I", "1.5.0", deps())
i160 := rel("I", "1.6.0", deps())
i170 := rel("I", "1.7.0", deps())
i180 := rel("I", "1.8.0", deps())
arch := NewResolver[*customRel, *customDep]()
arch.AddReleases(
a100, a110, a111, a120, a121,
b131, b130, b121, b120, b111, b110, b100,
c200, c120, c111, c110, c102, c101, c100, c021, c020, c010,
d100, d120,
g130, g140, g150, g160, g170, g180,
h130, h140, h150, h160, h170, h180,
i130, i140, i150, i160, i170, i180,
)
arch.AddRelease(e100) // use this method for 100% code coverage
arch.AddRelease(e101)
a130 := rel("A", "1.3.0", deps())
r0 := arch.Resolve(a130) // Non-existent in archive
require.Nil(t, r0)
r1 := arch.Resolve(a100)
require.Len(t, r1, 3)
require.Contains(t, r1, a100)
require.Contains(t, r1, b130)
require.Contains(t, r1, c200)
fmt.Println(r1)
r2 := arch.Resolve(a110)
require.Len(t, r2, 3)
require.Contains(t, r2, a110)
require.Contains(t, r2, b120)
require.Contains(t, r2, c200)
fmt.Println(r2)
r3 := arch.Resolve(a111)
require.Len(t, r3, 3)
require.Contains(t, r3, a111)
require.Contains(t, r3, b111)
require.Contains(t, r3, c111)
fmt.Println(r3)
r4 := arch.Resolve(a120)
require.Nil(t, r4)
fmt.Println(r4)
r5 := arch.Resolve(c010)
require.Contains(t, r5, c010)
require.Contains(t, r5, d120)
require.Contains(t, r5, e100)
fmt.Println(r5)
done := make(chan bool)
go func() {
r6 := arch.Resolve(a121)
require.Nil(t, r6)
fmt.Println(r6)
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
require.FailNow(t, "test didn't complete in the allocated time")
}
r7 := arch.Resolve(e101)
require.Nil(t, r7)
}