From dae6e0ebd5adff548c77759bddb43626a9a1dfc1 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 16 Feb 2016 17:06:59 -0800 Subject: [PATCH] Use Linux.Host in xUnit tests Make its internals visible to the `powershell-tests` assembly, and use its `MyHost` and `Listener` classes instead of facades. --- .../AssemblyInfo.cs | 2 + test/csharp/MyHost.cs | 166 ------------------ test/csharp/test_Runspace.cs | 7 +- 3 files changed, 7 insertions(+), 168 deletions(-) create mode 100644 src/Microsoft.PowerShell.Linux.Host/AssemblyInfo.cs delete mode 100644 test/csharp/MyHost.cs diff --git a/src/Microsoft.PowerShell.Linux.Host/AssemblyInfo.cs b/src/Microsoft.PowerShell.Linux.Host/AssemblyInfo.cs new file mode 100644 index 000000000..dd02d4834 --- /dev/null +++ b/src/Microsoft.PowerShell.Linux.Host/AssemblyInfo.cs @@ -0,0 +1,2 @@ +using System.Runtime.CompilerServices; +[assembly:InternalsVisibleTo("powershell-tests")] diff --git a/test/csharp/MyHost.cs b/test/csharp/MyHost.cs deleted file mode 100644 index 59cbd41c3..000000000 --- a/test/csharp/MyHost.cs +++ /dev/null @@ -1,166 +0,0 @@ -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/test/csharp/test_Runspace.cs b/test/csharp/test_Runspace.cs index 398c92472..96dbe6355 100644 --- a/test/csharp/test_Runspace.cs +++ b/test/csharp/test_Runspace.cs @@ -2,6 +2,7 @@ using Xunit; using System; using System.Management.Automation; using System.Management.Automation.Runspaces; +using Microsoft.PowerShell.Linux.Host; namespace PSTests { @@ -70,7 +71,8 @@ namespace PSTests [Fact] public void TestRunspaceWithPowerShellAndHost() { - MyHost myHost = new MyHost(this); + Listener listener = new Listener("", false); + MyHost myHost = new MyHost(listener); using (var runspace = RunspaceFactory.CreateRunspace(myHost)) { runspace.Open(); @@ -97,7 +99,8 @@ namespace PSTests [Fact] public void TestRunspaceWithFunction() { - MyHost myHost = new MyHost(this); + Listener listener = new Listener("", false); + MyHost myHost = new MyHost(listener); using (var runspace = RunspaceFactory.CreateRunspace(myHost)) { runspace.Open();