NetCore: MSDI refactor split CoreRuntime & Remove Current (#9356)

* Split Bootstrap code from CoreRuntime

* Make ContentService method signatures match interface
prevents need to hide the concrete class in tests

* Remove Current

Only bit that bugs me is TreeNode for IconFilePath
I get the feeling it's dead code, but can just use a setter in cs or work out path in js if required.

* Fix tests, remember to terminate IRuntime
This commit is contained in:
Paul Johnson
2020-11-10 08:50:47 +00:00
committed by GitHub
parent 190ff2ae6d
commit 3f5f85880e
50 changed files with 563 additions and 691 deletions

View File

@@ -71,7 +71,10 @@ namespace Umbraco.Tests.Integration.Testing
[TearDown]
public virtual void TearDown()
{
foreach (var a in _testTeardown) a();
if (_testTeardown != null)
{
foreach (var a in _testTeardown) a();
}
_testTeardown = null;
FirstTestInFixture = false;
FirstTestInSession = false;
@@ -88,6 +91,7 @@ namespace Umbraco.Tests.Integration.Testing
{
TestContext.Progress.Write($"Start test {TestCount++}: {TestContext.CurrentContext.Test.Name}");
}
[SetUp]
public virtual void Setup()
{
@@ -157,7 +161,7 @@ namespace Umbraco.Tests.Integration.Testing
}
/// <summary>
/// Creates a <see cref="CoreRuntime"/> instance for testing and registers an event handler for database install
/// Creates a <see cref="CoreRuntimeBootstrapper"/> instance for testing and registers an event handler for database install
/// </summary>
/// <param name="connectionStrings"></param>
/// <param name="umbracoVersion"></param>
@@ -172,7 +176,7 @@ namespace Umbraco.Tests.Integration.Testing
/// <param name="dbProviderFactoryCreator"></param>
/// <param name="globalSettings"></param>
/// <returns></returns>
public CoreRuntime CreateTestRuntime(
public CoreRuntimeBootstrapper CreateTestRuntime(
GlobalSettings globalSettings,
ConnectionStrings connectionStrings,
IUmbracoVersion umbracoVersion, IIOHelper ioHelper,
@@ -199,7 +203,7 @@ namespace Umbraco.Tests.Integration.Testing
}
/// <summary>
/// Creates a <see cref="CoreRuntime"/> instance for testing and registers an event handler for database install
/// Creates a <see cref="CoreRuntimeBootstrapper"/> instance for testing and registers an event handler for database install
/// </summary>
/// <param name="connectionStrings"></param>
/// <param name="umbracoVersion"></param>
@@ -216,15 +220,15 @@ namespace Umbraco.Tests.Integration.Testing
/// <param name="eventHandler">The event handler used for DB installation</param>
/// <param name="globalSettings"></param>
/// <returns></returns>
public static CoreRuntime CreateTestRuntime(
public static CoreRuntimeBootstrapper CreateTestRuntime(
GlobalSettings globalSettings,
ConnectionStrings connectionStrings,
IUmbracoVersion umbracoVersion, IIOHelper ioHelper,
ILoggerFactory loggerFactory, IProfiler profiler, Core.Hosting.IHostingEnvironment hostingEnvironment, IBackOfficeInfo backOfficeInfo,
ITypeFinder typeFinder, AppCaches appCaches, IDbProviderFactoryCreator dbProviderFactoryCreator,
IMainDom mainDom, Action<CoreRuntime, RuntimeEssentialsEventArgs> eventHandler)
IMainDom mainDom, Action<CoreRuntimeBootstrapper, RuntimeEssentialsEventArgs> eventHandler)
{
var runtime = new CoreRuntime(
var runtime = new CoreRuntimeBootstrapper(
globalSettings,
connectionStrings,
umbracoVersion,
@@ -292,9 +296,21 @@ namespace Umbraco.Tests.Integration.Testing
Services.GetRequiredService<IBackofficeSecurityFactory>().EnsureBackofficeSecurity();
Services.GetRequiredService<IUmbracoContextFactory>().EnsureUmbracoContext();
app.UseUmbracoCore(); // Takes 200 ms
OnTestTearDown(TerminateCoreRuntime);
}
}
/// <remarks>
/// Some IComponents hook onto static events (e.g. Published in ContentService)
/// If these fire after the components host has been shutdown, errors can occur.
/// If CoreRuntime.Start() is called We also need to de-register the events.
/// </remarks>
protected void TerminateCoreRuntime()
{
Services.GetRequiredService<IRuntime>().Terminate();
}
#endregion
#region LocalDb
@@ -303,19 +319,23 @@ namespace Umbraco.Tests.Integration.Testing
private static LocalDbTestDatabase _dbInstance;
/// <summary>
/// Event handler for the <see cref="CoreRuntime.RuntimeEssentials"/> to install the database and register the <see cref="IRuntime"/> to Terminate
/// Event handler for the <see cref="CoreRuntimeBootstrapper.RuntimeEssentials"/> to install the database
/// </summary>
/// <param name="runtime"></param>
/// <param name="runtimeBootstrapper"></param>
/// <param name="args"></param>
protected void UseTestLocalDb(CoreRuntime runtime, RuntimeEssentialsEventArgs args)
protected void UseTestLocalDb(CoreRuntimeBootstrapper runtimeBootstrapper, RuntimeEssentialsEventArgs args)
{
// MUST be terminated on teardown
OnTestTearDown(() => runtime.Terminate());
// This will create a db, install the schema and ensure the app is configured to run
InstallTestLocalDb(args.DatabaseFactory, runtime.RuntimeLoggerFactory, runtime.State, TestHelper.WorkingDirectory, out var connectionString);
TestDBConnectionString = connectionString;
InstallTestLocalDb(args.DatabaseFactory, runtimeBootstrapper.RuntimeLoggerFactory, runtimeBootstrapper.State, TestHelper.WorkingDirectory);
TestDBConnectionString = args.DatabaseFactory.ConnectionString;
InMemoryConfiguration["ConnectionStrings:" + Constants.System.UmbracoConnectionName] = TestDBConnectionString;
// Re-configure IOptions<ConnectionStrings> now that we have a test db
// This is what will be resolved first time IUmbracoDatabaseFactory is resolved from container (e.g. post CoreRuntime bootstrap)
args.Composition.Services.Configure<ConnectionStrings>((x) =>
{
x.UmbracoConnectionString = new ConfigConnectionString(Constants.System.UmbracoConnectionName, TestDBConnectionString);
});
}
/// <summary>
@@ -356,9 +376,8 @@ namespace Umbraco.Tests.Integration.Testing
/// <returns></returns>
private void InstallTestLocalDb(
IUmbracoDatabaseFactory databaseFactory, ILoggerFactory loggerFactory,
IRuntimeState runtimeState, string workingDirectory, out string connectionString)
IRuntimeState runtimeState, string workingDirectory)
{
connectionString = null;
var dbFilePath = Path.Combine(workingDirectory, "LocalDb");
// get the currently set db options
@@ -461,7 +480,6 @@ namespace Umbraco.Tests.Integration.Testing
default:
throw new ArgumentOutOfRangeException(nameof(testOptions), testOptions, null);
}
connectionString = db.ConnectionString;
}
#endregion