public async void RunScript_ReadsDataFromInputStream() { var lf = TestLoggerFactory.Create(); var script = @"$Input | ForEach { Write-Output $_ }"; var expected = new[] { 32, 120, 71, 89, 20 }; var input = new PSDataCollection <PSObject>(5); _ = Task.Run( () => { foreach (var num in expected) { input.Add(num); Thread.Sleep(num); } input.Complete(); } ); var result = await PwshRunner.RunScript(script, lf.CreateLogger("Test"), null, input); var actual = result.Select(o => (int)o.BaseObject).ToArray(); Assert.Equal(expected, actual); }
public async void RunScript_CorrectlyPassesVariablesToScript() { var lf = TestLoggerFactory.Create(); var script = @"1..6 | % { Write-Output (Get-Variable -Name ""Var$_"").Value }"; var entity = Entity.Create( ("Var1", "value1"), ("Var2", 2), ("Var3", 3.3), ("Var4", true), ("Var5", Core.SCLType.Enum), ("Var6", new DateTime(2020, 12, 12)) ); var result = await PwshRunner.RunScript(script, lf.CreateLogger("Test"), entity, null); for (var i = 0; i < entity.Dictionary.Count; i++) { Assert.Equal( entity.Dictionary[$"Var{i + 1}"].BestValue.ObjectValue, result[i].BaseObject ); } }
public async void RunScript_ReadsDataFromOutputStream() { var logger = TestLoggerFactory.Create().CreateLogger("Test"); var script = @"Write-Output 'one'; Write-Output 2"; var result = await PwshRunner.RunScript(script, logger); Assert.Equal(new List <PSObject> { "one", 2 }, result); }
public async void RunScript_LogsErrorsWarningsAndInformation() { var lf = TestLoggerFactory.Create(); var script = @"Write-Output 'one'; Write-Error 'error'; Write-Output 'two'; Write-Warning 'warning'; Write-Information 'info'"; _ = await PwshRunner.RunScript(script, lf.CreateLogger("Test")); Assert.Equal(3, lf.Sink.LogEntries.Count()); Assert.Contains(lf.Sink.LogEntries, o => o.Message != null && o.Message.Equals("error")); Assert.Contains(lf.Sink.LogEntries, o => o.Message != null && o.Message.Equals("warning")); Assert.Contains(lf.Sink.LogEntries, o => o.Message != null && o.Message.Equals("info")); }