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

Enable error wildcards #547

Merged
merged 2 commits into from
Mar 27, 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 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);
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved
testConsole.Lines.ShouldContain($"Successfully analyzed package '{result.fileInfo.FullName}'");
}

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