diff --git a/src/Microsoft.PowerShell.Linux.UnitTests/MyHost.cs b/src/Microsoft.PowerShell.Linux.UnitTests/MyHost.cs new file mode 100644 index 000000000..59cbd41c3 --- /dev/null +++ b/src/Microsoft.PowerShell.Linux.UnitTests/MyHost.cs @@ -0,0 +1,166 @@ +namespace PSTests +{ + using System; + using System.Globalization; + using System.Management.Automation.Host; + + /// + /// 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. + /// + internal class MyHost : PSHost + { + /// + /// A reference to the PSHost implementation. + /// + private RunspaceTests program; + + /// + /// The culture information of the thread that created + /// this object. + /// + private CultureInfo originalCultureInfo = + CultureInfo.CurrentCulture; + // System.Threading.Thread.CurrentThread.CurrentCulture; + + /// + /// The UI culture information of the thread that created + /// this object. + /// + private CultureInfo originalUICultureInfo = + CultureInfo.CurrentCulture; + // System.Threading.Thread.CurrentThread.CurrentUICulture; + + /// + /// The identifier of this PSHost implementation. + /// + private Guid myId = Guid.NewGuid(); + + /// + /// 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. + /// + /// + /// A reference to the host application object. + /// + public MyHost(RunspaceTests program) + { + this.program = program; + } + + /// + /// Return the culture information to use. This implementation + /// returns a snapshot of the culture information of the thread + /// that created this object. + /// + public override System.Globalization.CultureInfo CurrentCulture + { + get { return this.originalCultureInfo; } + } + + /// + /// Return the UI culture information to use. This implementation + /// returns a snapshot of the UI culture information of the thread + /// that created this object. + /// + public override System.Globalization.CultureInfo CurrentUICulture + { + get { return this.originalUICultureInfo; } + } + + /// + /// This implementation always returns the GUID allocated at + /// instantiation time. + /// + public override Guid InstanceId + { + get { return this.myId; } + } + + /// + /// 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. + /// + public override string Name + { + get { return "MySampleConsoleHostImplementation"; } + } + + /// + /// This sample does not implement a PSHostUserInterface component so + /// this property simply returns null. + /// + public override PSHostUserInterface UI + { + get { return null; } + } + + /// + /// Return the version object for this application. Typically this + /// should match the version resource in the application. + /// + public override Version Version + { + get { return new Version(1, 0, 0, 0); } + } + + /// + /// Not implemented by this example class. The call fails with + /// a NotImplementedException exception. + /// + public override void EnterNestedPrompt() + { + throw new NotImplementedException( + "The method or operation is not implemented."); + } + + /// + /// Not implemented by this example class. The call fails + /// with a NotImplementedException exception. + /// + public override void ExitNestedPrompt() + { + throw new NotImplementedException( + "The method or operation is not implemented."); + } + + /// + /// 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. + /// + public override void NotifyBeginApplication() + { + return; + } + + /// + /// 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. + /// + public override void NotifyEndApplication() + { + return; + } + + /// + /// Indicate to the host application that exit has + /// been requested. Pass the exit code that the host + /// application should use when exiting the process. + /// + /// The exit code to use. + public override void SetShouldExit(int exitCode) + { + // this.program.ShouldExit = true; + // this.program.ExitCode = exitCode; + } + } +} diff --git a/src/Microsoft.PowerShell.Linux.UnitTests/test_Runspace.cs b/src/Microsoft.PowerShell.Linux.UnitTests/test_Runspace.cs index 9181711a2..3e1f1d690 100644 --- a/src/Microsoft.PowerShell.Linux.UnitTests/test_Runspace.cs +++ b/src/Microsoft.PowerShell.Linux.UnitTests/test_Runspace.cs @@ -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);