| |
| |
|
|
| using System; |
| using System.Management.Automation; |
| using System.Management.Automation.Runspaces; |
| using System.Reflection; |
| using Xunit; |
|
|
| namespace PSTests.Sequential |
| { |
| |
| |
| 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() |
| { |
| |
| InitialSessionState iss = InitialSessionState.CreateDefault(); |
|
|
| |
| |
| using (Runspace runspace = RunspaceFactory.CreateRunspace(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) |
| { |
| |
| |
| ++objCount; |
| Assert.NotNull(result); |
| } |
|
|
| Assert.Equal(count, objCount); |
| } |
|
|
| runspace.Close(); |
| } |
| } |
|
|
| [SkippableFact] |
| public void TestAppDomainProcessExitEvenHandlerNotLeaking() |
| { |
| |
| Skip.IfNot(false); |
|
|
| Skip.IfNot(Platform.IsWindows); |
|
|
| EventHandler eventHandler; |
| Delegate[] delegates; |
| FieldInfo field = typeof(AppContext).GetField("ProcessExit", BindingFlags.NonPublic | BindingFlags.Static); |
|
|
| |
| 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"); |
| } |
|
|
| |
| eventHandler = (EventHandler)field.GetValue(null); |
| delegates = eventHandler.GetInvocationList(); |
| Assert.DoesNotContain(delegates, d => d.Method.Name == "CurrentDomain_ProcessExit"); |
| } |
| } |
| } |
|
|