|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +using Microsoft.TestPlatform.Extensions.TrxLogger.Utility; |
| 10 | +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Client; |
| 11 | +using Microsoft.VisualStudio.TestPlatform.ObjectModel.Logging; |
| 12 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 13 | + |
| 14 | +using Moq; |
| 15 | + |
| 16 | +using TrxLoggerConstants = Microsoft.TestPlatform.Extensions.TrxLogger.Utility.Constants; |
| 17 | +using TrxLoggerObjectModel = Microsoft.TestPlatform.Extensions.TrxLogger.ObjectModel; |
| 18 | + |
| 19 | +using DefaultLoggerParameterNames = Microsoft.VisualStudio.TestPlatform.ObjectModel.DefaultLoggerParameterNames; |
| 20 | + |
| 21 | +namespace Microsoft.TestPlatform.Extensions.TrxLogger.UnitTests; |
| 22 | + |
| 23 | +/// <summary> |
| 24 | +/// Regression tests for: |
| 25 | +/// - GH-2319: Setting ErrorStackTrace before ErrorMessage must not crash. |
| 26 | +/// - GH-5132: WarnOnFileOverwrite=false must suppress overwrite warning. |
| 27 | +/// - GH-4243: TestOutcome.Error must have value 0, must serialize as "Error" (not "Min"). |
| 28 | +/// </summary> |
| 29 | +[TestClass] |
| 30 | +public class RegressionBugFixTests |
| 31 | +{ |
| 32 | + private static readonly string DefaultTestRunDirectory = Path.GetTempPath(); |
| 33 | + |
| 34 | + #region GH-2319: ErrorStackTrace without ErrorMessage doesn't crash |
| 35 | + |
| 36 | + [TestMethod] |
| 37 | + public void ErrorStackTrace_SetBeforeErrorMessage_MustNotThrow() |
| 38 | + { |
| 39 | + // GH-2319: Previously, setting ErrorStackTrace first would crash with Debug.Assert |
| 40 | + // because _errorInfo was null. The fix uses ??= to lazily initialize. |
| 41 | + var testResult = CreateTestResult(); |
| 42 | + |
| 43 | + // Act: set ErrorStackTrace FIRST, before any ErrorMessage |
| 44 | + testResult.ErrorStackTrace = "at SomeTest.Method() in file.cs:line 42"; |
| 45 | + |
| 46 | + // Assert: should not throw and ErrorMessage should return empty string (not null or crash) |
| 47 | + Assert.AreEqual("at SomeTest.Method() in file.cs:line 42", testResult.ErrorStackTrace); |
| 48 | + Assert.AreEqual(string.Empty, testResult.ErrorMessage, |
| 49 | + "GH-2319: ErrorMessage must return empty string when only ErrorStackTrace is set."); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void ErrorMessage_ThenErrorStackTrace_BothAccessible() |
| 54 | + { |
| 55 | + // Setting ErrorMessage first, then ErrorStackTrace: both must be readable. |
| 56 | + var testResult = CreateTestResult(); |
| 57 | + |
| 58 | + testResult.ErrorMessage = "Assert.Fail hit"; |
| 59 | + testResult.ErrorStackTrace = "at MyTest.Run()"; |
| 60 | + |
| 61 | + Assert.AreEqual("Assert.Fail hit", testResult.ErrorMessage); |
| 62 | + Assert.AreEqual("at MyTest.Run()", testResult.ErrorStackTrace); |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public void ErrorMessage_NeverSet_ReturnsEmptyString() |
| 67 | + { |
| 68 | + // Before the fix, accessing getters on a fresh TestResult without _errorInfo |
| 69 | + // would behave unpredictably. The fix returns string.Empty via null-coalescing. |
| 70 | + var testResult = CreateTestResult(); |
| 71 | + |
| 72 | + Assert.AreEqual(string.Empty, testResult.ErrorMessage); |
| 73 | + Assert.AreEqual(string.Empty, testResult.ErrorStackTrace); |
| 74 | + } |
| 75 | + |
| 76 | + #endregion |
| 77 | + |
| 78 | + #region GH-5132: WarnOnFileOverwrite parameter |
| 79 | + |
| 80 | + [TestMethod] |
| 81 | + public void Initialize_WarnOnFileOverwriteFalse_FieldMustBeFalse() |
| 82 | + { |
| 83 | + // GH-5132: When WarnOnFileOverwrite=false, the _warnOnFileOverwrite field must be false. |
| 84 | + // If the fix were reverted (field removed), this would fail. |
| 85 | + var logger = new TestableTrxLogger(); |
| 86 | + var events = new Mock<TestLoggerEvents>(); |
| 87 | + var parameters = CreateDefaultParameters(); |
| 88 | + parameters[TrxLoggerConstants.WarnOnFileOverwrite] = "false"; |
| 89 | + |
| 90 | + logger.Initialize(events.Object, parameters); |
| 91 | + |
| 92 | + var warnField = typeof(VisualStudio.TestPlatform.Extensions.TrxLogger.TrxLogger) |
| 93 | + .GetField("_warnOnFileOverwrite", BindingFlags.NonPublic | BindingFlags.Instance); |
| 94 | + Assert.IsNotNull(warnField, "_warnOnFileOverwrite field must exist."); |
| 95 | + Assert.IsFalse((bool)warnField.GetValue(logger)!, |
| 96 | + "GH-5132: _warnOnFileOverwrite must be false when parameter is 'false'."); |
| 97 | + } |
| 98 | + |
| 99 | + [TestMethod] |
| 100 | + public void Initialize_WarnOnFileOverwriteNotSet_DefaultsToTrue() |
| 101 | + { |
| 102 | + // GH-5132: When WarnOnFileOverwrite parameter is not provided, default must be true |
| 103 | + // (preserving existing behavior for users who did not opt out). |
| 104 | + var logger = new TestableTrxLogger(); |
| 105 | + var events = new Mock<TestLoggerEvents>(); |
| 106 | + var parameters = CreateDefaultParameters(); |
| 107 | + |
| 108 | + logger.Initialize(events.Object, parameters); |
| 109 | + |
| 110 | + var warnField = typeof(VisualStudio.TestPlatform.Extensions.TrxLogger.TrxLogger) |
| 111 | + .GetField("_warnOnFileOverwrite", BindingFlags.NonPublic | BindingFlags.Instance); |
| 112 | + Assert.IsNotNull(warnField, "_warnOnFileOverwrite field must exist."); |
| 113 | + Assert.IsTrue((bool)warnField.GetValue(logger)!, |
| 114 | + "GH-5132: _warnOnFileOverwrite must default to true when parameter is not provided."); |
| 115 | + } |
| 116 | + |
| 117 | + #endregion |
| 118 | + |
| 119 | + #region GH-4243: TestOutcome has no Min/Max aliases |
| 120 | + |
| 121 | + [TestMethod] |
| 122 | + public void TestOutcome_Error_MustHaveIntValue0() |
| 123 | + { |
| 124 | + // GH-4243: Error must be the first enum member (value 0). |
| 125 | + // Previously Min=Error alias made Error.ToString() return "Min". |
| 126 | + // Box through object to avoid compile-time constant folding (MSTEST0032). |
| 127 | + object errorEnum = TrxLoggerObjectModel.TestOutcome.Error; |
| 128 | + Assert.AreEqual(0, (int)(TrxLoggerObjectModel.TestOutcome)errorEnum, |
| 129 | + "GH-4243: TestOutcome.Error must have integer value 0."); |
| 130 | + } |
| 131 | + |
| 132 | + [TestMethod] |
| 133 | + public void TestOutcome_Error_MustSerializeAsError_NotMin() |
| 134 | + { |
| 135 | + // GH-4243: The fix removed Min=Error alias. Error.ToString() must return "Error". |
| 136 | + // If the fix were reverted and Min=Error added back, ToString() would return "Min". |
| 137 | + Assert.AreEqual("Error", TrxLoggerObjectModel.TestOutcome.Error.ToString(), |
| 138 | + "GH-4243: TestOutcome.Error.ToString() must be 'Error', not 'Min'."); |
| 139 | + } |
| 140 | + |
| 141 | + [TestMethod] |
| 142 | + public void TestOutcome_NoMinOrMaxMember() |
| 143 | + { |
| 144 | + // GH-4243: The enum must NOT have a member named "Min" or "Max". |
| 145 | + // Verify via reflection. |
| 146 | + var names = Enum.GetNames(typeof(TrxLoggerObjectModel.TestOutcome)); |
| 147 | + CollectionAssert.DoesNotContain(names, "Min", |
| 148 | + "GH-4243: TestOutcome enum must not have a 'Min' member."); |
| 149 | + CollectionAssert.DoesNotContain(names, "Max", |
| 150 | + "GH-4243: TestOutcome enum must not have a 'Max' member."); |
| 151 | + } |
| 152 | + |
| 153 | + #endregion |
| 154 | + |
| 155 | + #region Helpers |
| 156 | + |
| 157 | + private static TrxLoggerObjectModel.TestResult CreateTestResult() |
| 158 | + { |
| 159 | + return new TrxLoggerObjectModel.TestResult( |
| 160 | + runId: Guid.NewGuid(), |
| 161 | + testId: Guid.NewGuid(), |
| 162 | + executionId: Guid.NewGuid(), |
| 163 | + parentExecutionId: Guid.Empty, |
| 164 | + resultName: "TestResult1", |
| 165 | + computerName: Environment.MachineName, |
| 166 | + outcome: TrxLoggerObjectModel.TestOutcome.Failed, |
| 167 | + testType: new TrxLoggerObjectModel.TestType(Guid.NewGuid()), |
| 168 | + testCategoryId: TrxLoggerObjectModel.TestListCategoryId.Uncategorized, |
| 169 | + trxFileHelper: new TrxFileHelper()); |
| 170 | + } |
| 171 | + |
| 172 | + private static Dictionary<string, string?> CreateDefaultParameters() |
| 173 | + { |
| 174 | + return new Dictionary<string, string?> |
| 175 | + { |
| 176 | + [DefaultLoggerParameterNames.TestRunDirectory] = DefaultTestRunDirectory, |
| 177 | + [TrxLoggerConstants.LogFileNameKey] = "test.trx" |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + #endregion |
| 182 | +} |
0 commit comments