File size: 4,426 Bytes
8c763fb | 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 124 125 126 127 128 129 130 131 132 133 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using Xunit;
namespace PSTests.Sequential
{
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
// otherwise Invoke will not return any objects
public class RunspaceTests
{
private static readonly int count = 1;
private static readonly string script = string.Format($"get-command get-command");
[Fact]
public void TestRunspaceWithPipeline()
{
using (Runspace runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
using (var pipeline = runspace.CreatePipeline(script))
{
int objCount = 0;
foreach (var result in pipeline.Invoke())
{
++objCount;
Assert.NotNull(result);
}
Assert.Equal(count, objCount);
}
runspace.Close();
}
}
[Fact]
public void TestRunspaceWithPowerShell()
{
using (var runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddScript(script);
int objCount = 0;
foreach (var result in powerShell.Invoke())
{
++objCount;
Assert.NotNull(result);
}
Assert.Equal(count, objCount);
}
runspace.Close();
}
}
[Fact]
public void TestRunspaceWithPowerShellAndInitialSessionState()
{
// CreateDefault2 is intentional.
InitialSessionState iss = InitialSessionState.CreateDefault();
// NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output
// in addition to just the PSObjects
using (Runspace runspace = RunspaceFactory.CreateRunspace(/*myHost,*/iss))
{
runspace.Open();
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddScript("Import-Module Microsoft.PowerShell.Utility -Force");
powerShell.AddScript(script);
int objCount = 0;
var results = powerShell.Invoke();
foreach (var result in results)
{
// this is how an object would be captured here and looked at,
// each result is a PSObject with the data from the pipeline
++objCount;
Assert.NotNull(result);
}
Assert.Equal(count, objCount);
}
runspace.Close();
}
}
[SkippableFact]
public void TestAppDomainProcessExitEvenHandlerNotLeaking()
{
// Skip this flaky test for now.
Skip.IfNot(false);
Skip.IfNot(Platform.IsWindows);
EventHandler eventHandler;
Delegate[] delegates;
FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static);
// Open runspace and invoke script.
using (var ps = PowerShell.Create())
{
ps.AddScript("1").Invoke();
eventHandler = (EventHandler)field.GetValue(null);
delegates = eventHandler.GetInvocationList();
Assert.Contains(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit");
}
// Handler registered by PowerShell should be unregistered.
eventHandler = (EventHandler)field.GetValue(null);
delegates = eventHandler.GetInvocationList();
Assert.DoesNotContain(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit");
}
}
}
|