Skip to content

Commit

Permalink
Merge pull request #1651 from PowerShell/andschwa/pses-host-tests
Browse files Browse the repository at this point in the history
Enable `PsesInternalHostTests` (previously `PowerShellContextTests`)
  • Loading branch information
andyleejordan authored Jan 5, 2022
2 parents 1cf808f + 57bdb36 commit 20a3516
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,7 @@ public async Task<bool> TryStartAsync(HostStartOptions startOptions, Cancellatio

if (startOptions.LoadProfiles)
{
await ExecuteDelegateAsync(
"LoadProfiles",
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
cancellationToken).ConfigureAwait(false);

await LoadHostProfilesAsync(cancellationToken).ConfigureAwait(false);
_logger.LogInformation("Profiles loaded");
}

Expand Down Expand Up @@ -391,6 +386,15 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp
task.ExecuteAndGetResult(cancellationToken);
}

internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
{
return ExecuteDelegateAsync(
"LoadProfiles",
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
cancellationToken);
}

public Task SetInitialWorkingDirectoryAsync(string path, CancellationToken cancellationToken)
{
InitialWorkingDirectory = path;
Expand Down Expand Up @@ -919,7 +923,7 @@ private Task PopOrReinitializeRunspaceAsync()
CancellationToken.None);
}

private bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
internal bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
{
psrlReadLine = null;
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static ErrorRecordExtensions()
s_setWriteStreamProperty = Expression.Lambda<Action<PSObject>>(
Expression.Call(
errorObjectParameter,
writeStreamProperty.GetSetMethod(),
writeStreamProperty.GetSetMethod(nonPublic: true),
Expression.Constant(errorStreamType)),
errorObjectParameter)
.Compile();
Expand Down
11 changes: 7 additions & 4 deletions src/PowerShellEditorServices/Utility/PSCommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ public static PSCommand AddDebugOutputCommand(this PSCommand psCommand)

public static PSCommand MergePipelineResults(this PSCommand psCommand)
{
// We need to do merge errors and output before rendering with an Out- cmdlet
Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1];
lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output);
if (psCommand.Commands.Count > 0)
{
// We need to do merge errors and output before rendering with an Out- cmdlet
Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1];
lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output);
}
return psCommand;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ public static string NormalizePath(string unixPath)
return unixPath;
}

/// <summary>
/// Gets a normalized path from the directory of this assembly to the given path under the
/// shared test folder.
/// </summary>
/// <param name="path">A path or file under the shared test folder.</param>
/// <returns>The normalized and resolved path to it.</returns>
public static string GetSharedPath(string path)
{
// TODO: When testing net461 with x64 host, another .. is needed!
return NormalizePath(Path.Combine(
Path.GetDirectoryName(typeof(TestUtilities).Assembly.Location),
"../../../../PowerShellEditorServices.Test.Shared",
path));
}

/// <summary>
/// Take a string with UNIX newlines and replaces them with platform-appropriate newlines.
/// </summary>
Expand Down
32 changes: 2 additions & 30 deletions test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Xunit;
namespace Microsoft.PowerShell.EditorServices.Test.Debugging
{
[Trait("Category", "DebugService")]
public class DebugServiceTests : IDisposable
{
private readonly PsesInternalHost psesHost;
Expand Down Expand Up @@ -81,13 +82,7 @@ private void OnDebuggerStopped(object sender, DebuggerStoppedEventArgs e)

private ScriptFile GetDebugScript(string fileName)
{
return workspace.GetFile(
TestUtilities.NormalizePath(Path.Combine(
Path.GetDirectoryName(typeof(DebugServiceTests).Assembly.Location),
// TODO: When testing net461 with x64 host, another .. is needed!
"../../../../PowerShellEditorServices.Test.Shared/Debugging",
fileName
)));
return workspace.GetFile(TestUtilities.GetSharedPath(Path.Combine("Debugging", fileName)));
}

private VariableDetailsBase[] GetVariables(string scopeName)
Expand Down Expand Up @@ -147,7 +142,6 @@ private Task<IReadOnlyList<LineBreakpoint>> GetConfirmedBreakpoints(ScriptFile s
CancellationToken.None);
}

[Trait("Category", "DebugService")]
[Fact]
// This regression test asserts that `ExecuteScriptWithArgsAsync` works for both script
// files and, in this case, in-line scripts (commands). The bug was that the cwd was
Expand Down Expand Up @@ -176,7 +170,6 @@ await debugService.SetCommandBreakpointsAsync(
Assert.Equal("[ArrayList: 0]", var.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerAcceptsScriptArgs()
{
Expand Down Expand Up @@ -238,7 +231,6 @@ public async Task DebuggerAcceptsScriptArgs()
Assert.Equal("\"Extra1\"", childVars[0].ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerSetsAndClearsFunctionBreakpoints()
{
Expand All @@ -264,7 +256,6 @@ public async Task DebuggerSetsAndClearsFunctionBreakpoints()
Assert.Empty(breakpoints);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerStopsOnFunctionBreakpoints()
{
Expand Down Expand Up @@ -295,7 +286,6 @@ public async Task DebuggerStopsOnFunctionBreakpoints()
Assert.Equal("2", i.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerSetsAndClearsLineBreakpoints()
{
Expand Down Expand Up @@ -329,7 +319,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Empty(remainingBreakpoints);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerStopsOnLineBreakpoints()
{
Expand All @@ -346,7 +335,6 @@ await debugService.SetLineBreakpointsAsync(
AssertDebuggerStopped(debugScriptFile.FilePath, 7);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerStopsOnConditionalBreakpoints()
{
Expand Down Expand Up @@ -384,7 +372,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal($"{breakpointValue2}", i.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerStopsOnHitConditionBreakpoint()
{
Expand All @@ -408,7 +395,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal($"{hitCount}", i.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerStopsOnConditionalAndHitConditionBreakpoint()
{
Expand All @@ -431,7 +417,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal("10", i.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerProvidesMessageForInvalidConditionalBreakpoint()
{
Expand Down Expand Up @@ -460,7 +445,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Contains("Unexpected token '-ez'", breakpoints[0].Message);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerFindsParseableButInvalidSimpleBreakpointConditions()
{
Expand All @@ -483,7 +467,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Contains("Use '-gt' instead of '>'", breakpoints[1].Message);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerBreaksWhenRequested()
{
Expand All @@ -495,7 +478,6 @@ public async Task DebuggerBreaksWhenRequested()
AssertDebuggerPaused();
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerRunsCommandsWhileStopped()
{
Expand All @@ -510,7 +492,6 @@ public async Task DebuggerRunsCommandsWhileStopped()
Assert.Equal(17, (await executeTask.ConfigureAwait(true))[0]);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerVariableStringDisplaysCorrectly()
{
Expand All @@ -529,7 +510,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.False(var.IsExpandable);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerGetsVariables()
{
Expand Down Expand Up @@ -579,7 +559,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal("$false", falseVar.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerSetsVariablesNoConversion()
{
Expand Down Expand Up @@ -634,7 +613,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal(newGlobalIntValue, intGlobalVar.ValueString);
}

[Trait("Category", "DebugService")]
[Fact(Skip = "Variable conversion is broken")]
public async Task DebuggerSetsVariablesWithConversion()
{
Expand Down Expand Up @@ -693,7 +671,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal(newGlobalValue, globalVar.ValueString);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerVariableEnumDisplaysCorrectly()
{
Expand All @@ -714,7 +691,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.False(var.IsExpandable);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerVariableHashtableDisplaysCorrectly()
{
Expand Down Expand Up @@ -751,7 +727,6 @@ await debugService.SetLineBreakpointsAsync(
}
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerVariableNullStringDisplaysCorrectly()
{
Expand All @@ -772,7 +747,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.True(nullStringVar.IsExpandable);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerVariablePSObjectDisplaysCorrectly()
{
Expand Down Expand Up @@ -800,7 +774,6 @@ await debugService.SetLineBreakpointsAsync(
Assert.Equal("\"John\"", childVars["Name"]);
}

[Trait("Category", "DebugService")]
[Fact]
public async Task DebuggerVariablePSCustomObjectDisplaysCorrectly()
{
Expand Down Expand Up @@ -830,7 +803,6 @@ await debugService.SetLineBreakpointsAsync(

// Verifies fix for issue #86, $proc = Get-Process foo displays just the ETS property set
// and not all process properties.
[Trait("Category", "DebugService")]
[Fact(Skip = "Length of child vars is wrong now")]
public async Task DebuggerVariableProcessObjDisplaysCorrectly()
{
Expand Down
Loading

0 comments on commit 20a3516

Please sign in to comment.