Skip to content

Commit

Permalink
api/contentdb: properly detect mods at root folder
Browse files Browse the repository at this point in the history
If the mod is packaged at the root folder, the
PackageArchive.FindFile() function would not find any
file. This commit fixes that issue with a reproducible
test case.
  • Loading branch information
ronoaldo committed Mar 7, 2021
1 parent 21b368d commit 845ceef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/contentdb/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (p *PackageArchive) Contents() []string {
// are found.
func (p *PackageArchive) FindFile(name string, max int) (count int, dir string) {
for _, f := range p.Contents() {
if strings.HasSuffix(f, "/"+name) {
if strings.HasSuffix(f, "/"+name) || f == name {
dir, _ = path.Split(f)
count++
}
Expand Down
8 changes: 8 additions & 0 deletions api/contentdb/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var (
bytesTelegram []byte = mustReadFile("testdata/telegram.zip")
zipTelegram *zip.Reader = mustZip(bytesTelegram)

bytesRespawn []byte = mustReadFile("testdata/respawn.zip")
zipRespawn *zip.Reader = mustZip(bytesRespawn)

bytesNestedMod []byte = mustReadFile("testdata/nestedmod.zip")
zipNestedMod *zip.Reader = mustZip(bytesNestedMod)

Expand Down Expand Up @@ -137,6 +140,11 @@ func TestPackageArchive_Type(t *testing.T) {
fields: fields{b: bytes.NewBuffer(bytesInvalidModpack), z: zipInvalidModpack},
want: Invalid,
},
{
name: "should detect valid mod at the root of zip folder",
fields: fields{b: bytes.NewBuffer(bytesRespawn), z: zipRespawn},
want: Mod,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Binary file added api/contentdb/testdata/respawn.zip
Binary file not shown.

1 comment on commit 845ceef

@ronoaldo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes #1

Please sign in to comment.