public void CanObtainExitCodes()
        {
            {
                var si = new ProcessPipelineStartInfo();
                si.Add(TestUtil.TestChildPath, "ExitCode", "0");
                si.Add(TestUtil.TestChildPath, "ExitCode", "0");
                si.Add(TestUtil.TestChildPath, "ExitCode", "0");

                using (var sut = ProcessPipeline.Start(si))
                {
                    sut.WaitForExit();
                    Assert.True(sut.IsSuccessful);
                    Assert.Equal(0, sut.ExitCode);
                }
            }

            {
                var si = new ProcessPipelineStartInfo();
                si.Add(TestUtil.TestChildPath, "ExitCode", "0");
                si.Add(TestUtil.TestChildPath, "ExitCode", "1");
                si.Add(TestUtil.TestChildPath, "ExitCode", "-1");

                using (var sut = ProcessPipeline.Start(si))
                {
                    sut.WaitForExit();
                    Assert.False(sut.IsSuccessful);
                    Assert.Equal(-1, sut.ExitCode);
                    Assert.Equal(new int?[] { 0, 1, -1 }, sut.GetExitCodes());
                }
            }
        }
        public void ExitCodesThrowBeforeChildrenExit()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdInputRedirection = InputRedirection.InputPipe,
            };

            si.Add(TestUtil.TestChildPath, new[] { "EchoBack" });
            si.Add(TestUtil.TestChildPath, new[] { "EchoBack" });
            si.Add(TestUtil.TestChildPath, new[] { "EchoBack" });

            using (var sut = ProcessPipeline.Start(si))
            {
                Assert.Throws <InvalidOperationException>(() => sut.IsSuccessful);
                Assert.Throws <InvalidOperationException>(() => sut.GetExitCodes());
                Assert.Throws <InvalidOperationException>(() => sut.ExitCode);

                sut.StandardInput.Close();
                sut.WaitForExit();

                Assert.True(sut.IsSuccessful);
                Assert.Equal(0, sut.ExitCode);
                Assert.Equal(new int?[] { 0, 0, 0 }, sut.GetExitCodes());
            }
        }
        public async Task CorrectlyConnectOutputPipes()
        {
            {
                var si = new ProcessPipelineStartInfo()
                {
                    StdOutputRedirection = OutputRedirection.OutputPipe,
                    StdErrorRedirection  = OutputRedirection.ErrorPipe,
                };
                si.Add(TestUtil.TestChildPath, "EchoOutAndError");
                si.Add(TestUtil.TestChildPath, "EchoBack");
                si.Add(TestUtil.TestChildPath, "EchoBack");

                using (var sut = ProcessPipeline.Start(si))
                {
                    await ChildProcessAssert.CorrectlyConnectsPipesAsync(sut, "TestChild.Out", "TestChild.Error");
                }
            }

            {
                // invert stdout and stderr
                var si = new ProcessPipelineStartInfo()
                {
                    StdOutputRedirection = OutputRedirection.ErrorPipe,
                    StdErrorRedirection  = OutputRedirection.OutputPipe,
                };
                si.Add(TestUtil.TestChildPath, "EchoOutAndError");
                si.Add(TestUtil.TestChildPath, "EchoBack");
                si.Add(TestUtil.TestChildPath, "EchoBack");

                using (var sut = ProcessPipeline.Start(si))
                {
                    await ChildProcessAssert.CorrectlyConnectsPipesAsync(sut, "TestChild.Error", "TestChild.Out");
                }
            }
        }
        public void CanCreateChildProcess()
        {
            {
                var si = new ProcessPipelineStartInfo()
                {
                    StdOutputRedirection = OutputRedirection.OutputPipe,
                };
                si.Add(TestUtil.TestChildPath);

                using (var sut = ProcessPipeline.Start(si))
                {
                    ChildProcessAssert.CanCreateChildProcess(sut);
                }
            }

            {
                var si = new ProcessPipelineStartInfo()
                {
                    StdOutputRedirection = OutputRedirection.OutputPipe,
                };
                si.Add(TestUtil.TestChildPath);
                si.Add(TestUtil.TestChildPath, "EchoBack");

                using (var sut = ProcessPipeline.Start(si))
                {
                    ChildProcessAssert.CanCreateChildProcess(sut);
                }
            }
        }
Beispiel #5
0
        public void DefaultValueTest()
        {
            var sut = new ProcessPipelineStartInfo();

            Assert.Equal(InputRedirection.NullDevice, sut.StdInputRedirection);
            Assert.Equal(OutputRedirection.ParentOutput, sut.StdOutputRedirection);
            Assert.Equal(OutputRedirection.ParentError, sut.StdErrorRedirection);
            Assert.Null(sut.StdInputFile);
            Assert.Null(sut.StdInputHandle);
            Assert.Null(sut.StdOutputFile);
            Assert.Null(sut.StdOutputHandle);
            Assert.Null(sut.StdErrorFile);
            Assert.Null(sut.StdErrorHandle);
        }
        public void ReportsCreationFailure()
        {
            var si = new ProcessPipelineStartInfo();

            si.Add(TestUtil.TestChildPath, "ExitCode", "0");
            si.Add("nonexistentfile");
            si.Add(TestUtil.TestChildPath, "ExitCode", "0");

            using (var sut = ProcessPipeline.Start(si))
            {
                sut.WaitForExit();
                Assert.Equal(new int?[] { 0, null, 0 }, sut.GetExitCodes());
            }
        }
        private static ProcessPipeline CreateForWaitForExitTest()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdInputRedirection  = InputRedirection.InputPipe,
                StdOutputRedirection = OutputRedirection.NullDevice,
            };

            si.Add(TestUtil.TestChildPath, "EchoBack");
            si.Add(TestUtil.TestChildPath, "EchoBack");
            si.Add(TestUtil.TestChildPath, "EchoBack");

            return(ProcessPipeline.Start(si));
        }
Beispiel #8
0
        public void ProcessPipelineWaitForAsyncIsTrulyAsynchronous()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdOutputRedirection = OutputRedirection.NullDevice,
                StdErrorRedirection  = OutputRedirection.NullDevice,
            };

            si.Add(TestUtil.TestChildPath, "Sleep", "1000");
            si.Add(TestUtil.TestChildPath, "Sleep", "1000");

            using (var sut = ProcessPipeline.Start(si))
            {
                WaitForAsyncIsTrulyAsynchronous(sut);
            }
        }
        public async Task PipesAreAsynchronous()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdInputRedirection  = InputRedirection.InputPipe,
                StdOutputRedirection = OutputRedirection.OutputPipe,
                StdErrorRedirection  = OutputRedirection.ErrorPipe,
            };

            si.Add(TestUtil.TestChildPath, "EchoBack");
            si.Add(TestUtil.TestChildPath, "EchoBack");
            si.Add(TestUtil.TestChildPath, "EchoBack");

            using (var sut = ProcessPipeline.Start(si))
            {
                await ChildProcessAssert.PipesAreAsynchronousAsync(sut);
            }
        }
        public void ProcessPipelineWaitForAsyncIsTrulyAsynchronous()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdOutputRedirection = OutputRedirection.NullDevice,
                StdErrorRedirection  = OutputRedirection.NullDevice,
            };

            si.Add(TestUtil.DotnetCommand, TestUtil.TestChildPath, "Sleep", "1000");
            si.Add(TestUtil.DotnetCommand, TestUtil.TestChildPath, "Sleep", "1000");

            using (var sut = ProcessPipeline.Start(si))
            {
                WaitForAsyncIsTrulyAsynchronous(sut);
                sut.WaitForExit();
                Assert.True(sut.IsSuccessful);
            }
        }
        public async Task RedirectBothOutput()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdOutputRedirection = OutputRedirection.OutputPipe,
            };

            si.Add(ProcessPipelineItemFlags.RedirectBothOutput, TestUtil.TestChildPath, "EchoOutAndError");
            si.Add(new ProcessPipelineItem(TestUtil.TestChildPath, "EchoBack"));

            using (var sut = ProcessPipeline.Start(si))
                using (var sr = new StreamReader(sut.StandardOutput))
                {
                    var output = await sr.ReadToEndAsync();

                    sut.WaitForExit();

                    Assert.Equal("TestChild.OutTestChild.Error", output);
                }
        }
Beispiel #12
0
        private static async Task PipelineAsync()
        {
            var si = new ProcessPipelineStartInfo()
            {
                StdOutputRedirection = OutputRedirection.File,
                StdOutputFile        = "env.txt",
            };

            si.Add("cmd", "/C", "set");
            si.Add("findstr", "PROCESSOR");

            using (var p = ProcessPipeline.Start(si))
            {
                await p.WaitForExitAsync();
            }

            // NUMBER_OF_PROCESSORS=16
            // PROCESSOR_ARCHITECTURE = AMD64
            // ...
            Console.WriteLine(File.ReadAllText("env.txt"));
        }
Beispiel #13
0
        /// <summary>
        /// Starts a process pipeline as specified in <paramref name="startInfo"/>.
        /// </summary>
        /// <remarks>
        /// Please note that <see cref="Start(ProcessPipelineStartInfo)"/> does not throw <see cref="ProcessCreationFailedException"/>
        /// when one of the child processes cannot be created. Instead <see cref="GetExitCodes"/> returns an exit code of null for such a process.
        /// </remarks>
        /// <param name="startInfo"><see cref="ProcessPipelineStartInfo"/>.</param>
        /// <returns>The started process pipeline.</returns>
        /// <exception cref="IOException">Failed to open a specified file.</exception>
        public static ProcessPipeline Start(ProcessPipelineStartInfo startInfo)
        {
            startInfo = startInfo ?? throw new ArgumentNullException(nameof(startInfo));

            if (startInfo.ProcessPipelineItems.Count == 0)
            {
                throw new ArgumentException("At least one item must be added to ProcessPipelineStartInfo.", nameof(startInfo));
            }

            using (var stdHandles = new PipelineStdHandleCreator(
                       startInfo.StdInputRedirection,
                       startInfo.StdOutputRedirection,
                       startInfo.StdErrorRedirection,
                       startInfo.StdInputFile,
                       startInfo.StdOutputFile,
                       startInfo.StdErrorFile,
                       startInfo.StdInputHandle,
                       startInfo.StdOutputHandle,
                       startInfo.StdErrorHandle))
            {
                var count           = startInfo.ProcessPipelineItems.Count;
                var entries         = new ProcessEntry[count];
                var interChildPipes = new (SafeFileHandle readPipe, SafeFileHandle writePipe)[count - 1];