Skip to content

Commit

Permalink
Fix Scope Match code (#421)
Browse files Browse the repository at this point in the history
* Within Operations

They were missing instantiating the code scopes.
  • Loading branch information
gfs authored Jan 19, 2022
1 parent 8000439 commit ccdc2c4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
9 changes: 6 additions & 3 deletions RulesEngine/Ruleset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ public IEnumerable<ConvertedOatRule> GetUniversalRules()
Invert = condition.NegateFinding,
Arguments = condition.Pattern.Modifiers?.ToList() ?? new List<string>(),
FindingOnly = true,
CustomOperation = "Within"
CustomOperation = "Within",
Scopes = condition.Pattern.Scopes
});
expression.Append(" AND ");
expression.Append(clauseNumber);
Expand Down Expand Up @@ -265,7 +266,8 @@ public IEnumerable<ConvertedOatRule> GetUniversalRules()
FindingOnly = false,
CustomOperation = "Within",
Before = argList[0],
After = argList[1]
After = argList[1],
Scopes = condition.Pattern.Scopes
});
expression.Append(" AND ");
expression.Append(clauseNumber);
Expand All @@ -281,7 +283,8 @@ public IEnumerable<ConvertedOatRule> GetUniversalRules()
Invert = condition.NegateFinding,
Arguments = condition.Pattern.Modifiers?.ToList() ?? new List<string>(),
SameLineOnly = true,
CustomOperation = "Within"
CustomOperation = "Within",
Scopes = condition.Pattern.Scopes
});
expression.Append(" AND ");
expression.Append(clauseNumber);
Expand Down
3 changes: 2 additions & 1 deletion RulesEngine/TextContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Microsoft.ApplicationInspector.RulesEngine
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

/// <summary>
/// Class to handle text as a searchable container
Expand Down Expand Up @@ -231,7 +232,7 @@ public bool IsCommented(int index)
/// <returns> True if boundary is in a provided scope </returns>
public bool ScopeMatch(IEnumerable<PatternScope> scopes, Boundary boundary)
{
if (scopes is null)
if (scopes is null || !scopes.Any() || scopes.Contains(PatternScope.All))
{
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions RulesEngine/WithinClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public WithinClause(string? field = null) : base(Operation.Custom, field)
public int After { get; set; }
public int Before { get; set; }
public bool FindingOnly { get; set; }
public bool SameLineOnly { get; internal set; }
public PatternScope[]? Scopes { get; }
public bool SameLineOnly { get; set; }
public PatternScope[] Scopes { get; set; } = new PatternScope[1] { PatternScope.All };
}
}
2 changes: 1 addition & 1 deletion RulesEngine/WithinOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ OperationResult ProcessLambda(string target, Boundary targetBoundary)
Index = targetBoundary.Index + m.Index
};
// Should return only scoped matches
if (tc.ScopeMatch(wc.Scopes ?? Array.Empty<PatternScope>(), translatedBoundary))
if (tc.ScopeMatch(wc.Scopes, translatedBoundary))
{
boundaries.Add(translatedBoundary);
}
Expand Down
8 changes: 4 additions & 4 deletions UnitTest.Commands/Tests_NuGet/TestAnalyzeCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,8 @@ public void ScanUnknownFileTypesTest_Include()
Assert.AreEqual(2, result.Metadata.TotalFiles);
Assert.AreEqual(0, result.Metadata.FilesSkipped);
Assert.AreEqual(2, result.Metadata.FilesAffected);
Assert.AreEqual(69, result.Metadata.TotalMatchesCount);
Assert.AreEqual(22, result.Metadata.UniqueMatchesCount);
Assert.AreEqual(67, result.Metadata.TotalMatchesCount);
Assert.AreEqual(21, result.Metadata.UniqueMatchesCount);
}

[TestMethod]
Expand All @@ -407,8 +407,8 @@ public void ScanUnknownFileTypesTest_Exclude()
Assert.AreEqual(2, result.Metadata.TotalFiles);
Assert.AreEqual(1, result.Metadata.FilesSkipped);
Assert.AreEqual(1, result.Metadata.FilesAffected);
Assert.AreEqual(37, result.Metadata.TotalMatchesCount);
Assert.AreEqual(21, result.Metadata.UniqueMatchesCount);
Assert.AreEqual(35, result.Metadata.TotalMatchesCount);
Assert.AreEqual(20, result.Metadata.UniqueMatchesCount);
}

[TestMethod]
Expand Down

0 comments on commit ccdc2c4

Please sign in to comment.