@@ -235,3 +235,174 @@ func TestParseGitRef(t *testing.T) {
235235 })
236236 }
237237}
238+
239+ func TestFragmentFormat (t * testing.T ) {
240+ cases := []struct {
241+ ref string
242+ expected string
243+ ok bool
244+ }{
245+ {
246+ ref : "https://example.com/" ,
247+ expected : "https://example.com/" ,
248+ ok : false ,
249+ },
250+ {
251+ ref : "https://example.com/foo.git" ,
252+ expected : "https://example.com/foo.git" ,
253+ ok : true ,
254+ },
255+ {
256+ ref : "https://example.com/foo.git#deadbeef" ,
257+ expected : "https://example.com/foo.git#deadbeef" ,
258+ ok : true ,
259+ },
260+ {
261+ ref : "https://example.com/foo.git#release/1.2" ,
262+ expected : "https://example.com/foo.git#release/1.2" ,
263+ ok : true ,
264+ },
265+ {
266+ ref : "https://example.com/foo.git/" ,
267+ expected : "https://example.com/foo.git/" ,
268+ ok : false ,
269+ },
270+ {
271+ ref : "https://example.com/foo.git.bar" ,
272+ expected : "https://example.com/foo.git.bar" ,
273+ ok : false ,
274+ },
275+ {
276+ ref : "git://example.com/foo" ,
277+ expected : "git://example.com/foo" ,
278+ ok : true ,
279+ },
280+ {
281+ ref : "github.com/moby/buildkit" ,
282+ expected : "github.com/moby/buildkit" ,
283+ ok : true ,
284+ },
285+ {
286+ ref : "github.com/moby/buildkit#master" ,
287+ expected : "github.com/moby/buildkit#master" ,
288+ ok : true ,
289+ },
290+ {
291+ ref : "custom.xyz/moby/buildkit.git" ,
292+ expected : "custom.xyz/moby/buildkit.git" ,
293+ ok : false ,
294+ },
295+ {
296+ ref : "https://github.com/moby/buildkit" ,
297+ expected : "https://github.com/moby/buildkit" ,
298+ ok : false ,
299+ },
300+ {
301+ ref : "https://github.com/moby/buildkit.git" ,
302+ expected : "https://github.com/moby/buildkit.git" ,
303+ ok : true ,
304+ },
305+ {
306+ ref :
"https://foo:[email protected] /moby/buildkit.git" ,
307+ expected :
"https://foo:[email protected] /moby/buildkit.git" ,
308+ ok : true ,
309+ },
310+ {
311+ ref :
"[email protected] :moby/buildkit" ,
312+ expected :
"[email protected] :moby/buildkit" ,
313+ ok : true ,
314+ },
315+ {
316+ ref :
"[email protected] :moby/buildkit.git" ,
317+ expected :
"[email protected] :moby/buildkit.git" ,
318+ ok : true ,
319+ },
320+ {
321+ ref :
"[email protected] :atlassianlabs/atlassian-docker.git" ,
322+ expected :
"[email protected] :atlassianlabs/atlassian-docker.git" ,
323+ ok : true ,
324+ },
325+ {
326+ ref : "https://github.com/foo/bar.git#baz/qux:quux/quuz" ,
327+ expected : "https://github.com/foo/bar.git#baz/qux" ,
328+ ok : true ,
329+ },
330+ {
331+ ref : "http://github.com/docker/docker.git:#branch" ,
332+ expected : "http://github.com/docker/docker.git:#branch" ,
333+ ok : false ,
334+ },
335+ {
336+ ref : "https://github.com/docker/docker.git#:myfolder" ,
337+ expected : "https://github.com/docker/docker.git" ,
338+ ok : true ,
339+ },
340+ {
341+ ref : "./.git" ,
342+ expected : "./.git" ,
343+ ok : false ,
344+ },
345+ {
346+ ref : ".git" ,
347+ expected : ".git" ,
348+ ok : false ,
349+ },
350+ {
351+ ref : "https://github.com/docker/docker.git?ref=v1.0.0&subdir=/subdir" ,
352+ expected : "https://github.com/docker/docker.git#v1.0.0" ,
353+ ok : true ,
354+ },
355+ {
356+ ref : "https://github.com/moby/buildkit.git?subdir=/subdir#v1.0.0" ,
357+ expected : "https://github.com/moby/buildkit.git#v1.0.0" ,
358+ ok : true ,
359+ },
360+ {
361+ ref : "https://github.com/moby/buildkit.git?tag=v1.0.0" ,
362+ expected : "https://github.com/moby/buildkit.git#refs/tags/v1.0.0" ,
363+ ok : true ,
364+ },
365+ {
366+ ref : "github.com/moby/buildkit?tag=v1.0.0" ,
367+ expected : "github.com/moby/buildkit#refs/tags/v1.0.0" ,
368+ ok : true ,
369+ },
370+ {
371+ ref : "https://github.com/moby/buildkit.git?branch=v1.0" ,
372+ expected : "https://github.com/moby/buildkit.git#refs/heads/v1.0" ,
373+ ok : true ,
374+ },
375+ {
376+ ref : "https://github.com/moby/buildkit.git?ref=v1.0.0#v1.2.3" ,
377+ expected : "https://github.com/moby/buildkit.git?ref=v1.0.0#v1.2.3" ,
378+ ok : false ,
379+ },
380+ {
381+ ref : "https://github.com/moby/buildkit.git?ref=v1.0.0&tag=v1.2.3" ,
382+ expected : "https://github.com/moby/buildkit.git?ref=v1.0.0&tag=v1.2.3" ,
383+ ok : false ,
384+ },
385+ {
386+ ref : "https://github.com/moby/buildkit.git?tag=v1.0.0&branch=v1.0" ,
387+ expected : "https://github.com/moby/buildkit.git?tag=v1.0.0&branch=v1.0" ,
388+ ok : false ,
389+ },
390+ {
391+ ref :
"[email protected] :moby/buildkit.git?subdir=/subdir#v1.0.0" ,
392+ expected :
"[email protected] :moby/buildkit.git#v1.0.0" ,
393+ ok : true ,
394+ },
395+ {
396+ ref : "https://github.com/moby/buildkit.git?invalid=123" ,
397+ expected : "https://github.com/moby/buildkit.git?invalid=123" ,
398+ ok : false ,
399+ },
400+ }
401+ for i , tt := range cases {
402+ t .Run (fmt .Sprintf ("case%d" , i + 1 ), func (t * testing.T ) {
403+ got , ok := FragmentFormat (tt .ref )
404+ require .Equal (t , tt .expected , got )
405+ require .Equal (t , tt .ok , ok )
406+ })
407+ }
408+ }
0 commit comments