Skip to content

Commit

Permalink
Log analyzer load and warn if none
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ committed Sep 25, 2024
1 parent 7102e0a commit fd3809c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/DacpacTool/PackageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo[] analyzers)
{
ArgumentNullException.ThrowIfNull(model);
ArgumentNullException.ThrowIfNull(outputFile);
ArgumentNullException.ThrowIfNull(analyzers);

_console.WriteLine($"Analyzing package '{outputFile.FullName}'");
try
{
var factory = new CodeAnalysisServiceFactory();
var settings = new CodeAnalysisServiceSettings
var settings = new CodeAnalysisServiceSettings();

if (analyzers.Length == 0)
{
AssemblyLookupPath = string.Join(';', analyzers.Select(a => a.DirectoryName)),
};
// warning SR0016: Microsoft.Rules.Data : Stored procedure(sp_Test) includes sp_ prefix in its name.
_console.WriteLine("DacpacTool warning SQLPROJ0001: No additional rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj.");
}
else
{
_console.WriteLine("Loading analyzers: " + string.Join(", ", analyzers.Select(a => a.FullName)));
settings.AssemblyLookupPath = string.Join(';', analyzers.Select(a => a.DirectoryName));
}

var service = factory.CreateAnalysisService(model, settings);

Expand Down
36 changes: 30 additions & 6 deletions test/DacpacTool.Tests/PackageAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Dac.Model;
Expand All @@ -25,7 +26,7 @@ public void RunsAnalyzer()
packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths());

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

testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.ShouldContain($"proc1.sql(1,47): Warning SRD0006 : SqlServer.Rules : Avoid using SELECT *.");
Expand All @@ -48,7 +49,7 @@ public void RunsAnalyzerWithSupressions()
packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths());

// Assert
testConsole.Lines.Count.ShouldBe(13);
testConsole.Lines.Count.ShouldBe(14);

testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.Any(l => l.Contains("SRD0006")).ShouldBeFalse();
Expand All @@ -70,7 +71,7 @@ public void RunsAnalyzerWithWildcardSupressions()
packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths());

// Assert
testConsole.Lines.Count.ShouldBe(13);
testConsole.Lines.Count.ShouldBe(14);

testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.Any(l => l.Contains("SRD")).ShouldBeFalse();
Expand All @@ -90,7 +91,7 @@ public void RunsAnalyzerWithWarningsAsErrors()
packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths());

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

testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.ShouldContain($"proc1.sql(1,47): Error SRD0006 : SqlServer.Rules : Avoid using SELECT *.");
Expand All @@ -111,15 +112,38 @@ public void RunsAnalyzerWithWarningsAsErrorsUsingWildcard()
packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths());

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

testConsole.Lines.Count(l => l.Contains("Loading analyzers: ")).ShouldBe(1);
testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.ShouldNotContain("DacpacTool warning SQLPROJ0001: No additional rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj.");
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}'");
}

[TestMethod]
public void RunsAnalyzerWithoutAdditionalAnalyzers()
{
// Arrange
var testConsole = (TestConsole)_console;
testConsole.Lines.Clear();
var result = BuildSimpleModel();
var packageAnalyzer = new PackageAnalyzer(_console, null);

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

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

testConsole.Lines[1].ShouldBe("DacpacTool warning SQLPROJ0001: No additional rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj.");
testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.Count(l => l.Contains("): Error ")).ShouldBe(0);
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 fd3809c

Please sign in to comment.