-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dotnet): add license support for NuGet #5217
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
52ebd32
add nuspec files support
DmitriyLewen 07d4a3c
docs: docs, log messages, comments refactoring
DmitriyLewen 1fa542f
save found licences to use next time
DmitriyLewen 21eb6d8
refactor
DmitriyLewen d1d4397
Merge branch 'main' of github.com:DmitriyLewen/trivy into feat/nuget-…
DmitriyLewen 1d0fed1
refactor
DmitriyLewen d45fb32
fix typo
DmitriyLewen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ package nuget | |
import ( | ||
"context" | ||
"os" | ||
"sort" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
@@ -15,19 +14,22 @@ import ( | |
|
||
func Test_nugetibraryAnalyzer_Analyze(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputFile string | ||
want *analyzer.AnalysisResult | ||
wantErr string | ||
name string | ||
dir string | ||
env map[string]string | ||
want *analyzer.AnalysisResult | ||
}{ | ||
{ | ||
name: "happy path config file", | ||
inputFile: "testdata/packages.config", | ||
name: "happy path config file.", | ||
dir: "testdata/config", | ||
env: map[string]string{ | ||
"HOME": "testdata/repository", | ||
}, | ||
want: &analyzer.AnalysisResult{ | ||
Applications: []types.Application{ | ||
{ | ||
Type: types.NuGet, | ||
FilePath: "testdata/packages.config", | ||
FilePath: "packages.config", | ||
Libraries: types.Packages{ | ||
{ | ||
Name: "Microsoft.AspNet.WebApi", | ||
|
@@ -43,13 +45,57 @@ func Test_nugetibraryAnalyzer_Analyze(t *testing.T) { | |
}, | ||
}, | ||
{ | ||
name: "happy path lock file", | ||
inputFile: "testdata/packages.lock.json", | ||
name: "happy path lock file.", | ||
dir: "testdata/lock", | ||
env: map[string]string{ | ||
"HOME": "testdata/repository", | ||
}, | ||
want: &analyzer.AnalysisResult{ | ||
Applications: []types.Application{ | ||
{ | ||
Type: types.NuGet, | ||
FilePath: "packages.lock.json", | ||
Libraries: types.Packages{ | ||
{ | ||
ID: "[email protected]", | ||
Name: "Newtonsoft.Json", | ||
Version: "12.0.3", | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 5, | ||
EndLine: 10, | ||
}, | ||
}, | ||
Licenses: []string{"MIT"}, | ||
}, | ||
{ | ||
ID: "[email protected]", | ||
Name: "NuGet.Frameworks", | ||
Version: "5.7.0", | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 11, | ||
EndLine: 19, | ||
}, | ||
}, | ||
DependsOn: []string{"[email protected]"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "happy path lock file. `NUGET_PACKAGES` env is used", | ||
dir: "testdata/lock", | ||
env: map[string]string{ | ||
"NUGET_PACKAGES": "testdata/repository/.nuget/packages", | ||
}, | ||
want: &analyzer.AnalysisResult{ | ||
Applications: []types.Application{ | ||
{ | ||
Type: types.NuGet, | ||
FilePath: "testdata/packages.lock.json", | ||
FilePath: "packages.lock.json", | ||
Libraries: types.Packages{ | ||
{ | ||
ID: "[email protected]", | ||
|
@@ -61,6 +107,7 @@ func Test_nugetibraryAnalyzer_Analyze(t *testing.T) { | |
EndLine: 10, | ||
}, | ||
}, | ||
Licenses: []string{"MIT"}, | ||
}, | ||
{ | ||
ID: "[email protected]", | ||
|
@@ -80,35 +127,58 @@ func Test_nugetibraryAnalyzer_Analyze(t *testing.T) { | |
}, | ||
}, | ||
{ | ||
name: "sad path", | ||
inputFile: "testdata/invalid.txt", | ||
wantErr: "NuGet analysis error", | ||
name: "happy path lock file. `.nuget` directory doesn't exist", | ||
dir: "testdata/lock", | ||
env: map[string]string{ | ||
"HOME": "testdata/invalid", | ||
}, | ||
want: &analyzer.AnalysisResult{ | ||
Applications: []types.Application{ | ||
{ | ||
Type: types.NuGet, | ||
FilePath: "packages.lock.json", | ||
Libraries: types.Packages{ | ||
{ | ||
ID: "[email protected]", | ||
Name: "Newtonsoft.Json", | ||
Version: "12.0.3", | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 5, | ||
EndLine: 10, | ||
}, | ||
}, | ||
}, | ||
{ | ||
ID: "[email protected]", | ||
Name: "NuGet.Frameworks", | ||
Version: "5.7.0", | ||
Locations: []types.Location{ | ||
{ | ||
StartLine: 11, | ||
EndLine: 19, | ||
}, | ||
}, | ||
DependsOn: []string{"[email protected]"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f, err := os.Open(tt.inputFile) | ||
for env, path := range tt.env { | ||
t.Setenv(env, path) | ||
} | ||
a, err := newNugetLibraryAnalyzer(analyzer.AnalyzerOptions{}) | ||
require.NoError(t, err) | ||
defer f.Close() | ||
|
||
a := nugetLibraryAnalyzer{} | ||
ctx := context.Background() | ||
got, err := a.Analyze(ctx, analyzer.AnalysisInput{ | ||
FilePath: tt.inputFile, | ||
Content: f, | ||
got, err := a.PostAnalyze(context.Background(), analyzer.PostAnalysisInput{ | ||
FS: os.DirFS(tt.dir), | ||
}) | ||
|
||
if tt.wantErr != "" { | ||
require.NotNil(t, err) | ||
assert.Contains(t, err.Error(), tt.wantErr) | ||
return | ||
} | ||
|
||
// Sort libraries for consistency | ||
for _, app := range got.Applications { | ||
sort.Sort(app.Libraries) | ||
} | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We might want to aggregate the statements.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! Thanks! Updated in 1d0fed1.