Do not make the test framework depends on CurrentDirectory

This commit is contained in:
nicolas.dorier 2024-10-03 16:04:16 +09:00
parent 1ffbab7338
commit 0f79526566
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
5 changed files with 36 additions and 7 deletions

View File

@ -7,6 +7,15 @@
<!--https://devblogs.microsoft.com/aspnet/testing-asp-net-core-mvc-web-apps-in-memory/-->
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
<AssemblyAttribute Include="BTCPayServer.Tests.OutputPathAttribute">
<!-- _Parameter1, _Parameter2, etc. correspond to the
matching parameter of a constructor of that .NET attribute type -->
<_Parameter1>$([System.IO.Path]::Combine('$(MSBuildProjectDirectory)', '$(OutputPath)'))</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<!--https://devblogs.microsoft.com/aspnet/testing-asp-net-core-mvc-web-apps-in-memory/-->
<Target Name="CopyAditionalFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
<ItemGroup>
@ -61,4 +70,7 @@
<ProjectReference Include="..\BTCPayServer\BTCPayServer.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="obj\Debug\net8.0\" />
</ItemGroup>
</Project>

View File

@ -163,7 +163,7 @@ namespace BTCPayServer.Tests
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development");
var confBuilder = new DefaultConfiguration() { Logger = LoggerProvider.CreateLogger("Console") }.CreateConfigurationBuilder(new[] { "--datadir", _Directory, "--conf", confPath, "--disable-registration", DisableRegistration ? "true" : "false" });
// This make sure that tests work outside of this assembly (ie, test project it a plugin)
confBuilder.SetBasePath(Directory.GetCurrentDirectory());
confBuilder.SetBasePath(TestUtils.TestDirectory);
#if DEBUG
confBuilder.AddJsonFile("appsettings.dev.json", true, false);
#endif
@ -267,7 +267,7 @@ namespace BTCPayServer.Tests
private string FindBTCPayServerDirectory()
{
var solutionDirectory = TestUtils.TryGetSolutionDirectoryInfo(Directory.GetCurrentDirectory());
var solutionDirectory = TestUtils.TryGetSolutionDirectoryInfo();
return Path.Combine(solutionDirectory.FullName, "BTCPayServer");
}

View File

@ -0,0 +1,13 @@
using System;
namespace BTCPayServer.Tests
{
public class OutputPathAttribute : Attribute
{
public OutputPathAttribute(string builtPath)
{
BuiltPath = builtPath;
}
public string BuiltPath { get; }
}
}

View File

@ -45,7 +45,7 @@ namespace BTCPayServer.Tests
var runInBrowser = config["RunSeleniumInBrowser"] == "true";
// Reset this using `dotnet user-secrets remove RunSeleniumInBrowser`
var chromeDriverPath = config["ChromeDriverDirectory"] ?? (Server.PayTester.InContainer ? "/usr/bin" : Directory.GetCurrentDirectory());
var chromeDriverPath = config["ChromeDriverDirectory"] ?? (Server.PayTester.InContainer ? "/usr/bin" : TestUtils.TestDirectory);
var options = new ChromeOptions();
if (!runInBrowser)

View File

@ -20,10 +20,9 @@ namespace BTCPayServer.Tests
#else
public const int TestTimeout = 90_000;
#endif
public static DirectoryInfo TryGetSolutionDirectoryInfo(string currentPath = null)
public static DirectoryInfo TryGetSolutionDirectoryInfo()
{
var directory = new DirectoryInfo(
currentPath ?? Directory.GetCurrentDirectory());
var directory = new DirectoryInfo(TestDirectory);
while (directory != null && !directory.GetFiles("*.sln").Any())
{
directory = directory.Parent;
@ -31,10 +30,15 @@ namespace BTCPayServer.Tests
return directory;
}
static TestUtils()
{
TestDirectory = ((OutputPathAttribute)typeof(TestUtils).Assembly.GetCustomAttributes(typeof(OutputPathAttribute), true)[0]).BuiltPath;
}
public readonly static string TestDirectory;
public static string GetTestDataFullPath(string relativeFilePath)
{
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
var directory = new DirectoryInfo(TestDirectory);
while (directory != null && !directory.GetFiles("*.csproj").Any())
{
directory = directory.Parent;