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(); }
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); }
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(); }
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); }
public void PipelineTest2() { Assert.AreEqual("xxx" + Environment.NewLine, TestHost.Execute("'xxx' | Write-Host")); }
public void TrapDoesNotTrapExitStatement() { string result = TestHost.Execute("Write-Host 'Exiting'; exit; trap { 'Trapped' }"); Assert.AreEqual("Exiting" + Environment.NewLine, result); }
public void ComparisonTest(string input, string expected) { string result = TestHost.Execute(input); StringAssert.AreEqualIgnoringCase(expected + Environment.NewLine, result); }
public void TryBlockThrowsExceptionAndErrorRecordAvailableInCatchBlock() { string result = TestHost.Execute(@"try { 'abc'.Substring(-1) } catch { Write-Host $_.GetType() }"); StringAssert.AreEqualIgnoringCase("System.Management.Automation.ErrorRecord" + Environment.NewLine, result); }
public void UndefinedVariableIsNull() { var result = TestHost.Execute("$a -eq $null"); Assert.AreEqual("True" + Environment.NewLine, result); }
public void IfNotEqualTest() { StringAssert.AreEqualIgnoringCase("", TestHost.Execute("if (1 -ne 1) { 'xxx' }")); StringAssert.AreEqualIgnoringCase("xxx" + Environment.NewLine, TestHost.Execute("if (1 -ne 2) { 'xxx' }")); }
public void TryBlockThrowsExceptionAndSingleStatementInCatchBlockIsExecuted() { string result = TestHost.Execute(@"try { $null.GetType() } catch { Write-Host 'catch' }"); StringAssert.AreEqualIgnoringCase("catch" + Environment.NewLine, result); }
public void TrueTest() { StringAssert.AreEqualIgnoringCase("True" + Environment.NewLine, TestHost.Execute("$true")); }
public void IfTrueTest() { StringAssert.AreEqualIgnoringCase("xxx" + Environment.NewLine, TestHost.Execute("if ($true) { 'xxx' }")); }
public void WriteHostNothing() { Assert.AreEqual(Environment.NewLine, TestHost.Execute("Write-Host")); }
public void GetChildItemFromRootDefaultProviderShouldReturnSomething() { var result = TestHost.ExecuteWithZeroErrors("Get-ChildItem /"); Assert.Greater(result.Length, 0); }
public void WriteHost() { Assert.AreEqual("xxx" + Environment.NewLine, TestHost.Execute("Write-Host 'xxx'")); }
public void RootPathTest() { StringAssert.AreEqualIgnoringCase(Path.GetPathRoot(Environment.CurrentDirectory) + Environment.NewLine, TestHost.Execute("Set-Location / ; (Get-Location).Path")); }
public void WriteOutputString() { Assert.AreEqual("xxx" + Environment.NewLine, TestHost.Execute("Write-Output 'xxx'")); }
public void ForEachCharacterInStringIsString() { string result = TestHost.Execute("foreach ($char in 'abc') { $char }"); Assert.AreEqual("abc" + Environment.NewLine, result); }
public void ByPropertyLowercase() { Assert.DoesNotThrow(delegate() { TestHost.Execute("Get-ChildItem | Sort-Object name"); }); }
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); }
public void ScriptBlock() { var result = TestHost.Execute("& { 1 }"); Assert.AreEqual("1" + Environment.NewLine, result); }
public void TryBlockStatementIsExecuted() { string result = TestHost.Execute(@"try { Write-Host 'try' } catch { }"); StringAssert.AreEqualIgnoringCase("try" + Environment.NewLine, result); }
public void Return() { var result = TestHost.Execute("& { return; 1 } ; 2"); Assert.AreEqual("2" + Environment.NewLine, result); }
public void TryBlockReturnsAndSingleStatementInCatchBlockIsNotExecuted() { string result = TestHost.Execute(@"try { Write-Host 'exiting'; return } catch { Write-Host 'catch' }"); StringAssert.AreEqualIgnoringCase("exiting" + Environment.NewLine, result); }
public void EnumParameter() { var result = TestHost.Execute("[Text.RegularExpressions.Regex]::IsMatch('FOO', 'foo', [Text.RegularExpressions.RegexOptions] 'IgnoreCase' )"); Assert.AreEqual("True" + Environment.NewLine, result); }
public void AssignmentByModulusOperator(string input, string expected) { string result = TestHost.Execute(input); StringAssert.AreEqualIgnoringCase(expected + Environment.NewLine, result); }
public void TopLevelSideEffects(string input, string expected) { var result = TestHost.Execute(input); Assert.AreEqual(expected, result.Replace(Environment.NewLine, "")); }
public void TrapWithContinueStatement() { string result = TestHost.Execute("throw 'Error'; trap { 'Trapped'; continue }"); Assert.AreEqual("Trapped" + Environment.NewLine, result); }
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(); }
public void ElseTest() { StringAssert.AreEqualIgnoringCase("yyy" + Environment.NewLine, TestHost.Execute("if (1 -eq 2) { 'xxx' } else { 'yyy' }")); }
public void ForEachWithAssignmentStatementAsBodyShouldNotOutputAssignmentResultOnEachIteration() { string result = TestHost.Execute("$j = 0; foreach ($i in 0..10) { $j++ }; $j"); Assert.AreEqual("11" + Environment.NewLine, result); }
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); }
public void PipelineTest() { Assert.AreEqual("xxx", TestHost.Execute("'xxx' | Write-Host -NoNewline")); }
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); }
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); }