forked from tomchavakis/nuget-license
-
-
Notifications
You must be signed in to change notification settings - Fork 31
Added CSV output format #468
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
Merged
sensslen
merged 8 commits into
sensslen:main
from
makeentosch:feature/csv_output_addition
Mar 23, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2042ca0
feature: csv output formatter implemented
makeentosch 22e2d7b
feature: tests implemented
makeentosch 1b97523
Merge branch 'main' into pr-468
sensslen 654f4e1
fix formatting and add proper verify files
sensslen efa17a5
Merge pull request #1 from sensslen/pr-468
makeentosch 72a4408
fix: changes after review applied
makeentosch 10fd50b
fix: explicit type to implicit one
makeentosch a5a72fa
fix: changes after review added
makeentosch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Licensed to the projects contributors. | ||
| // The license conditions are provided in the LICENSE file located in the project root | ||
|
|
||
| using System.Text; | ||
| using NuGetLicense.LicenseValidator; | ||
|
|
||
| namespace NuGetLicense.Output.Csv | ||
| { | ||
| public class CsvOutputFormatter : IOutputFormatter | ||
| { | ||
| private readonly bool _printErrorsOnly; | ||
| private readonly bool _skipIgnoredPackages; | ||
|
|
||
| public CsvOutputFormatter(bool printErrorsOnly, bool skipIgnoredPackages) | ||
| { | ||
| _printErrorsOnly = printErrorsOnly; | ||
| _skipIgnoredPackages = skipIgnoredPackages; | ||
| } | ||
|
|
||
| public async Task Write(Stream stream, IList<LicenseValidationResult> results) | ||
| { | ||
| if (_printErrorsOnly) | ||
| { | ||
| results = results.Where(r => r.ValidationErrors.Any()).ToList(); | ||
| } | ||
|
|
||
| if (_skipIgnoredPackages) | ||
| { | ||
| results = results | ||
| .Where(r => r.LicenseInformationOrigin != LicenseInformationOrigin.Ignored) | ||
| .ToList(); | ||
| } | ||
|
|
||
| using var writer = new StreamWriter(stream, new UTF8Encoding(false), bufferSize: 1024, leaveOpen: true); | ||
|
|
||
| await writer.WriteLineAsync( | ||
| "Package,Version,License Information Origin,License,License Url,Copyright,Authors,Package Project Url,Errors with Context"); | ||
|
|
||
| foreach (LicenseValidationResult license in results) | ||
| { | ||
| string[] row = new[] | ||
| { | ||
| EscapeCsvValue(license.PackageId), EscapeCsvValue(license.PackageVersion.ToString()), | ||
| EscapeCsvValue(license.LicenseInformationOrigin.ToString()), | ||
| EscapeCsvValue(license.License ?? string.Empty), | ||
| EscapeCsvValue(license.LicenseUrl ?? string.Empty), | ||
| EscapeCsvValue(license.Copyright ?? string.Empty), | ||
| EscapeCsvValue(license.Authors ?? string.Empty), | ||
| EscapeCsvValue(license.PackageProjectUrl ?? string.Empty), | ||
| GetValidationErrorsString(license.ValidationErrors) | ||
| }; | ||
|
|
||
| await writer.WriteLineAsync(string.Join(",", row)); | ||
| } | ||
|
|
||
| await writer.FlushAsync(); | ||
| } | ||
|
|
||
| private static string EscapeCsvValue(string? value) | ||
| { | ||
| if (string.IsNullOrEmpty(value)) | ||
| { | ||
| return string.Empty; | ||
| } | ||
|
|
||
| if (value.Contains(',') || value.Contains('"') || value.Contains('\n') || value.Contains('\r')) | ||
| { | ||
| string escaped = value!.Replace("\"", "\"\""); | ||
| return $"\"{escaped}\""; | ||
| } | ||
|
|
||
| return value!; | ||
| } | ||
|
|
||
| private static string GetValidationErrorsString(IEnumerable<ValidationError> errors) | ||
| { | ||
| string result = string.Join("; ", errors.Select(e => $"{e.Error} ({e.Context})")); | ||
| return EscapeCsvValue(result); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ public enum OutputType | |
| Table, | ||
| Json, | ||
| JsonPretty, | ||
| Markdown | ||
| Markdown, | ||
| Csv | ||
| } | ||
| } | ||
This file contains hidden or 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
269 changes: 269 additions & 0 deletions
269
tests/NuGetLicense.Test/Output/Csv/CsvOutputFormatterSpecialCases.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,269 @@ | ||
| // Licensed to the projects contributors. | ||
| // The license conditions are provided in the LICENSE file located in the project root | ||
|
|
||
| using System.Text; | ||
| using NuGetLicense.LicenseValidator; | ||
| using NuGetLicense.Output.Csv; | ||
| using HelperNuGetVersion = NuGetLicense.Test.Output.Helper.NuGetVersion; | ||
|
|
||
| namespace NuGetLicense.Test.Output.Csv | ||
| { | ||
| [TestFixture] | ||
| public class CsvOutputFormatterSpecialCases | ||
| { | ||
| private static readonly string s_newLine = Environment.NewLine; | ||
|
|
||
| [Test] | ||
| public async Task Should_EscapeCsv_WithSpecialCharacters_Correctly() | ||
| { | ||
| var licenses = new List<LicenseValidationResult> | ||
| { | ||
| new( | ||
| PackageId: "PackageId,With,Commas", | ||
| PackageVersion: new HelperNuGetVersion("1.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT License", | ||
| LicenseUrl: null, | ||
| "Copyright \"2024\"", | ||
| "Author1, Author2", | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression, | ||
| ValidationErrors: new List<ValidationError>() | ||
| ), | ||
| new( | ||
| PackageId: "PackageIdWith\nNewline", | ||
| PackageVersion: new HelperNuGetVersion("2.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "Apache-2.0", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression, | ||
| ValidationErrors: new List<ValidationError>() | ||
| ) | ||
| }; | ||
|
|
||
| string expected = | ||
| $"Package,Version,License Information Origin,License,License Url,Copyright,Authors,Package Project Url,Errors with Context{s_newLine}" + | ||
| $"\"PackageId,With,Commas\",1.0.0,Expression,MIT License,,\"Copyright \"\"2024\"\"\",\"Author1, Author2\",,{s_newLine}" + | ||
| $"\"PackageIdWith\nNewline\",2.0.0,Expression,Apache-2.0,,,,,{s_newLine}"; | ||
|
|
||
| var csvFormatter = new CsvOutputFormatter(false, false); | ||
| using var memoryStream = new MemoryStream(); | ||
|
|
||
| await csvFormatter.Write(memoryStream, licenses); | ||
|
|
||
| string result = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
|
|
||
| Assert.That(result, Is.EqualTo(expected)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_EscapeCsv_WithErrors_Correctly() | ||
| { | ||
| var licenses = new List<LicenseValidationResult> | ||
| { | ||
| new( | ||
| PackageId: "TestPackage", | ||
| PackageVersion: new HelperNuGetVersion("1.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression, | ||
| ValidationErrors: new List<ValidationError> | ||
| { | ||
| new("License not allowed", "MIT is not in the allowed list"), | ||
| new("Missing copyright", "No copyright information") | ||
| } | ||
| ) | ||
| }; | ||
|
|
||
| string expected = | ||
| $"Package,Version,License Information Origin,License,License Url,Copyright,Authors,Package Project Url,Errors with Context{s_newLine}" + | ||
| $"TestPackage,1.0.0,Expression,MIT,,,,,License not allowed (MIT is not in the allowed list); Missing copyright (No copyright information){s_newLine}"; | ||
|
|
||
| var csvFormatter = new CsvOutputFormatter(false, false); | ||
| using var memoryStream = new MemoryStream(); | ||
|
|
||
| await csvFormatter.Write(memoryStream, licenses); | ||
|
|
||
| string result = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
|
|
||
| Assert.That(result, Is.EqualTo(expected)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_EscapeCsv_WithSkipIgnoredFilter_Correctly() | ||
| { | ||
| var licenses = new List<LicenseValidationResult> | ||
| { | ||
| new( | ||
| PackageId: "NormalPackage", | ||
| PackageVersion: new HelperNuGetVersion("1.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression, | ||
| ValidationErrors: new List<ValidationError>() | ||
| ), | ||
| new( | ||
| PackageId: "IgnoredPackage", | ||
| PackageVersion: new HelperNuGetVersion("2.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Ignored, | ||
| ValidationErrors: new List<ValidationError>() | ||
| ) | ||
| }; | ||
|
|
||
| string expected = | ||
| $"Package,Version,License Information Origin,License,License Url,Copyright,Authors,Package Project Url,Errors with Context{s_newLine}" + | ||
| $"NormalPackage,1.0.0,Expression,MIT,,,,,{s_newLine}"; | ||
|
|
||
| var csvFormatter = new CsvOutputFormatter(false, skipIgnoredPackages: true); | ||
| using var memoryStream = new MemoryStream(); | ||
|
|
||
| await csvFormatter.Write(memoryStream, licenses); | ||
|
|
||
| string result = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
|
|
||
| Assert.That(result, Is.EqualTo(expected)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_EscapeCsvCorrectly_IfPrintErrorsOnly() | ||
| { | ||
| var licenses = new List<LicenseValidationResult> | ||
| { | ||
| new( | ||
| PackageId: "Package1", | ||
| PackageVersion: new HelperNuGetVersion("1.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression | ||
| ), | ||
| new( | ||
| PackageId: "Package2", | ||
| PackageVersion: new HelperNuGetVersion("2.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression | ||
| ) | ||
| }; | ||
|
|
||
| string expected = | ||
| $"Package,Version,License Information Origin,License,License Url,Copyright,Authors,Package Project Url,Errors with Context{s_newLine}"; | ||
|
|
||
| var csvFormatter = new CsvOutputFormatter(printErrorsOnly: true, false); | ||
| using var memoryStream = new MemoryStream(); | ||
|
|
||
| await csvFormatter.Write(memoryStream, licenses); | ||
|
|
||
| string result = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
| Assert.That(result, Is.EqualTo(expected)); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Should_ApplyBothFilters_WhenSpecialOptionsAreTrue() | ||
| { | ||
| var licenses = new List<LicenseValidationResult> | ||
| { | ||
| new( | ||
| PackageId: "Package1", | ||
| PackageVersion: new HelperNuGetVersion("1.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression, | ||
| ValidationErrors: new List<ValidationError>() | ||
| ), | ||
| new( | ||
| PackageId: "Package2", | ||
| PackageVersion: new HelperNuGetVersion("2.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Ignored, | ||
| ValidationErrors: new List<ValidationError> { new("Test error", "Context") } | ||
| ), | ||
| new( | ||
| PackageId: "Package3", // should contain because _printErrorsOnly = true & _skipIgnoredPackages = true | ||
| PackageVersion: new HelperNuGetVersion("3.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Expression, | ||
| ValidationErrors: new List<ValidationError> { new("Test error", "Context") } | ||
| ), | ||
| new( | ||
| PackageId: "Package4", | ||
| PackageVersion: new HelperNuGetVersion("4.0.0"), | ||
| PackageProjectUrl: null, | ||
| License: "MIT", | ||
| LicenseUrl: null, | ||
| Copyright: null, | ||
| Authors: null, | ||
| Description: null, | ||
| Summary: null, | ||
| LicenseInformationOrigin.Ignored, | ||
| ValidationErrors: new List<ValidationError>() | ||
| ) | ||
| }; | ||
|
|
||
| string expected = | ||
| $"Package,Version,License Information Origin,License,License Url,Copyright,Authors,Package Project Url,Errors with Context{s_newLine}" + | ||
| $"Package3,3.0.0,Expression,MIT,,,,,Test error (Context){s_newLine}"; | ||
|
|
||
| var csvFormatter = new CsvOutputFormatter(printErrorsOnly: true, skipIgnoredPackages: true); | ||
| using var memoryStream = new MemoryStream(); | ||
|
|
||
| await csvFormatter.Write(memoryStream, licenses); | ||
|
|
||
| string result = Encoding.UTF8.GetString(memoryStream.ToArray()); | ||
|
|
||
| Assert.That(result, Does.Contain("Package3")); | ||
| Assert.That(result, Does.Not.Contain("Package1")); | ||
| Assert.That(result, Does.Not.Contain("Package2")); | ||
| Assert.That(result, Does.Not.Contain("Package4")); | ||
| Assert.That(result, Is.EqualTo(expected)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.