Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-22 09:54:24 +01:00

fix: handle renamed dependency for cargo registery

- When a dependency is renamed, specified via `package="actual-name"` in
Cargo.toml, this should become the name of the depedency when the
package is retrieved from the registery by cargo and the old name should
be available in the `package` field.
- The reference implementation also does this: 490e66a9d6/src/controllers/krate/publish.rs (L702-L705)
- Resolves #5936
- Unit test added.
This commit is contained in:
Gusted 2024-11-13 21:26:49 +01:00
parent 5614719e0a
commit bb93d3e6c8
No known key found for this signature in database
GPG key ID: FD821B732837125F
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))
})
}