Skip to content

Commit bd7b7a9

Browse files
committed
Add error check to PsesHostFactory.Create() and clean up
1 parent f1c4ae5 commit bd7b7a9

File tree

1 file changed

+16
-43
lines changed

1 file changed

+16
-43
lines changed

test/PowerShellEditorServices.Test/PsesHostFactory.cs

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Generic;
6-
using System.Collections.ObjectModel;
75
using System.Globalization;
86
using System.IO;
9-
using System.Management.Automation;
107
using System.Management.Automation.Host;
118
using System.Management.Automation.Runspaces;
12-
using System.Security;
139
using System.Threading;
14-
using System.Threading.Tasks;
1510
using Microsoft.Extensions.Logging;
16-
using Microsoft.Extensions.Logging.Abstractions;
1711
using Microsoft.PowerShell.EditorServices.Hosting;
1812
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1913
using Microsoft.PowerShell.EditorServices.Test.Shared;
@@ -23,11 +17,11 @@ namespace Microsoft.PowerShell.EditorServices.Test
2317
{
2418
internal static class PsesHostFactory
2519
{
26-
// NOTE: These paths are arbitrarily chosen just to verify that the profile paths
27-
// can be set to whatever they need to be for the given host.
20+
// NOTE: These paths are arbitrarily chosen just to verify that the profile paths can be set
21+
// to whatever they need to be for the given host.
2822

2923
public static readonly ProfilePathInfo TestProfilePaths =
30-
new ProfilePathInfo(
24+
new(
3125
Path.GetFullPath(
3226
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/Profile/Test.PowerShellEditorServices_profile.ps1")),
3327
Path.GetFullPath(
@@ -58,7 +52,7 @@ public static PsesInternalHost Create(ILoggerFactory loggerFactory)
5852
initialSessionState.ExecutionPolicy = ExecutionPolicy.Bypass;
5953
}
6054

61-
HostStartupInfo testHostDetails = new HostStartupInfo(
55+
HostStartupInfo testHostDetails = new(
6256
"PowerShell Editor Services Test Host",
6357
"Test.PowerShellEditorServices",
6458
new Version("1.0.0"),
@@ -75,50 +69,29 @@ public static PsesInternalHost Create(ILoggerFactory loggerFactory)
7569

7670
var psesHost = new PsesInternalHost(loggerFactory, null, testHostDetails);
7771

78-
psesHost.TryStartAsync(new HostStartOptions { LoadProfiles = true }, CancellationToken.None).GetAwaiter().GetResult();
72+
// NOTE: Because this is used by constructors it can't use await.
73+
// TODO: Should we actually load profiles here?
74+
if (psesHost.TryStartAsync(new HostStartOptions { LoadProfiles = true }, CancellationToken.None).GetAwaiter().GetResult())
75+
{
76+
return psesHost;
77+
}
7978

80-
return psesHost;
79+
throw new Exception("Host didn't start!");
8180
}
8281
}
8382

8483
internal class NullPSHost : PSHost
8584
{
8685
public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture;
87-
8886
public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture;
89-
9087
public override Guid InstanceId { get; } = Guid.NewGuid();
91-
9288
public override string Name => nameof(NullPSHost);
93-
9489
public override PSHostUserInterface UI { get; } = new NullPSHostUI();
95-
9690
public override Version Version { get; } = new Version(1, 0, 0);
97-
98-
public override void EnterNestedPrompt()
99-
{
100-
// Do nothing
101-
}
102-
103-
public override void ExitNestedPrompt()
104-
{
105-
// Do nothing
106-
}
107-
108-
public override void NotifyBeginApplication()
109-
{
110-
// Do nothing
111-
}
112-
113-
public override void NotifyEndApplication()
114-
{
115-
// Do nothing
116-
}
117-
118-
public override void SetShouldExit(int exitCode)
119-
{
120-
// Do nothing
121-
}
91+
public override void EnterNestedPrompt() { /* Do nothing */ }
92+
public override void ExitNestedPrompt() { /* Do nothing */ }
93+
public override void NotifyBeginApplication() { /* Do nothing */ }
94+
public override void NotifyEndApplication() { /* Do nothing */ }
95+
public override void SetShouldExit(int exitCode) { /* Do nothing */ }
12296
}
12397
}
124-

0 commit comments

Comments
 (0)