Skip to content

Commit 61ae8f2

Browse files
committed
Add Host factory
1 parent dfee772 commit 61ae8f2

File tree

2 files changed

+26
-72
lines changed

2 files changed

+26
-72
lines changed

test/PowerShellEditorServices.Test/Language/LanguageServiceTests.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.Extensions.Logging.Abstractions;
1111
using Microsoft.PowerShell.EditorServices.Handlers;
1212
using Microsoft.PowerShell.EditorServices.Services;
13+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1314
using Microsoft.PowerShell.EditorServices.Services.Symbols;
1415
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1516
using Microsoft.PowerShell.EditorServices.Test.Shared;
@@ -26,13 +27,12 @@
2627

2728
namespace Microsoft.PowerShell.EditorServices.Test.Language
2829
{
29-
/*
3030
public class LanguageServiceTests : IDisposable
3131
{
3232
private readonly WorkspaceService workspace;
3333
private readonly SymbolsService symbolsService;
3434
private readonly PsesCompletionHandler completionHandler;
35-
private readonly PowerShellContextService powerShellContext;
35+
private readonly PsesInternalHost _psesHost;
3636
private static readonly string s_baseSharedScriptPath =
3737
Path.Combine(
3838
Path.GetDirectoryName(VersionUtils.IsWindows
@@ -45,16 +45,16 @@ public class LanguageServiceTests : IDisposable
4545

4646
public LanguageServiceTests()
4747
{
48-
var logger = NullLogger.Instance;
49-
powerShellContext = PowerShellContextFactory.Create(logger);
48+
_psesHost = PsesHostFactory.Create(NullLoggerFactory.Instance);
49+
5050
workspace = new WorkspaceService(NullLoggerFactory.Instance);
51-
symbolsService = new SymbolsService(NullLoggerFactory.Instance, powerShellContext, workspace, new ConfigurationService());
52-
completionHandler = new PsesCompletionHandler(NullLoggerFactory.Instance, powerShellContext, workspace);
51+
symbolsService = new SymbolsService(NullLoggerFactory.Instance, _psesHost, _psesHost, workspace, new ConfigurationService());
52+
completionHandler = new PsesCompletionHandler(NullLoggerFactory.Instance, _psesHost, _psesHost, workspace);
5353
}
5454

5555
public void Dispose()
5656
{
57-
this.powerShellContext.Close();
57+
// TODO: Dispose of the host
5858
}
5959

6060
[Trait("Category", "Completions")]
@@ -464,14 +464,12 @@ await this.completionHandler.GetCompletionsInFileAsync(
464464
scriptRegion.StartColumnNumber).ConfigureAwait(false);
465465
}
466466

467-
private async Task<ParameterSetSignatures> GetParamSetSignatures(ScriptRegion scriptRegion)
467+
private Task<ParameterSetSignatures> GetParamSetSignatures(ScriptRegion scriptRegion)
468468
{
469-
return
470-
await this.symbolsService.FindParameterSetsInFileAsync(
469+
return this.symbolsService.FindParameterSetsInFileAsync(
471470
GetScriptFile(scriptRegion),
472471
scriptRegion.StartLineNumber,
473-
scriptRegion.StartColumnNumber,
474-
powerShellContext).ConfigureAwait(false);
472+
scriptRegion.StartColumnNumber);
475473
}
476474

477475
private async Task<SymbolReference> GetDefinition(ScriptRegion scriptRegion)
@@ -527,5 +525,4 @@ private List<SymbolReference> FindSymbolsInFile(ScriptRegion scriptRegion)
527525
GetScriptFile(scriptRegion));
528526
}
529527
}
530-
*/
531528
}

test/PowerShellEditorServices.Test/PowerShellContextFactory.cs renamed to test/PowerShellEditorServices.Test/PsesHostFactory.cs

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.ObjectModel;
67
using System.IO;
8+
using System.Management.Automation;
9+
using System.Management.Automation.Host;
710
using System.Management.Automation.Runspaces;
11+
using System.Security;
812
using System.Threading;
913
using System.Threading.Tasks;
1014
using Microsoft.Extensions.Logging;
1115
using Microsoft.Extensions.Logging.Abstractions;
1216
using Microsoft.PowerShell.EditorServices.Hosting;
13-
using Microsoft.PowerShell.EditorServices.Services;
17+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
1418
using Microsoft.PowerShell.EditorServices.Test.Shared;
1519
using Microsoft.PowerShell.EditorServices.Utility;
1620

1721
namespace Microsoft.PowerShell.EditorServices.Test
1822
{
19-
/*
20-
internal static class PowerShellContextFactory
23+
internal static class PsesHostFactory
2124
{
2225
// NOTE: These paths are arbitrarily chosen just to verify that the profile paths
2326
// can be set to whatever they need to be for the given host.
@@ -38,10 +41,8 @@ internal static class PowerShellContextFactory
3841

3942
public static System.Management.Automation.Runspaces.Runspace InitialRunspace;
4043

41-
public static PowerShellContextService Create(ILogger logger)
44+
public static PsesInternalHost Create(ILoggerFactory loggerFactory)
4245
{
43-
PowerShellContextService powerShellContext = new PowerShellContextService(logger, null, isPSReadLineEnabled: false);
44-
4546
// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core` only,
4647
// which is a more minimal and therefore safer state.
4748
var initialSessionState = InitialSessionState.CreateDefault2();
@@ -60,67 +61,23 @@ public static PowerShellContextService Create(ILogger logger)
6061
"PowerShell Editor Services Test Host",
6162
"Test.PowerShellEditorServices",
6263
new Version("1.0.0"),
63-
null,
64+
psHost: null,
6465
TestProfilePaths,
65-
new List<string>(),
66-
new List<string>(),
66+
featureFlags: Array.Empty<string>(),
67+
additionalModules: Array.Empty<string>(),
6768
initialSessionState,
68-
null,
69-
0,
69+
logPath: null,
70+
(int)LogLevel.None,
7071
consoleReplEnabled: false,
7172
usesLegacyReadLine: false,
7273
bundledModulePath: BundledModulePath);
7374

74-
InitialRunspace = PowerShellContextService.CreateTestRunspace(
75-
testHostDetails,
76-
powerShellContext,
77-
new TestPSHostUserInterface(powerShellContext, logger),
78-
logger);
75+
var psesHost = new PsesInternalHost(loggerFactory, null, testHostDetails);
7976

80-
powerShellContext.Initialize(
81-
TestProfilePaths,
82-
InitialRunspace,
83-
ownsInitialRunspace: true,
84-
consoleHost: null);
77+
psesHost.StartAsync(new HostStartOptions { LoadProfiles = true }, CancellationToken.None).GetAwaiter().GetResult();
8578

86-
return powerShellContext;
79+
return psesHost;
8780
}
8881
}
89-
90-
internal class TestPSHostUserInterface : EditorServicesPSHostUserInterface
91-
{
92-
public TestPSHostUserInterface(
93-
PowerShellContextService powerShellContext,
94-
ILogger logger)
95-
: base(
96-
powerShellContext,
97-
new SimplePSHostRawUserInterface(logger),
98-
NullLogger.Instance)
99-
{
100-
}
101-
102-
public override void WriteOutput(string outputString, bool includeNewLine, OutputType outputType, ConsoleColor foregroundColor, ConsoleColor backgroundColor)
103-
{
104-
}
105-
106-
protected override ChoicePromptHandler OnCreateChoicePromptHandler()
107-
{
108-
throw new NotImplementedException();
109-
}
110-
111-
protected override InputPromptHandler OnCreateInputPromptHandler()
112-
{
113-
throw new NotImplementedException();
114-
}
115-
116-
protected override Task<string> ReadCommandLineAsync(CancellationToken cancellationToken)
117-
{
118-
return Task.FromResult("USER COMMAND");
119-
}
120-
121-
protected override void UpdateProgress(long sourceId, ProgressDetails progressDetails)
122-
{
123-
}
124-
}
125-
*/
12682
}
83+

0 commit comments

Comments
 (0)