Fix issue parsing package key (#1860)

This was causing a problem with container images packages using the `@`
sign in versions.

Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
This commit is contained in:
Sergio C. Arteaga 2022-03-22 13:14:11 +01:00 committed by GitHub
parent c75f1e715f
commit 89b27ee65a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -402,7 +402,7 @@ func BuildKey(p *hub.Package) string {
// ParseKey parses a key identifying a package version and returns its name and // ParseKey parses a key identifying a package version and returns its name and
// version. // version.
func ParseKey(key string) (string, string) { func ParseKey(key string) (string, string) {
p := strings.Split(key, "@") p := strings.SplitN(key, "@", 2)
return p[0], p[1] return p[0], p[1]
} }

View File

@ -1473,3 +1473,60 @@ func TestUnregister(t *testing.T) {
db.AssertExpectations(t) db.AssertExpectations(t)
}) })
} }
func TestBuildKey(t *testing.T) {
testCases := []struct {
p *hub.Package
expectedKey string
}{
{
&hub.Package{
Name: "package1",
Version: "1.0",
},
"package1@1.0",
},
{
&hub.Package{
Name: "package1",
Version: "1.0@something-else",
},
"package1@1.0@something-else",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.expectedKey, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.expectedKey, BuildKey(tc.p))
})
}
}
func TestParseKey(t *testing.T) {
testCases := []struct {
key string
expectedName string
expectedVersion string
}{
{
"package1@1.0",
"package1",
"1.0",
},
{
"package1@1.0@something-else",
"package1",
"1.0@something-else",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.key, func(t *testing.T) {
t.Parallel()
name, version := ParseKey(tc.key)
assert.Equal(t, tc.expectedName, name)
assert.Equal(t, tc.expectedVersion, version)
})
}
}