-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
201 lines (191 loc) · 6.19 KB
/
config_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
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"log/slog"
"slices"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/utilitywarehouse/git-mirror/pkg/mirror"
)
func Test_diffRepositories(t *testing.T) {
tests := []struct {
name string
initialConfig *mirror.RepoPoolConfig
newConfig *mirror.RepoPoolConfig
wantNewRepos []mirror.RepositoryConfig
wantRemovedRepos []string
}{
{
name: "empty",
initialConfig: &mirror.RepoPoolConfig{},
newConfig: &mirror.RepoPoolConfig{
Defaults: mirror.DefaultConfig{Root: "/root"},
Repositories: []mirror.RepositoryConfig{
{Remote: "[email protected]:path/to/repo1.git"},
{Remote: "[email protected]:path/to/repo2.git"},
},
},
wantNewRepos: []mirror.RepositoryConfig{
{Remote: "[email protected]:path/to/repo1.git"},
{Remote: "[email protected]:path/to/repo2.git"},
},
wantRemovedRepos: nil,
},
{
name: "replace_repo2_repo3",
initialConfig: &mirror.RepoPoolConfig{
Defaults: mirror.DefaultConfig{Root: "/root", Interval: 10 * time.Second},
Repositories: []mirror.RepositoryConfig{
{Remote: "[email protected]:path/to/repo1.git"},
{Remote: "[email protected]:path/to/repo2.git"},
},
},
newConfig: &mirror.RepoPoolConfig{
Defaults: mirror.DefaultConfig{Root: "/root"},
Repositories: []mirror.RepositoryConfig{
{Remote: "[email protected]:path/to/repo1.git"},
{
Remote: "[email protected]:path/to/repo3.git",
Root: "/another-root",
Interval: 2 * time.Second,
MirrorTimeout: 4 * time.Second,
GitGC: "off",
Auth: mirror.Auth{SSHKeyPath: "/another/path/to/key"},
},
},
},
wantNewRepos: []mirror.RepositoryConfig{
{
Remote: "[email protected]:path/to/repo3.git",
Root: "/another-root",
Interval: 2 * time.Second,
MirrorTimeout: 4 * time.Second,
GitGC: "off",
Auth: mirror.Auth{SSHKeyPath: "/another/path/to/key"},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
applyGitDefaults(tt.initialConfig)
repoPool, err := mirror.NewRepoPool(t.Context(), *tt.initialConfig, nil, nil)
if err != nil {
t.Fatalf("could not create git mirror pool err:%v", err)
}
gotNewRepos, gotRemovedRepos := diffRepositories(repoPool, tt.newConfig)
if diff := cmp.Diff(gotNewRepos, tt.wantNewRepos); diff != "" {
t.Errorf("diffRepositories() NewRepos mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(gotRemovedRepos, tt.wantRemovedRepos); diff != "" {
t.Errorf("diffRepositories() RemovedRepos mismatch (-want +got):\n%s", diff)
}
})
}
}
func Test_diffWorktrees(t *testing.T) {
tests := []struct {
name string
initialRepoConf *mirror.RepositoryConfig
newRepoConf *mirror.RepositoryConfig
wantNewWTCs []mirror.WorktreeConfig
wantRemovedWTs []string
}{
{
name: "no_worktree",
initialRepoConf: &mirror.RepositoryConfig{
Remote: "[email protected]:path/to/repo1.git",
Root: "/root", Interval: 10 * time.Second, GitGC: "always",
},
newRepoConf: &mirror.RepositoryConfig{
Remote: "[email protected]:path/to/repo1.git",
Root: "/root", Interval: 10 * time.Second, GitGC: "always",
Worktrees: []mirror.WorktreeConfig{
{Link: "link", Ref: "master", Pathspec: ""},
{Link: "link2", Ref: "other-branch", Pathspec: "path"},
},
},
wantNewWTCs: []mirror.WorktreeConfig{
{Link: "link", Ref: "master"},
{Link: "link2", Ref: "other-branch", Pathspec: "path"},
},
wantRemovedWTs: nil,
},
{
name: "replace_link_ref_path",
initialRepoConf: &mirror.RepositoryConfig{
Remote: "[email protected]:path/to/repo1.git",
Root: "/root", Interval: 10 * time.Second, GitGC: "always",
Worktrees: []mirror.WorktreeConfig{
{Link: "link", Ref: "master", Pathspec: ""},
{Link: "link2", Ref: "other-branch", Pathspec: "path"},
},
},
newRepoConf: &mirror.RepositoryConfig{
Remote: "[email protected]:path/to/repo1.git",
Root: "/root", Interval: 10 * time.Second, GitGC: "always",
Worktrees: []mirror.WorktreeConfig{
{Link: "link", Ref: "master", Pathspec: "new-path"},
{Link: "link2", Ref: "new-branch", Pathspec: "path"},
},
},
wantNewWTCs: []mirror.WorktreeConfig{
{Link: "link", Ref: "master", Pathspec: "new-path"},
{Link: "link2", Ref: "new-branch", Pathspec: "path"},
},
wantRemovedWTs: []string{"link", "link2"},
},
{
name: "add_new_link",
initialRepoConf: &mirror.RepositoryConfig{
Remote: "[email protected]:path/to/repo1.git",
Root: "/root", Interval: 10 * time.Second, GitGC: "always",
Worktrees: []mirror.WorktreeConfig{
{Link: "link", Ref: "master", Pathspec: ""},
{Link: "link2", Ref: "other-branch", Pathspec: "path"},
},
},
newRepoConf: &mirror.RepositoryConfig{
Remote: "[email protected]:path/to/repo1.git",
Root: "/root", Interval: 10 * time.Second, GitGC: "always",
Worktrees: []mirror.WorktreeConfig{
{Link: "link", Ref: "master", Pathspec: ""},
{Link: "link3", Ref: "other-branch", Pathspec: "path"},
},
},
wantNewWTCs: []mirror.WorktreeConfig{
{Link: "link3", Ref: "other-branch", Pathspec: "path"},
},
wantRemovedWTs: []string{"link2"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo, err := mirror.NewRepository(*tt.initialRepoConf, nil, slog.Default())
if err != nil {
t.Fatalf("failed to create repo error = %v", err)
}
gotNewWTCs, gotRemovedWTs := diffWorktrees(repo, tt.newRepoConf)
// since these slices are based on map of worktrees order of elements
// differs between runs
slices.SortFunc(gotNewWTCs, func(a, b mirror.WorktreeConfig) int {
switch {
case a.Link > b.Link:
return 1
case a.Link == b.Link:
return 0
default:
return -1
}
})
slices.Sort(gotRemovedWTs)
if diff := cmp.Diff(gotNewWTCs, tt.wantNewWTCs); diff != "" {
t.Errorf("diffWorktrees() NewWTCs mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(gotRemovedWTs, tt.wantRemovedWTs); diff != "" {
t.Errorf("diffWorktrees() RemovedWTs mismatch (-want +got):\n%s", diff)
}
})
}
}