Skip to content

Commit

Permalink
Enable error wildcards (#547)
Browse files Browse the repository at this point in the history
* Enable error wildcards

* Update test
  • Loading branch information
ErikEJ authored Mar 27, 2024
1 parent e811cfb commit 8f65733
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,13 @@ The optional `CodeAnalysisRules` property allows you to disable individual rules

Any rule violations found during analysis are reported as build warnings.

Individual rule violations can be configured to be reported as build errors as shown below.
Individual rule violations or groups of rules can be configured to be reported as build errors as shown below.

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.7.1">
<PropertyGroup>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>+!SqlServer.Rules.SRN0005</CodeAnalysisRules>
<CodeAnalysisRules>+!SqlServer.Rules.SRN0005;+!SqlServer.Rules.SRD*</CodeAnalysisRules>
</PropertyGroup>
</Project>
```
Expand Down
8 changes: 8 additions & 0 deletions src/DacpacTool/SqlRuleProblemExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dac.CodeAnalysis;

Expand All @@ -20,6 +21,13 @@ public static string GetOutputMessage(this SqlRuleProblem sqlRuleProblem, HashSe
{
sqlRuleProblemSeverity = SqlRuleProblemSeverity.Error;
}

var wildCardErrorRules = errorRules
.Where(r => r.EndsWith("*", StringComparison.OrdinalIgnoreCase));
if (wildCardErrorRules.Any(s => sqlRuleProblem.RuleId.StartsWith(s[..^1])))
{
sqlRuleProblemSeverity = SqlRuleProblemSeverity.Error;
}

var stringBuilder = new StringBuilder();
stringBuilder.Append(sqlRuleProblem.SourceName);
Expand Down
43 changes: 43 additions & 0 deletions test/DacpacTool.Tests/PackageAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,49 @@ public void RunsAnalyzerWithWildcardSupressions()
testConsole.Lines.ShouldContain($"Successfully analyzed package '{result.fileInfo.FullName}'");
}

[TestMethod]
public void RunsAnalyzerWithWarningsAsErrors()
{
// Arrange
var testConsole = (TestConsole)_console;
testConsole.Lines.Clear();
var result = BuildSimpleModel();
var packageAnalyzer = new PackageAnalyzer(_console, "+!SqlServer.Rules.SRD0006");

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);

// Assert
testConsole.Lines.Count.ShouldBe(15);

testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.ShouldContain($"proc1.sql(1,47): Error SRD0006 : SqlServer.Rules : Avoid using SELECT *.");
testConsole.Lines.Count(l => l.Contains("Error ")).ShouldBe(1);
testConsole.Lines.ShouldContain($"Successfully analyzed package '{result.fileInfo.FullName}'");
}

[TestMethod]
public void RunsAnalyzerWithWarningsAsErrorsUsingWildcard()
{
// Arrange
var testConsole = (TestConsole)_console;
testConsole.Lines.Clear();
var result = BuildSimpleModel();
var packageAnalyzer = new PackageAnalyzer(_console, "+!SqlServer.Rules.SRD*");

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);

// Assert
testConsole.Lines.Count.ShouldBe(15);

testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.ShouldContain($"proc1.sql(1,47): Error SRD0006 : SqlServer.Rules : Avoid using SELECT *.");
testConsole.Lines.ShouldContain($"-1(1,1): Error SRD0002 : SqlServer.Rules : Table does not have a primary key.");
testConsole.Lines.Count(l => l.Contains("): Error ")).ShouldBe(2);
testConsole.Lines.ShouldContain($"Successfully analyzed package '{result.fileInfo.FullName}'");
}

private static (FileInfo fileInfo, TSqlModel model) BuildSimpleModel()
{
var tmodel = new TestModelBuilder()
Expand Down

0 comments on commit 8f65733

Please sign in to comment.