From 5857f678b44a3504a332d15127a8d38eef8a5401 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Thu, 21 Nov 2024 14:23:11 +0400 Subject: [PATCH] refactor: sort dependsOn Signed-off-by: knqyf263 --- pkg/dependency/parser/golang/binary/parse.go | 16 +++++++++------- .../parser/golang/binary/parse_test.go | 4 ++-- .../language/golang/binary/binary_test.go | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/pkg/dependency/parser/golang/binary/parse.go b/pkg/dependency/parser/golang/binary/parse.go index edc2bb37a1ff..617303dd5497 100644 --- a/pkg/dependency/parser/golang/binary/parse.go +++ b/pkg/dependency/parser/golang/binary/parse.go @@ -93,7 +93,6 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc Relationship: ftypes.RelationshipUnknown, }) } - sort.Sort(pkgs) // There are times when gobinaries don't contain Main information. // e.g. `Go` binaries (e.g. `go`, `gofmt`, etc.) @@ -111,20 +110,23 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc Version: version, Relationship: ftypes.RelationshipRoot, } + + depIDs := lo.Map(pkgs, func(pkg ftypes.Package, _ int) string { + return pkg.ID + }) + sort.Strings(depIDs) + deps = []ftypes.Dependency{ { - ID: root.ID, - // Consider all packages as dependencies of the main module. - DependsOn: lo.Map(pkgs, func(pkg ftypes.Package, _ int) string { - return pkg.ID - }), + ID: root.ID, + DependsOn: depIDs, // Consider all packages as dependencies of the main module. }, } // Add main module pkgs = append(pkgs, root) - sort.Sort(pkgs) } + sort.Sort(pkgs) return pkgs, deps, nil } diff --git a/pkg/dependency/parser/golang/binary/parse_test.go b/pkg/dependency/parser/golang/binary/parse_test.go index 8efbad64bd51..eebb652a2a4d 100644 --- a/pkg/dependency/parser/golang/binary/parse_test.go +++ b/pkg/dependency/parser/golang/binary/parse_test.go @@ -45,10 +45,10 @@ func TestParse(t *testing.T) { { ID: "github.com/aquasecurity/test", DependsOn: []string{ - "stdlib@v1.15.2", "github.com/aquasecurity/go-pep440-version@v0.0.0-20210121094942-22b2f8951d46", "github.com/aquasecurity/go-version@v0.0.0-20210121072130-637058cfe492", "golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1", + "stdlib@v1.15.2", }, }, } @@ -109,9 +109,9 @@ func TestParse(t *testing.T) { { ID: "github.com/ebati/trivy-mod-parse", DependsOn: []string{ - "stdlib@v1.16.4", "github.com/davecgh/go-spew@v1.1.1", "github.com/go-sql-driver/mysql@v1.5.0", + "stdlib@v1.16.4", }, }, }, diff --git a/pkg/fanal/analyzer/language/golang/binary/binary_test.go b/pkg/fanal/analyzer/language/golang/binary/binary_test.go index b426f9a349ba..650968667e06 100644 --- a/pkg/fanal/analyzer/language/golang/binary/binary_test.go +++ b/pkg/fanal/analyzer/language/golang/binary/binary_test.go @@ -35,10 +35,10 @@ func Test_gobinaryLibraryAnalyzer_Analyze(t *testing.T) { Version: "", Relationship: types.RelationshipRoot, DependsOn: []string{ - "stdlib@v1.15.2", "github.com/aquasecurity/go-pep440-version@v0.0.0-20210121094942-22b2f8951d46", "github.com/aquasecurity/go-version@v0.0.0-20210121072130-637058cfe492", "golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1", + "stdlib@v1.15.2", }, }, {