Skip to content

Commit

Permalink
Added fixes for tests which are not robust against line endings (dotn…
Browse files Browse the repository at this point in the history
…et#68231)

* Added Fix For System.CodeDom.Tests

* Added Fix for Microsoft.XmlSerializer.Generator tests

* Removed unnecessary File IO

* Substituted LineEndingsHelper.Normalize
  • Loading branch information
Danyy427 authored and directhex committed Apr 21, 2022
1 parent d0a5d5d commit d581835
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static string Normalize(string expected)
if (s_consistentNewlines)
return expected;

return expected.Replace(CompiledNewline, Environment.NewLine);
return expected.Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.


using Xunit;
using Microsoft.XmlSerializer.Generator;
using System.IO;
Expand Down Expand Up @@ -31,6 +30,7 @@ public static void SgenCommandTest()

const string CodeFile = "SerializableAssembly.XmlSerializers.cs";
const string LKGCodeFile = "Expected.SerializableAssembly.XmlSerializers.cs";

var type = Type.GetType("Microsoft.XmlSerializer.Generator.Sgen, dotnet-Microsoft.XmlSerializer.Generator");
MethodInfo md = type.GetMethod("Main", BindingFlags.Static | BindingFlags.Public);
string[] args = new string[] { "SerializableAssembly.dll", "--force", "--quiet" };
Expand All @@ -40,7 +40,7 @@ public static void SgenCommandTest()
Assert.True(File.Exists(CodeFile), string.Format("Fail to generate {0}.", CodeFile));
// Compare the generated CodeFiles from the LKG with the live built shared framework one.
// Not comparing byte per byte as the generated output isn't deterministic.
Assert.Equal(new System.IO.FileInfo(LKGCodeFile).Length, new System.IO.FileInfo(CodeFile).Length);
Assert.Equal(LineEndingsHelper.Normalize(File.ReadAllText(LKGCodeFile)).Length, File.ReadAllText(CodeFile).Length);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2583,9 +2583,11 @@ public void GenerateCodeFromType_Invoke_Success(CodeTypeDeclaration e, CodeGener

private void AssertEqualLong(string expected, string actual)
{
string normalizedExpected = LineEndingsHelper.Normalize(expected);

try
{
Assert.Equal(LineEndingsHelper.Normalize(expected), actual);
Assert.Equal(normalizedExpected, actual);
}
catch (Xunit.Sdk.AssertActualExpectedException)
{
Expand All @@ -2599,7 +2601,7 @@ string Normalize(string s)

var s = new StringBuilder();
s.AppendLine();
s.AppendLine($"Expected: {Environment.NewLine}$@\"{Normalize(expected)}\"");
s.AppendLine($"Expected: {Environment.NewLine}$@\"{Normalize(normalizedExpected)}\"");
s.AppendLine($"Actual: {Environment.NewLine}$@\"{Normalize(actual)}\"");

throw new Exception(s.ToString());
Expand Down

0 comments on commit d581835

Please sign in to comment.