Skip to content

Commit

Permalink
Set current dir
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ committed Oct 18, 2024
1 parent 42b3ef1 commit 2aae363
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
1 change: 0 additions & 1 deletion src/DacpacTool/BuildOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
5 changes: 2 additions & 3 deletions src/DacpacTool/PackageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<SuppressedProblemInfo> suppressedProblems = new();

Expand Down
3 changes: 1 addition & 2 deletions src/DacpacTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ static async Task<int> Main(string[] args)
new Option<string>(new string[] { "--name", "-n" }, "Name of the package") { IsRequired = true },
new Option<string>(new string[] { "--version", "-v" }, "Version of the package"),
new Option<FileInfo>(new string[] { "--output", "-o" }, "Filename of the output package"),
new Option<FileInfo>(new string[] { "--projectpath", "-pp" }, "Filename of the project"),
new Option<SqlServerVersion>(new string[] { "--sqlServerVersion", "-sv" }, () => SqlServerVersion.Sql150, description: "Target version of the model"),
new Option<FileInfo>(new string[] { "--inputfile", "-i" }, "Text file listing all input files"),
new Option<string[]>(new string[] { "--reference", "-r" }, "Reference(s) to include"),
Expand Down Expand Up @@ -217,7 +216,7 @@ private static async Task<int> BuildDacpac(BuildOptions options)
{
var analyzer = new PackageAnalyzer(new ActualConsole(), options.CodeAnalysisRules);

analyzer.Analyze(packageBuilder.Model, options.Output, options.ProjectPath, options.CodeAnalysisAssemblies ?? Array.Empty<FileInfo>());
analyzer.Analyze(packageBuilder.Model, options.Output, options.CodeAnalysisAssemblies ?? Array.Empty<FileInfo>());
}

return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@
<!-- Build arguments for the command line tool -->
<PropertyGroup>
<OutputPathArgument>@(IntermediateAssembly->'-o &quot;%(Identity)&quot;', ' ')</OutputPathArgument>
<ProjectPathArgument>-pp &quot;$(ProjectPath)&quot;</ProjectPathArgument>
<MetadataArguments>-n &quot;$(MSBuildProjectName)&quot; -v &quot;$(PackageVersion)&quot;</MetadataArguments>
<SqlServerVersionArgument>-sv $(SqlServerVersion)</SqlServerVersionArgument>
<ReferenceArguments>@(DacpacReference->'-r &quot;%(DacpacFile);%(ExternalParts);%(SuppressMissingDependenciesErrors)&quot;', ' ')</ReferenceArguments>
Expand All @@ -238,11 +237,12 @@
<TargetDatabaseNameArgument Condition="'$(TargetDatabaseName)' != '$(MSBuildProjectName)'">-tdn &quot;$(TargetDatabaseName)&quot;</TargetDatabaseNameArgument>
<SuppressTSqlWarningsArgument Condition="'$(SuppressTSqlWarnings)'!=''">-spw &quot;$(SuppressTSqlWarnings)&quot;</SuppressTSqlWarningsArgument>
<WarningsSuppressionListArgument Condition="'@(WarningsSuppressionFiles->'%(Identity)')'!=''">-spl &quot;$(IntermediateOutputPath)$(MSBuildProjectName).WarningsSuppression.txt&quot;</WarningsSuppressionListArgument>
<DacpacToolCommand>dotnet &quot;$(DacpacToolExe)&quot; build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(BuildPropertyArguments) $(DeployPropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) $(TreatTSqlWarningsAsErrorsArgument) $(SuppressTSqlWarningsArgument) $(WarningsSuppressionListArgument) $(DebugArgument) $(GenerateCreateScriptArgument) $(TargetDatabaseNameArgument) $(RunSqlCodeAnalysisArgument) $(CodeAnalysisRulesArgument) $(CodeAnalysisAssemblyLookupPathsArgument) $(ProjectPathArgument) </DacpacToolCommand>
<DacpacToolCommand>dotnet &quot;$(DacpacToolExe)&quot; build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(BuildPropertyArguments) $(DeployPropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) $(TreatTSqlWarningsAsErrorsArgument) $(SuppressTSqlWarningsArgument) $(WarningsSuppressionListArgument) $(DebugArgument) $(GenerateCreateScriptArgument) $(TargetDatabaseNameArgument) $(RunSqlCodeAnalysisArgument) $(CodeAnalysisRulesArgument) $(CodeAnalysisAssemblyLookupPathsArgument) </DacpacToolCommand>
</PropertyGroup>
<!-- Run it, except during design-time builds -->
<Message Importance="Low" Text="Running command: $(DacpacToolCommand)" />
<Exec Command="$(DacpacToolCommand)"
WorkingDirectory="$(ProjectDir)"
Condition="'$(DesignTimeBuild)' != 'true' AND '$(BuildingProject)' == 'true'"/>

<!-- Copy any referenced .dacpac packages to the output folder -->
Expand Down
28 changes: 19 additions & 9 deletions test/DacpacTool.Tests/PackageAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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<FileInfo>());
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(16);
Expand All @@ -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<FileInfo>());
try
{
//Set the current directory.
Directory.SetCurrentDirectory(Path.Combine(Path.GetDirectoryName(typeof(PackageAnalyzerTests).Assembly.Location), "Suppression"));
// Act
packageAnalyzer.Analyze(packageBuilder.Model, path, Array.Empty<FileInfo>());
}
finally
{
//Reset the current directory.
Directory.SetCurrentDirectory(Path.GetDirectoryName(typeof(PackageAnalyzerTests).Assembly.Location));
}

// Assert
testConsole.Lines.Count.ShouldBe(25);
Expand Down

0 comments on commit 2aae363

Please sign in to comment.