Skip to content
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

Clamp the Excerpt end index to the end of the file #571

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions AppInspector.Benchmarks/AppInspector.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.10" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.11" />

</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion AppInspector.CLI/AppInspector.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

<ItemGroup>
<PackageReference Include="DotLiquid" Version="2.2.692" />
<PackageReference Include="Sarif.Sdk" Version="4.3.7" />
<PackageReference Include="Sarif.Sdk" Version="4.4.0" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
Expand Down
4 changes: 2 additions & 2 deletions AppInspector.RulesEngine/AppInspector.RulesEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<ItemGroup>
<PackageReference Include="gstocco.YamlDotNet.YamlPath" Version="1.0.21" />
<PackageReference Include="JsonCons.JsonPath" Version="1.1.0" />
<PackageReference Include="Microsoft.CST.OAT" Version="1.2.56" />
<PackageReference Include="Microsoft.CST.RecursiveExtractor" Version="1.2.21" />
<PackageReference Include="Microsoft.CST.OAT" Version="1.2.59" />
<PackageReference Include="Microsoft.CST.RecursiveExtractor" Version="1.2.23" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="YamlDotNet" Version="13.7.1" />
</ItemGroup>
Expand Down
11 changes: 8 additions & 3 deletions AppInspector.RulesEngine/RuleProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -17,6 +18,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

[assembly: InternalsVisibleTo("AppInspector.Tests")]

namespace Microsoft.ApplicationInspector.RulesEngine;

[ExcludeFromCodeCoverage]
Expand Down Expand Up @@ -531,7 +534,7 @@ private IEnumerable<ConvertedOatRule> GetRulesByFileName(string fileName)
/// Simple wrapper but keeps calling code consistent
/// Do not html code result which is accomplished later before out put to report
/// </summary>
private string ExtractTextSample(string fileText, int index, int length)
internal string ExtractTextSample(string fileText, int index, int length)
{
if (index < 0 || length < 0)
{
Expand All @@ -554,7 +557,7 @@ private string ExtractTextSample(string fileText, int index, int length)
/// from the template
/// </summary>
/// <returns></returns>
private static string ExtractExcerpt(TextContainer text, Location start, Location end, Boundary matchBoundary, int context = 3)
internal static string ExtractExcerpt(TextContainer text, Location start, Location end, Boundary matchBoundary, int context = 3)
{
if (context == 0)
{
Expand All @@ -576,8 +579,10 @@ private static string ExtractExcerpt(TextContainer text, Location start, Locatio
// instead gather an appropriate number of characters
if (endIndex - startIndex - matchBoundary.Length > maxCharacterContext * 2)
{
// Limit start index to 0
startIndex = Math.Max(0, matchBoundary.Index - maxCharacterContext);
endIndex = Math.Max(0, matchBoundary.Index + matchBoundary.Length + maxCharacterContext);
// Limit end index to length of overall content
endIndex = Math.Min(text.FullContent.Length, Math.Max(0, matchBoundary.Index + matchBoundary.Length + maxCharacterContext));
}

return text.FullContent[startIndex..endIndex];
Expand Down
60 changes: 60 additions & 0 deletions AppInspector.Tests/RuleProcessor/ExtractSamplesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.ApplicationInspector.Logging;
using Microsoft.ApplicationInspector.RulesEngine;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Serilog.Events;

namespace AppInspector.Tests.RuleProcessor;

[TestClass]
public class ExtractSamplesTests
{


private readonly ILoggerFactory _loggerFactory =
new LogOptions { ConsoleVerbosityLevel = LogEventLevel.Verbose }.GetLoggerFactory();

private ILogger _logger = new NullLogger<ExtractSamplesTests>();

[TestInitialize]
public void TestInit()
{
_logger = _loggerFactory.CreateLogger<ExtractSamplesTests>();
}

[TestCleanup]
public void TestCleanup()
{
}

const string shortTestString = "lorem ipsum dolor sit amet\nlorem ipsum dolor sit amet\nlorem ipsum dolor sit amet";
// Two lines 1000 long
const string longTestString = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789";
// Two lines 1000 long and a 10 character line at end
const string longTestStringShortThirdLine = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789\n1234567890";

// The string is short, so we get the whole thing (clamped on both sides)
[DataRow(shortTestString, 1,1,5,1,0,5,3,80)]
[DataRow(shortTestString, 1, 3, 5, 3, 54, 5, 3, 80)]
// These are 5 length at 0 index so we should only get context after (clamped on start)
[DataRow(longTestString, 1, 1, 5, 1, 0, 5, 3, 305)]
[DataRow(longTestString, 1, 1, 5, 1, 0, 5, 5, 505)]
// Third line is 10 character, context is set to 500 (specified as 5 lines then multiplied by 100) so 500 before, 10 after (clamped on end)
[DataRow(longTestStringShortThirdLine, 1, 3, 5, 3, 2000, 5, 5, 510)]

[DataTestMethod]
public void ExtractExcerptClampingTests(string testText, int StartCol, int StartLine, int EndCol, int EndLine, int Idx, int Length, int context, int expectedLength)
{
// Extract excerpt requires a text container
var tc = new TextContainer(testText, "csharp", new Microsoft.ApplicationInspector.RulesEngine.Languages(_loggerFactory), _loggerFactory);
var locStart = new Location() { Column = StartCol, Line = StartLine };
var locEnd = new Location() { Column = EndCol, Line = EndLine };
var boundary = new Boundary() { Index = Idx, Length = Length };
var result = Microsoft.ApplicationInspector.RulesEngine.RuleProcessor.ExtractExcerpt(tc, locStart, locEnd, boundary, context);
Assert.AreEqual(expectedLength, result.Length);
}
}
6 changes: 3 additions & 3 deletions AppInspector/AppInspector.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="DotLiquid" Version="2.2.692" />
<PackageReference Include="Glob" Version="1.1.9" />
<PackageReference Include="LibGit2Sharp" Version="0.28.0" />
<PackageReference Include="Microsoft.CST.OAT" Version="1.2.56" />
<PackageReference Include="Microsoft.CST.RecursiveExtractor" Version="1.2.21" />
<PackageReference Include="LibGit2Sharp" Version="0.29.0" />
<PackageReference Include="Microsoft.CST.OAT" Version="1.2.59" />
<PackageReference Include="Microsoft.CST.RecursiveExtractor" Version="1.2.23" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="ShellProgressBar" Version="5.2.0" />
<PackageReference Include="System.Reflection.Metadata" Version="8.0.0" />
Expand Down