-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo_fake_test.go
189 lines (157 loc) · 4.71 KB
/
repo_fake_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
package pkgcraft_test
import (
"testing"
"github.com/stretchr/testify/assert"
. "github.com/pkgcraft/pkgcraft-go"
)
func TestNewFakeRepo(t *testing.T) {
// empty
repo, _ := NewFakeRepo("fake", 0, []string{})
assert.Equal(t, repo.Len(), 0)
// single pkg
repo, _ = NewFakeRepo("fake", 0, []string{"cat/pkg-1"})
assert.Equal(t, repo.Len(), 1)
// multiple pkgs with invalid cpv
repo, _ = NewFakeRepo("fake", 0, []string{"a/b-1", "c/d-2", "=cat/pkg-1"})
assert.Equal(t, repo.Len(), 2)
// invalid repo id
_, err := NewFakeRepo("fake\xc5", 0, []string{})
assert.NotNil(t, err)
// CPV string with invalid utf8
_, err = NewFakeRepo("fake", 0, []string{"cat/pkg\xc5-1"})
assert.NotNil(t, err)
}
func TestFakeRepoExtend(t *testing.T) {
// empty
repo, _ := NewFakeRepo("fake", 0, []string{})
assert.Equal(t, repo.Len(), 0)
// add single pkg
err := repo.Extend([]string{"cat/pkg-1"})
assert.Equal(t, repo.Len(), 1)
assert.Nil(t, err)
// add multiple pkgs with overlap
err = repo.Extend([]string{"cat/pkg-1", "cat/pkg-2"})
assert.Equal(t, repo.Len(), 2)
assert.Nil(t, err)
// mutations allowed after adding to a config
config := NewConfig()
defer config.Close()
err = config.AddRepo(repo)
assert.Nil(t, err)
err = repo.Extend([]string{"cat/pkg-3"})
assert.Nil(t, err)
}
func TestFakeRepoIter(t *testing.T) {
var cpvs []string
// empty
repo, _ := NewFakeRepo("fake", 0, []string{})
iter := repo.Iter()
assert.False(t, iter.HasNext())
assert.Nil(t, iter.Next())
// add single pkg
err := repo.Extend([]string{"cat/pkg-1"})
assert.Nil(t, err)
for iter := repo.Iter(); iter.HasNext(); {
pkg := iter.Next()
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1"})
// reset slice
cpvs = cpvs[:0]
// add multiple pkgs with overlap
err = repo.Extend([]string{"cat/pkg-1", "cat/pkg-2"})
assert.Nil(t, err)
for iter := repo.Iter(); iter.HasNext(); {
pkg := iter.Next()
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1", "cat/pkg-2"})
}
func TestFakeRepoPkgs(t *testing.T) {
var cpvs []string
// empty
repo, _ := NewFakeRepo("fake", 0, []string{})
assert.Empty(t, repo.Pkgs())
// add single pkg
err := repo.Extend([]string{"cat/pkg-1"})
assert.Nil(t, err)
for pkg := range repo.Pkgs() {
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1"})
// reset slice
cpvs = cpvs[:0]
// add multiple pkgs with overlap
err = repo.Extend([]string{"cat/pkg-1", "cat/pkg-2"})
assert.Nil(t, err)
for pkg := range repo.Pkgs() {
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1", "cat/pkg-2"})
}
func TestFakeRepoIterRestrict(t *testing.T) {
var cpvs []string
restrict, _ := NewRestrict("<cat/pkg-2")
// empty
repo, _ := NewFakeRepo("fake", 0, []string{})
iter := repo.IterRestrict(restrict)
assert.False(t, iter.HasNext())
assert.Nil(t, iter.Next())
// add single pkg
err := repo.Extend([]string{"cat/pkg-1"})
assert.Nil(t, err)
for iter := repo.IterRestrict(restrict); iter.HasNext(); {
pkg := iter.Next()
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1"})
// reset slice
cpvs = cpvs[:0]
// add multiple pkgs with overlap
err = repo.Extend([]string{"cat/pkg-1", "cat/pkg-2"})
assert.Nil(t, err)
for iter := repo.IterRestrict(restrict); iter.HasNext(); {
pkg := iter.Next()
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1"})
}
func TestFakeRepoRestrictPkgs(t *testing.T) {
var cpvs []string
restrict, _ := NewRestrict("<cat/pkg-2")
// empty
repo, _ := NewFakeRepo("fake", 0, []string{})
assert.Empty(t, repo.RestrictPkgs(restrict))
// add single pkg
err := repo.Extend([]string{"cat/pkg-1"})
assert.Nil(t, err)
for pkg := range repo.RestrictPkgs(restrict) {
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1"})
// reset slice
cpvs = cpvs[:0]
// add multiple pkgs with overlap
err = repo.Extend([]string{"cat/pkg-1", "cat/pkg-2"})
assert.Nil(t, err)
for pkg := range repo.RestrictPkgs(restrict) {
// verify repos are equal
assert.True(t, repo.Cmp(pkg.Repo()) == 0)
cpvs = append(cpvs, pkg.Cpv().String())
}
assert.Equal(t, cpvs, []string{"cat/pkg-1"})
}