Skip to content

Commit 20a3516

Browse files
Merge pull request #1651 from PowerShell/andschwa/pses-host-tests
Enable `PsesInternalHostTests` (previously `PowerShellContextTests`)
2 parents 1cf808f + 57bdb36 commit 20a3516

File tree

7 files changed

+198
-220
lines changed

7 files changed

+198
-220
lines changed

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

+11-7
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,7 @@ public async Task<bool> TryStartAsync(HostStartOptions startOptions, Cancellatio
216216

217217
if (startOptions.LoadProfiles)
218218
{
219-
await ExecuteDelegateAsync(
220-
"LoadProfiles",
221-
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
222-
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
223-
cancellationToken).ConfigureAwait(false);
224-
219+
await LoadHostProfilesAsync(cancellationToken).ConfigureAwait(false);
225220
_logger.LogInformation("Profiles loaded");
226221
}
227222

@@ -391,6 +386,15 @@ public void InvokePSDelegate(string representation, ExecutionOptions executionOp
391386
task.ExecuteAndGetResult(cancellationToken);
392387
}
393388

389+
internal Task LoadHostProfilesAsync(CancellationToken cancellationToken)
390+
{
391+
return ExecuteDelegateAsync(
392+
"LoadProfiles",
393+
new PowerShellExecutionOptions { MustRunInForeground = true, ThrowOnError = false },
394+
(pwsh, _) => pwsh.LoadProfiles(_hostInfo.ProfilePaths),
395+
cancellationToken);
396+
}
397+
394398
public Task SetInitialWorkingDirectoryAsync(string path, CancellationToken cancellationToken)
395399
{
396400
InitialWorkingDirectory = path;
@@ -919,7 +923,7 @@ private Task PopOrReinitializeRunspaceAsync()
919923
CancellationToken.None);
920924
}
921925

922-
private bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
926+
internal bool TryLoadPSReadLine(PowerShell pwsh, EngineIntrinsics engineIntrinsics, out IReadLine psrlReadLine)
923927
{
924928
psrlReadLine = null;
925929
try

src/PowerShellEditorServices/Services/PowerShell/Utility/ErrorRecordExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static ErrorRecordExtensions()
3434
s_setWriteStreamProperty = Expression.Lambda<Action<PSObject>>(
3535
Expression.Call(
3636
errorObjectParameter,
37-
writeStreamProperty.GetSetMethod(),
37+
writeStreamProperty.GetSetMethod(nonPublic: true),
3838
Expression.Constant(errorStreamType)),
3939
errorObjectParameter)
4040
.Compile();

src/PowerShellEditorServices/Utility/PSCommandExtensions.cs

+7-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,13 @@ public static PSCommand AddDebugOutputCommand(this PSCommand psCommand)
6161

6262
public static PSCommand MergePipelineResults(this PSCommand psCommand)
6363
{
64-
// We need to do merge errors and output before rendering with an Out- cmdlet
65-
Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1];
66-
lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
67-
lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output);
64+
if (psCommand.Commands.Count > 0)
65+
{
66+
// We need to do merge errors and output before rendering with an Out- cmdlet
67+
Command lastCommand = psCommand.Commands[psCommand.Commands.Count - 1];
68+
lastCommand.MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
69+
lastCommand.MergeMyResults(PipelineResultTypes.Information, PipelineResultTypes.Output);
70+
}
6871
return psCommand;
6972
}
7073

test/PowerShellEditorServices.Test.Shared/TestUtilities/TestUtilities.cs

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ public static string NormalizePath(string unixPath)
3636
return unixPath;
3737
}
3838

39+
/// <summary>
40+
/// Gets a normalized path from the directory of this assembly to the given path under the
41+
/// shared test folder.
42+
/// </summary>
43+
/// <param name="path">A path or file under the shared test folder.</param>
44+
/// <returns>The normalized and resolved path to it.</returns>
45+
public static string GetSharedPath(string path)
46+
{
47+
// TODO: When testing net461 with x64 host, another .. is needed!
48+
return NormalizePath(Path.Combine(
49+
Path.GetDirectoryName(typeof(TestUtilities).Assembly.Location),
50+
"../../../../PowerShellEditorServices.Test.Shared",
51+
path));
52+
}
53+
3954
/// <summary>
4055
/// Take a string with UNIX newlines and replaces them with platform-appropriate newlines.
4156
/// </summary>

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+2-30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Xunit;
2020
namespace Microsoft.PowerShell.EditorServices.Test.Debugging
2121
{
22+
[Trait("Category", "DebugService")]
2223
public class DebugServiceTests : IDisposable
2324
{
2425
private readonly PsesInternalHost psesHost;
@@ -81,13 +82,7 @@ private void OnDebuggerStopped(object sender, DebuggerStoppedEventArgs e)
8182

8283
private ScriptFile GetDebugScript(string fileName)
8384
{
84-
return workspace.GetFile(
85-
TestUtilities.NormalizePath(Path.Combine(
86-
Path.GetDirectoryName(typeof(DebugServiceTests).Assembly.Location),
87-
// TODO: When testing net461 with x64 host, another .. is needed!
88-
"../../../../PowerShellEditorServices.Test.Shared/Debugging",
89-
fileName
90-
)));
85+
return workspace.GetFile(TestUtilities.GetSharedPath(Path.Combine("Debugging", fileName)));
9186
}
9287

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

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

179-
[Trait("Category", "DebugService")]
180173
[Fact]
181174
public async Task DebuggerAcceptsScriptArgs()
182175
{
@@ -238,7 +231,6 @@ public async Task DebuggerAcceptsScriptArgs()
238231
Assert.Equal("\"Extra1\"", childVars[0].ValueString);
239232
}
240233

241-
[Trait("Category", "DebugService")]
242234
[Fact]
243235
public async Task DebuggerSetsAndClearsFunctionBreakpoints()
244236
{
@@ -264,7 +256,6 @@ public async Task DebuggerSetsAndClearsFunctionBreakpoints()
264256
Assert.Empty(breakpoints);
265257
}
266258

267-
[Trait("Category", "DebugService")]
268259
[Fact]
269260
public async Task DebuggerStopsOnFunctionBreakpoints()
270261
{
@@ -295,7 +286,6 @@ public async Task DebuggerStopsOnFunctionBreakpoints()
295286
Assert.Equal("2", i.ValueString);
296287
}
297288

298-
[Trait("Category", "DebugService")]
299289
[Fact]
300290
public async Task DebuggerSetsAndClearsLineBreakpoints()
301291
{
@@ -329,7 +319,6 @@ await debugService.SetLineBreakpointsAsync(
329319
Assert.Empty(remainingBreakpoints);
330320
}
331321

332-
[Trait("Category", "DebugService")]
333322
[Fact]
334323
public async Task DebuggerStopsOnLineBreakpoints()
335324
{
@@ -346,7 +335,6 @@ await debugService.SetLineBreakpointsAsync(
346335
AssertDebuggerStopped(debugScriptFile.FilePath, 7);
347336
}
348337

349-
[Trait("Category", "DebugService")]
350338
[Fact]
351339
public async Task DebuggerStopsOnConditionalBreakpoints()
352340
{
@@ -384,7 +372,6 @@ await debugService.SetLineBreakpointsAsync(
384372
Assert.Equal($"{breakpointValue2}", i.ValueString);
385373
}
386374

387-
[Trait("Category", "DebugService")]
388375
[Fact]
389376
public async Task DebuggerStopsOnHitConditionBreakpoint()
390377
{
@@ -408,7 +395,6 @@ await debugService.SetLineBreakpointsAsync(
408395
Assert.Equal($"{hitCount}", i.ValueString);
409396
}
410397

411-
[Trait("Category", "DebugService")]
412398
[Fact]
413399
public async Task DebuggerStopsOnConditionalAndHitConditionBreakpoint()
414400
{
@@ -431,7 +417,6 @@ await debugService.SetLineBreakpointsAsync(
431417
Assert.Equal("10", i.ValueString);
432418
}
433419

434-
[Trait("Category", "DebugService")]
435420
[Fact]
436421
public async Task DebuggerProvidesMessageForInvalidConditionalBreakpoint()
437422
{
@@ -460,7 +445,6 @@ await debugService.SetLineBreakpointsAsync(
460445
Assert.Contains("Unexpected token '-ez'", breakpoints[0].Message);
461446
}
462447

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

486-
[Trait("Category", "DebugService")]
487470
[Fact]
488471
public async Task DebuggerBreaksWhenRequested()
489472
{
@@ -495,7 +478,6 @@ public async Task DebuggerBreaksWhenRequested()
495478
AssertDebuggerPaused();
496479
}
497480

498-
[Trait("Category", "DebugService")]
499481
[Fact]
500482
public async Task DebuggerRunsCommandsWhileStopped()
501483
{
@@ -510,7 +492,6 @@ public async Task DebuggerRunsCommandsWhileStopped()
510492
Assert.Equal(17, (await executeTask.ConfigureAwait(true))[0]);
511493
}
512494

513-
[Trait("Category", "DebugService")]
514495
[Fact]
515496
public async Task DebuggerVariableStringDisplaysCorrectly()
516497
{
@@ -529,7 +510,6 @@ await debugService.SetLineBreakpointsAsync(
529510
Assert.False(var.IsExpandable);
530511
}
531512

532-
[Trait("Category", "DebugService")]
533513
[Fact]
534514
public async Task DebuggerGetsVariables()
535515
{
@@ -579,7 +559,6 @@ await debugService.SetLineBreakpointsAsync(
579559
Assert.Equal("$false", falseVar.ValueString);
580560
}
581561

582-
[Trait("Category", "DebugService")]
583562
[Fact]
584563
public async Task DebuggerSetsVariablesNoConversion()
585564
{
@@ -634,7 +613,6 @@ await debugService.SetLineBreakpointsAsync(
634613
Assert.Equal(newGlobalIntValue, intGlobalVar.ValueString);
635614
}
636615

637-
[Trait("Category", "DebugService")]
638616
[Fact(Skip = "Variable conversion is broken")]
639617
public async Task DebuggerSetsVariablesWithConversion()
640618
{
@@ -693,7 +671,6 @@ await debugService.SetLineBreakpointsAsync(
693671
Assert.Equal(newGlobalValue, globalVar.ValueString);
694672
}
695673

696-
[Trait("Category", "DebugService")]
697674
[Fact]
698675
public async Task DebuggerVariableEnumDisplaysCorrectly()
699676
{
@@ -714,7 +691,6 @@ await debugService.SetLineBreakpointsAsync(
714691
Assert.False(var.IsExpandable);
715692
}
716693

717-
[Trait("Category", "DebugService")]
718694
[Fact]
719695
public async Task DebuggerVariableHashtableDisplaysCorrectly()
720696
{
@@ -751,7 +727,6 @@ await debugService.SetLineBreakpointsAsync(
751727
}
752728
}
753729

754-
[Trait("Category", "DebugService")]
755730
[Fact]
756731
public async Task DebuggerVariableNullStringDisplaysCorrectly()
757732
{
@@ -772,7 +747,6 @@ await debugService.SetLineBreakpointsAsync(
772747
Assert.True(nullStringVar.IsExpandable);
773748
}
774749

775-
[Trait("Category", "DebugService")]
776750
[Fact]
777751
public async Task DebuggerVariablePSObjectDisplaysCorrectly()
778752
{
@@ -800,7 +774,6 @@ await debugService.SetLineBreakpointsAsync(
800774
Assert.Equal("\"John\"", childVars["Name"]);
801775
}
802776

803-
[Trait("Category", "DebugService")]
804777
[Fact]
805778
public async Task DebuggerVariablePSCustomObjectDisplaysCorrectly()
806779
{
@@ -830,7 +803,6 @@ await debugService.SetLineBreakpointsAsync(
830803

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

0 commit comments

Comments
 (0)