Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fa8b2f7
Initial changes to new error view
SteveL-MSFT Sep 25, 2019
ae20c6f
make $ErrorView an enum. Move Analytic view entirely to formatxml. …
SteveL-MSFT Sep 27, 2019
71e4893
rename Analytic to Concise and fix it so invocation name doesn't show…
SteveL-MSFT Sep 27, 2019
8471e8e
use command name as reason if it exists to different multiple cmdlets…
SteveL-MSFT Sep 27, 2019
e88a02d
rename enum to include View suffix
SteveL-MSFT Sep 27, 2019
c38c601
move vt100 helpers as private function to Get-ConciseViewPositionMess…
SteveL-MSFT Sep 27, 2019
63421fb
add tests
SteveL-MSFT Sep 27, 2019
2dfad59
remove unused variable
SteveL-MSFT Sep 27, 2019
9c0411e
fix redirection test
SteveL-MSFT Sep 27, 2019
313d37a
fix test failures
SteveL-MSFT Sep 28, 2019
2c73723
another attempt to fix write-error test that fails in CI but not locally
SteveL-MSFT Sep 28, 2019
e2c9ee8
fix redirection in write-error test
SteveL-MSFT Sep 28, 2019
fde04e2
address Ilya's comments
SteveL-MSFT Sep 28, 2019
a9e5b46
fix consolehost test
SteveL-MSFT Sep 28, 2019
b7ec14b
Change `At` to `In`. Add highlight to where error is in line
SteveL-MSFT Sep 28, 2019
deba3af
change `In` to lowercase
SteveL-MSFT Sep 28, 2019
f5738eb
add verification of line number
SteveL-MSFT Sep 28, 2019
fe59da5
make the error message flow better in concise mode
SteveL-MSFT Sep 30, 2019
0284146
fix case where message is multiple lines
SteveL-MSFT Sep 30, 2019
3500ff3
fix so message is already error color
SteveL-MSFT Oct 1, 2019
adee296
when showing error with line, replace newlines so the message lines u…
SteveL-MSFT Oct 1, 2019
30e1bc8
fix small alignment issue
SteveL-MSFT Oct 1, 2019
4066273
fix some rendering of parser messages
SteveL-MSFT Oct 1, 2019
ba92741
make `error in` text in error color, fix rendering of long error mess…
SteveL-MSFT Oct 2, 2019
4951806
Update test/powershell/Host/ConsoleHost.Tests.ps1
SteveL-MSFT Oct 4, 2019
f03a0a1
address Jim's comments
SteveL-MSFT Oct 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,17 @@ public ConsoleColorProxy(ConsoleHostUserInterface ui)
_ui = ui;
}

public ConsoleColor ErrorAccentColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
get
{ return _ui.ErrorAccentColor; }

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
set
{ _ui.ErrorAccentColor = value; }
}

public ConsoleColor ErrorForegroundColor
{
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,7 @@ public override void WriteErrorLine(string value)
}

// Error colors
public ConsoleColor ErrorAccentColor { get; set; } = ConsoleColor.Cyan;
public ConsoleColor ErrorForegroundColor { get; set; } = ConsoleColor.Red;
public ConsoleColor ErrorBackgroundColor { get; set; } = Console.BackgroundColor;

Expand Down

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions src/System.Management.Automation/engine/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,23 @@ internal void InternalDispose(bool isDisposing)

namespace System.Management.Automation
{
#region ErrorView
/// <summary>
/// Defines the potential ErrorView options.
/// </summary>
public enum ErrorView
{
/// <summary>Existing all red multi-line output.</summary>
NormalView = 0,

/// <summary>Only show category information.</summary>
CategoryView = 1,

/// <summary>Concise shows more information on the context of the error or just the message if not a script or parser error.</summary>
ConciseView = 2,
}
#endregion ErrorView

#region ActionPreference
/// <summary>
/// Defines the Action Preference options. These options determine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ static ExperimentalFeature()
description: "New parameter set for ForEach-Object to run script blocks in parallel"),
new ExperimentalFeature(
name: "PSTernaryOperator",
description: "Support the ternary operator in PowerShell language")
description: "Support the ternary operator in PowerShell language"),
new ExperimentalFeature(
name: "PSErrorView",
description: "New formatting for ErrorRecord")
};
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4326,6 +4326,8 @@ .ForwardHelpCategory Cmdlet
internal const ActionPreference defaultVerbosePreference = ActionPreference.SilentlyContinue;
internal const ActionPreference defaultWarningPreference = ActionPreference.Continue;
internal const ActionPreference defaultInformationPreference = ActionPreference.SilentlyContinue;

internal const ErrorView defaultErrorView = ErrorView.NormalView;
internal const bool defaultWhatIfPreference = false;
internal const ConfirmImpact defaultConfirmPreference = ConfirmImpact.High;

Expand Down Expand Up @@ -4408,8 +4410,10 @@ .ForwardHelpCategory Cmdlet
),
new SessionStateVariableEntry(
SpecialVariables.ErrorView,
"NormalView",
RunspaceInit.ErrorViewDescription
ExperimentalFeature.IsEnabled("PSErrorView") ? ErrorView.ConciseView : defaultErrorView,
RunspaceInit.ErrorViewDescription,
ScopedItemOptions.None,
new ArgumentTypeConverterAttribute(typeof(ErrorView))
),
new SessionStateVariableEntry(
SpecialVariables.NestedPromptLevel,
Expand Down
22 changes: 17 additions & 5 deletions src/System.Management.Automation/engine/parser/Position.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,32 @@ internal static string VerboseMessage(IScriptExtent position)
}

if (needsPrefixDots)
sb.Append("... ");
{
sb.Append("\u2026 "); // Unicode ellipsis character
}

sb.Append(sourceLine);

if (needsSuffixDots)
sb.Append(" ...");
{
sb.Append(" \u2026"); // Unicode ellipsis character
}

sb.Append(Environment.NewLine);
sb.Append("+ ");
sb.Append(' ', spacesBeforeError + (needsPrefixDots ? 4 : 0));
sb.Append(' ', spacesBeforeError + (needsPrefixDots ? 2 : 0));
// errorLength of 0 happens at EOF - always write out 1.
sb.Append('~', errorLength > 0 ? errorLength : 1);

message = sb.ToString();
}

return StringUtil.Format(ParserStrings.TextForPositionMessage, fileName, position.StartLineNumber,
position.StartColumnNumber, message);
return StringUtil.Format(
ParserStrings.TextForPositionMessage,
fileName,
position.StartLineNumber,
position.StartColumnNumber,
message);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export $envVarName='$guid'
It "errors are in text if error is redirected, encoded command, non-interactive, and outputformat specified" {
$p = [Diagnostics.Process]::new()
$p.StartInfo.FileName = "pwsh"
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes("throw 'boom'"))
$encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes('$ErrorView="NormalView";throw "boom"'))
$p.StartInfo.Arguments = "-EncodedCommand $encoded -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -OutputFormat text"
$p.StartInfo.UseShellExecute = $false
$p.StartInfo.RedirectStandardError = $true
Expand Down
9 changes: 9 additions & 0 deletions test/powershell/Language/Parser/RedirectionOperator.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ Describe "File redirection mixed with Out-Null" -Tags CI {
}

Describe "File redirection should have 'DoComplete' called on the underlying pipeline processor" -Tags CI {
BeforeAll {
$originalErrorView = $ErrorView
$ErrorView = "NormalView"
}

AfterAll {
$ErrorView = $originalErrorView
}

It "File redirection should result in the same file as Out-File" {
$object = [pscustomobject] @{ one = 1 }
$redirectFile = Join-Path $TestDrive fileRedirect.txt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,14 @@ try
BeforeAll {
if ($skipTest) { return }
$module = Import-PSSession -Session $session -Name Get-Variable -Prefix My -AllowClobber
$oldErrorView = $ErrorView
$ErrorView = "NormalView"
}

AfterAll {
if ($skipTest) { return }
if ($null -ne $module) { Remove-Module $module -Force -ErrorAction SilentlyContinue }
$ErrorView = $oldErrorView
}

It "Test non-terminating error" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ Describe "Write-Error Tests" -Tags "CI" {
while ($longtext.Length -lt [console]::WindowWidth) {
$longtext += $longtext
}
$pwsh = $pshome + "/pwsh"
$result = & $pwsh -c Write-Error -Message $longtext 2>&1
$result.Count | Should -BeExactly 4
$result = pwsh -noprofile -command "`$ErrorView = 'NormalView'; Write-Error -Message '$longtext'" 2>&1
$result.Count | Should -BeExactly 3
$result[0] | Should -Match $longtext
}
}
49 changes: 49 additions & 0 deletions test/powershell/engine/Formatting/ErrorView.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe 'Tests for $ErrorView' -Tag CI {

It '$ErrorView is an enum' {
$ErrorView | Should -BeOfType [System.Management.Automation.ErrorView]
}

It '$ErrorView should have correct default value' {
$expectedDefault = 'NormalView'

if ((Get-ExperimentalFeature -Name PSErrorView).Enabled) {
$expectedDefault = 'ConciseView'
}

$ErrorView | Should -BeExactly $expectedDefault
}

Context 'ConciseView tests' {

It 'Cmdlet error should be one line of text' {
Get-Item (New-Guid) -ErrorVariable e -ErrorAction SilentlyContinue
($e | Out-String).Trim().Count | Should -Be 1
}

It 'Script error should contain path to script and line for error' {
$testScript = @'
[cmdletbinding()]
param()
$a = 1
123)
$b = 2
'@

$testScriptPath = Join-Path -Path $TestDrive -ChildPath 'test.ps1'
Set-Content -Path $testScriptPath -Value $testScript
$e = { & $testScriptPath } | Should -Throw -ErrorId 'UnexpectedToken' -PassThru
$e | Out-String | Should -BeLike "*$testScriptPath*"
# validate line number is shown
$e | Out-String | Should -BeLike '* 4 *'
}

It "Remote errors show up correctly" {
Start-Job -ScriptBlock { get-item (new-guid) } | Wait-Job | Receive-Job -ErrorVariable e -ErrorAction SilentlyContinue
($e | Out-String).Trim().Count | Should -Be 1
}
}
}