diff --git a/modules/packages/cargo/parser.go b/modules/packages/cargo/parser.go index 36cd44df84..a09cfc1f73 100644 --- a/modules/packages/cargo/parser.go +++ b/modules/packages/cargo/parser.go @@ -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, }) } diff --git a/modules/packages/cargo/parser_test.go b/modules/packages/cargo/parser_test.go index 4b357cb869..8792a7a977 100644 --- a/modules/packages/cargo/parser_test.go +++ b/modules/packages/cargo/parser_test.go @@ -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)) + }) }