|
| 1 | +// Copyright (c) 2026 SIL International |
| 2 | +// This software is licensed under the LGPL, version 2.1 or later |
| 3 | +// (http://www.gnu.org/licenses/lgpl-2.1.html) |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics; |
| 7 | +using NUnit.Framework; |
| 8 | +using NUnit.Framework.Interfaces; |
| 9 | + |
| 10 | +namespace SIL.FieldWorks.Common.FwUtils.Attributes |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// NUnit assembly-level attribute that suppresses all assertion dialog boxes during tests. |
| 14 | + /// Sets environment variables that DebugProcs.dll (native) and EnvVarTraceListener (managed) |
| 15 | + /// honor, and ensures debug trace/assert output is mirrored to the console. |
| 16 | + /// |
| 17 | + /// For test projects that link AppForTests.config, EnvVarTraceListener already handles |
| 18 | + /// assert-to-exception conversion and file logging. For the remaining projects, this |
| 19 | + /// attribute installs a listener that converts Debug.Fail into test failures. |
| 20 | + /// </summary> |
| 21 | + [AttributeUsage(AttributeTargets.Assembly)] |
| 22 | + public class SuppressAssertDialogsAttribute : TestActionAttribute |
| 23 | + { |
| 24 | + private ConsoleErrorTraceListener m_listener; |
| 25 | + |
| 26 | + /// <summary/> |
| 27 | + public override ActionTargets Targets => ActionTargets.Suite; |
| 28 | + |
| 29 | + /// <summary/> |
| 30 | + public override void BeforeTest(ITest test) |
| 31 | + { |
| 32 | + base.BeforeTest(test); |
| 33 | + |
| 34 | + // Force environment variables that control native (DebugProcs.dll) and |
| 35 | + // managed (EnvVarTraceListener) assertion behavior. |
| 36 | + Environment.SetEnvironmentVariable("AssertUiEnabled", "false"); |
| 37 | + Environment.SetEnvironmentVariable("AssertExceptionEnabled", "true"); |
| 38 | + Environment.SetEnvironmentVariable("FW_TEST_MODE", "1"); |
| 39 | + |
| 40 | + // If EnvVarTraceListener is already installed (via AppForTests.config), keep |
| 41 | + // its assert-to-exception and file logging behavior, but still mirror output |
| 42 | + // to the console. Otherwise, our listener is responsible for failing tests. |
| 43 | + bool hasEnvVarListener = false; |
| 44 | + foreach (TraceListener listener in Trace.Listeners) |
| 45 | + { |
| 46 | + // Check by type name to avoid a hard dependency on SIL.LCModel.Utils. |
| 47 | + if (listener.GetType().Name == "EnvVarTraceListener") |
| 48 | + { |
| 49 | + hasEnvVarListener = true; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Suppress the DefaultTraceListener dialog even when another listener is |
| 55 | + // present so assertions never degrade back to modal UI. |
| 56 | + foreach (TraceListener listener in Trace.Listeners) |
| 57 | + { |
| 58 | + if (listener is DefaultTraceListener dtl) |
| 59 | + { |
| 60 | + dtl.AssertUiEnabled = false; |
| 61 | + break; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + m_listener = new ConsoleErrorTraceListener(throwOnFail: !hasEnvVarListener); |
| 66 | + Trace.Listeners.Insert(0, m_listener); |
| 67 | + } |
| 68 | + |
| 69 | + /// <summary/> |
| 70 | + public override void AfterTest(ITest test) |
| 71 | + { |
| 72 | + if (m_listener != null) |
| 73 | + { |
| 74 | + Trace.Listeners.Remove(m_listener); |
| 75 | + m_listener = null; |
| 76 | + } |
| 77 | + |
| 78 | + base.AfterTest(test); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Mirrors debug trace output to Console.Error and optionally converts |
| 84 | + /// Debug.Fail/failed Debug.Assert calls into exceptions. |
| 85 | + /// </summary> |
| 86 | + internal class ConsoleErrorTraceListener : TraceListener |
| 87 | + { |
| 88 | + private readonly bool m_throwOnFail; |
| 89 | + |
| 90 | + public ConsoleErrorTraceListener(bool throwOnFail) |
| 91 | + { |
| 92 | + m_throwOnFail = throwOnFail; |
| 93 | + } |
| 94 | + |
| 95 | + public override void Fail(string message) |
| 96 | + { |
| 97 | + WriteFailure(message, null); |
| 98 | + } |
| 99 | + |
| 100 | + public override void Fail(string message, string detailMessage) |
| 101 | + { |
| 102 | + WriteFailure(message, detailMessage); |
| 103 | + } |
| 104 | + |
| 105 | + public override void Write(string message) |
| 106 | + { |
| 107 | + Console.Error.Write(message); |
| 108 | + Console.Error.Flush(); |
| 109 | + } |
| 110 | + |
| 111 | + public override void WriteLine(string message) |
| 112 | + { |
| 113 | + Console.Error.WriteLine(message); |
| 114 | + Console.Error.Flush(); |
| 115 | + } |
| 116 | + |
| 117 | + private void WriteFailure(string message, string detailMessage) |
| 118 | + { |
| 119 | + var full = string.IsNullOrEmpty(detailMessage) |
| 120 | + ? message |
| 121 | + : $"{message}{Environment.NewLine}{detailMessage}"; |
| 122 | + |
| 123 | + Console.Error.WriteLine("Debug.Fail/Assert fired during test:"); |
| 124 | + Console.Error.WriteLine(full); |
| 125 | + Console.Error.Flush(); |
| 126 | + |
| 127 | + if (m_throwOnFail) |
| 128 | + throw new AssertionDialogException(full); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// <summary> |
| 133 | + /// Exception thrown when a Debug.Fail or Debug.Assert fires during a test, |
| 134 | + /// replacing the modal Abort/Retry/Ignore dialog with a clear test failure. |
| 135 | + /// </summary> |
| 136 | + public class AssertionDialogException : Exception |
| 137 | + { |
| 138 | + public AssertionDialogException(string message) |
| 139 | + : base($"Debug.Fail/Assert fired during test (would have shown a modal dialog):\n{message}") |
| 140 | + { |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments