1
1
package plugin
2
2
3
3
import (
4
+ "fmt"
5
+ "strings"
4
6
"testing"
5
7
8
+ "github.com/samber/lo"
6
9
"github.com/stretchr/testify/assert"
7
10
"go.jetpack.io/devbox/nix/flake"
8
11
)
@@ -81,7 +84,31 @@ func TestNewGitPlugin(t *testing.T) {
81
84
expectedURL : "https://raw.githubusercontent.com/jetify-com/devbox-plugins/my-branch/mongodb" ,
82
85
},
83
86
{
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" ,
85
112
Include : []flake.Ref {
86
113
{
87
114
Type : "github" ,
@@ -95,23 +122,24 @@ func TestNewGitPlugin(t *testing.T) {
95
122
expected : gitPlugin {
96
123
ref : flake.Ref {
97
124
Type : "github" ,
125
+ Host : "github.com" ,
98
126
Owner : "jetify-com" ,
99
127
Repo : "devbox-plugins" ,
100
128
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
102
130
Rev : "initials" ,
103
131
},
104
132
name : "jetify-com.devbox-plugins.mongodb" ,
105
133
},
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" ,
107
135
},
108
136
{
109
- name : "parse gitlab plugin" ,
137
+ name : "parse basic gitlab plugin" ,
110
138
Include : []flake.Ref {
111
139
{
112
140
Type : "gitlab" ,
113
141
Owner : "username" ,
114
- Repo : "my-repo " ,
142
+ Repo : "my-plugin " ,
115
143
},
116
144
},
117
145
@@ -120,17 +148,234 @@ func TestNewGitPlugin(t *testing.T) {
120
148
Type : "gitlab" ,
121
149
Host : "gitlab.com" ,
122
150
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" ,
124
174
},
125
- name : "username.my-repo " ,
175
+ name : "username.my-plugin.mongodb " ,
126
176
},
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" ,
128
373
},
129
374
}
130
375
131
376
for _ , testCase := range testCases {
132
377
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
134
379
assert .NoError (t , err )
135
380
assert .Equal (t , & testCase .expected , actual )
136
381
u , err := testCase .expected .url ("" )
@@ -140,6 +385,23 @@ func TestNewGitPlugin(t *testing.T) {
140
385
}
141
386
}
142
387
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
+
143
405
func TestGitPluginAuth (t * testing.T ) {
144
406
gitPlugin := gitPlugin {
145
407
ref : flake.Ref {
0 commit comments