Skip to content

Commit 1ff53cd

Browse files
add tests
1 parent 342d5d9 commit 1ff53cd

File tree

3 files changed

+21
-34
lines changed

3 files changed

+21
-34
lines changed

modules/packages/arch/metadata_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,26 +140,6 @@ arch = x86_64
140140
}
141141

142142
func TestParsePackageInfo(t *testing.T) {
143-
const PKGINFO = `# Generated by makepkg 6.0.2
144-
# using fakeroot version 1.31
145-
pkgname = a
146-
pkgbase = b
147-
pkgver = 1-2
148-
pkgdesc = comment
149-
url = https://example.com/
150-
group = group
151-
builddate = 3
152-
packager = Name Surname <[email protected]>
153-
size = 5
154-
arch = x86_64
155-
license = BSD
156-
provides = pvd
157-
depend = smth
158-
optdepend = hex
159-
checkdepend = ola
160-
makedepend = cmake
161-
backup = usr/bin/paket1
162-
`
163143
p, err := ParsePackageInfo(strings.NewReader(`PKGINFO`))
164144
assert.NoError(t, err)
165145
assert.Equal(t, Package{

services/packages/arch/repository.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,13 @@ func flushDB(ctx context.Context, ownerID int64, distro, arch string) (*packages
170170
}
171171
gw := gzip.NewWriter(db)
172172
tw := tar.NewWriter(gw)
173-
defer tw.Close()
174-
defer gw.Close()
173+
count := 0
175174
for _, pkg := range pkgs {
176175
versions, err := packages_model.GetVersionsByPackageName(
177176
ctx, ownerID, packages_model.TypeArch, pkg.Name,
178177
)
179178
if err != nil {
180-
return nil, errors.Join(db.Close(), err)
179+
return nil, errors.Join(tw.Close(), gw.Close(), db.Close(), err)
181180
}
182181
sort.Slice(versions, func(i, j int) bool {
183182
return versions[i].CreatedUnix > versions[j].CreatedUnix
@@ -197,7 +196,7 @@ func flushDB(ctx context.Context, ownerID int64, distro, arch string) (*packages
197196
ctx, packages_model.PropertyTypeFile, pf.ID, arch_module.PropertyDescription,
198197
)
199198
if err != nil {
200-
return nil, errors.Join(db.Close(), err)
199+
return nil, errors.Join(tw.Close(), gw.Close(), db.Close(), err)
201200
}
202201
if len(pps) >= 1 {
203202
meta := []byte(pps[0].Value)
@@ -207,15 +206,21 @@ func flushDB(ctx context.Context, ownerID int64, distro, arch string) (*packages
207206
Mode: int64(os.ModePerm),
208207
}
209208
if err = tw.WriteHeader(header); err != nil {
210-
return nil, errors.Join(db.Close(), err)
209+
return nil, errors.Join(tw.Close(), gw.Close(), db.Close(), err)
211210
}
212211
if _, err := tw.Write(meta); err != nil {
213-
return nil, errors.Join(db.Close(), err)
212+
return nil, errors.Join(tw.Close(), gw.Close(), db.Close(), err)
214213
}
214+
count++
215215
break
216216
}
217217
}
218218
}
219+
defer gw.Close()
220+
defer tw.Close()
221+
if count == 0 {
222+
return nil, errors.Join(db.Close(), io.EOF)
223+
}
219224
return db, nil
220225
}
221226

tests/integration/api_packages_arch_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ import (
1010
"compress/gzip"
1111
"encoding/base64"
1212
"fmt"
13-
"github.com/ProtonMail/go-crypto/openpgp/armor"
14-
"github.com/ProtonMail/go-crypto/openpgp/packet"
15-
"github.com/pkg/errors"
1613
"io"
1714
"net/http"
1815
"strings"
@@ -26,6 +23,9 @@ import (
2623
arch_model "code.gitea.io/gitea/modules/packages/arch"
2724
"code.gitea.io/gitea/tests"
2825

26+
"github.com/ProtonMail/go-crypto/openpgp/armor"
27+
"github.com/ProtonMail/go-crypto/openpgp/packet"
28+
"github.com/pkg/errors"
2929
"github.com/stretchr/testify/assert"
3030
)
3131

@@ -245,11 +245,15 @@ nYAR`),
245245
req = NewRequestWithBody(t, "DELETE", rootURL+"/base/test2/1.0.0-1", nil).
246246
AddBasicAuth(user.Name)
247247
MakeRequest(t, req, http.StatusNoContent)
248-
249248
req = NewRequest(t, "GET", rootURL+"/base/x86_64/base.db")
250249
MakeRequest(t, req, http.StatusNotFound)
251-
})
252250

251+
req = NewRequest(t, "GET", rootURL+"/default/x86_64/base.db")
252+
respPkg = MakeRequest(t, req, http.StatusOK)
253+
files, err = listGzipFiles(respPkg.Body.Bytes())
254+
assert.NoError(t, err)
255+
assert.Equal(t, 1, len(files))
256+
})
253257
}
254258

255259
func getProperty(data, key string) string {
@@ -279,9 +283,7 @@ func listGzipFiles(data []byte) (fstest.MapFS, error) {
279283
if err == io.EOF {
280284
break
281285
} else if err != nil {
282-
if err != nil {
283-
return nil, err
284-
}
286+
return nil, err
285287
}
286288
if cur.Typeflag != tar.TypeReg {
287289
continue

0 commit comments

Comments
 (0)