Skip to content

Commit 817a3f6

Browse files
committed
Guard expansion of PSPropertyInfo.Value
Which fixes our test that gets the properties of a process object, since the value for `ExitCode` fails (as the process hasn't ended), we now handle that a bit more gracefully and can get all 130 properties.
1 parent d2d1202 commit 817a3f6

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/Debugging/VariableDetails.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public VariableDetails(PSObject psVariableObject)
6868
/// The PSPropertyInfo instance from which variable details will be obtained.
6969
/// </param>
7070
public VariableDetails(PSPropertyInfo psProperty)
71-
: this(psProperty.Name, psProperty.Value)
71+
: this(psProperty.Name, SafeGetValue(psProperty))
7272
{
7373
}
7474

@@ -113,6 +113,20 @@ public override VariableDetailsBase[] GetChildren(ILogger logger)
113113

114114
#region Private Methods
115115

116+
private static object SafeGetValue(PSPropertyInfo psProperty)
117+
{
118+
try
119+
{
120+
return psProperty.Value;
121+
}
122+
catch (GetValueInvocationException ex)
123+
{
124+
// Sometimes we can't get the value, like ExitCode, for reasons beyond our control,
125+
// so just return the message from the exception that arises.
126+
return ex.Message;
127+
}
128+
}
129+
116130
private static bool GetIsExpandable(object valueObject)
117131
{
118132
if (valueObject == null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$file = Get-ChildItem -Path "." | Select-Object -First 1
2+
Write-Host "Debug over"

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+29-3
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,8 @@ await debugService.SetLineBreakpointsAsync(
10781078

10791079
// Verifies fix for issue #86, $proc = Get-Process foo displays just the ETS property set
10801080
// and not all process properties.
1081-
[Fact(Skip = "Length of child vars is wrong now")]
1082-
public async Task DebuggerVariableProcessObjDisplaysCorrectly()
1081+
[Fact]
1082+
public async Task DebuggerVariableProcessObjectDisplaysCorrectly()
10831083
{
10841084
await debugService.SetLineBreakpointsAsync(
10851085
variableScriptFile,
@@ -1098,7 +1098,33 @@ await debugService.SetLineBreakpointsAsync(
10981098
Assert.True(var.IsExpandable);
10991099

11001100
VariableDetailsBase[] childVars = await debugService.GetVariables(var.Id, CancellationToken.None).ConfigureAwait(true);
1101-
Assert.Equal(53, childVars.Length);
1101+
Assert.Equal(132, childVars.Length);
1102+
Assert.Contains(childVars, i => i.Name is "Name" && i.ValueString is "\"dotnet\"");
1103+
Assert.Contains(childVars, i => i.Name is "Handles");
1104+
Assert.Contains(childVars, i => i.Name is "CommandLine");
1105+
Assert.Contains(childVars, i => i.Name is "ExitCode");
1106+
Assert.Contains(childVars, i => i.Name is "HasExited" && i.ValueString is "$false");
1107+
Assert.Contains(childVars, i => i.Name is "Id" && i.Type is "[System.Int32]");
1108+
}
1109+
1110+
[Fact]
1111+
public async Task DebuggerVariableFileObjectDisplaysCorrectly()
1112+
{
1113+
await debugService.SetCommandBreakpointsAsync(
1114+
new[] { CommandBreakpointDetails.Create("Write-Host") }).ConfigureAwait(true);
1115+
1116+
ScriptFile testScript = GetDebugScript("GetChildItemTest.ps1");
1117+
Task _ = ExecuteScriptFileAsync(testScript.FilePath);
1118+
AssertDebuggerStopped(testScript.FilePath, 2);
1119+
1120+
VariableDetailsBase[] variables = await GetVariables(VariableContainerDetails.LocalScopeName).ConfigureAwait(true);
1121+
VariableDetailsBase var = Array.Find(variables, v => v.Name == "$file");
1122+
VariableDetailsBase[] childVars = await debugService.GetVariables(var.Id, CancellationToken.None).ConfigureAwait(true);
1123+
Assert.Equal(54, childVars.Length);
1124+
Assert.Contains(childVars, i => i.Name is "PSPath");
1125+
Assert.Contains(childVars, i => i.Name is "PSProvider" && i.ValueString is "Microsoft.PowerShell.Core\\FileSystem");
1126+
Assert.Contains(childVars, i => i.Name is "Exists" && i.ValueString is "$true");
1127+
Assert.Contains(childVars, i => i.Name is "LastAccessTime");
11021128
}
11031129
}
11041130
}

0 commit comments

Comments
 (0)