-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dotnet): add license support for NuGet (#5217)
* add nuspec files support * docs: docs, log messages, comments refactoring * save found licences to use next time * refactor * refactor * fix typo
- Loading branch information
1 parent
5c18475
commit 3dd5b1e
Showing
10 changed files
with
349 additions
and
51 deletions.
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.