Example #1
0
        private static string Execute(bool logErrors, Action <string> onErrorHandler, params string[] statements)
        {
            TestHostUserInterface ui = new TestHostUserInterface();

            if (logErrors)
            {
                ui.OnWriteErrorLineString = s => ui.Log.Append(s);
            }

            TestHost host       = new TestHost(ui);
            var      myRunSpace = CreateRunspace(host);

            myRunSpace.Open();

            foreach (var statement in statements)
            {
                using (var currentPipeline = myRunSpace.CreatePipeline())
                {
                    currentPipeline.Commands.Add(statement);
                    currentPipeline.Commands.Add("Out-Default");
                    currentPipeline.Invoke();
                }
            }

            return(ui.Log.ToString());
        }
Example #2
0
 public void ValueGatheringWithArrayInputAndConversion()
 {
     var ui = new TestHostUserInterface();
     var intArray = new [] { 4, 5, 6 };
     ui.SetInput(String.Join(Environment.NewLine, intArray) + Environment.NewLine + Environment.NewLine);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestIntegerArraySumCommand)));
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.AreEqual(TestIntegerArraySumCommand.Transform(intArray), lines[lines.Length - 1]);
 }
Example #3
0
 public void MandatoryValueIsGatheredIfNotProvided()
 {
     var ui = new TestHostUserInterface();
     string val = "foobar";
     ui.SetInput(val + Environment.NewLine);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestWithMandatoryCommand)));
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.AreEqual(TestWithMandatoryCommand.Transform(val), lines[lines.Length - 1]);
 }
Example #4
0
 public void ValueGatheringWithArrayInputAndConversion()
 {
     var ui = new TestHostUserInterface();
     var intArray = new [] { 4, 5, 6 };
     ui.SetInput(String.Join(Environment.NewLine, intArray) + Environment.NewLine + Environment.NewLine);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestIntegerArraySumCommand)));
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.AreEqual(TestIntegerArraySumCommand.Transform(intArray), lines[lines.Length - 1]);
 }
Example #5
0
 public void ValueGatheringForPSCredentialWithStringAsCredential()
 {
     var ui = new TestHostUserInterface();
     ui.SetInput("Bo" + Environment.NewLine + "SecretPassword" + Environment.NewLine);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestPrintCredentialsCommand)) + " 'BiBa'");
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.That(lines[lines.Length - 2], Is.EqualTo("User: BiBaBo"));
     Assert.That(lines[lines.Length - 1], Is.EqualTo("Password: SecretPassword"));
 }
Example #6
0
 public void MandatoryValueIsGatheredIfNotProvided()
 {
     var ui = new TestHostUserInterface();
     string val = "foobar";
     ui.SetInput(val + Environment.NewLine);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestWithMandatoryCommand)));
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.AreEqual(TestWithMandatoryCommand.Transform(val), lines[lines.Length - 1]);
 }
Example #7
0
 public void ValueGatheringCanProvideHelp()
 {
     var ui = new TestHostUserInterface();
     string val = "test";
     string input = "!?" + Environment.NewLine + val + Environment.NewLine;
     ui.SetInput(input);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestWithMandatoryCommand)));
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.Greater(lines.Length, 3, "Not enough output");
     Assert.AreEqual(TestWithMandatoryCommand.Transform(val), lines[lines.Length - 1]);
     Assert.AreEqual(TestWithMandatoryCommand.HELP_MSG, lines[lines.Length - 3]);
 }
Example #8
0
 public void ValueGatheringCanProvideHelp()
 {
     var ui = new TestHostUserInterface();
     string val = "test";
     string input = "!?" + Environment.NewLine + val + Environment.NewLine;
     ui.SetInput(input);
     var res = TestHost.Execute(true, null, ui, CmdletName(typeof(TestWithMandatoryCommand)));
     var lines = res.Split(new [] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
     Assert.Greater(lines.Length, 3, "Not enough output");
     Assert.AreEqual(TestWithMandatoryCommand.Transform(val), lines[lines.Length - 1]);
     Assert.AreEqual(TestWithMandatoryCommand.HELP_MSG, lines[lines.Length - 3]);
 }
Example #9
0
        public static string Execute(bool logErrors, Action <string> onErrorHandler, TestHostUserInterface ui,
                                     params string[] statements)
        {
            if (logErrors)
            {
                ui.OnWriteErrorLineString = onErrorHandler ?? (s => ui.Log.Append(s));
            }

            TestHost host = new TestHost(ui);

            // use public static property, so we can access e.g. the ExecutionContext after execution
            LastUsedRunspace = CreateRunspace(host);
            LastUsedRunspace.Open();
            _doExit      = false;
            LastExitCode = null;

            foreach (var statement in statements)
            {
                if (_doExit)
                {
                    break;
                }
                using (var currentPipeline = LastUsedRunspace.CreatePipeline())
                {
                    currentPipeline.Commands.AddScript(statement, false);
                    currentPipeline.Commands.Add("Out-Default");
                    try
                    {
                        currentPipeline.Invoke();
                    }
                    catch (Exception e)
                    {
                        ui.WriteErrorLine(e.ToString());
                    }
                    // pipeline might failed, write errors to ui
                    if (currentPipeline.PipelineStateInfo.State.Equals(PipelineState.Failed))
                    {
                        foreach (var error in currentPipeline.Error.ReadToEnd())
                        {
                            ui.WriteErrorLine(error.ToString());
                        }
                    }
                }
            }

            return(ui.Log.ToString());
        }
Example #10
0
        public static string Execute(bool logErrors, Action<string> onErrorHandler, TestHostUserInterface ui,
                                     params string[] statements)
        {
            if (logErrors)
            {
                ui.OnWriteErrorLineString = onErrorHandler ?? (s => ui.Log.Append(s));
            }

            TestHost host = new TestHost(ui);
            // use public static property, so we can access e.g. the ExecutionContext after execution
            LastUsedRunspace = CreateRunspace(host);
            LastUsedRunspace.Open();
            _doExit = false;
            LastExitCode = null;

            foreach (var statement in statements)
            {
                if (_doExit)
                {
                    break;
                }
                using (var currentPipeline = LastUsedRunspace.CreatePipeline())
                {
                    currentPipeline.Commands.AddScript(statement, false);
                    currentPipeline.Commands.Add("Out-Default");
                    try
                    {
                        currentPipeline.Invoke();
                    }
                    catch (Exception e)
                    {
                        ui.WriteErrorLine(e.ToString());
                    }
                    // pipeline might failed, write errors to ui
                    if (currentPipeline.PipelineStateInfo.State.Equals(PipelineState.Failed))
                    {
                        foreach (var error in currentPipeline.Error.ReadToEnd())
                        {
                            ui.WriteErrorLine(error.ToString());
                        }
                    }
                }
            }

            return ui.Log.ToString();
        }
Example #11
0
        public static string Execute(params string[] statements)
        {
            TestHostUserInterface ui = new TestHostUserInterface();

            TestHost host = new TestHost(ui);
            var myRunSpace = RunspaceFactory.CreateRunspace(host);
            myRunSpace.Open();

            foreach (var statement in statements)
            {
                using (var currentPipeline = myRunSpace.CreatePipeline())
                {
                    currentPipeline.Commands.Add(statement);
                    currentPipeline.Commands.Add("Out-Default");
                    currentPipeline.Invoke();
                }
            }

            return ui.Log.ToString();
        }
Example #12
0
        public static string Execute(params string[] statements)
        {
            TestHostUserInterface ui = new TestHostUserInterface();

            TestHost host       = new TestHost(ui);
            var      myRunSpace = RunspaceFactory.CreateRunspace(host);

            myRunSpace.Open();

            foreach (var statement in statements)
            {
                using (var currentPipeline = myRunSpace.CreatePipeline())
                {
                    currentPipeline.Commands.Add(statement);
                    currentPipeline.Commands.Add("Out-Default");
                    currentPipeline.Invoke();
                }
            }

            return(ui.Log.ToString());
        }
Example #13
0
 public TestHost(TestHostUserInterface ui)
 {
     // TODO: Complete member initialization
     this._ui = ui;
 }
Example #14
0
 private void CreateFreshHostAndUI(bool interactive)
 {
     HostUI = new TestHostUserInterface();
     FullHost = new FullHost(interactive);
     FullHost.LocalHost.SetHostUserInterface(HostUI);
 }
Example #15
0
        private static string Execute(bool logErrors, Action<string> onErrorHandler, params string[] statements)
        {
            TestHostUserInterface ui = new TestHostUserInterface();

            if (logErrors)
                ui.OnWriteErrorLineString = s => ui.Log.Append(s);

            TestHost host = new TestHost(ui);
            var myRunSpace = CreateRunspace(host);
            myRunSpace.Open();

            foreach (var statement in statements)
            {
                using (var currentPipeline = myRunSpace.CreatePipeline())
                {
                    currentPipeline.Commands.Add(statement);
                    currentPipeline.Commands.Add("Out-Default");
                    currentPipeline.Invoke();
                }
            }

            return ui.Log.ToString();
        }
Example #16
0
 private void CreateFreshHostAndUI(bool interactive)
 {
     HostUI   = new TestHostUserInterface();
     FullHost = new FullHost(interactive);
     FullHost.LocalHost.SetHostUserInterface(HostUI);
 }
Example #17
0
 public TestHost(TestHostUserInterface ui)
 {
     // TODO: Complete member initialization
     this._ui = ui;
 }