Files
AI-Studio/app/Tests/TestHost.cs
T

55 lines
2.1 KiB
C#

using AIStudio.Tools;
using Microsoft.Extensions.Logging.Abstractions;
//
// Deliberately without a namespace: NUnit then applies this fixture to the whole assembly, so every
// test -- the ones written today and the ones written later in some other folder -- starts with the
// static state below already in place. A second setup fixture would only ever be needed for state
// that must not leak between areas.
//
namespace AIStudio.Tests;
[SetUpFixture]
public sealed class TestHost
{
/// <summary>
/// The data directory of the tests, below which TemporaryFiles keeps its folder.
/// </summary>
public static readonly string DATA_DIRECTORY = Path.Join(Path.GetTempPath(), $"ai-studio-tests-{Guid.NewGuid():N}");
[OneTimeSetUp]
public void PrepareStaticApplicationState() => PrepareStaticState();
[OneTimeTearDown]
public void DeleteDataDirectory()
{
if (Directory.Exists(DATA_DIRECTORY))
Directory.Delete(DATA_DIRECTORY, true);
}
/// <summary>
/// Fills the static state of the app which the tests need.
/// </summary>
/// <remarks>
/// NUnit builds the cases of a TestCaseSource while it discovers the tests, before this fixture runs.
/// A source which touches a type of the app with a static logger calls this first.
/// </remarks>
public static void PrepareStaticState()
{
//
// A number of types in the app hold a static logger field that is initialized from
// Program.LOGGER_FACTORY, among them Settings.Provider. The app assigns that factory while
// Kestrel comes up; in a test process nobody does, so it stays null and the first touch of
// such a type dies inside its type initializer -- before a single assertion runs. A factory
// that writes nowhere is all it takes to get past that.
//
Program.LOGGER_FACTORY = NullLoggerFactory.Instance;
//
// The app sets its temporary folder below its data directory while it starts. The tests have no
// data directory, so the folder lies below one of their own:
//
TemporaryFiles.Initialize(DATA_DIRECTORY, NullLogger.Instance);
}
}