Skip to content

Commit

Permalink
Add PackageReference test
Browse files Browse the repository at this point in the history
  • Loading branch information
zijchen committed Aug 5, 2024
1 parent 0a83ba2 commit db547ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
37 changes: 37 additions & 0 deletions test/Microsoft.Build.Sql.Tests/CodeAnalysisTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public void VerifyCodeAnalyzerFromProjectReference()
{
string tempFolder = Path.Combine(Path.GetTempPath(), TestContext.CurrentContext.Test.Name);
TestUtils.CopyDirectoryRecursive(Path.Combine(this.CommonTestDataDirectory, "CodeAnalyzerSample"), tempFolder);

// Add the analyzer csproj as a ProjectReference to the test sqlproj
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "ProjectReference",
new string[] { Path.Combine(tempFolder, "CodeAnalyzerSample.csproj") },
item =>
Expand All @@ -25,6 +27,41 @@ public void VerifyCodeAnalyzerFromProjectReference()
item.AddMetadata("SetTargetFramework", "TargetFramework=netstandard2.1");
});

// Set up code analysis properties
ProjectUtils.AddProperties(this.GetProjectFilePath(), new Dictionary<string, string>()
{
{ "RunSqlCodeAnalysis", "true" },
{ "SqlCodeAnalysisRules", "+!CodeAnalyzerSample.TableNameRule001" } // Should fail build on this rule
});

int exitCode = this.RunDotnetCommandOnProject("build", out string stdOutput, out string stdError);

Assert.AreNotEqual(0, exitCode, "Build should have failed");
Assert.IsTrue(stdOutput.Contains("Table name [dbo].[NotAView] ends in View. This can cause confusion and should be avoided"), "Unexpected stderr");
}

[Test]
public void VerifyCodeAnalyzerFromPackageReference()
{
// Set up and create the analyzer package
string tempFolder = Path.Combine(Path.GetTempPath(), TestContext.CurrentContext.Test.Name);
TestUtils.CopyDirectoryRecursive(Path.Combine(CommonTestDataDirectory, "CodeAnalyzerSample"), tempFolder);
RunGenericDotnetCommand($"pack {Path.Combine(tempFolder, "CodeAnalyzerSample.csproj")} -o {tempFolder}", out _, out _);

// Copy analyzer package to local Nuget source
string analyzerPackagePath = Path.Combine(tempFolder, "CodeAnalyzerSample.1.0.0.nupkg");
FileAssert.Exists(analyzerPackagePath);
File.Copy(analyzerPackagePath, Path.Combine(WorkingDirectory, "pkg", "CodeAnalyzerSample.1.0.0.nupkg"));

// Add the analyzer package as a PackageReference to the test sqlproj
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "PackageReference",
new string[] { "CodeAnalyzerSample" },
item =>
{
item.AddMetadata("Version", "1.0.0");
});

// Set up code analysis properties
ProjectUtils.AddProperties(this.GetProjectFilePath(), new Dictionary<string, string>()
{
{ "RunSqlCodeAnalysis", "true" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="162.3.566" />
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="162.3.566" PrivateAssets="all" />
</ItemGroup>
<Target Name="GetTargetPath" />
<Target Name="_AddAnalyzersToOutput">
<ItemGroup>
<TfmSpecificPackageFile Include="$(OutputPath)\CodeAnalyzerSample.dll" PackagePath="analyzers/dotnet/cs" />
</ItemGroup>
</Target>
</Project>

0 comments on commit db547ec

Please sign in to comment.