Skip to content

Commit

Permalink
Fix issue with validation failing for mismatched line endings in verb…
Browse files Browse the repository at this point in the history
…atim strings (#342)

closes #244
  • Loading branch information
belav authored Jun 28, 2021
1 parent 6b624d9 commit 3a37426
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ public void Should_Format_StandardInput_When_Provided()
lines.First().Should().Be(FormattedClassContent);
}

[Test]
public void File_With_Mismatched_Line_Endings_In_Verbatim_String_Should_Pass_Validation()
{
WhenAFileExists(
"file1.cs",
"public class ClassName\n{\npublic string Value = @\"EndThisLineWith\r\nEndThisLineWith\n\";\n}"
);

var (exitCode, _) = this.Format();

exitCode.Should().Be(0);
}

private (int exitCode, IList<string> lines) Format(
bool skipWrite = false,
bool check = false,
Expand Down
18 changes: 18 additions & 0 deletions Src/CSharpier.Tests/SyntaxNodeComparerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ public class ClassName { }
);
}

[TestCase("@")]
[TestCase("@$")]
[TestCase("$@")]
public void Mismatched_Line_Endings_In_Verbatim_String_Should_Not_Print_Error(string start)
{
var left =
"public class ClassName\n{\npublic string Value = "
+ start
+ "\"EndThisLineWith\r\nEndThisLineWith\n\";\n}";
var right =
"public class ClassName\n{\npublic string Value = "
+ start
+ "\"EndThisLineWith\nEndThisLineWith\n\";\n}";

var result = this.AreEqual(left, right);
result.Should().BeEmpty();
}

private void ResultShouldBe(string result, string be)
{
if (Environment.GetEnvironmentVariable("NormalizeLineEndings") != null)
Expand Down
8 changes: 7 additions & 1 deletion Src/CSharpier/SyntaxNodeComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CSharpier.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace CSharpier
Expand Down Expand Up @@ -189,7 +191,11 @@ private CompareResult Compare(
SyntaxNode? originalNode,
SyntaxNode? formattedNode
) {
if (originalToken.Text != formattedToken.Text)
// when a verbatim string contains mismatched line endings they will become consistent
// this validation will fail unless we also get them consistent here
// adding a semi-complicated if check to determine when to do the string replacement
// did not appear to have any performance benefits
if (originalToken.Text.Replace("\r", "") != formattedToken.Text.Replace("\r", ""))
{
return NotEqual(
originalToken.RawKind == 0 ? originalNode?.Span : originalToken.Span,
Expand Down

0 comments on commit 3a37426

Please sign in to comment.