Files
Terminal.Gui/Tests/UnitTests/AutoInitShutdownAttribute.cs
Tig b0f32811eb Fixes #3930 - Splits tests to Tests/UnitTests, Tests/IntegrationTests, Tests/StressTests (#3954)
* Tons of API doc updates

* Removed stale test

* Removed stale tests

* Fixed Skipped Shadow test 1

* Fixed Skipped Shadow test 2

* Fixed Skipped Shadow test 3

* Removed stale test

* Removed stale test2

* Explicit unregister of event handler on Application.Driver!.ClearedContents

* Added Toplevels to dict

* code cleanup

* spelling error

* Removed stale test3

* Removed stale test4

* Removed stale test5

* added script

* tweaked script

* tweaked script

* Created StressTests project; moved some tests

* Created IntegrationTests project; moved some tests

* New yml

* made old yml just unit tests

* Tweaked Button_IsDefault_Raises_Accepted_Correctly

* tweaked script

* cleaned up ymls

* tweakled up ymls

* stress tests...

* stress tests on ubuntu only

* Fixed WindowsDriver in InvokeLeakTest

* Fixed WindowsDriver in InvokeLeakTest2

* Added Directory.Packages.props.
Added Directory.Build.props

* Shortened StressTest time

* Removed dupe file.

* DemoFiles

* Moved all tests to ./Tests dir.

* Fixed release build issue

* Fixed .sln file

* Fixed .sl* files

* Fixing ymls

* Fixing interation tests

* Create link to the file TestHelpers.

* Created Tests/UnitTestsParallelizable.
Moved all obviously parallelizable tests.
Updated yml.

* fixing logs

* fixing logs2

* fixing logs3

* don't require stress to pass for PRs

* Fix a failure?

* tweaked script

* Coudl this be it?

* Moved tons of tests to parallelizable

* Fixed some stuff

* Script to find duplicate tests

* Testing workflows

* Updated to v4

* Fix RelativeBasePath issue

* Replace powershell to pwsh

* Add ignore projects.

* Removed dupe unit tests

* Code cleanup of tests

* Cleaned up test warnings

* yml tweak

* Moved setter

* tweak ymls

* just randomly throwing spaghetti at a wall

* Enable runing 5 test runners in par

* Turned off DEBUG_DISPOSABLE for par tests

* RunningUnitTests=true

* code cleanup (forcing more Action runs)

* DISABLE_DEBUG_IDISPOSABLE

* Added View.DebugIDisposable. False by default.

* Remobed bogus tareet

* Remobed bogus tareet2

* fixed warning

* added api doc

* fixed warning

* fixed warning

* fixed warning2

* fixed warning3

* fixed warning4

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2025-03-05 23:44:27 -07:00

140 lines
5.4 KiB
C#

using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
namespace UnitTests;
/// <summary>
/// Enables test functions annotated with the [AutoInitShutdown] attribute to
/// automatically call Application.Init at start of the test and Application.Shutdown after the
/// test exits.
/// This is necessary because a) Application is a singleton and Init/Shutdown must be called
/// as a pair, and b) all unit test functions should be atomic..
/// </summary>
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]
public class AutoInitShutdownAttribute : BeforeAfterTestAttribute
{
/// <summary>
/// Initializes a [AutoInitShutdown] attribute, which determines if/how Application.Init and Application.Shutdown
/// are automatically called Before/After a test runs.
/// </summary>
/// <param name="autoInit">If true, Application.Init will be called Before the test runs.</param>
/// <param name="consoleDriverType">
/// Determines which IConsoleDriver (FakeDriver, WindowsDriver, CursesDriver, NetDriver)
/// will be used when Application.Init is called. If null FakeDriver will be used. Only valid if
/// <paramref name="autoInit"/> is true.
/// </param>
/// <param name="useFakeClipboard">
/// If true, will force the use of <see cref="FakeDriver.FakeClipboard"/>. Only valid if
/// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
/// </param>
/// <param name="fakeClipboardAlwaysThrowsNotSupportedException">
/// Only valid if <paramref name="autoInit"/> is true. Only
/// valid if <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
/// </param>
/// <param name="fakeClipboardIsSupportedAlwaysTrue">
/// Only valid if <paramref name="autoInit"/> is true. Only valid if
/// <see cref="IConsoleDriver"/> == <see cref="FakeDriver"/> and <paramref name="autoInit"/> is true.
/// </param>
/// <param name="configLocation">Determines what config file locations <see cref="ConfigurationManager"/> will load from.</param>
/// <param name="verifyShutdown">If true and <see cref="Application.Initialized"/> is true, the test will fail.</param>
public AutoInitShutdownAttribute (
bool autoInit = true,
Type consoleDriverType = null,
bool useFakeClipboard = true,
bool fakeClipboardAlwaysThrowsNotSupportedException = false,
bool fakeClipboardIsSupportedAlwaysTrue = false,
ConfigLocations configLocation = ConfigLocations.Default, // DefaultOnly is the default for tests
bool verifyShutdown = false
)
{
AutoInit = autoInit;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.GetCultureInfo ("en-US");
_driverType = consoleDriverType ?? typeof (FakeDriver);
FakeDriver.FakeBehaviors.UseFakeClipboard = useFakeClipboard;
FakeDriver.FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException =
fakeClipboardAlwaysThrowsNotSupportedException;
FakeDriver.FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
ConfigurationManager.Locations = configLocation;
_verifyShutdown = verifyShutdown;
}
private readonly bool _verifyShutdown;
private readonly Type _driverType;
public override void After (MethodInfo methodUnderTest)
{
Debug.WriteLine ($"After: {methodUnderTest.Name}");
// Turn off diagnostic flags in case some test left them on
View.Diagnostics = ViewDiagnosticFlags.Off;
if (AutoInit)
{
// try
{
if (!_verifyShutdown)
{
Application.ResetState (ignoreDisposed: true);
}
Application.Shutdown ();
#if DEBUG_IDISPOSABLE
if (View.Instances.Count == 0)
{
Assert.Empty (View.Instances);
}
else
{
View.Instances.Clear ();
}
#endif
}
//catch (Exception e)
//{
// Assert.Fail ($"Application.Shutdown threw an exception after the test exited: {e}");
//}
//finally
{
#if DEBUG_IDISPOSABLE
View.Instances.Clear ();
Application.ResetState (true);
#endif
}
}
// Reset to defaults
ConfigurationManager.Locations = ConfigLocations.Default;
ConfigurationManager.Reset ();
// Enable subsequent tests that call Init to get all config files (the default).
//Locations = ConfigLocations.All;
}
public override void Before (MethodInfo methodUnderTest)
{
Debug.WriteLine ($"Before: {methodUnderTest.Name}");
if (AutoInit)
{
#if DEBUG_IDISPOSABLE
View.DebugIDisposable = true;
// Clear out any lingering Responder instances from previous tests
if (View.Instances.Count == 0)
{
Assert.Empty (View.Instances);
}
else
{
View.Instances.Clear ();
}
#endif
Application.Init ((IConsoleDriver)Activator.CreateInstance (_driverType));
}
}
private bool AutoInit { get; }
}