Skip to content

Commit

Permalink
add tests & fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ExplodingDragon committed Jan 15, 2025
1 parent 08b3967 commit 3b55815
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 40 deletions.
4 changes: 2 additions & 2 deletions modules/setting/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var (

DefaultRPMSignEnabled bool

DefaultMetaArchLatestVersion bool
RepoLatestVersionArch bool
}{
Enabled: true,
LimitTotalOwnerCount: -1,
Expand Down Expand Up @@ -104,7 +104,7 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
Packages.LimitSizeSwift = mustBytes(sec, "LIMIT_SIZE_SWIFT")
Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
Packages.DefaultRPMSignEnabled = sec.Key("DEFAULT_RPM_SIGN_ENABLED").MustBool(false)
Packages.DefaultMetaArchLatestVersion = sec.Key("METADATA_ARCH_LATEST_VERSION").MustBool(false)
Packages.RepoLatestVersionArch = sec.Key("METADATA_ARCH_LATEST_VERSION").MustBool(false)
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions services/packages/arch/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
return packages_service.DeletePackageFile(ctx, pf)
}

vpfs := make(map[string]*entryOptions)
vpfs := make(map[int64]*entryOptions)
for _, pf := range pfs {
current := &entryOptions{
File: pf,
Expand All @@ -246,17 +246,17 @@ func buildPackagesIndex(ctx context.Context, ownerID int64, repoVersion *package
return err
}

if setting.Packages.DefaultMetaArchLatestVersion {
old := vpfs[pf.Name]
if old != nil {
if setting.Packages.RepoLatestVersionArch {
if old, ok := vpfs[current.Version.PackageID]; ok {
if compareVersions(old.Version.Version, current.Version.Version) == -1 {
vpfs[pf.Name] = current
vpfs[current.Version.PackageID] = current
}
} else {
vpfs[pf.Name] = current
vpfs[current.Version.PackageID] = current
}
} else {
vpfs[pf.Name] = current
// keep all files
vpfs[current.File.ID] = current
}
}

Expand Down
3 changes: 1 addition & 2 deletions services/packages/arch/vercmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func compareSegments(a, b []string) int {
return 0
} else if l == lenA {
return -1
} else {
return 1
}
return 1
}

func compare(a, b string) int {
Expand Down
95 changes: 66 additions & 29 deletions tests/integration/api_packages_arch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
arch_module "code.gitea.io/gitea/modules/packages/arch"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
arch_service "code.gitea.io/gitea/services/packages/arch"
"code.gitea.io/gitea/tests"

Expand Down Expand Up @@ -79,6 +81,34 @@ license = MIT`)

return buf.Bytes()
}
readIndexContent := func(r io.Reader) (map[string]string, error) {
gzr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}

content := make(map[string]string)

tr := tar.NewReader(gzr)
for {
hd, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

buf, err := io.ReadAll(tr)
if err != nil {
return nil, err
}

content[hd.Name] = string(buf)
}

return content, nil
}

compressions := []string{"gz", "xz", "zst"}
repositories := []string{"main", "testing", "with/slash", ""}
Expand Down Expand Up @@ -171,35 +201,6 @@ license = MIT`)
MakeRequest(t, req, http.StatusConflict)
})

readIndexContent := func(r io.Reader) (map[string]string, error) {
gzr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}

content := make(map[string]string)

tr := tar.NewReader(gzr)
for {
hd, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

buf, err := io.ReadAll(tr)
if err != nil {
return nil, err
}

content[hd.Name] = string(buf)
}

return content, nil
}

t.Run("Index", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()

Expand Down Expand Up @@ -299,4 +300,40 @@ license = MIT`)
})
}
}
t.Run("KeepLastVersion", func(t *testing.T) {
defer tests.PrintCurrentTest(t)()
defer test.MockVariableValue(&setting.Packages.RepoLatestVersionArch, true)()
pkgVer1 := createPackage("gz", "gitea-test", "1.0.0", "aarch64")
pkgVer2 := createPackage("gz", "gitea-test", "1.0.1", "aarch64")
req := NewRequestWithBody(t, "PUT", rootURL, bytes.NewReader(pkgVer1)).
AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusCreated)
req = NewRequestWithBody(t, "PUT", rootURL, bytes.NewReader(pkgVer2)).
AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusCreated)

req = NewRequest(t, "GET", fmt.Sprintf("%s/aarch64/%s", rootURL, arch_service.IndexArchiveFilename))
resp := MakeRequest(t, req, http.StatusOK)

content, err := readIndexContent(resp.Body)
assert.NoError(t, err)
assert.Len(t, content, 2)

_, has := content["gitea-test-1.0.0/desc"]
assert.False(t, has)
_, has = content["gitea-test-1.0.1/desc"]
assert.True(t, has)

req = NewRequest(t, "DELETE", fmt.Sprintf("%s/gitea-test/1.0.1/aarch64", rootURL)).
AddBasicAuth(user.Name)
MakeRequest(t, req, http.StatusNoContent)

req = NewRequest(t, "GET", fmt.Sprintf("%s/aarch64/%s", rootURL, arch_service.IndexArchiveFilename))
resp = MakeRequest(t, req, http.StatusOK)
content, err = readIndexContent(resp.Body)
assert.NoError(t, err)
assert.Len(t, content, 2)
_, has = content["gitea-test-1.0.0/desc"]
assert.True(t, has)
})
}

0 comments on commit 3b55815

Please sign in to comment.