static int Main(string[] args) { try { SubProcess.Call("ls", "-l"); SubProcess.CheckCall("psql", "my-database", "fred"); // throws if exits with non 0 exit code SubProcess p = new SubProcess("ssh", "*****@*****.**") { Out = new FileStream("ssh-output.txt", FileMode.OpenOrCreate), Error = SubProcess.Capture, In = SubProcess.Pipe }; p.Wait(); Console.WriteLine(p.ErrorString); return(0); } catch (Exception e) { Console.Error.WriteLine("Fatal Error: " + e.Message); return(1); } }
public static void Test_SubProcess_calls() { // params versions SubProcess.Call(echoArgs, "1", "2", "3"); SubProcess.CheckCall(echoArgs, "4", "5"); SubProcess.CheckOutput(echoArgs, "A", "B", "C").With(_("A\nB\nC\n")); // IEnumerable<string> versions List <string> args = new List <string> { echoArgs, "10", "20", "30" }; SubProcess.Call(args); args = new List <string> { echoArgs, "40", "50" }; SubProcess.CheckCall(args); args = new List <string> { echoArgs, "ABBA", "BBQ", "CQ" }; SubProcess.CheckOutput(args).With(_("ABBA\nBBQ\nCQ\n")); SubProcess s = new SubProcess(echoArgs, "an arg", "another arg", "--error", "some error"); s.Wait(); Assert.AreEqual(_("an arg\nanother arg\n"), s.OutputString); Assert.AreEqual(_("some error\n"), s.ErrorString); args = new List <string> { echoArgs, "an arg", "another arg", "--error", "some error" }; s = new SubProcess(args); s.Check(); Assert.AreEqual(_("an arg\nanother arg\n"), s.OutputString); Assert.AreEqual(_("some error\n"), s.ErrorString); s = new SubProcess(echoArgs, "hello"); int rc = s.Wait(); Assert.AreEqual(0, rc); s = new SubProcess(echoArgs, "hello", "--exit", "3"); rc = s.Wait(); Assert.AreEqual(3, rc); // captured stderr on failure args = new List <string> { echoArgs, "hello", "--error", "FAILED", "--exit", "1" }; s = new SubProcess(args); SubProcess.Failed failed = Assert.Throws <SubProcess.Failed>(() => { s.Check(); }); Assert.AreEqual(_("FAILED\n"), failed.ErrorOutput); Assert.AreEqual(1, failed.ExitCode); Assert.IsTrue(failed.Message.Contains("FAILED")); // did not capture stderr on failyre args = new List <string> { echoArgs, "hello", "--error", "FAILED", "--exit", "1" }; s = new SubProcess(args) { Error = SubProcess.Through }; failed = Assert.Throws <SubProcess.Failed>(() => { s.Check(); }); Assert.AreEqual(1, failed.ExitCode); // Out tests s = new SubProcess(echoArgs, "helllo world") { Out = SubProcess.Swallow }; s.Wait(); s = new SubProcess(echoArgs, "helllo world") { Out = SubProcess.Through }; s.Wait(); string filename = Path.Combine(Path.GetTempPath(), "Test_SubProcess-" + Path.GetRandomFileName() + ".tmp"); FileStream fs = new FileStream(filename, FileMode.CreateNew); s = new SubProcess(echoArgs, "hello world", "--error", "something has", "--error", "failed") { Out = fs }; s.Wait(); fs.Close(); string fileContents = ReadFile(filename); Assert.AreEqual(_("hello world\n"), fileContents); File.Delete(filename); // Error tests s = new SubProcess(echoArgs, "hello world") { Error = SubProcess.Swallow }; s.Wait(); s = new SubProcess(echoArgs, "hello world") { Error = SubProcess.Through }; s.Wait(); s = new SubProcess(echoArgs, "hello world", "--error", "error message") { Error = SubProcess.ToOut }; s.Wait(); Assert.AreEqual(_("hello world\nerror message\n"), s.OutputString); s = new SubProcess(echoArgs, "hello world", "--error", "error message"); s.Wait(); Assert.AreEqual(_("hello world\n"), s.OutputString); Assert.AreEqual(_("error message\n"), s.ErrorString); filename = Path.Combine(Path.GetTempPath(), "Test_SubProcess-" + Path.GetRandomFileName() + ".tmp"); fs = new FileStream(filename, FileMode.CreateNew); s = new SubProcess(echoArgs, "hello world", "--error", "something has", "--error", "failed") { Error = fs }; s.Wait(); fs.Close(); fileContents = ReadFile(filename); Assert.AreEqual(_("something has\nfailed\n"), fileContents); File.Delete(filename); // redirect both to files filename = Path.Combine(Path.GetTempPath(), "Test_SubProcess-" + Path.GetRandomFileName() + ".tmp"); string filenameError = Path.Combine(Path.GetTempPath(), "Test_SubProcess-error-" + Path.GetRandomFileName() + ".tmp"); fs = new FileStream(filename, FileMode.CreateNew); FileStream fsError = new FileStream(filenameError, FileMode.CreateNew); s = new SubProcess(echoArgs, "hello world", "--error", "something has", "--error", "failed") { Out = fs, Error = fsError }; s.Wait(); fs.Close(); fsError.Close(); fileContents = ReadFile(filename); Assert.AreEqual(_("hello world\n"), fileContents); string fileContentsError = ReadFile(filenameError); Assert.AreEqual(_("something has\nfailed\n"), fileContentsError); File.Delete(filename); File.Delete(filenameError); }