Skip to content

Commit

Permalink
Merge branch 'main' into scott/versionConflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcfanning authored Dec 11, 2023
2 parents 50508ab + 057f86d commit b73fe16
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
3 changes: 3 additions & 0 deletions ReleaseHistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* DEP: Remove explicit versioning for `System.Memory` and `System.Runtime.CompilerServices.Unsafe`.
* DEP: Remove spurious references to `System.Collections.Immutable`.

## **v4.4.1 UNRELEASED
* BUG: Emit `WRN997.OneOrMoreFilesSkippedDueToExceedingSizeLimit` when no valid analysis targets are detected (due to exceeding size limits).

## **v4.4.0 [Sdk](https://www.nuget.org/packages/Sarif.Sdk/v4.4.0) | [Driver](https://www.nuget.org/packages/Sarif.Driver/v4.4.0) | [Converters](https://www.nuget.org/packages/Sarif.Converters/v4.4.0) | [Multitool](https://www.nuget.org/packages/Sarif.Multitool/v4.4.0) | [Multitool Library](https://www.nuget.org/packages/Sarif.Multitool.Library/v4.4.0)
* DEP: Add reference to `System.Text.Encoding.CodePages` v8.0.0 (to support Windows 1252 code pages in binary vs. text classification).
* DEP: Update `Newtonsoft.Json` reference from 8.0.3 to v9.0.1 to provide `net462` compatibility.
Expand Down
17 changes: 9 additions & 8 deletions src/Sarif.Driver/Sdk/MultithreadedAnalyzeCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,6 @@ private void MultithreadedAnalyzeTargets(TContext globalContext,
enumerateTargets.Wait();
logResults.Wait();

if (_filesExceedingSizeLimitCount > 0)
{
Warnings.LogOneOrMoreFilesSkippedDueToExceedingSizeLimit(globalContext, _filesExceedingSizeLimitCount);
}

if (_filesMatchingGlobalFileDenyRegex > 0)
{
string reason = "file path(s) matched the global file deny regex";
Expand Down Expand Up @@ -605,25 +600,31 @@ private static void LogCachingLogger(TContext globalContext, CachingLogger cachi
globalContext.Logger.TargetAnalyzed(globalContext);
}

private async Task<bool> EnumerateTargetsAsync(TContext context)
private async Task<bool> EnumerateTargetsAsync(TContext globalContext)
{
try
{
this._fileContextsCount = 0;
this._fileContexts = new ConcurrentDictionary<uint, TContext>();

DriverEventSource.Log.EnumerateArtifactsStart();
await EnumerateFilesFromArtifactsProvider(context);
await EnumerateFilesFromArtifactsProvider(globalContext);
DriverEventSource.Log.EnumerateArtifactsStop();
}
finally
{
readyToScanChannel.Writer.Complete();
}

if (_filesExceedingSizeLimitCount > 0)
{
Warnings.LogOneOrMoreFilesSkippedDueToExceedingSizeLimit(globalContext, _filesExceedingSizeLimitCount);
}

if (_fileContextsCount == 0)
{
Errors.LogNoValidAnalysisTargets(context);
Errors.LogNoValidAnalysisTargets(globalContext);

ThrowExitApplicationException(ExitReason.NoValidAnalysisTargets);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Sarif/Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static class Errors
private const string ERR997_FileAlreadyExists = "ERR997.FileAlreadyExists";
internal const string ERR997_NoPluginsConfigured = "ERR997.NoPluginsConfigured";
private const string ERR997_ExceptionLoadingPlugIn = "ERR997.ExceptionLoadingPlugIn";
private const string ERR997_NoValidAnalysisTargets = "ERR997.NoValidAnalysisTargets";
internal const string ERR997_NoValidAnalysisTargets = "ERR997.NoValidAnalysisTargets";
private const string ERR997_ExceptionAccessingFile = "ERR997.ExceptionAccessingFile";
internal const string ERR997_IncompatibleRulesDetected = "ERR997.IncompatibleRulesDetected";
internal const string ERR997_AllRulesExplicitlyDisabled = "ERR997.AllRulesExplicitlyDisabled";
Expand Down
94 changes: 94 additions & 0 deletions src/Test.UnitTests.Sarif.Driver/Sdk/AnalyzeCommandBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text;

using FluentAssertions;
using FluentAssertions.Execution;

using Microsoft.CodeAnalysis.Sarif.Converters;
using Microsoft.CodeAnalysis.Sarif.Writers;
Expand All @@ -36,6 +37,99 @@ public AnalyzeCommandBaseTests(ITestOutputHelper output)
Output.WriteLine($"The seed that will be used is: {TestRule.s_seed}");
}


[Fact]
public void AnalyzeCommandBase_ScanWithFilesThatExceedSizeLimitEmitsSkippedFilesWarning()
{
var logger = new TestMessageLogger();

var artifact = new EnumeratedArtifact(new FileSystem())
{
Uri = new Uri(@"c:\testfile1.txt"),
Contents = new string('x', 1024), // Withing threshold.
};

var anotherArtifact = new EnumeratedArtifact(new FileSystem())
{
Uri = new Uri(@"c:\testfile2.txt"),
Contents = new string('x', 1025), // Just exceeds 1k.
};


var context = new TestAnalysisContext
{
TargetsProvider = new ArtifactProvider(new[] { artifact, anotherArtifact }),
MaxFileSizeInKilobytes = 1,
Logger = logger,
};

int result = new TestMultithreadedAnalyzeCommand().Run(options: null, ref context);

RuntimeConditions conditions = RuntimeConditions.OneOrMoreFilesSkippedDueToExceedingSizeLimits;

using (new AssertionScope())
{
context.RuntimeErrors.Should().Be(conditions);

logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Note).Count().Should().Be(1);
Notification note = logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Note).First();
note.Descriptor.Id.Should().Be(Notes.Msg002_FileExceedingSizeLimitSkipped);

logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Warning).Count().Should().Be(1);
Notification warning = logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Warning).First();
warning.Descriptor.Id.Should().Be(Warnings.Wrn997_OneOrMoreFilesSkippedDueToExceedingSizeLimits);
}
}

[Fact]
public void AnalyzeCommandBase_ScanWithOnlyFilesThatExceedSizeLimitEmitsSkippedFilesWarning()
{
var logger = new TestMessageLogger();

var artifact = new EnumeratedArtifact(new FileSystem())
{
Uri = new Uri(@"c:\testfile1.txt"),
Contents = new string('x', 1025), // Just exceeds 1k.
};

var anotherArtifact = new EnumeratedArtifact(new FileSystem())
{
Uri = new Uri(@"c:\testfile2.txt"),
Contents = new string('x', 1025), // Just exceeds 1k.
};

EnumeratedArtifact[] allArtifacts = new[] { artifact, anotherArtifact };

var context = new TestAnalysisContext
{
TargetsProvider = new ArtifactProvider(allArtifacts),
MaxFileSizeInKilobytes = 1,
Logger = logger,
};

int result = new TestMultithreadedAnalyzeCommand().Run(options: null, ref context);

RuntimeConditions conditions = RuntimeConditions.NoValidAnalysisTargets |
RuntimeConditions.OneOrMoreFilesSkippedDueToExceedingSizeLimits;

using (new AssertionScope())
{
context.RuntimeErrors.Should().Be(conditions);

logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Note).Count().Should().Be(allArtifacts.Length);
Notification note = logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Note).First();
note.Descriptor.Id.Should().Be(Notes.Msg002_FileExceedingSizeLimitSkipped);

logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Warning).Count().Should().Be(1);
Notification warning = logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Warning).First();
warning.Descriptor.Id.Should().Be(Warnings.Wrn997_OneOrMoreFilesSkippedDueToExceedingSizeLimits);

logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Error).Count().Should().Be(1);
Notification error = logger.ConfigurationNotifications.Where(n => n.Level == FailureLevel.Error).First();
error.Descriptor.Id.Should().Be(Errors.ERR997_NoValidAnalysisTargets);
}
}

[Fact]
public void AnalyzeCommandBase_OptionsSettingsOverrideContextSettings()
{
Expand Down

0 comments on commit b73fe16

Please sign in to comment.