Files
OpenNest/OpenNest.WinForms.Tests/TestAssemblySetup.cs
T
ajandClaude Sonnet 5 5f9e8814a7 test(desktop): fix Windows-only WinForms and cross-platform test failures
The new windows-desktop CI job surfaced several tests that had never
actually run on Windows:

- SeparatorPenLifetimeTests expected Control.Disposed to fire only
  once; current WinForms re-fires it on every Dispose() call.
- EditNestFocusTraversalTests assumed the Plates tab was selected by
  default, which stopped being true once Drawings became the landing
  tab; select it explicitly before asserting focus.
- NestReportExportTests asserted an exact plate count that ignored
  PlateManager's trailing sentinel plate, and didn't expect the nest
  name in the busy-export message.
- AtomicReportFileTests only expected IOException when replacing a
  directory target; Windows raises UnauthorizedAccessException instead,
  which callers already treat the same way.

Also register a ThreadException handler so an exception inside a
WndProc callback fails the test normally instead of popping a modal
dialog that blocks the desktop.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-04 18:24:20 -04:00

23 lines
1.0 KiB
C#

using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace OpenNest.WinForms.Tests;
internal static class TestAssemblySetup
{
// SetUnhandledExceptionMode only changes which *default* dialog style WinForms would
// show; it still shows one. A registered ThreadException handler is what actually
// suppresses it (WinForms always prefers the handler over the default dialog), which
// matters here because none of our tests call Application.Run() to install one.
// Without this, an exception thrown inside a WndProc callback (e.g. a DataGridView
// commit failure during Show()) pops a modal ThreadExceptionDialog that blocks the
// desktop until someone dismisses it by hand, instead of failing the test normally.
[ModuleInitializer]
internal static void Initialize()
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
Application.ThreadException += (_, e) =>
Console.Error.WriteLine($"Suppressed ThreadExceptionDialog: {e.Exception}");
}
}