Skip to content

Commit f7c21b9

Browse files
authored
Fix empty configuation when tests run by Rider (#151)
* Fixed running tests in Rider * Added missing Microsoft.NET.Test.Sdk to common tests
1 parent 068e629 commit f7c21b9

File tree

6 files changed

+16
-40
lines changed

6 files changed

+16
-40
lines changed

CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/TestsContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestsContext
1313
public void RunBeforeAnyTests()
1414
{
1515
#if !NETFX
16-
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, "coredistributedcache");
16+
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, ConfigurationProvider.SetConfiguration);
1717
#endif
1818
ConfigureLog4Net();
1919
}

CoreMemoryCache/NHibernate.Caches.CoreMemoryCache.Tests/TestsContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestsContext
1313
public void RunBeforeAnyTests()
1414
{
1515
#if !NETFX
16-
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, "corememorycache");
16+
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, ConfigurationProvider.SetConfiguration);
1717
#endif
1818
ConfigureLog4Net();
1919
}

NHibernate.Caches.Common.Tests/NHibernate.Caches.Common.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
2424
<PackageReference Include="NUnitLite" Version="3.14.0" />
2525
</ItemGroup>
26+
<ItemGroup>
27+
<PackageReference Include="log4net" Version="2.0.17" />
28+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.2" />
29+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
30+
</ItemGroup>
2631
<ItemGroup>
2732
<ProjectReference Include="..\NHibernate.Caches.Common\NHibernate.Caches.Common.csproj" />
2833
</ItemGroup>

NHibernate.Caches.Common.Tests/TestsContextHelper.cs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,35 @@
11
#if !NETFX
2+
using System;
23
using System.Configuration;
34
using System.IO;
45
using System.Reflection;
5-
using NHibernate.Cfg;
66
using NHibernate.Cfg.ConfigurationSchema;
77
using NUnit.Framework;
8-
using Environment = NHibernate.Cfg.Environment;
98

109
namespace NHibernate.Caches.Common.Tests
1110
{
1211
public static class TestsContextHelper
1312
{
14-
public static bool ExecutingWithVsTest { get; } =
15-
Assembly.GetEntryAssembly()?.GetName().Name == "testhost";
16-
17-
private static bool _removeTesthostConfig;
18-
19-
public static void RunBeforeAnyTests(Assembly testAssembly, string configSectionName)
13+
public static void RunBeforeAnyTests(Assembly testAssembly, Action<Configuration> configure)
2014
{
2115
//When .NET Core App 2.0 tests run from VS/VSTest the entry assembly is "testhost.dll"
2216
//so we need to explicitly load the configuration
23-
if (ExecutingWithVsTest)
17+
if (Assembly.GetEntryAssembly() != null)
2418
{
2519
var assemblyPath =
2620
Path.Combine(TestContext.CurrentContext.TestDirectory, Path.GetFileName(testAssembly.Location));
27-
Environment.InitializeGlobalProperties(GetTestAssemblyHibernateConfiguration(assemblyPath));
28-
ReadCoreCacheSectionFromTesthostConfig(assemblyPath, configSectionName);
21+
var configuration = ConfigurationManager.OpenExeConfiguration(assemblyPath);
22+
Cfg.Environment.InitializeGlobalProperties(GetTestAssemblyHibernateConfiguration(configuration));
23+
configure?.Invoke(configuration);
2924
}
3025
}
3126

3227
public static void RunAfterAnyTests()
3328
{
34-
if (_removeTesthostConfig)
35-
{
36-
File.Delete(GetTesthostConfigPath());
37-
}
38-
}
39-
40-
private static void ReadCoreCacheSectionFromTesthostConfig(string assemblyPath, string configSectionName)
41-
{
42-
// For caches section, ConfigurationManager being used, the only general workaround is to provide
43-
// the configuration with its expected file name... (Another option would be to explicitly setup each cache
44-
// ConfigurationProvider.)
45-
var configPath = assemblyPath + ".config";
46-
// If this copy fails: either testconfig has started having its own file, and this hack can no more be used,
47-
// or a previous test run was interrupted before its cleanup (RunAfterAnyTests): go clean it manually.
48-
// Discussion about this mess: https://github.com/dotnet/corefx/issues/22101
49-
File.Copy(configPath, GetTesthostConfigPath());
50-
_removeTesthostConfig = true;
51-
ConfigurationManager.RefreshSection(configSectionName);
52-
}
53-
54-
private static string GetTesthostConfigPath()
55-
{
56-
return Assembly.GetEntryAssembly().Location + ".config";
5729
}
5830

59-
private static IHibernateConfiguration GetTestAssemblyHibernateConfiguration(string assemblyPath)
31+
private static Cfg.IHibernateConfiguration GetTestAssemblyHibernateConfiguration(Configuration configuration)
6032
{
61-
var configuration = ConfigurationManager.OpenExeConfiguration(assemblyPath);
6233
var section = configuration.GetSection(CfgXmlHelper.CfgSectionName);
6334
return HibernateConfiguration.FromAppConfig(section.SectionInformation.GetRawXml());
6435
}

RtMemoryCache/NHibernate.Caches.RtMemoryCache.Tests/TestsContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestsContext
1313
public void RunBeforeAnyTests()
1414
{
1515
#if !NETFX
16-
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, "coredistributedcache");
16+
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, ConfigurationProvider.SetConfiguration);
1717
#endif
1818
ConfigureLog4Net();
1919
}

StackExchangeRedis/NHibernate.Caches.StackExchangeRedis.Tests/TestsContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestsContext
1313
public void RunBeforeAnyTests()
1414
{
1515
#if !NETFX
16-
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, "redis");
16+
TestsContextHelper.RunBeforeAnyTests(typeof(TestsContext).Assembly, ConfigurationProvider.SetConfiguration);
1717
#endif
1818
ConfigureLog4Net();
1919
}

0 commit comments

Comments
 (0)