From ac1b83f1521af7f6a9a4360ae47bf5186e5ee496 Mon Sep 17 00:00:00 2001 From: Tyler James Leonhardt Date: Mon, 3 Feb 2020 14:03:12 -0800 Subject: [PATCH] Use `Environment.NewLine` instead of hard-coded linefeed in `ParseError.ToString` (#11746) --- .../engine/parser/Parser.cs | 2 +- .../Language/Parser/Parsing.Tests.ps1 | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs index 149c971127..b0b84572dd 100644 --- a/src/System.Management.Automation/engine/parser/Parser.cs +++ b/src/System.Management.Automation/engine/parser/Parser.cs @@ -8059,7 +8059,7 @@ namespace System.Management.Automation.Language /// public override string ToString() { - return PositionUtilities.VerboseMessage(Extent) + "\n" + Message; + return PositionUtilities.VerboseMessage(Extent) + Environment.NewLine + Message; } /// diff --git a/test/powershell/Language/Parser/Parsing.Tests.ps1 b/test/powershell/Language/Parser/Parsing.Tests.ps1 index 586d3b213c..8b79d99474 100644 --- a/test/powershell/Language/Parser/Parsing.Tests.ps1 +++ b/test/powershell/Language/Parser/Parsing.Tests.ps1 @@ -459,3 +459,23 @@ Describe "Ternary Operator parsing" -Tags CI { $expr.IfFalse | Should -BeOfType System.Management.Automation.Language.ConstantExpressionAst } } + +Describe "ParserError type tests" -Tag CI { + # This test was added because there use to be a hardcoded newline in the ToString() method of + # the ParseError class. This makes sure the proper newlines are used. + It "Should use consistant newline depending on OS" { + $ers = $null + [System.Management.Automation.Language.Parser]::ParseInput('$x =', [ref]$null, [ref]$ers) | Out-Null + $measureResult = $ers[0].ToString() -split [System.Environment]::NewLine | Measure-Object + + # We expect the string to have 4 lines. That means that if we split by NewLine for that platform, + # We should have 4 as the count. + $measureResult.Count | Should -BeExactly 4 + + # Just checking the above is not enough. We should also make sure that on non-Windows, there are no + # `r`n + if (!$IsWindows) { + $measureResult = $ers[0].ToString() | Should -Not -Contain "`r`n" + } + } +}