Skip to content

Commit

Permalink
Prevent format exceptions when parameters array is empty (#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Evangelink committed Jun 9, 2022
1 parent 99ccf0d commit 7510cb8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/TestFramework/MSTest.Core/Assertions/Assert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions test/UnitTests/MSTest.Core.Unit.Tests/Assertions/AssertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}

0 comments on commit 7510cb8

Please sign in to comment.