Repository navigation
Expand file tree
/
Copy pathPsesHostFactory.cs
More file actions
124 lines (101 loc) · 4.73 KB
/
Copy pathPsesHostFactory.cs
File metadata and controls
124 lines (101 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Runspaces;
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Hosting;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Microsoft.PowerShell.EditorServices.Utility;
namespace Microsoft.PowerShell.EditorServices.Test
{
internal static class PsesHostFactory
{
// NOTE: These paths are arbitrarily chosen just to verify that the profile paths
// can be set to whatever they need to be for the given host.
public static readonly ProfilePathInfo TestProfilePaths =
new ProfilePathInfo(
Path.GetFullPath(
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/Profile/Test.PowerShellEditorServices_profile.ps1")),
Path.GetFullPath(
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/Profile/ProfileTest.ps1")),
Path.GetFullPath(
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/Test.PowerShellEditorServices_profile.ps1")),
Path.GetFullPath(
TestUtilities.NormalizePath("../../../../PowerShellEditorServices.Test.Shared/ProfileTest.ps1")));
public static readonly string BundledModulePath = Path.GetFullPath(
TestUtilities.NormalizePath("../../../../../module"));
public static System.Management.Automation.Runspaces.Runspace InitialRunspace;
public static PsesInternalHost Create(ILoggerFactory loggerFactory)
{
// We intentionally use `CreateDefault2()` as it loads `Microsoft.PowerShell.Core` only,
// which is a more minimal and therefore safer state.
var initialSessionState = InitialSessionState.CreateDefault2();
// We set the process scope's execution policy (which is really the runspace's scope) to
// `Bypass` so we can import our bundled modules. This is equivalent in scope to the CLI
// argument `-ExecutionPolicy Bypass`, which (for instance) the extension passes. Thus
// we emulate this behavior for consistency such that unit tests can pass in a similar
// environment.
if (VersionUtils.IsWindows)
{
initialSessionState.ExecutionPolicy = ExecutionPolicy.Bypass;
}
HostStartupInfo testHostDetails = new HostStartupInfo(
"PowerShell Editor Services Test Host",
"Test.PowerShellEditorServices",
new Version("1.0.0"),
psHost: new NullPSHost(),
TestProfilePaths,
featureFlags: Array.Empty<string>(),
additionalModules: Array.Empty<string>(),
initialSessionState,
logPath: null,
(int)LogLevel.None,
consoleReplEnabled: false,
usesLegacyReadLine: false,
bundledModulePath: BundledModulePath);
var psesHost = new PsesInternalHost(loggerFactory, null, testHostDetails);
psesHost.TryStartAsync(new HostStartOptions { LoadProfiles = true }, CancellationToken.None).GetAwaiter().GetResult();
return psesHost;
}
}
internal class NullPSHost : PSHost
{
public override CultureInfo CurrentCulture => CultureInfo.CurrentCulture;
public override CultureInfo CurrentUICulture => CultureInfo.CurrentUICulture;
public override Guid InstanceId { get; } = Guid.NewGuid();
public override string Name => nameof(NullPSHost);
public override PSHostUserInterface UI { get; } = new NullPSHostUI();
public override Version Version { get; } = new Version(1, 0, 0);
public override void EnterNestedPrompt()
{
// Do nothing
}
public override void ExitNestedPrompt()
{
// Do nothing
}
public override void NotifyBeginApplication()
{
// Do nothing
}
public override void NotifyEndApplication()
{
// Do nothing
}
public override void SetShouldExit(int exitCode)
{
// Do nothing
}
}
}