Ejemplo n.º 1
0
        public async Task CorrectlyConnectOutputPipes()
        {
            {
                var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoOutAndError")
                {
                    StdOutputRedirection = OutputRedirection.OutputPipe,
                    StdErrorRedirection  = OutputRedirection.ErrorPipe,
                };

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

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

                using (var sut = ChildProcess.Start(si))
                {
                    await ChildProcessAssert.CorrectlyConnectsPipesAsync(sut, "TestChild.Error", "TestChild.Out");
                }
            }
        }
Ejemplo n.º 2
0
        public async Task PipesAreAsynchronous()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoBack")
            {
                StdInputRedirection  = InputRedirection.InputPipe,
                StdOutputRedirection = OutputRedirection.OutputPipe,
                StdErrorRedirection  = OutputRedirection.ErrorPipe,
            };

            using (var sut = ChildProcess.Start(si))
            {
                Assert.True(((FileStream)sut.StandardInput).IsAsync);
                Assert.True(((FileStream)sut.StandardOutput).IsAsync);
                Assert.True(((FileStream)sut.StandardError).IsAsync);

                using (var sr = new StreamReader(sut.StandardOutput))
                {
                    const string text       = "foobar";
                    var          stdoutTask = sr.ReadToEndAsync();
                    using (var sw = new StreamWriter(sut.StandardInput))
                    {
                        await sw.WriteAsync(text);
                    }
                    Assert.Equal(text, await stdoutTask);
                }

                sut.WaitForExit();
                Assert.Equal(0, sut.ExitCode);
            }
        }
Ejemplo n.º 3
0
        public void RedirectionToHandle()
        {
            using (var tmp = new TemporaryDirectory())
            {
                var inFile  = Path.Combine(tmp.Location, "in");
                var outFile = Path.Combine(tmp.Location, "out");
                var errFile = Path.Combine(tmp.Location, "err");

                // StdOutputHandle StdErrorHandle
                {
                    using (var fsOut = File.Create(outFile))
                        using (var fsErr = File.Create(errFile))
                        {
                            // File
                            var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoOutAndError")
                            {
                                StdOutputRedirection = OutputRedirection.Handle,
                                StdOutputHandle      = fsOut.SafeFileHandle,
                                StdErrorRedirection  = OutputRedirection.Handle,
                                StdErrorHandle       = fsErr.SafeFileHandle,
                            };

                            using (var sut = ChildProcess.Start(si))
                            {
                                sut.WaitForExit();
                                Assert.True(sut.IsSuccessful);
                            }
                        }

                    Assert.Equal("TestChild.Out", File.ReadAllText(outFile));
                    Assert.Equal("TestChild.Error", File.ReadAllText(errFile));
                }

                // StdInputHandle
                {
                    const string text = "foobar";
                    File.WriteAllText(inFile, text);

                    using (var fsIn = File.OpenRead(inFile))
                    {
                        var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoBack")
                        {
                            StdInputRedirection  = InputRedirection.Handle,
                            StdInputHandle       = fsIn.SafeFileHandle,
                            StdOutputRedirection = OutputRedirection.File,
                            StdOutputFile        = outFile,
                        };

                        using (var sut = ChildProcess.Start(si))
                        {
                            sut.WaitForExit();
                            Assert.True(sut.IsSuccessful);
                        }
                    }

                    Assert.Equal(text, File.ReadAllText(outFile));
                }
            }
        }
Ejemplo n.º 4
0
        private static ChildProcess CreateForWaitForExitTest()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoBack")
            {
                StdInputRedirection  = InputRedirection.InputPipe,
                StdOutputRedirection = OutputRedirection.NullDevice,
            };

            return(ChildProcess.Start(si));
        }
Ejemplo n.º 5
0
        public void CanCreateChildProcess()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath)
            {
                StdOutputRedirection = OutputRedirection.OutputPipe,
            };

            using (var sut = ChildProcess.Start(si))
            {
                ChildProcessAssert.CanCreateChildProcess(sut);
            }
        }
Ejemplo n.º 6
0
        public void ChildProcessWaitForAsyncIsTrulyAsynchronous()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommandName, TestUtil.TestChildPath, "Sleep", "1000")
            {
                StdOutputRedirection = OutputRedirection.NullDevice,
                StdErrorRedirection  = OutputRedirection.NullDevice,
            };

            using var sut = ChildProcess.Start(si);
            WaitForAsyncIsTrulyAsynchronous(sut);
            sut.WaitForExit();
            Assert.True(sut.IsSuccessful);
        }
Ejemplo n.º 7
0
        public void ChildProcessWaitForAsyncIsTrulyAsynchronous()
        {
            var si = new ChildProcessStartInfo(TestUtil.TestChildPath, "Sleep", "1000")
            {
                StdOutputRedirection = OutputRedirection.NullDevice,
                StdErrorRedirection  = OutputRedirection.NullDevice,
            };

            using (var sut = ChildProcess.Start(si))
            {
                WaitForAsyncIsTrulyAsynchronous(sut);
            }
        }
Ejemplo n.º 8
0
        public async Task PipesAreAsynchronous()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoBack")
            {
                StdInputRedirection  = InputRedirection.InputPipe,
                StdOutputRedirection = OutputRedirection.OutputPipe,
                StdErrorRedirection  = OutputRedirection.ErrorPipe,
            };

            using (var sut = ChildProcess.Start(si))
            {
                await ChildProcessAssert.PipesAreAsynchronousAsync(sut);
            }
        }
Ejemplo n.º 9
0
        public void CanCreateChildProcess()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommandName, TestUtil.TestChildPath)
            {
                StdOutputRedirection = OutputRedirection.OutputPipe,
            };

            using var sut = ChildProcess.Start(si);
            sut.WaitForExit();
            Assert.Equal(0, sut.ExitCode);

            // This closes StandardOutput, which should be acceptable.
            using var sr = new StreamReader(sut.StandardOutput !);
            Assert.Equal("TestChild", sr.ReadToEnd());
        }
Ejemplo n.º 10
0
        public void CanRedirectToSameFile()
        {
            using (var tmp = new TemporaryDirectory())
            {
                var outFile = Path.Combine(tmp.Location, "out");

                // StdOutputFile StdErrorFile
                {
                    // File
                    var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoOutAndError")
                    {
                        StdOutputRedirection = OutputRedirection.File,
                        StdOutputFile        = outFile,
                        StdErrorRedirection  = OutputRedirection.File,
                        StdErrorFile         = outFile,
                    };

                    using (var sut = ChildProcess.Start(si))
                    {
                        sut.WaitForExit();
                        Assert.True(sut.IsSuccessful);
                    }

                    Assert.Equal("TestChild.OutTestChild.Error", File.ReadAllText(outFile));

                    // AppendToFile
                    si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoOutAndError")
                    {
                        StdOutputRedirection = OutputRedirection.AppendToFile,
                        StdOutputFile        = outFile,
                        StdErrorRedirection  = OutputRedirection.AppendToFile,
                        StdErrorFile         = outFile,
                    };

                    using (var sut = ChildProcess.Start(si))
                    {
                        sut.WaitForExit();
                        Assert.True(sut.IsSuccessful);
                    }

                    Assert.Equal("TestChild.OutTestChild.ErrorTestChild.OutTestChild.Error", File.ReadAllText(outFile));
                }
            }
        }
Ejemplo n.º 11
0
        public void ExitCodeThrowsBeforeChildExits()
        {
            var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "EchoBack")
            {
                StdInputRedirection = InputRedirection.InputPipe,
            };

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

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

                Assert.True(sut.IsSuccessful);
                Assert.Equal(0, sut.ExitCode);
            }
        }
Ejemplo n.º 12
0
        public void CanObtainExitCode()
        {
            {
                var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "ExitCode", "0");

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

            {
                var si = new ChildProcessStartInfo(TestUtil.DotnetCommand, TestUtil.TestChildPath, "ExitCode", "-1");

                using (var sut = ChildProcess.Start(si))
                {
                    sut.WaitForExit();
                    Assert.False(sut.IsSuccessful);
                    Assert.Equal(-1, sut.ExitCode);
                }
            }
        }
Ejemplo n.º 13
0
        public void ReportsCreationFailure()
        {
            var si = new ChildProcessStartInfo("nonexistentfile");

            Assert.Throws <ProcessCreationFailedException>(() => ChildProcess.Start(si));
        }
Ejemplo n.º 14
0
        public void RedirectionToFile()
        {
            using (var tmp = new TemporaryDirectory())
            {
                var inFile  = Path.Combine(tmp.Location, "in");
                var outFile = Path.Combine(tmp.Location, "out");
                var errFile = Path.Combine(tmp.Location, "err");

                // StdOutputFile StdErrorFile
                {
                    // File
                    var si = new ChildProcessStartInfo(TestUtil.TestChildPath, "EchoOutAndError")
                    {
                        StdOutputRedirection = OutputRedirection.File,
                        StdOutputFile        = outFile,
                        StdErrorRedirection  = OutputRedirection.File,
                        StdErrorFile         = errFile,
                    };

                    using (var sut = ChildProcess.Start(si))
                    {
                        sut.WaitForExit();
                        Assert.True(sut.IsSuccessful);
                    }

                    Assert.Equal("TestChild.Out", File.ReadAllText(outFile));
                    Assert.Equal("TestChild.Error", File.ReadAllText(errFile));

                    // AppendToFile
                    si = new ChildProcessStartInfo(TestUtil.TestChildPath, "EchoOutAndError")
                    {
                        StdOutputRedirection = OutputRedirection.AppendToFile,
                        StdOutputFile        = errFile,
                        StdErrorRedirection  = OutputRedirection.AppendToFile,
                        StdErrorFile         = outFile,
                    };

                    using (var sut = ChildProcess.Start(si))
                    {
                        sut.WaitForExit();
                        Assert.True(sut.IsSuccessful);
                    }

                    Assert.Equal("TestChild.OutTestChild.Error", File.ReadAllText(outFile));
                    Assert.Equal("TestChild.ErrorTestChild.Out", File.ReadAllText(errFile));
                }

                // StdInputFile
                {
                    const string text = "foobar";
                    File.WriteAllText(inFile, text);

                    var si = new ChildProcessStartInfo(TestUtil.TestChildPath, "EchoBack")
                    {
                        StdInputRedirection  = InputRedirection.File,
                        StdInputFile         = inFile,
                        StdOutputRedirection = OutputRedirection.File,
                        StdOutputFile        = outFile,
                    };

                    using (var sut = ChildProcess.Start(si))
                    {
                        sut.WaitForExit();
                        Assert.True(sut.IsSuccessful);
                    }

                    Assert.Equal(text, File.ReadAllText(outFile));
                }
            }
        }