Skip to content

Commit

Permalink
Downgrade invalid packages.config files from an error to warning (#730
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JamieMagee authored Aug 18, 2023
1 parent 140c88d commit 2192418
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ namespace Microsoft.ComponentDetection.Detectors.NuGet;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.Extensions.Logging;

public class NuGetPackagesConfigDetector : FileComponentDetector
/// <summary>
/// Detects NuGet packages in packages.config files.
/// </summary>
public sealed class NuGetPackagesConfigDetector : FileComponentDetector
{
/// <summary>
/// Initializes a new instance of the <see cref="NuGetPackagesConfigDetector"/> class.
/// </summary>
/// <param name="componentStreamEnumerableFactory">The factory for handing back component streams to File detectors.</param>
/// <param name="walkerFactory">The factory for creating directory walkers.</param>
/// <param name="logger">The logger to use.</param>
public NuGetPackagesConfigDetector(
IComponentStreamEnumerableFactory componentStreamEnumerableFactory,
IObservableDirectoryWalkerFactory walkerFactory,
Expand All @@ -22,17 +31,23 @@ public NuGetPackagesConfigDetector(
this.Logger = logger;
}

/// <inheritdoc />
public override IList<string> SearchPatterns => new[] { "packages.config" };

/// <inheritdoc />
public override string Id => "NuGetPackagesConfig";

/// <inheritdoc />
public override IEnumerable<string> Categories =>
new[] { Enum.GetName(typeof(DetectorClass), DetectorClass.NuGet) };

/// <inheritdoc />
public override IEnumerable<ComponentType> SupportedComponentTypes => new[] { ComponentType.NuGet };

/// <inheritdoc />
public override int Version => 1;

/// <inheritdoc />
protected override Task OnFileFoundAsync(ProcessRequest processRequest, IDictionary<string, string> detectorArgs)
{
try
Expand All @@ -52,7 +67,7 @@ protected override Task OnFileFoundAsync(ProcessRequest processRequest, IDiction
}
catch (Exception e) when (e is PackagesConfigReaderException or XmlException)
{
this.Logger.LogError(e, "Failed to read packages.config file {File}", processRequest.ComponentStream.Location);
this.Logger.LogWarning(e, "Failed to read packages.config file {File}", processRequest.ComponentStream.Location);
}

return Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,29 @@ public async Task Should_WorkAsync()
.WithFile("packages.config", packagesConfig)
.ExecuteDetectorAsync();

scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDetectedComponents();
detectedComponents.Should().NotBeEmpty()
.And.HaveCount(2)
.And.ContainEquivalentOf(new DetectedComponent(new NuGetComponent("jQuery", "3.1.1")))
.And.ContainEquivalentOf(new DetectedComponent(new NuGetComponent("NLog", "4.3.10")));
}

[TestMethod]
public async Task Should_SkipWithInvalidVersionAsync()
{
var packagesConfig =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
<package id=""jQuery"" version=""3.1.1"" targetFramework=""net46"" />
<package id=""NLog"" version=""
</packages>";
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("packages.config", packagesConfig)
.ExecuteDetectorAsync();

scanResult.ResultCode.Should().Be(ProcessingResultCode.Success);
var detectedComponents = componentRecorder.GetDetectedComponents();
detectedComponents.Should().BeEmpty();
}
}

0 comments on commit 2192418

Please sign in to comment.