Add Runspace with PSHost test
This commit is contained in:
parent
87da04e4cb
commit
b60ea88ddb
2 changed files with 193 additions and 1 deletions
166
src/Microsoft.PowerShell.Linux.UnitTests/MyHost.cs
Normal file
166
src/Microsoft.PowerShell.Linux.UnitTests/MyHost.cs
Normal file
|
@ -0,0 +1,166 @@
|
|||
namespace PSTests
|
||||
{
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Management.Automation.Host;
|
||||
|
||||
/// <summary>
|
||||
/// This is a sample implementation of the PSHost abstract class for
|
||||
/// console applications. Not all members are implemented. Those that
|
||||
/// are not implemented throw a NotImplementedException exception or
|
||||
/// return nothing.
|
||||
/// </summary>
|
||||
internal class MyHost : PSHost
|
||||
{
|
||||
/// <summary>
|
||||
/// A reference to the PSHost implementation.
|
||||
/// </summary>
|
||||
private RunspaceTests program;
|
||||
|
||||
/// <summary>
|
||||
/// The culture information of the thread that created
|
||||
/// this object.
|
||||
/// </summary>
|
||||
private CultureInfo originalCultureInfo =
|
||||
CultureInfo.CurrentCulture;
|
||||
// System.Threading.Thread.CurrentThread.CurrentCulture;
|
||||
|
||||
/// <summary>
|
||||
/// The UI culture information of the thread that created
|
||||
/// this object.
|
||||
/// </summary>
|
||||
private CultureInfo originalUICultureInfo =
|
||||
CultureInfo.CurrentCulture;
|
||||
// System.Threading.Thread.CurrentThread.CurrentUICulture;
|
||||
|
||||
/// <summary>
|
||||
/// The identifier of this PSHost implementation.
|
||||
/// </summary>
|
||||
private Guid myId = Guid.NewGuid();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the MyHost class. Keep
|
||||
/// a reference to the host application object so that it
|
||||
/// can be informed of when to exit.
|
||||
/// </summary>
|
||||
/// <param name="program">
|
||||
/// A reference to the host application object.
|
||||
/// </param>
|
||||
public MyHost(RunspaceTests program)
|
||||
{
|
||||
this.program = program;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the culture information to use. This implementation
|
||||
/// returns a snapshot of the culture information of the thread
|
||||
/// that created this object.
|
||||
/// </summary>
|
||||
public override System.Globalization.CultureInfo CurrentCulture
|
||||
{
|
||||
get { return this.originalCultureInfo; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the UI culture information to use. This implementation
|
||||
/// returns a snapshot of the UI culture information of the thread
|
||||
/// that created this object.
|
||||
/// </summary>
|
||||
public override System.Globalization.CultureInfo CurrentUICulture
|
||||
{
|
||||
get { return this.originalUICultureInfo; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This implementation always returns the GUID allocated at
|
||||
/// instantiation time.
|
||||
/// </summary>
|
||||
public override Guid InstanceId
|
||||
{
|
||||
get { return this.myId; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a string that contains the name of the host implementation.
|
||||
/// Keep in mind that this string may be used by script writers to
|
||||
/// identify when your host is being used.
|
||||
/// </summary>
|
||||
public override string Name
|
||||
{
|
||||
get { return "MySampleConsoleHostImplementation"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This sample does not implement a PSHostUserInterface component so
|
||||
/// this property simply returns null.
|
||||
/// </summary>
|
||||
public override PSHostUserInterface UI
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return the version object for this application. Typically this
|
||||
/// should match the version resource in the application.
|
||||
/// </summary>
|
||||
public override Version Version
|
||||
{
|
||||
get { return new Version(1, 0, 0, 0); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented by this example class. The call fails with
|
||||
/// a NotImplementedException exception.
|
||||
/// </summary>
|
||||
public override void EnterNestedPrompt()
|
||||
{
|
||||
throw new NotImplementedException(
|
||||
"The method or operation is not implemented.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Not implemented by this example class. The call fails
|
||||
/// with a NotImplementedException exception.
|
||||
/// </summary>
|
||||
public override void ExitNestedPrompt()
|
||||
{
|
||||
throw new NotImplementedException(
|
||||
"The method or operation is not implemented.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This API is called before an external application process is
|
||||
/// started. Typically it is used to save state so the parent can
|
||||
/// restore state that has been modified by a child process (after
|
||||
/// the child exits). In this example, this functionality is not
|
||||
/// needed so the method returns nothing.
|
||||
/// </summary>
|
||||
public override void NotifyBeginApplication()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This API is called after an external application process finishes.
|
||||
/// Typically it is used to restore state that a child process may
|
||||
/// have altered. In this example, this functionality is not
|
||||
/// needed so the method returns nothing.
|
||||
/// </summary>
|
||||
public override void NotifyEndApplication()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Indicate to the host application that exit has
|
||||
/// been requested. Pass the exit code that the host
|
||||
/// application should use when exiting the process.
|
||||
/// </summary>
|
||||
/// <param name="exitCode">The exit code to use.</param>
|
||||
public override void SetShouldExit(int exitCode)
|
||||
{
|
||||
// this.program.ShouldExit = true;
|
||||
// this.program.ExitCode = exitCode;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,7 +63,33 @@ namespace PSTests
|
|||
foreach (var result in powerShell.Invoke())
|
||||
{
|
||||
++objCount;
|
||||
Console.WriteLine(result);
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue