Merge pull request '[v9.0/forgejo] fix: handle renamed dependency for cargo registry' (#5945) from bp-v9.0/forgejo-bb93d3e into v9.0/forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5945
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
Otto 2024-11-13 23:13:42 +00:00
commit 86496d701d
2 changed files with 37 additions and 7 deletions

View file

@ -96,7 +96,7 @@ func parsePackage(r io.Reader) (*Package, error) {
Target *string `json:"target"`
Kind string `json:"kind"`
Registry *string `json:"registry"`
ExplicitNameInToml string `json:"explicit_name_in_toml"`
ExplicitNameInToml *string `json:"explicit_name_in_toml"`
} `json:"deps"`
Features map[string][]string `json:"features"`
Authors []string `json:"authors"`
@ -136,8 +136,16 @@ func parsePackage(r io.Reader) (*Package, error) {
dependencies := make([]*Dependency, 0, len(meta.Deps))
for _, dep := range meta.Deps {
name := dep.Name
packageName := dep.ExplicitNameInToml
// If the explicit_name_in_toml field is set, the package is renamed and
// should be set accordingly.
if dep.ExplicitNameInToml != nil {
name = *dep.ExplicitNameInToml
packageName = &dep.Name
}
dependencies = append(dependencies, &Dependency{
Name: dep.Name,
Name: name,
Req: dep.VersionReq,
Features: dep.Features,
Optional: dep.Optional,
@ -145,6 +153,7 @@ func parsePackage(r io.Reader) (*Package, error) {
Target: dep.Target,
Kind: dep.Kind,
Registry: dep.Registry,
Package: packageName,
})
}

View file

@ -22,7 +22,7 @@ const (
)
func TestParsePackage(t *testing.T) {
createPackage := func(name, version string) io.Reader {
createPackage := func(name, version, dependency string) io.Reader {
metadata := `{
"name":"` + name + `",
"vers":"` + version + `",
@ -32,7 +32,7 @@ func TestParsePackage(t *testing.T) {
{
"name":"dep",
"version_req":"1.0"
}
}` + dependency + `
],
"homepage":"` + homepage + `",
"license":"` + license + `"
@ -48,7 +48,7 @@ func TestParsePackage(t *testing.T) {
t.Run("InvalidName", func(t *testing.T) {
for _, name := range []string{"", "0test", "-test", "_test", strings.Repeat("a", 65)} {
data := createPackage(name, "1.0.0")
data := createPackage(name, "1.0.0", "")
cp, err := ParsePackage(data)
assert.Nil(t, cp)
@ -58,7 +58,7 @@ func TestParsePackage(t *testing.T) {
t.Run("InvalidVersion", func(t *testing.T) {
for _, version := range []string{"", "1.", "-1.0", "1.0.0/1"} {
data := createPackage("test", version)
data := createPackage("test", version, "")
cp, err := ParsePackage(data)
assert.Nil(t, cp)
@ -67,7 +67,7 @@ func TestParsePackage(t *testing.T) {
})
t.Run("Valid", func(t *testing.T) {
data := createPackage("test", "1.0.0")
data := createPackage("test", "1.0.0", "")
cp, err := ParsePackage(data)
assert.NotNil(t, cp)
@ -84,4 +84,25 @@ func TestParsePackage(t *testing.T) {
content, _ := io.ReadAll(cp.Content)
assert.Equal(t, "test", string(content))
})
t.Run("Renamed dependency", func(t *testing.T) {
data := createPackage("test", "1.0.0", `, {"name":"v4l2-sys", "version":"0.3.0", "explicit_name_in_toml":"v4l2-sys-mit"}`)
cp, err := ParsePackage(data)
assert.NotNil(t, cp)
require.NoError(t, err)
assert.Equal(t, "test", cp.Name)
assert.Equal(t, "1.0.0", cp.Version)
assert.Equal(t, description, cp.Metadata.Description)
assert.Equal(t, []string{author}, cp.Metadata.Authors)
assert.Len(t, cp.Metadata.Dependencies, 2)
assert.Equal(t, "dep", cp.Metadata.Dependencies[0].Name)
assert.EqualValues(t, "v4l2-sys-mit", cp.Metadata.Dependencies[1].Name)
assert.EqualValues(t, "v4l2-sys", *cp.Metadata.Dependencies[1].Package)
assert.Equal(t, homepage, cp.Metadata.ProjectURL)
assert.Equal(t, license, cp.Metadata.License)
content, _ := io.ReadAll(cp.Content)
assert.Equal(t, "test", string(content))
})
}