Skip to content

Commit c2973f5

Browse files
committed
fix removeRegistryFromTag and add tests
1 parent b48444e commit c2973f5

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

packages/shared/pkg/dockerhub/repository.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dockerhub
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"time"
78

89
"github.com/google/go-containerregistry/pkg/name"
@@ -59,7 +60,10 @@ func removeRegistryFromTag(tag string) (string, error) {
5960
if err != nil {
6061
return "", fmt.Errorf("invalid image reference: %w", err)
6162
}
62-
refWithoutRegistry := ref.String()
6363

64-
return refWithoutRegistry, nil
64+
registry := ref.Context().RegistryStr()
65+
withoutRegistry := strings.TrimPrefix(ref.Name(), registry)
66+
withoutRegistry = strings.TrimPrefix(withoutRegistry, "/")
67+
68+
return withoutRegistry, nil
6569
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dockerhub
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRemoveRegistryFromTag(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
tag string
11+
want string
12+
wantErr bool
13+
}{
14+
{
15+
name: "tag with registry prefix",
16+
tag: "docker.io/library/ubuntu:latest",
17+
want: "library/ubuntu:latest",
18+
wantErr: false,
19+
},
20+
{
21+
name: "tag without registry",
22+
tag: "ubuntu:latest",
23+
want: "library/ubuntu:latest",
24+
wantErr: false,
25+
},
26+
{
27+
name: "tag without registry",
28+
tag: "index.docker.io/ubuntu:latest",
29+
want: "library/ubuntu:latest",
30+
wantErr: false,
31+
},
32+
{
33+
name: "tag with custom registry",
34+
tag: "gcr.io/my-project/my-image:v1.0.0",
35+
want: "my-project/my-image:v1.0.0",
36+
wantErr: false,
37+
},
38+
{
39+
name: "tag with port in registry",
40+
tag: "localhost:5000/my-image:latest",
41+
want: "my-image:latest",
42+
wantErr: false,
43+
},
44+
{
45+
name: "invalid tag format",
46+
tag: ":::invalid",
47+
want: "",
48+
wantErr: true,
49+
},
50+
{
51+
name: "empty tag",
52+
tag: "",
53+
want: "",
54+
wantErr: true,
55+
},
56+
}
57+
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
got, err := removeRegistryFromTag(tt.tag)
61+
if (err != nil) != tt.wantErr {
62+
t.Errorf("removeRegistryFromTag() error = %v, wantErr %v", err, tt.wantErr)
63+
return
64+
}
65+
if got != tt.want {
66+
t.Errorf("removeRegistryFromTag() = %v, want %v", got, tt.want)
67+
}
68+
})
69+
}
70+
}

0 commit comments

Comments
 (0)