From 7510cb87dd7fa19df0bf4d4baba467d93a36fe93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amaury=20Lev=C3=A9?= Date: Thu, 9 Jun 2022 12:56:13 +0200 Subject: [PATCH] Prevent format exceptions when parameters array is empty (#1124) --- .../MSTest.Core/Assertions/Assert.cs | 4 ++-- .../Assertions/AssertTests.cs | 24 +++++++++++++++++++ .../Assertions/StringAssertTests.cs | 8 +++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/TestFramework/MSTest.Core/Assertions/Assert.cs b/src/TestFramework/MSTest.Core/Assertions/Assert.cs index 252ecb30e1..1fc5d3ab94 100644 --- a/src/TestFramework/MSTest.Core/Assertions/Assert.cs +++ b/src/TestFramework/MSTest.Core/Assertions/Assert.cs @@ -2374,7 +2374,7 @@ public static void Inconclusive(string message, params object[] parameters) string finalMessage = string.Empty; if (!string.IsNullOrEmpty(message)) { - if (parameters == null) + if (parameters == null || parameters.Length == 0) { finalMessage = ReplaceNulls(message); } @@ -2763,7 +2763,7 @@ internal static void HandleFail(string assertionName, string message, params obj string finalMessage = string.Empty; if (!string.IsNullOrEmpty(message)) { - if (parameters == null) + if (parameters == null || parameters.Length == 0) { finalMessage = ReplaceNulls(message); } diff --git a/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs b/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs index bbf06fdcb4..345339743d 100644 --- a/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs +++ b/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs @@ -608,5 +608,29 @@ public void AreEqualShouldFailWhenNotEqualFloatWithDelta() } #endregion + + #region HandleFail tests + [TestMethod] // See https://github.com/dotnet/sdk/issues/25373 + public void HandleFailDoesNotFailWithFormatExceptionOnEmptyParameters() + { + var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.HandleFail("name", "{")); + + Assert.IsNotNull(ex); + Assert.AreEqual(typeof(TestFrameworkV2.AssertFailedException), ex.GetType()); + StringAssert.Contains(ex.Message, "name failed. {"); + } + #endregion + + #region Inconclusive tests + [TestMethod] // See https://github.com/dotnet/sdk/issues/25373 + public void InconclusiveDoesNotFailWithFormatExceptionOnEmptyParameters() + { + var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.Assert.Inconclusive("{")); + + Assert.IsNotNull(ex); + Assert.AreEqual(typeof(TestFrameworkV2.AssertInconclusiveException), ex.GetType()); + StringAssert.Contains(ex.Message, "Assert.Inconclusive failed. {"); + } + #endregion } } diff --git a/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/StringAssertTests.cs b/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/StringAssertTests.cs index 72f64e00a4..5304d8095a 100644 --- a/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/StringAssertTests.cs +++ b/test/UnitTests/MSTest.Core.Unit.Tests/Assertions/StringAssertTests.cs @@ -97,5 +97,13 @@ public void StringAssertEndsWithIgnoreCase() var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.StringAssert.EndsWith(actual, inString, StringComparison.OrdinalIgnoreCase)); Assert.IsNull(ex); } + + [TestMethod] // See https://github.com/dotnet/sdk/issues/25373 + public void StringAssertContainsDoesNotThrowFormatException() + { + var ex = ActionUtility.PerformActionAndReturnException(() => TestFrameworkV2.StringAssert.Contains(":-{", "x")); + Assert.IsNotNull(ex); + TestFrameworkV1.StringAssert.Contains(ex.Message, "StringAssert.Contains failed"); + } } }