Skip to content

Commit

Permalink
Fix removing archive ext from name (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Aug 24, 2023
1 parent e88f808 commit a5542f5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
30 changes: 26 additions & 4 deletions internal/plugin/index_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ func (i *IndexBuilder) appendIndexEntry(entries map[string][]pluginBinariesIndex
}

pType, pName, os, arch := parts[0], parts[1], parts[2], parts[3]
for ext := range getter.Decompressors {
arch = strings.TrimSuffix(arch, "."+ext) // normalize in case archive was used
}
arch = trimArchiveExtension(arch) // normalize in case archive was used

if !pNameRegex.MatchString(pName) {
i.log.WithField("file", entryName).Debug("Ignoring file as it doesn't match filter")
Expand Down Expand Up @@ -288,10 +286,34 @@ func (i *IndexBuilder) getJSONSchemaLoaderForEntry(entry IndexEntry) gojsonschem
}

func hasArchiveExtension(path string) bool {
for ext := range getter.Decompressors {
for _, ext := range getAvailableDecompressors() {
if strings.HasSuffix(path, "."+ext) {
return true
}
}
return false
}

func trimArchiveExtension(in string) string {
for _, ext := range getAvailableDecompressors() {
in = strings.TrimSuffix(in, "."+ext)
}
return in
}

func getAvailableDecompressors() []string {
var (
multiExt []string
singleExt []string
)

for ext := range getter.Decompressors {
if strings.Contains(ext, ".") {
multiExt = append(multiExt, ext)
continue
}
singleExt = append(singleExt, ext)
}

return append(multiExt, singleExt...)
}
33 changes: 33 additions & 0 deletions internal/plugin/index_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,36 @@ func TestIndexBuilder_ValidateJSONSchemas(t *testing.T) {
// then
assert.EqualError(t, err, expectedErrMsg)
}

func TestRemoveArchiveExtension(t *testing.T) {
tests := []struct {
name string
givenName string
expName string
}{
{
name: "Trim single extension archive",
givenName: "test.gz",
expName: "test",
},
{
name: "Trim multi extension archive",
givenName: "test.tar.gz",
expName: "test",
},
{
name: "Does nothing if not archive",
givenName: "test.exe",
expName: "test.exe",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// when
gotName := trimArchiveExtension(tc.givenName)

// then
assert.Equal(t, tc.expName, gotName)
})
}
}

0 comments on commit a5542f5

Please sign in to comment.