Skip to content

Commit

Permalink
refactor: sort dependsOn
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 committed Nov 21, 2024
1 parent 4bfc5b6 commit 5857f67
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
16 changes: 9 additions & 7 deletions pkg/dependency/parser/golang/binary/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/golang/binary/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func TestParse(t *testing.T) {
{
ID: "github.com/aquasecurity/test",
DependsOn: []string{
"[email protected]",
"github.com/aquasecurity/[email protected]",
"github.com/aquasecurity/[email protected]",
"golang.org/x/[email protected]",
"[email protected]",
},
},
}
Expand Down Expand Up @@ -109,9 +109,9 @@ func TestParse(t *testing.T) {
{
ID: "github.com/ebati/trivy-mod-parse",
DependsOn: []string{
"[email protected]",
"github.com/davecgh/[email protected]",
"github.com/go-sql-driver/[email protected]",
"[email protected]",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/golang/binary/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func Test_gobinaryLibraryAnalyzer_Analyze(t *testing.T) {
Version: "",
Relationship: types.RelationshipRoot,
DependsOn: []string{
"[email protected]",
"github.com/aquasecurity/[email protected]",
"github.com/aquasecurity/[email protected]",
"golang.org/x/[email protected]",
"[email protected]",
},
},
{
Expand Down

0 comments on commit 5857f67

Please sign in to comment.