Clamp the Excerpt end index to the end of the file (#571)
* Clamp the Excerpt end index to the end of the file * Add clamping test for Extract Excerpt * Update dependencies
This commit is contained in:
Родитель
edf169f9fd
Коммит
3a5c3f97bc
|
@ -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>
|
||||
|
|
|
@ -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" />
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -8,6 +8,7 @@ using System.Diagnostics.CodeAnalysis;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
@ -17,6 +18,8 @@ using Microsoft.CST.RecursiveExtractor;
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
[assembly: InternalsVisibleTo("AppInspector.Tests")]
|
||||
|
||||
namespace Microsoft.ApplicationInspector.RulesEngine;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
|
@ -531,7 +534,7 @@ public class RuleProcessor
|
|||
/// 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)
|
||||
{
|
||||
|
@ -554,7 +557,7 @@ public class RuleProcessor
|
|||
/// 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)
|
||||
{
|
||||
|
@ -576,8 +579,10 @@ public class RuleProcessor
|
|||
// 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];
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче