Skip to content

Commit

Permalink
add the deprecation decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
nkolev92 committed Sep 12, 2023
1 parent 960e83a commit ca47018
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
14 changes: 0 additions & 14 deletions src/Common/DependencyNodeIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,5 @@ public DependencyNodeIdentity(string id, NuGetVersion version, DependencyType ty
{
Type = type;
}

public override int GetHashCode()
{
return HashCode.Combine(base.GetHashCode(), Type, Vulnerable, Deprecated);
}

public override bool Equals(object? obj)
{
return obj is DependencyNodeIdentity identity &&
base.Equals(obj) &&
Type == identity.Type &&
Vulnerable == identity.Vulnerable &&
Deprecated == identity.Deprecated;
}
}
}
2 changes: 0 additions & 2 deletions src/Common/DeprecationInfoDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace Common
{
public class DeprecationInfoDecorator : IPackageDependencyNodeDecorator
{
private readonly ILogger _logger;
private readonly List<SourceRepository> _sourceRepositories;
private readonly SourceCacheContext _sourceCacheContext;
private readonly Dictionary<PackageIdentity, bool> PackageDeprecationData = new();
Expand All @@ -18,7 +17,6 @@ public DeprecationInfoDecorator(List<SourceRepository> sourceRepositories, Sourc
{
_sourceRepositories = sourceRepositories ?? throw new ArgumentNullException(nameof(sourceRepositories));
_sourceCacheContext = sourceCacheContext ?? throw new ArgumentNullException(nameof(sourceCacheContext));
_logger = AppLogger.Logger;
}

public async Task DecorateAsync(PackageDependencyNode dependencyNode, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<None Remove="compiler\resources\diamonddependency.assets.json" />
<None Remove="compiler\resources\diamonddependency.withvulnerabilitiesanddeprecations.dgml" />
<None Remove="compiler\resources\diamonddependencywithtoplevel.assets.json" />
<None Remove="compiler\resources\missingpackageversion.assets.json" />
<None Remove="compiler\resources\multipleversions.assets.json" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Reflection.Emit;
using System.Text.RegularExpressions;
using Common;
using FluentAssertions;
using FluentAssertions.Equivalency;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.Versioning;
using SharedUtility;

namespace DependencyVisualizerTool.Test
Expand Down Expand Up @@ -143,22 +147,67 @@ public async Task TransGraphToDGMLXDocument_transitiveprojectreference_CreateDGM
actualDGML.Should().Be(expectedDGML, because: actualDGML);
}

private static string RemoveWhitespace(string s)
[Fact]
public async Task TransGraphToDGMLXDocument_WithVulnerableAndDeprecatedPackages_CreateDGMLCorrectly()
{
return Regex.Replace(s, @"\s+", string.Empty);
var packageA = new PackageIdentity("A", new NuGetVersion(1, 0, 0));
var packageB = new PackageIdentity("B", new NuGetVersion(1, 0, 0));
var packageC = new PackageIdentity("C", new NuGetVersion(1, 1, 0));
var decorator = new VulnerabilityAndDeprecationDecorator(
vulnerablePackages: new HashSet<PackageIdentity> { packageA, packageC },
deprecatedPackages: new HashSet<PackageIdentity> { packageB, packageC });

var graph = await GetOnlyDependencyGraphAsync("DependencyVisualizerTool.Test.compiler.resources.diamonddependency.assets.json", new() { decorator });

string actualDGML = RemoveWhitespace(DGMLDependencyVisualizerTool.TransGraphToDGMLXDocument(graph).ToString());

string expectedDGML = RemoveWhitespace(TestHelpers.GetResource("DependencyVisualizerTool.Test.compiler.resources.diamonddependency.withvulnerabilitiesanddeprecations.dgml", GetType()));

actualDGML.Should().Be(expectedDGML, because: actualDGML);
}

private async Task<PackageDependencyGraph> GetOnlyDependencyGraphAsync(string resourceName)
private async Task<PackageDependencyGraph> GetOnlyDependencyGraphAsync(string resourceName, List<IPackageDependencyNodeDecorator> decorators = null)

Check warning on line 169 in src/DependencyVisualizerTool/DependencyVisualizerTool.Test/PackageDependencyVisualizerToolTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 169 in src/DependencyVisualizerTool/DependencyVisualizerTool.Test/PackageDependencyVisualizerToolTests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
var assetsFileText = TestHelpers.GetResource(resourceName, GetType());

var assetsFile = new LockFileFormat().Parse(assetsFileText, Path.GetTempPath());
var dependencyGraphSpec = new DependencyGraphSpec();
dependencyGraphSpec.AddProject(assetsFile.PackageSpec);
Dictionary<string, PackageDependencyGraph> graphs = await PackageDependencyGraph.GenerateAllDependencyGraphsFromAssetsFileAsync(assetsFile, dependencyGraphSpec, projectsOnly: false, new(), CancellationToken.None);
Dictionary<string, PackageDependencyGraph> graphs = await PackageDependencyGraph.GenerateAllDependencyGraphsFromAssetsFileAsync(assetsFile, dependencyGraphSpec, projectsOnly: false, decorators ?? new(), CancellationToken.None);
graphs.Should().HaveCount(1);
var graph = graphs.Single().Value;
return graph;
}

private static string RemoveWhitespace(string s)
{
return Regex.Replace(s, @"\s+", string.Empty);
}

private class VulnerabilityAndDeprecationDecorator : IPackageDependencyNodeDecorator
{
private readonly HashSet<PackageIdentity> _vulnerablePackages;
private readonly HashSet<PackageIdentity> _deprecatedPackages;

public VulnerabilityAndDeprecationDecorator(HashSet<PackageIdentity> vulnerablePackages, HashSet<PackageIdentity> deprecatedPackages)
{
_vulnerablePackages = vulnerablePackages ?? throw new ArgumentNullException(nameof(vulnerablePackages));
_deprecatedPackages = deprecatedPackages ?? throw new ArgumentNullException(nameof(deprecatedPackages));
}

public Task DecorateAsync(PackageDependencyNode dependencyNode, CancellationToken cancellationToken)
{
PackageIdentity identity = dependencyNode.Identity;
if (_vulnerablePackages.Contains(identity))
{
dependencyNode.Identity.Vulnerable = true;
}
if (_deprecatedPackages.Contains(identity))
{
dependencyNode.Identity.Deprecated = true;
}
return Task.CompletedTask;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<DirectedGraph xmlns="http://schemas.microsoft.com/vs/2009/dgml">
<Nodes>
<Node Id="TestProject.1.0.0" Label="TestProject.1.0.0" Category="Project" />
<Node Id="A.1.0.0" Label="A.1.0.0" Category="VulnerablePackage" />
<Node Id="B.1.0.0" Label="B.1.0.0" Category="DeprecatedPackage" />
<Node Id="C.1.1.0" Label="C.1.1.0" Category="VulnerableAndDeprecatedPackage" />
</Nodes>
<Links>
<Link Source="TestProject.1.0.0" Target="A.1.0.0" Label="[1.0.0, )" />
<Link Source="TestProject.1.0.0" Target="B.1.0.0" Label="[1.0.0, )" />
<Link Source="A.1.0.0" Target="C.1.1.0" Label="[1.0.0, )" />
<Link Source="B.1.0.0" Target="C.1.1.0" Label="[1.1.0, )" />
</Links>
<Categories>
<Category Id="Project" Background="Lightblue" StrokeThickness="2" />
<Category Id="Package" Background="None" StrokeThickness="1" />
<Category Id="VulnerablePackage" Background="None" StrokeThickness="4" Stroke="Red" />
<Category Id="DeprecatedPackage" Background="Yellow" StrokeThickness="1" />
<Category Id="VulnerableAndDeprecatedPackage" Background="Yellow" StrokeThickness="4" Stroke="Red" />
</Categories>

</DirectedGraph>

0 comments on commit ca47018

Please sign in to comment.