Backport #32430 by usbalbin Co-authored-by: Albin Hedman <albin9604@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
eb5733636b
commit
b48df1082e
@ -136,8 +136,16 @@ func parsePackage(r io.Reader) (*Package, error) {
|
|||||||
|
|
||||||
dependencies := make([]*Dependency, 0, len(meta.Deps))
|
dependencies := make([]*Dependency, 0, len(meta.Deps))
|
||||||
for _, dep := range meta.Deps {
|
for _, dep := range meta.Deps {
|
||||||
|
// https://doc.rust-lang.org/cargo/reference/registry-web-api.html#publish
|
||||||
|
// It is a string of the new package name if the dependency is renamed, otherwise empty
|
||||||
|
name := dep.ExplicitNameInToml
|
||||||
|
pkg := &dep.Name
|
||||||
|
if name == "" {
|
||||||
|
name = dep.Name
|
||||||
|
pkg = nil
|
||||||
|
}
|
||||||
dependencies = append(dependencies, &Dependency{
|
dependencies = append(dependencies, &Dependency{
|
||||||
Name: dep.Name,
|
Name: name,
|
||||||
Req: dep.VersionReq,
|
Req: dep.VersionReq,
|
||||||
Features: dep.Features,
|
Features: dep.Features,
|
||||||
Optional: dep.Optional,
|
Optional: dep.Optional,
|
||||||
@ -145,6 +153,7 @@ func parsePackage(r io.Reader) (*Package, error) {
|
|||||||
Target: dep.Target,
|
Target: dep.Target,
|
||||||
Kind: dep.Kind,
|
Kind: dep.Kind,
|
||||||
Registry: dep.Registry,
|
Registry: dep.Registry,
|
||||||
|
Package: pkg,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,16 +13,16 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
description = "Package Description"
|
|
||||||
author = "KN4CK3R"
|
|
||||||
homepage = "https://gitea.io/"
|
|
||||||
license = "MIT"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestParsePackage(t *testing.T) {
|
func TestParsePackage(t *testing.T) {
|
||||||
createPackage := func(name, version string) io.Reader {
|
const (
|
||||||
metadata := `{
|
description = "Package Description"
|
||||||
|
author = "KN4CK3R"
|
||||||
|
homepage = "https://gitea.io/"
|
||||||
|
license = "MIT"
|
||||||
|
payload = "gitea test dummy payload" // a fake payload for test only
|
||||||
|
)
|
||||||
|
makeDefaultPackageMeta := func(name, version string) string {
|
||||||
|
return `{
|
||||||
"name":"` + name + `",
|
"name":"` + name + `",
|
||||||
"vers":"` + version + `",
|
"vers":"` + version + `",
|
||||||
"description":"` + description + `",
|
"description":"` + description + `",
|
||||||
@ -36,18 +36,19 @@ func TestParsePackage(t *testing.T) {
|
|||||||
"homepage":"` + homepage + `",
|
"homepage":"` + homepage + `",
|
||||||
"license":"` + license + `"
|
"license":"` + license + `"
|
||||||
}`
|
}`
|
||||||
|
}
|
||||||
|
createPackage := func(metadata string) io.Reader {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
binary.Write(&buf, binary.LittleEndian, uint32(len(metadata)))
|
binary.Write(&buf, binary.LittleEndian, uint32(len(metadata)))
|
||||||
buf.WriteString(metadata)
|
buf.WriteString(metadata)
|
||||||
binary.Write(&buf, binary.LittleEndian, uint32(4))
|
binary.Write(&buf, binary.LittleEndian, uint32(len(payload)))
|
||||||
buf.WriteString("test")
|
buf.WriteString(payload)
|
||||||
return &buf
|
return &buf
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("InvalidName", func(t *testing.T) {
|
t.Run("InvalidName", func(t *testing.T) {
|
||||||
for _, name := range []string{"", "0test", "-test", "_test", strings.Repeat("a", 65)} {
|
for _, name := range []string{"", "0test", "-test", "_test", strings.Repeat("a", 65)} {
|
||||||
data := createPackage(name, "1.0.0")
|
data := createPackage(makeDefaultPackageMeta(name, "1.0.0"))
|
||||||
|
|
||||||
cp, err := ParsePackage(data)
|
cp, err := ParsePackage(data)
|
||||||
assert.Nil(t, cp)
|
assert.Nil(t, cp)
|
||||||
@ -57,7 +58,7 @@ func TestParsePackage(t *testing.T) {
|
|||||||
|
|
||||||
t.Run("InvalidVersion", func(t *testing.T) {
|
t.Run("InvalidVersion", func(t *testing.T) {
|
||||||
for _, version := range []string{"", "1.", "-1.0", "1.0.0/1"} {
|
for _, version := range []string{"", "1.", "-1.0", "1.0.0/1"} {
|
||||||
data := createPackage("test", version)
|
data := createPackage(makeDefaultPackageMeta("test", version))
|
||||||
|
|
||||||
cp, err := ParsePackage(data)
|
cp, err := ParsePackage(data)
|
||||||
assert.Nil(t, cp)
|
assert.Nil(t, cp)
|
||||||
@ -66,7 +67,7 @@ func TestParsePackage(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Valid", func(t *testing.T) {
|
t.Run("Valid", func(t *testing.T) {
|
||||||
data := createPackage("test", "1.0.0")
|
data := createPackage(makeDefaultPackageMeta("test", "1.0.0"))
|
||||||
|
|
||||||
cp, err := ParsePackage(data)
|
cp, err := ParsePackage(data)
|
||||||
assert.NotNil(t, cp)
|
assert.NotNil(t, cp)
|
||||||
@ -78,9 +79,34 @@ func TestParsePackage(t *testing.T) {
|
|||||||
assert.Equal(t, []string{author}, cp.Metadata.Authors)
|
assert.Equal(t, []string{author}, cp.Metadata.Authors)
|
||||||
assert.Len(t, cp.Metadata.Dependencies, 1)
|
assert.Len(t, cp.Metadata.Dependencies, 1)
|
||||||
assert.Equal(t, "dep", cp.Metadata.Dependencies[0].Name)
|
assert.Equal(t, "dep", cp.Metadata.Dependencies[0].Name)
|
||||||
|
assert.Nil(t, cp.Metadata.Dependencies[0].Package)
|
||||||
assert.Equal(t, homepage, cp.Metadata.ProjectURL)
|
assert.Equal(t, homepage, cp.Metadata.ProjectURL)
|
||||||
assert.Equal(t, license, cp.Metadata.License)
|
assert.Equal(t, license, cp.Metadata.License)
|
||||||
content, _ := io.ReadAll(cp.Content)
|
content, _ := io.ReadAll(cp.Content)
|
||||||
assert.Equal(t, "test", string(content))
|
assert.Equal(t, payload, string(content))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("Renamed", func(t *testing.T) {
|
||||||
|
data := createPackage(`{
|
||||||
|
"name":"test-pkg",
|
||||||
|
"vers":"1.0",
|
||||||
|
"description":"test-desc",
|
||||||
|
"authors": ["test-author"],
|
||||||
|
"deps":[
|
||||||
|
{
|
||||||
|
"name":"dep-renamed",
|
||||||
|
"explicit_name_in_toml":"dep-explicit",
|
||||||
|
"version_req":"1.0"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"homepage":"https://gitea.io/",
|
||||||
|
"license":"MIT"
|
||||||
|
}`)
|
||||||
|
cp, err := ParsePackage(data)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "test-pkg", cp.Name)
|
||||||
|
assert.Equal(t, "https://gitea.io/", cp.Metadata.ProjectURL)
|
||||||
|
assert.Equal(t, "dep-explicit", cp.Metadata.Dependencies[0].Name)
|
||||||
|
assert.Equal(t, "dep-renamed", *cp.Metadata.Dependencies[0].Package)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user