From db547ecf430d7018dcc7427b9c2a4e85399640e4 Mon Sep 17 00:00:00 2001 From: "Zi Chen (from Dev Box)" Date: Mon, 5 Aug 2024 13:59:33 -0700 Subject: [PATCH] Add PackageReference test --- .../CodeAnalysisTests.cs | 37 +++++++++++++++++++ .../CodeAnalyzerSample.csproj | 8 +++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/test/Microsoft.Build.Sql.Tests/CodeAnalysisTests.cs b/test/Microsoft.Build.Sql.Tests/CodeAnalysisTests.cs index 639f2e6..777cec7 100644 --- a/test/Microsoft.Build.Sql.Tests/CodeAnalysisTests.cs +++ b/test/Microsoft.Build.Sql.Tests/CodeAnalysisTests.cs @@ -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 => @@ -25,6 +27,41 @@ public void VerifyCodeAnalyzerFromProjectReference() item.AddMetadata("SetTargetFramework", "TargetFramework=netstandard2.1"); }); + // Set up code analysis properties + ProjectUtils.AddProperties(this.GetProjectFilePath(), new Dictionary() + { + { "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() { { "RunSqlCodeAnalysis", "true" }, diff --git a/test/Microsoft.Build.Sql.Tests/TestData/CodeAnalyzerSample/CodeAnalyzerSample.csproj b/test/Microsoft.Build.Sql.Tests/TestData/CodeAnalyzerSample/CodeAnalyzerSample.csproj index 8170281..f98e8aa 100644 --- a/test/Microsoft.Build.Sql.Tests/TestData/CodeAnalyzerSample/CodeAnalyzerSample.csproj +++ b/test/Microsoft.Build.Sql.Tests/TestData/CodeAnalyzerSample/CodeAnalyzerSample.csproj @@ -2,9 +2,15 @@ Library netstandard2.1 + $(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput - + + + + + + \ No newline at end of file