PowerShell/test/csharp/test_Runspace.cs
Andrew Schwartzmeyer 473ac41630 Initialize the ALC for the runspace tests
This initialization was moved out of the native host and into the
managed host, so the same setup must now be done before tests.
2016-02-17 17:03:59 -08:00

147 lines
4.5 KiB
C#

using Xunit;
using System;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
namespace PSTests
{
// NOTE: do not call AddCommand("out-host") after invoking or MergeMyResults,
// otherwise Invoke will not return any objects
public class RunspaceTests
{
private static int count = 3;
private static string script = String.Format($"get-process | select-object -first {count}");
// Initialize the Core PowerShell AssemblyLoadContext
public RunspaceTests()
{
PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(AppContext.BaseDirectory);
}
[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 TestRunspaceWithPowerShellAndHost()
{
MyHost myHost = new MyHost(this);
using (var runspace = RunspaceFactory.CreateRunspace(myHost))
{
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 TestRunspaceWithFunction()
{
MyHost myHost = new MyHost(this);
using (var runspace = RunspaceFactory.CreateRunspace(myHost))
{
runspace.Open();
using (PowerShell powerShell = PowerShell.Create())
{
powerShell.Runspace = runspace;
powerShell.AddScript("{1}.Invoke()");
powerShell.Invoke();
}
runspace.Close();
}
}
[Fact]
public void TestRunspaceWithPowerShellAndInitialSessionState()
{
InitialSessionState iss = InitialSessionState.CreateDefault2();
// 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(script);
int objCount = 0;
foreach (var result in powerShell.Invoke())
{
// 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);
powerShell.Dispose();
}
}
}
}
}