mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-12-11 15:22:33 +01:00
4de909747b
Some checks are pending
/ release (push) Waiting to run
testing / test-remote-cacher (map[image:redis:7.2 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (push) Blocked by required conditions
testing / test-mysql (push) Blocked by required conditions
testing / test-pgsql (push) Blocked by required conditions
testing / backend-checks (push) Waiting to run
testing / frontend-checks (push) Waiting to run
testing / test-unit (push) Blocked by required conditions
testing / test-remote-cacher (map[image:docker.io/valkey/valkey:7.2.5-alpine3.19 port:6379]) (push) Blocked by required conditions
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (push) Blocked by required conditions
testing / test-sqlite (push) Blocked by required conditions
testing / security-check (push) Blocked by required conditions
go-require lint is ignored for now Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4535 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-committed-by: TheFox0x7 <thefox0x7@gmail.com>
151 lines
4.2 KiB
Go
151 lines
4.2 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package conda
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/dsnet/compress/bzip2"
|
|
"github.com/klauspost/compress/zstd"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
const (
|
|
packageName = "gitea"
|
|
packageVersion = "1.0.1"
|
|
description = "Package Description"
|
|
projectURL = "https://gitea.com"
|
|
repositoryURL = "https://gitea.com/gitea/gitea"
|
|
documentationURL = "https://docs.gitea.com"
|
|
)
|
|
|
|
func TestParsePackage(t *testing.T) {
|
|
createArchive := func(files map[string][]byte) *bytes.Buffer {
|
|
var buf bytes.Buffer
|
|
tw := tar.NewWriter(&buf)
|
|
for filename, content := range files {
|
|
hdr := &tar.Header{
|
|
Name: filename,
|
|
Mode: 0o600,
|
|
Size: int64(len(content)),
|
|
}
|
|
tw.WriteHeader(hdr)
|
|
tw.Write(content)
|
|
}
|
|
tw.Close()
|
|
return &buf
|
|
}
|
|
|
|
t.Run("MissingIndexFile", func(t *testing.T) {
|
|
buf := createArchive(map[string][]byte{"dummy.txt": {}})
|
|
|
|
p, err := parsePackageTar(buf)
|
|
assert.Nil(t, p)
|
|
require.ErrorIs(t, err, ErrInvalidStructure)
|
|
})
|
|
|
|
t.Run("MissingAboutFile", func(t *testing.T) {
|
|
buf := createArchive(map[string][]byte{"info/index.json": []byte(`{"name":"name","version":"1.0"}`)})
|
|
|
|
p, err := parsePackageTar(buf)
|
|
assert.NotNil(t, p)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "name", p.Name)
|
|
assert.Equal(t, "1.0", p.Version)
|
|
assert.Empty(t, p.VersionMetadata.ProjectURL)
|
|
})
|
|
|
|
t.Run("InvalidName", func(t *testing.T) {
|
|
for _, name := range []string{"", "name!", "nAMe"} {
|
|
buf := createArchive(map[string][]byte{"info/index.json": []byte(`{"name":"` + name + `","version":"1.0"}`)})
|
|
|
|
p, err := parsePackageTar(buf)
|
|
assert.Nil(t, p)
|
|
require.ErrorIs(t, err, ErrInvalidName)
|
|
}
|
|
})
|
|
|
|
t.Run("InvalidVersion", func(t *testing.T) {
|
|
for _, version := range []string{"", "1.0-2"} {
|
|
buf := createArchive(map[string][]byte{"info/index.json": []byte(`{"name":"name","version":"` + version + `"}`)})
|
|
|
|
p, err := parsePackageTar(buf)
|
|
assert.Nil(t, p)
|
|
require.ErrorIs(t, err, ErrInvalidVersion)
|
|
}
|
|
})
|
|
|
|
t.Run("Valid", func(t *testing.T) {
|
|
buf := createArchive(map[string][]byte{
|
|
"info/index.json": []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `","subdir":"linux-64"}`),
|
|
"info/about.json": []byte(`{"description":"` + description + `","dev_url":"` + repositoryURL + `","doc_url":"` + documentationURL + `","home":"` + projectURL + `"}`),
|
|
})
|
|
|
|
p, err := parsePackageTar(buf)
|
|
assert.NotNil(t, p)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, packageName, p.Name)
|
|
assert.Equal(t, packageVersion, p.Version)
|
|
assert.Equal(t, "linux-64", p.Subdir)
|
|
assert.Equal(t, description, p.VersionMetadata.Description)
|
|
assert.Equal(t, projectURL, p.VersionMetadata.ProjectURL)
|
|
assert.Equal(t, repositoryURL, p.VersionMetadata.RepositoryURL)
|
|
assert.Equal(t, documentationURL, p.VersionMetadata.DocumentationURL)
|
|
})
|
|
|
|
t.Run(".tar.bz2", func(t *testing.T) {
|
|
tarArchive := createArchive(map[string][]byte{
|
|
"info/index.json": []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `"}`),
|
|
})
|
|
|
|
var buf bytes.Buffer
|
|
bw, _ := bzip2.NewWriter(&buf, nil)
|
|
io.Copy(bw, tarArchive)
|
|
bw.Close()
|
|
|
|
br := bytes.NewReader(buf.Bytes())
|
|
|
|
p, err := ParsePackageBZ2(br)
|
|
assert.NotNil(t, p)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, packageName, p.Name)
|
|
assert.Equal(t, packageVersion, p.Version)
|
|
assert.False(t, p.FileMetadata.IsCondaPackage)
|
|
})
|
|
|
|
t.Run(".conda", func(t *testing.T) {
|
|
tarArchive := createArchive(map[string][]byte{
|
|
"info/index.json": []byte(`{"name":"` + packageName + `","version":"` + packageVersion + `"}`),
|
|
})
|
|
|
|
var infoBuf bytes.Buffer
|
|
zsw, _ := zstd.NewWriter(&infoBuf)
|
|
io.Copy(zsw, tarArchive)
|
|
zsw.Close()
|
|
|
|
var buf bytes.Buffer
|
|
zpw := zip.NewWriter(&buf)
|
|
w, _ := zpw.Create("info-x.tar.zst")
|
|
w.Write(infoBuf.Bytes())
|
|
zpw.Close()
|
|
|
|
br := bytes.NewReader(buf.Bytes())
|
|
|
|
p, err := ParsePackageConda(br, int64(br.Len()))
|
|
assert.NotNil(t, p)
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, packageName, p.Name)
|
|
assert.Equal(t, packageVersion, p.Version)
|
|
assert.True(t, p.FileMetadata.IsCondaPackage)
|
|
})
|
|
}
|