diff --git a/scripts/Makefile b/scripts/Makefile index 174a68bd0..1bd25a099 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -194,7 +194,7 @@ native-tests: dotnetlibs/monad_native xunit-tests: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr $(addprefix $(TESTRUN_FOLDER)/, ps_test.dll xunit.console.netcore.exe xunit.runner.utility.dll xunit.abstractions.dll xunit.execution.dll) # execute the xUnit runner, with XML output - cd exec_env/app_base && PSMODULEPATH=$(shell pwd)/exec_env/app_base/Modules LD_LIBRARY_PATH=../coreclr:. ../coreclr/corerun xunit.console.netcore.exe ps_test.dll -xml ../../xunittests.xml + exec_env/app_base/runps-test.sh ps_test.dll -xml ../../xunittests.xml pester-tests: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr # execute the Pester tests, which needs a TEMP environment variable to be set diff --git a/scripts/runps-test.sh b/scripts/runps-test.sh new file mode 100755 index 000000000..5196f708b --- /dev/null +++ b/scripts/runps-test.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +CWD=$(pwd) +SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) + +cd "$SCRIPTDIR" +PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa xunit.console.netcore.exe xunit.console.netcore.exe "$@" diff --git a/src/ps_test/test_Runspace.cs b/src/ps_test/test_Runspace.cs new file mode 100644 index 000000000..f9b4745eb --- /dev/null +++ b/src/ps_test/test_Runspace.cs @@ -0,0 +1,48 @@ +using Xunit; +using System; +using System.Management.Automation; +using System.Management.Automation.Runspaces; + +namespace PSTests +{ + public static class RunspaceTests + { + [Fact] + public static void TestMethod() + { + InitialSessionState iss = InitialSessionState.CreateDefault2(); + + // NOTE: instantiate custom host myHost for the next line to capture stdout and stderr output + // in addition to just the PSObjects + using (Runspace rs = RunspaceFactory.CreateRunspace(/*myHost,*/iss)) + { + rs.Open(); + using (PowerShell ps = PowerShell.Create()) + { + ps.Runspace = rs; + + string script = "get-process | select-object -first 3"; + ps.AddScript(script); + + // IMPORTANT NOTE: do not call AddCommand("out-host") here or + // MergeMyResults, otherwise Invoke will not return any objects + + var results = ps.Invoke(); + + // check that there are 3 captured objects + int objCount = 0; + foreach (var result in results) + { + // this is how an object would be captured here and looked at, + // each result is a PSObject with the data from the pipeline + ++objCount; + Assert.NotNull(result); + } + Assert.Equal(3,objCount); + ps.Dispose(); + } + } + } + } +} +