From 2aae3631df5b93aedf6ab2c08a9b1c7f0a4088b7 Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Fri, 18 Oct 2024 14:04:46 +0200 Subject: [PATCH] Set current dir --- src/DacpacTool/BuildOptions.cs | 1 - src/DacpacTool/PackageAnalyzer.cs | 5 ++-- src/DacpacTool/Program.cs | 3 +- src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets | 4 +-- test/DacpacTool.Tests/PackageAnalyzerTests.cs | 28 +++++++++++++------ 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/DacpacTool/BuildOptions.cs b/src/DacpacTool/BuildOptions.cs index 23fc2768..cc3adc63 100644 --- a/src/DacpacTool/BuildOptions.cs +++ b/src/DacpacTool/BuildOptions.cs @@ -9,7 +9,6 @@ public class BuildOptions : BaseOptions public string Name { get; set; } public string Version { get; set; } public FileInfo Output { get; set; } - public FileInfo ProjectPath { get; set; } public SqlServerVersion SqlServerVersion { get; set; } public FileInfo InputFile { get; set; } diff --git a/src/DacpacTool/PackageAnalyzer.cs b/src/DacpacTool/PackageAnalyzer.cs index c06cf013..f177b23f 100644 --- a/src/DacpacTool/PackageAnalyzer.cs +++ b/src/DacpacTool/PackageAnalyzer.cs @@ -22,12 +22,11 @@ public PackageAnalyzer(IConsole console, string rulesExpression) BuildRuleLists(rulesExpression); } - public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo projectPath, FileInfo[] analyzers) + public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo[] analyzers) { ArgumentNullException.ThrowIfNull(model); ArgumentNullException.ThrowIfNull(outputFile); ArgumentNullException.ThrowIfNull(analyzers); - ArgumentNullException.ThrowIfNull(projectPath); _console.WriteLine($"Analyzing package '{outputFile.FullName}'"); try @@ -45,7 +44,7 @@ public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo projectPath, settings.AssemblyLookupPath = string.Join(';', analyzers.Select(a => a.DirectoryName)); } - var projectDir = Path.GetDirectoryName(projectPath.FullName); + var projectDir = Environment.CurrentDirectory; var suppressorPath = Path.Combine(projectDir, ProjectProblemSuppressor.SuppressionFilename); List suppressedProblems = new(); diff --git a/src/DacpacTool/Program.cs b/src/DacpacTool/Program.cs index 5de381fc..bcc8d1dc 100644 --- a/src/DacpacTool/Program.cs +++ b/src/DacpacTool/Program.cs @@ -21,7 +21,6 @@ static async Task Main(string[] args) new Option(new string[] { "--name", "-n" }, "Name of the package") { IsRequired = true }, new Option(new string[] { "--version", "-v" }, "Version of the package"), new Option(new string[] { "--output", "-o" }, "Filename of the output package"), - new Option(new string[] { "--projectpath", "-pp" }, "Filename of the project"), new Option(new string[] { "--sqlServerVersion", "-sv" }, () => SqlServerVersion.Sql150, description: "Target version of the model"), new Option(new string[] { "--inputfile", "-i" }, "Text file listing all input files"), new Option(new string[] { "--reference", "-r" }, "Reference(s) to include"), @@ -217,7 +216,7 @@ private static async Task BuildDacpac(BuildOptions options) { var analyzer = new PackageAnalyzer(new ActualConsole(), options.CodeAnalysisRules); - analyzer.Analyze(packageBuilder.Model, options.Output, options.ProjectPath, options.CodeAnalysisAssemblies ?? Array.Empty()); + analyzer.Analyze(packageBuilder.Model, options.Output, options.CodeAnalysisAssemblies ?? Array.Empty()); } return 0; diff --git a/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets b/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets index e9c6d5c1..29a5163b 100644 --- a/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets +++ b/src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets @@ -218,7 +218,6 @@ @(IntermediateAssembly->'-o "%(Identity)"', ' ') - -pp "$(ProjectPath)" -n "$(MSBuildProjectName)" -v "$(PackageVersion)" -sv $(SqlServerVersion) @(DacpacReference->'-r "%(DacpacFile);%(ExternalParts);%(SuppressMissingDependenciesErrors)"', ' ') @@ -238,11 +237,12 @@ -tdn "$(TargetDatabaseName)" -spw "$(SuppressTSqlWarnings)" -spl "$(IntermediateOutputPath)$(MSBuildProjectName).WarningsSuppression.txt" - dotnet "$(DacpacToolExe)" build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(BuildPropertyArguments) $(DeployPropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) $(TreatTSqlWarningsAsErrorsArgument) $(SuppressTSqlWarningsArgument) $(WarningsSuppressionListArgument) $(DebugArgument) $(GenerateCreateScriptArgument) $(TargetDatabaseNameArgument) $(RunSqlCodeAnalysisArgument) $(CodeAnalysisRulesArgument) $(CodeAnalysisAssemblyLookupPathsArgument) $(ProjectPathArgument) + dotnet "$(DacpacToolExe)" build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(BuildPropertyArguments) $(DeployPropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) $(TreatTSqlWarningsAsErrorsArgument) $(SuppressTSqlWarningsArgument) $(WarningsSuppressionListArgument) $(DebugArgument) $(GenerateCreateScriptArgument) $(TargetDatabaseNameArgument) $(RunSqlCodeAnalysisArgument) $(CodeAnalysisRulesArgument) $(CodeAnalysisAssemblyLookupPathsArgument) diff --git a/test/DacpacTool.Tests/PackageAnalyzerTests.cs b/test/DacpacTool.Tests/PackageAnalyzerTests.cs index abf2a279..883e3553 100644 --- a/test/DacpacTool.Tests/PackageAnalyzerTests.cs +++ b/test/DacpacTool.Tests/PackageAnalyzerTests.cs @@ -23,7 +23,7 @@ public void RunsAnalyzer() var packageAnalyzer = new PackageAnalyzer(_console, null); // Act - packageAnalyzer.Analyze(result.model, result.fileInfo, new FileInfo("x"), CollectAssemblyPaths()); + packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths()); // Assert testConsole.Lines.Count.ShouldBe(16); @@ -44,7 +44,7 @@ public void RunsAnalyzerWithSupressions() var packageAnalyzer = new PackageAnalyzer(_console, "-SqlServer.Rules.SRD0006;-Smells.SML005;-SqlServer.Rules.SRD999;+!SqlServer.Rules.SRN0002;"); // Act - packageAnalyzer.Analyze(result.model, result.fileInfo, new FileInfo("x"), CollectAssemblyPaths()); + packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths()); // Assert testConsole.Lines.Count.ShouldBe(14); @@ -66,7 +66,7 @@ public void RunsAnalyzerWithWildcardSupressions() var packageAnalyzer = new PackageAnalyzer(_console, "-SqlServer.Rules.SRD*"); // Act - packageAnalyzer.Analyze(result.model, result.fileInfo, new FileInfo("x"), CollectAssemblyPaths()); + packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths()); // Assert testConsole.Lines.Count.ShouldBe(14); @@ -86,7 +86,7 @@ public void RunsAnalyzerWithWarningsAsErrors() var packageAnalyzer = new PackageAnalyzer(_console, "+!SqlServer.Rules.SRD0006"); // Act - packageAnalyzer.Analyze(result.model, result.fileInfo, new FileInfo("x"), CollectAssemblyPaths()); + packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths()); // Assert testConsole.Lines.Count.ShouldBe(16); @@ -107,7 +107,7 @@ public void RunsAnalyzerWithWarningsAsErrorsUsingWildcard() var packageAnalyzer = new PackageAnalyzer(_console, "+!SqlServer.Rules.SRD*"); // Act - packageAnalyzer.Analyze(result.model, result.fileInfo, new FileInfo("x"), CollectAssemblyPaths()); + packageAnalyzer.Analyze(result.model, result.fileInfo, CollectAssemblyPaths()); // Assert testConsole.Lines.Count.ShouldBe(16); @@ -131,7 +131,7 @@ public void RunsAnalyzerWithoutAdditionalAnalyzers() var packageAnalyzer = new PackageAnalyzer(_console, null); // Act - packageAnalyzer.Analyze(result.model, result.fileInfo, new FileInfo("x"), Array.Empty()); + packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty()); // Assert testConsole.Lines.Count.ShouldBe(16); @@ -154,15 +154,25 @@ public void RunsAnalyzerWithSuppressionFile() packageBuilder.UsingVersion(SqlServerVersion.Sql150); packageBuilder.AddInputFile(new FileInfo("./Suppression/proc1.sql")); packageBuilder.AddInputFile(new FileInfo("./Suppression/proc2.sql")); - packageBuilder.SetMetadata("TestSupression", "1.0.0"); + packageBuilder.SetMetadata("TestSuppression", "1.0.0"); packageBuilder.ValidateModel(); packageBuilder.SaveToDisk(path); var packageAnalyzer = new PackageAnalyzer(_console, null); - // Act - packageAnalyzer.Analyze(packageBuilder.Model, path, new FileInfo(Path.Combine(Path.GetDirectoryName(typeof(PackageAnalyzerTests).Assembly.Location), "Suppression", "TestSuppression.csproj")), Array.Empty()); + try + { + //Set the current directory. + Directory.SetCurrentDirectory(Path.Combine(Path.GetDirectoryName(typeof(PackageAnalyzerTests).Assembly.Location), "Suppression")); + // Act + packageAnalyzer.Analyze(packageBuilder.Model, path, Array.Empty()); + } + finally + { + //Reset the current directory. + Directory.SetCurrentDirectory(Path.GetDirectoryName(typeof(PackageAnalyzerTests).Assembly.Location)); + } // Assert testConsole.Lines.Count.ShouldBe(25);