Esempio n. 1
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();
        }
Esempio n. 2
0
        public void SetUpRunspace()
        {
            TestHost host = new TestHost(new TestHostUserInterface());
            // use public static property, so we can access e.g. the ExecutionContext after execution
            _runspace = RunspaceFactory.CreateRunspace(host) as LocalRunspace;
            _runspace.Open();
            var alias = new AliasInfo("gciAlias", "Get-ChildItem", _runspace.CommandManager);
            _runspace.ExecutionContext.SessionState.Alias.New(alias, "global");
            _runspace.ExecutionContext.SessionState.Function.Set("MyFunction", new ScriptBlock(null));
            _runspace.ExecutionContext.SessionState.PSVariable.Set("myvar", 1);
            var newScope = _runspace.ExecutionContext.Clone(ScopeUsages.NewScope);
            newScope.SessionState.PSVariable.Set("myothervar", 2);
            newScope.SessionState.Function.Set("MyFun2", new ScriptBlock(null));
            _runspace.ExecutionContext = newScope;

            _tabExp = new TabExpansionProvider(_runspace);
        }
Esempio n. 3
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();
        }
Esempio n. 4
0
        public void createScopes()
        {
            TestHost testHost = new TestHost(new TestHostUserInterface());

            Runspace hostRunspace = TestHost.CreateRunspace(testHost);

            globalState = hostRunspace.ExecutionContext.SessionState;

            var dummyProviderInfo = new ProviderInfo(globalState, typeof(DummyProvider), "DummyProvider", "", null);
            globalState.Provider.Add(dummyProviderInfo, hostRunspace.ExecutionContext);
            dummyProvider = globalState.Provider.GetOne("DummyProvider");

            scriptState = new SessionState(globalState);
            scriptState.IsScriptScope = true;
            functionState = new SessionState(scriptState);
            localState = new SessionState(functionState);
            states = new Dictionary<AvailableStates, SessionState>();
            states.Add(AvailableStates.Global, globalState);
            states.Add(AvailableStates.Script, scriptState);
            states.Add(AvailableStates.Function, functionState);
            states.Add(AvailableStates.Local, localState);

            hostCommandManager = new CommandManager(hostRunspace as LocalRunspace);
        }
Esempio n. 5
0
 public void PipelineTest2()
 {
     Assert.AreEqual("xxx" + Environment.NewLine, TestHost.Execute("'xxx' | Write-Host"));
 }
Esempio n. 6
0
            public void TrapDoesNotTrapExitStatement()
            {
                string result = TestHost.Execute("Write-Host 'Exiting'; exit; trap { 'Trapped' }");

                Assert.AreEqual("Exiting" + Environment.NewLine, result);
            }
Esempio n. 7
0
        public void ComparisonTest(string input, string expected)
        {
            string result = TestHost.Execute(input);

            StringAssert.AreEqualIgnoringCase(expected + Environment.NewLine, result);
        }
Esempio n. 8
0
            public void TryBlockThrowsExceptionAndErrorRecordAvailableInCatchBlock()
            {
                string result = TestHost.Execute(@"try { 'abc'.Substring(-1) } catch { Write-Host $_.GetType() }");

                StringAssert.AreEqualIgnoringCase("System.Management.Automation.ErrorRecord" + Environment.NewLine, result);
            }
Esempio n. 9
0
        public void UndefinedVariableIsNull()
        {
            var result = TestHost.Execute("$a -eq $null");

            Assert.AreEqual("True" + Environment.NewLine, result);
        }
Esempio n. 10
0
 public void IfNotEqualTest()
 {
     StringAssert.AreEqualIgnoringCase("", TestHost.Execute("if (1 -ne 1) { 'xxx' }"));
     StringAssert.AreEqualIgnoringCase("xxx" + Environment.NewLine, TestHost.Execute("if (1 -ne 2) { 'xxx' }"));
 }
Esempio n. 11
0
            public void TryBlockThrowsExceptionAndSingleStatementInCatchBlockIsExecuted()
            {
                string result = TestHost.Execute(@"try { $null.GetType() } catch { Write-Host 'catch' }");

                StringAssert.AreEqualIgnoringCase("catch" + Environment.NewLine, result);
            }
Esempio n. 12
0
 public void TrueTest()
 {
     StringAssert.AreEqualIgnoringCase("True" + Environment.NewLine, TestHost.Execute("$true"));
 }
Esempio n. 13
0
 public void IfTrueTest()
 {
     StringAssert.AreEqualIgnoringCase("xxx" + Environment.NewLine, TestHost.Execute("if ($true) { 'xxx' }"));
 }
Esempio n. 14
0
 public void WriteHostNothing()
 {
     Assert.AreEqual(Environment.NewLine, TestHost.Execute("Write-Host"));
 }
Esempio n. 15
0
        public void GetChildItemFromRootDefaultProviderShouldReturnSomething()
        {
            var result = TestHost.ExecuteWithZeroErrors("Get-ChildItem /");

            Assert.Greater(result.Length, 0);
        }
Esempio n. 16
0
 public void WriteHost()
 {
     Assert.AreEqual("xxx" + Environment.NewLine, TestHost.Execute("Write-Host 'xxx'"));
 }
Esempio n. 17
0
 public void RootPathTest()
 {
     StringAssert.AreEqualIgnoringCase(Path.GetPathRoot(Environment.CurrentDirectory) + Environment.NewLine,
                                       TestHost.Execute("Set-Location / ; (Get-Location).Path"));
 }
Esempio n. 18
0
 public void WriteOutputString()
 {
     Assert.AreEqual("xxx" + Environment.NewLine, TestHost.Execute("Write-Output 'xxx'"));
 }
Esempio n. 19
0
        public void ForEachCharacterInStringIsString()
        {
            string result = TestHost.Execute("foreach ($char in 'abc') { $char }");

            Assert.AreEqual("abc" + Environment.NewLine, result);
        }
Esempio n. 20
0
 public void ByPropertyLowercase()
 {
     Assert.DoesNotThrow(delegate() {
         TestHost.Execute("Get-ChildItem | Sort-Object name");
     });
 }
Esempio n. 21
0
        public void ForEachCharacterInArray()
        {
            string result = TestHost.Execute("foreach ($char in 'abc'.ToCharArray()) { $char }");

            Assert.AreEqual(string.Format("a{0}b{0}c{0}", Environment.NewLine), result);
        }
Esempio n. 22
0
        public void ScriptBlock()
        {
            var result = TestHost.Execute("& { 1 }");

            Assert.AreEqual("1" + Environment.NewLine, result);
        }
Esempio n. 23
0
            public void TryBlockStatementIsExecuted()
            {
                string result = TestHost.Execute(@"try { Write-Host 'try' } catch { }");

                StringAssert.AreEqualIgnoringCase("try" + Environment.NewLine, result);
            }
Esempio n. 24
0
        public void Return()
        {
            var result = TestHost.Execute("& { return; 1 } ; 2");

            Assert.AreEqual("2" + Environment.NewLine, result);
        }
Esempio n. 25
0
            public void TryBlockReturnsAndSingleStatementInCatchBlockIsNotExecuted()
            {
                string result = TestHost.Execute(@"try { Write-Host 'exiting'; return } catch { Write-Host 'catch' }");

                StringAssert.AreEqualIgnoringCase("exiting" + Environment.NewLine, result);
            }
Esempio n. 26
0
        public void EnumParameter()
        {
            var result = TestHost.Execute("[Text.RegularExpressions.Regex]::IsMatch('FOO', 'foo', [Text.RegularExpressions.RegexOptions] 'IgnoreCase' )");

            Assert.AreEqual("True" + Environment.NewLine, result);
        }
Esempio n. 27
0
        public void AssignmentByModulusOperator(string input, string expected)
        {
            string result = TestHost.Execute(input);

            StringAssert.AreEqualIgnoringCase(expected + Environment.NewLine, result);
        }
Esempio n. 28
0
        public void TopLevelSideEffects(string input, string expected)
        {
            var result = TestHost.Execute(input);

            Assert.AreEqual(expected, result.Replace(Environment.NewLine, ""));
        }
Esempio n. 29
0
            public void TrapWithContinueStatement()
            {
                string result = TestHost.Execute("throw 'Error'; trap { 'Trapped'; continue }");

                Assert.AreEqual("Trapped" + Environment.NewLine, result);
            }
Esempio n. 30
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();
        }
Esempio n. 31
0
 public void ElseTest()
 {
     StringAssert.AreEqualIgnoringCase("yyy" + Environment.NewLine, TestHost.Execute("if (1 -eq 2) { 'xxx' } else { 'yyy' }"));
 }
Esempio n. 32
0
        public void ForEachWithAssignmentStatementAsBodyShouldNotOutputAssignmentResultOnEachIteration()
        {
            string result = TestHost.Execute("$j = 0; foreach ($i in 0..10) { $j++ }; $j");

            Assert.AreEqual("11" + Environment.NewLine, result);
        }
Esempio n. 33
0
        public void For()
        {
            var result = TestHost.Execute("for ($i = 0; $i -ile 10; $i++) { $i }");

            Assert.AreEqual(Enumerable.Range(0, 11).JoinString(Environment.NewLine) + Environment.NewLine, result);
        }
Esempio n. 34
0
 public void PipelineTest()
 {
     Assert.AreEqual("xxx", TestHost.Execute("'xxx' | Write-Host -NoNewline"));
 }
Esempio n. 35
0
        public void ForEach()
        {
            string result = TestHost.Execute("foreach ($i in (0..10)) { $i }");

            Assert.AreEqual(Enumerable.Range(0, 11).JoinString(Environment.NewLine) + Environment.NewLine, result);
        }
Esempio n. 36
0
        public void createScopes()
        {
            TestHost testHost = new TestHost(new TestHostUserInterface());
            Runspace hostRunspace = TestHost.CreateRunspace(testHost);

            globalState = hostRunspace.ExecutionContext.SessionState;
            scriptState = new SessionState(globalState);
            scriptState.IsScriptScope = true;
            functionState = new SessionState(scriptState);
            localState = new SessionState(functionState);
            states = new Dictionary<AvailableStates, SessionState>();
            states.Add(AvailableStates.Global, globalState);
            states.Add(AvailableStates.Script, scriptState);
            states.Add(AvailableStates.Function, functionState);
            states.Add(AvailableStates.Local, localState);

            hostCommandManager = new CommandManager(hostRunspace as LocalRunspace);
        }