Skip to content

Commit 0707569

Browse files
authored
refactor: replace with sortable packages (#4858)
1 parent fbe1c9e commit 0707569

File tree

43 files changed

+681
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+681
-455
lines changed

pkg/fanal/analyzer/analyzer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,7 @@ func (r *AnalysisResult) Sort() {
198198
})
199199

200200
for _, app := range r.Applications {
201-
sort.Slice(app.Libraries, func(i, j int) bool {
202-
if app.Libraries[i].Name != app.Libraries[j].Name {
203-
return app.Libraries[i].Name < app.Libraries[j].Name
204-
}
205-
return app.Libraries[i].Version < app.Libraries[j].Version
206-
})
201+
sort.Sort(app.Libraries)
207202
}
208203

209204
// Custom resources

pkg/fanal/analyzer/analyzer_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
5858
PackageInfos: []types.PackageInfo{
5959
{
6060
FilePath: "var/lib/dpkg/status.d/libc",
61-
Packages: []types.Package{
61+
Packages: types.Packages{
6262
{
6363
Name: "libc",
6464
Version: "1.2.3",
@@ -70,7 +70,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
7070
{
7171
Type: "bundler",
7272
FilePath: "app/Gemfile.lock",
73-
Libraries: []types.Package{
73+
Libraries: types.Packages{
7474
{
7575
Name: "rails",
7676
Version: "5.0.0",
@@ -84,7 +84,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
8484
PackageInfos: []types.PackageInfo{
8585
{
8686
FilePath: "var/lib/dpkg/status.d/openssl",
87-
Packages: []types.Package{
87+
Packages: types.Packages{
8888
{
8989
Name: "openssl",
9090
Version: "1.1.1",
@@ -96,7 +96,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
9696
{
9797
Type: "bundler",
9898
FilePath: "app2/Gemfile.lock",
99-
Libraries: []types.Package{
99+
Libraries: types.Packages{
100100
{
101101
Name: "nokogiri",
102102
Version: "1.0.0",
@@ -114,7 +114,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
114114
PackageInfos: []types.PackageInfo{
115115
{
116116
FilePath: "var/lib/dpkg/status.d/libc",
117-
Packages: []types.Package{
117+
Packages: types.Packages{
118118
{
119119
Name: "libc",
120120
Version: "1.2.3",
@@ -123,7 +123,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
123123
},
124124
{
125125
FilePath: "var/lib/dpkg/status.d/openssl",
126-
Packages: []types.Package{
126+
Packages: types.Packages{
127127
{
128128
Name: "openssl",
129129
Version: "1.1.1",
@@ -135,7 +135,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
135135
{
136136
Type: "bundler",
137137
FilePath: "app/Gemfile.lock",
138-
Libraries: []types.Package{
138+
Libraries: types.Packages{
139139
{
140140
Name: "rails",
141141
Version: "5.0.0",
@@ -145,7 +145,7 @@ func TestAnalysisResult_Merge(t *testing.T) {
145145
{
146146
Type: "bundler",
147147
FilePath: "app2/Gemfile.lock",
148-
Libraries: []types.Package{
148+
Libraries: types.Packages{
149149
{
150150
Name: "nokogiri",
151151
Version: "1.0.0",
@@ -335,7 +335,7 @@ func TestAnalyzerGroup_AnalyzeFile(t *testing.T) {
335335
PackageInfos: []types.PackageInfo{
336336
{
337337
FilePath: "/lib/apk/db/installed",
338-
Packages: []types.Package{
338+
Packages: types.Packages{
339339
{
340340
341341
Name: "musl",
@@ -375,7 +375,7 @@ func TestAnalyzerGroup_AnalyzeFile(t *testing.T) {
375375
{
376376
Type: "bundler",
377377
FilePath: "/app/Gemfile.lock",
378-
Libraries: []types.Package{
378+
Libraries: types.Packages{
379379
{
380380
381381
Name: "actioncable",
@@ -436,7 +436,7 @@ func TestAnalyzerGroup_AnalyzeFile(t *testing.T) {
436436
{
437437
Type: "bundler",
438438
FilePath: "/app/Gemfile-dev.lock",
439-
Libraries: []types.Package{
439+
Libraries: types.Packages{
440440
{
441441
442442
Name: "actioncable",
@@ -569,7 +569,7 @@ func TestAnalyzerGroup_PostAnalyze(t *testing.T) {
569569
{
570570
Type: string(analyzer.TypeJar),
571571
FilePath: "testdata/post-apps/jar/jackson-annotations-2.15.0-rc2.jar",
572-
Libraries: []types.Package{
572+
Libraries: types.Packages{
573573
{
574574
Name: "com.fasterxml.jackson.core:jackson-annotations",
575575
Version: "2.15.0-rc2",
@@ -589,7 +589,7 @@ func TestAnalyzerGroup_PostAnalyze(t *testing.T) {
589589
{
590590
Type: string(analyzer.TypePoetry),
591591
FilePath: "testdata/post-apps/poetry/happy/poetry.lock",
592-
Libraries: []types.Package{
592+
Libraries: types.Packages{
593593
{
594594
595595
Name: "certifi",

pkg/fanal/analyzer/language/analyze_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ func (p *mockParser) Parse(r dio.ReadSeekerAt) ([]godeptypes.Library, []godeptyp
2626

2727
switch string(b) {
2828
case "happy":
29-
return []godeptypes.Library{{Name: "test", Version: "1.2.3"}}, nil, nil
29+
return []godeptypes.Library{
30+
{
31+
Name: "test",
32+
Version: "1.2.3",
33+
},
34+
}, nil, nil
3035
case "sad":
3136
return nil, nil, xerrors.New("unexpected error")
3237
}
@@ -58,7 +63,7 @@ func TestAnalyze(t *testing.T) {
5863
{
5964
Type: types.GoBinary,
6065
FilePath: "app/myweb",
61-
Libraries: []types.Package{
66+
Libraries: types.Packages{
6267
{
6368
Name: "test",
6469
Version: "1.2.3",

pkg/fanal/analyzer/language/c/conan/conan_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_conanLockAnalyzer_Analyze(t *testing.T) {
2727
{
2828
Type: types.Conan,
2929
FilePath: "testdata/happy.lock",
30-
Libraries: []types.Package{
30+
Libraries: types.Packages{
3131
{
3232
ID: "openssl/3.0.5",
3333
Name: "openssl",
@@ -67,9 +67,7 @@ func Test_conanLockAnalyzer_Analyze(t *testing.T) {
6767

6868
if got != nil {
6969
for _, app := range got.Applications {
70-
sort.Slice(app.Libraries, func(i, j int) bool {
71-
return app.Libraries[i].ID < app.Libraries[j].ID
72-
})
70+
sort.Sort(app.Libraries)
7371
}
7472
}
7573

pkg/fanal/analyzer/language/conda/meta/meta_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_packagingAnalyzer_Analyze(t *testing.T) {
2727
{
2828
Type: types.CondaPkg,
2929
FilePath: "testdata/pip-22.2.2-py38h06a4308_0.json",
30-
Libraries: []types.Package{
30+
Libraries: types.Packages{
3131
{
3232
Name: "pip",
3333
Version: "22.2.2",

pkg/fanal/analyzer/language/dart/pub/pubspec_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Test_pubSpecLockAnalyzer_Analyze(t *testing.T) {
2525
{
2626
Type: types.Pub,
2727
FilePath: "testdata/happy.lock",
28-
Libraries: []types.Package{
28+
Libraries: types.Packages{
2929
{
3030
3131
Name: "crypto",
@@ -74,9 +74,7 @@ func Test_pubSpecLockAnalyzer_Analyze(t *testing.T) {
7474

7575
if got != nil {
7676
for _, app := range got.Applications {
77-
sort.Slice(app.Libraries, func(i, j int) bool {
78-
return app.Libraries[i].ID < app.Libraries[j].ID
79-
})
77+
sort.Sort(app.Libraries)
8078
}
8179
}
8280

pkg/fanal/analyzer/language/dotnet/deps/deps_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ func Test_depsLibraryAnalyzer_Analyze(t *testing.T) {
2727
{
2828
Type: types.DotNetCore,
2929
FilePath: "testdata/datacollector.deps.json",
30-
Libraries: []types.Package{
30+
Libraries: types.Packages{
3131
{
32-
Name: "Newtonsoft.Json",
33-
Version: "9.0.1",
34-
Locations: []types.Location{{StartLine: 8, EndLine: 14}},
32+
Name: "Newtonsoft.Json",
33+
Version: "9.0.1",
34+
Locations: []types.Location{
35+
{
36+
StartLine: 8,
37+
EndLine: 14,
38+
},
39+
},
3540
},
3641
},
3742
},

pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Test_nugetibraryAnalyzer_Analyze(t *testing.T) {
2828
{
2929
Type: types.NuGet,
3030
FilePath: "testdata/packages.config",
31-
Libraries: []types.Package{
31+
Libraries: types.Packages{
3232
{
3333
Name: "Microsoft.AspNet.WebApi",
3434
Version: "5.2.2",
@@ -50,18 +50,28 @@ func Test_nugetibraryAnalyzer_Analyze(t *testing.T) {
5050
{
5151
Type: types.NuGet,
5252
FilePath: "testdata/packages.lock.json",
53-
Libraries: []types.Package{
53+
Libraries: types.Packages{
5454
{
55-
56-
Name: "Newtonsoft.Json",
57-
Version: "12.0.3",
58-
Locations: []types.Location{{StartLine: 5, EndLine: 10}},
55+
56+
Name: "Newtonsoft.Json",
57+
Version: "12.0.3",
58+
Locations: []types.Location{
59+
{
60+
StartLine: 5,
61+
EndLine: 10,
62+
},
63+
},
5964
},
6065
{
61-
62-
Name: "NuGet.Frameworks",
63-
Version: "5.7.0",
64-
Locations: []types.Location{{StartLine: 11, EndLine: 19}},
66+
67+
Name: "NuGet.Frameworks",
68+
Version: "5.7.0",
69+
Locations: []types.Location{
70+
{
71+
StartLine: 11,
72+
EndLine: 19,
73+
},
74+
},
6575
DependsOn: []string{"[email protected]"},
6676
},
6777
},
@@ -96,9 +106,7 @@ func Test_nugetibraryAnalyzer_Analyze(t *testing.T) {
96106

97107
// Sort libraries for consistency
98108
for _, app := range got.Applications {
99-
sort.Slice(app.Libraries, func(i, j int) bool {
100-
return app.Libraries[i].Name < app.Libraries[j].Name
101-
})
109+
sort.Sort(app.Libraries)
102110
}
103111

104112
assert.NoError(t, err)

pkg/fanal/analyzer/language/elixir/mix/mix_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ func Test_mixLockAnalyzer_Analyze(t *testing.T) {
2323
{
2424
Type: types.Hex,
2525
FilePath: "testdata/happy.mix.lock",
26-
Libraries: []types.Package{
26+
Libraries: types.Packages{
2727
{
28-
29-
Name: "bunt",
30-
Version: "0.2.0",
31-
Locations: []types.Location{{StartLine: 2, EndLine: 2}},
28+
29+
Name: "bunt",
30+
Version: "0.2.0",
31+
Locations: []types.Location{
32+
{
33+
StartLine: 2,
34+
EndLine: 2,
35+
},
36+
},
3237
},
3338
},
3439
},

pkg/fanal/analyzer/language/golang/binary/binary_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Test_gobinaryLibraryAnalyzer_Analyze(t *testing.T) {
2828
{
2929
Type: types.GoBinary,
3030
FilePath: "testdata/executable_gobinary",
31-
Libraries: []types.Package{
31+
Libraries: types.Packages{
3232
{
3333
Name: "github.com/aquasecurity/go-pep440-version",
3434
Version: "v0.0.0-20210121094942-22b2f8951d46",

0 commit comments

Comments
 (0)