Skip to content

Commit 01220b1

Browse files
committed
basic tests for gitlab and bitbucket
1 parent 0839434 commit 01220b1

File tree

3 files changed

+273
-14
lines changed

3 files changed

+273
-14
lines changed

internal/devconfig/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ func (c *Config) loadRecursive(
239239

240240
// TODO UPDATEME
241241
for _, ref := range c.Root.Include {
242-
243242
switch ref.Type {
244243
case flake.TypeGitHub, flake.TypeGitLab, flake.TypeBitBucket:
245244
ref.Host = fmt.Sprintf("%s.com", ref.Type)

internal/plugin/git.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ func (p *gitPlugin) genericGitUrl(subpath string) (string, error) {
277277
return "", err
278278
}
279279

280-
// gitlab doesn't redirect master -> main or main -> master, so using "main"
281-
// as the default in this case
282280
query := parsed.Query()
283-
query.Add("ref", cmp.Or(p.ref.Rev, p.ref.Ref, "main"))
284281

285282
if p.ref.Dir != "" {
286283
query.Add("dir", p.ref.Dir)
@@ -290,7 +287,8 @@ func (p *gitPlugin) genericGitUrl(subpath string) (string, error) {
290287
query.Add("port", fmt.Sprintf("%d", p.ref.Port))
291288
}
292289

293-
parsed.RawQuery = query.Encode()
290+
// gitlab doesn't redirect master -> main or main -> master, so using "main"
291+
// as the default in this case
294292
query.Add("ref", cmp.Or(p.ref.Rev, p.ref.Ref, "main"))
295293
parsed.RawQuery = query.Encode()
296294

internal/plugin/git_test.go

Lines changed: 271 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package plugin
22

33
import (
4+
"fmt"
5+
"strings"
46
"testing"
57

8+
"github.com/samber/lo"
69
"github.com/stretchr/testify/assert"
710
"go.jetpack.io/devbox/nix/flake"
811
)
@@ -81,7 +84,31 @@ func TestNewGitPlugin(t *testing.T) {
8184
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/my-branch/mongodb",
8285
},
8386
{
84-
name: "parse github plugin with dir param and rev",
87+
name: "parse github plugin with dir param and ref",
88+
Include: []flake.Ref{
89+
{
90+
Type: "github",
91+
Owner: "jetify-com",
92+
Repo: "devbox-plugins",
93+
Dir: "mongodb",
94+
Ref: "initials/my-branch",
95+
},
96+
},
97+
expected: gitPlugin{
98+
ref: flake.Ref{
99+
Type: "github",
100+
Host: "github.com",
101+
Owner: "jetify-com",
102+
Repo: "devbox-plugins",
103+
Dir: "mongodb",
104+
Ref: "initials/my-branch",
105+
},
106+
name: "jetify-com.devbox-plugins.mongodb",
107+
},
108+
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/initials/my-branch/mongodb",
109+
},
110+
{
111+
name: "parse github plugin with dir param, rev, and ref",
85112
Include: []flake.Ref{
86113
{
87114
Type: "github",
@@ -95,23 +122,24 @@ func TestNewGitPlugin(t *testing.T) {
95122
expected: gitPlugin{
96123
ref: flake.Ref{
97124
Type: "github",
125+
Host: "github.com",
98126
Owner: "jetify-com",
99127
Repo: "devbox-plugins",
100128
Dir: "mongodb",
101-
Ref: "initials/my-branch", // FIXME
129+
Ref: "initials/my-branch", // Rev takes precendence over Ref; we exclude the Ref in the URL based on original useage of cmp.Or
102130
Rev: "initials",
103131
},
104132
name: "jetify-com.devbox-plugins.mongodb",
105133
},
106-
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/initials/my-branch/mongodb",
134+
expectedURL: "https://raw.githubusercontent.com/jetify-com/devbox-plugins/initials/mongodb",
107135
},
108136
{
109-
name: "parse gitlab plugin",
137+
name: "parse basic gitlab plugin",
110138
Include: []flake.Ref{
111139
{
112140
Type: "gitlab",
113141
Owner: "username",
114-
Repo: "my-repo",
142+
Repo: "my-plugin",
115143
},
116144
},
117145

@@ -120,17 +148,234 @@ func TestNewGitPlugin(t *testing.T) {
120148
Type: "gitlab",
121149
Host: "gitlab.com",
122150
Owner: "username",
123-
Repo: "my-repo",
151+
Repo: "my-plugin",
152+
},
153+
name: "username.my-plugin",
154+
},
155+
expectedURL: "https://gitlab.com/api/v4/projects/username%2Fmy-plugin/repository/files/raw?ref=main",
156+
},
157+
{
158+
name: "parse gitlab plugin with dir param",
159+
Include: []flake.Ref{
160+
{
161+
Type: "gitlab",
162+
Owner: "username",
163+
Repo: "my-plugin",
164+
Dir: "mongodb",
165+
},
166+
},
167+
expected: gitPlugin{
168+
ref: flake.Ref{
169+
Type: "gitlab",
170+
Host: "gitlab.com",
171+
Owner: "username",
172+
Repo: "my-plugin",
173+
Dir: "mongodb",
124174
},
125-
name: "username.my-repo",
175+
name: "username.my-plugin.mongodb",
126176
},
127-
expectedURL: "https://gitlab.com/api/v4/projects/username%2Fmy-repo/repository/files/raw?ref=main",
177+
expectedURL: "https://gitlab.com/api/v4/projects/username%2Fmy-plugin/repository/files/mongodb/raw?ref=main",
178+
},
179+
{
180+
name: "parse gitlab plugin with dir param and ref",
181+
Include: []flake.Ref{
182+
{
183+
Type: "gitlab",
184+
Owner: "username",
185+
Repo: "my-plugin",
186+
Dir: "mongodb",
187+
Ref: "some/branch",
188+
},
189+
},
190+
expected: gitPlugin{
191+
ref: flake.Ref{
192+
Type: "gitlab",
193+
Host: "gitlab.com",
194+
Owner: "username",
195+
Repo: "my-plugin",
196+
Dir: "mongodb",
197+
Ref: "some/branch",
198+
},
199+
name: "username.my-plugin.mongodb",
200+
},
201+
expectedURL: "https://gitlab.com/api/v4/projects/username%2Fmy-plugin/repository/files/mongodb/raw?ref=some%2Fbranch",
202+
},
203+
{
204+
name: "parse gitlab plugin with dir param and rev",
205+
Include: []flake.Ref{
206+
{
207+
Type: "gitlab",
208+
Owner: "username",
209+
Repo: "my-plugin",
210+
Dir: "mongodb",
211+
Rev: "1234567",
212+
},
213+
},
214+
expected: gitPlugin{
215+
ref: flake.Ref{
216+
Type: "gitlab",
217+
Host: "gitlab.com",
218+
Owner: "username",
219+
Repo: "my-plugin",
220+
Dir: "mongodb",
221+
Rev: "1234567",
222+
},
223+
name: "username.my-plugin.mongodb",
224+
},
225+
expectedURL: "https://gitlab.com/api/v4/projects/username%2Fmy-plugin/repository/files/mongodb/raw?ref=1234567",
226+
},
227+
{
228+
name: "parse gitlab plugin with dir param and rev",
229+
Include: []flake.Ref{
230+
{
231+
Type: "gitlab",
232+
Owner: "username",
233+
Repo: "my-plugin",
234+
Dir: "mongodb",
235+
Ref: "some/branch",
236+
Rev: "1234567",
237+
},
238+
},
239+
expected: gitPlugin{
240+
ref: flake.Ref{
241+
Type: "gitlab",
242+
Host: "gitlab.com",
243+
Owner: "username",
244+
Repo: "my-plugin",
245+
Dir: "mongodb",
246+
Ref: "some/branch",
247+
Rev: "1234567",
248+
},
249+
name: "username.my-plugin.mongodb",
250+
},
251+
expectedURL: "https://gitlab.com/api/v4/projects/username%2Fmy-plugin/repository/files/mongodb/raw?ref=1234567",
252+
},
253+
{
254+
name: "parse basic bitbucket plugin",
255+
Include: []flake.Ref{
256+
{
257+
Type: "bitbucket",
258+
Owner: "username",
259+
Repo: "my-plugin",
260+
},
261+
},
262+
263+
expected: gitPlugin{
264+
ref: flake.Ref{
265+
Type: "bitbucket",
266+
Host: "bitbucket.com",
267+
Owner: "username",
268+
Repo: "my-plugin",
269+
},
270+
name: "username.my-plugin",
271+
},
272+
expectedURL: "https://api.bitbucket.org/2.0/repositories/username/my-plugin/src/main",
273+
},
274+
{
275+
name: "parse bitbucket plugin with dir param",
276+
Include: []flake.Ref{
277+
{
278+
Type: "bitbucket",
279+
Owner: "username",
280+
Repo: "my-plugin",
281+
Dir: "subdir",
282+
},
283+
},
284+
285+
expected: gitPlugin{
286+
ref: flake.Ref{
287+
Type: "bitbucket",
288+
Host: "bitbucket.com",
289+
Owner: "username",
290+
Repo: "my-plugin",
291+
Dir: "subdir",
292+
},
293+
name: "username.my-plugin.subdir",
294+
},
295+
expectedURL: "https://api.bitbucket.org/2.0/repositories/username/my-plugin/src/main/subdir",
296+
},
297+
{
298+
name: "parse bitbucket plugin with dir param and ref",
299+
Include: []flake.Ref{
300+
{
301+
Type: "bitbucket",
302+
Owner: "username",
303+
Repo: "my-plugin",
304+
Dir: "subdir",
305+
Ref: "some/branch",
306+
},
307+
},
308+
309+
expected: gitPlugin{
310+
ref: flake.Ref{
311+
Type: "bitbucket",
312+
Host: "bitbucket.com",
313+
Owner: "username",
314+
Repo: "my-plugin",
315+
Dir: "subdir",
316+
Ref: "some/branch",
317+
},
318+
name: "username.my-plugin.subdir",
319+
},
320+
expectedURL: "https://api.bitbucket.org/2.0/repositories/username/my-plugin/src/some/branch/subdir",
321+
},
322+
{
323+
name: "parse bitbucket plugin with dir param and rev",
324+
Include: []flake.Ref{
325+
{
326+
Type: "bitbucket",
327+
Owner: "username",
328+
Repo: "my-plugin",
329+
Dir: "subdir",
330+
Rev: "1234567",
331+
},
332+
},
333+
334+
expected: gitPlugin{
335+
ref: flake.Ref{
336+
Type: "bitbucket",
337+
Host: "bitbucket.com",
338+
Owner: "username",
339+
Repo: "my-plugin",
340+
Dir: "subdir",
341+
Rev: "1234567",
342+
},
343+
name: "username.my-plugin.subdir",
344+
},
345+
expectedURL: "https://api.bitbucket.org/2.0/repositories/username/my-plugin/src/1234567/subdir",
346+
},
347+
{
348+
name: "parse bitbucket plugin with dir param, ref and rev",
349+
Include: []flake.Ref{
350+
{
351+
Type: "bitbucket",
352+
Owner: "username",
353+
Repo: "my-plugin",
354+
Dir: "subdir",
355+
Ref: "some/branch",
356+
Rev: "1234567",
357+
},
358+
},
359+
360+
expected: gitPlugin{
361+
ref: flake.Ref{
362+
Type: "bitbucket",
363+
Host: "bitbucket.com",
364+
Owner: "username",
365+
Repo: "my-plugin",
366+
Dir: "subdir",
367+
Ref: "some/branch",
368+
Rev: "1234567",
369+
},
370+
name: "username.my-plugin.subdir",
371+
},
372+
expectedURL: "https://api.bitbucket.org/2.0/repositories/username/my-plugin/src/1234567/subdir",
128373
},
129374
}
130375

131376
for _, testCase := range testCases {
132377
t.Run(testCase.name, func(t *testing.T) {
133-
actual, err := newGitPlugin(testCase.Include[0]) // FIXME: need to evaluate URL
378+
actual, err := newGitPluginForTest(testCase.Include[0]) // FIXME: need to evaluate URL
134379
assert.NoError(t, err)
135380
assert.Equal(t, &testCase.expected, actual)
136381
u, err := testCase.expected.url("")
@@ -140,6 +385,23 @@ func TestNewGitPlugin(t *testing.T) {
140385
}
141386
}
142387

388+
func newGitPluginForTest(ref flake.Ref) (*gitPlugin, error) {
389+
// added because this occurs much earlier in processing within `internal/devconfig/config.go`
390+
switch ref.Type {
391+
case flake.TypeGitHub, flake.TypeGitLab, flake.TypeBitBucket:
392+
ref.Host = fmt.Sprintf("%s.com", ref.Type)
393+
}
394+
395+
plugin := &gitPlugin{ref: ref}
396+
name := strings.ReplaceAll(ref.Dir, "/", "-")
397+
repoDotted := strings.ReplaceAll(ref.Repo, "/", ".")
398+
plugin.name = githubNameRegexp.ReplaceAllString(
399+
strings.Join(lo.Compact([]string{ref.Owner, repoDotted, name}), "."),
400+
" ",
401+
)
402+
return plugin, nil
403+
}
404+
143405
func TestGitPluginAuth(t *testing.T) {
144406
gitPlugin := gitPlugin{
145407
ref: flake.Ref{

0 commit comments

Comments
 (0)