public static void Test_SubProcess_multiple_Waits() { SubProcess sp = new SubProcess(echoArgs, "A"); sp.Wait(); Assert.AreEqual(0, sp.Wait()); }
public static void Test_SubProcess_utf8() { // output string ourEncWebName = Console.OutputEncoding.WebName; string waterTrebbleClef = "水𝄞"; SubProcess sp = new SubProcess(echoArgs, "--utf8", waterTrebbleClef); sp.Wait(); Assert.AreEqual(_(waterTrebbleClef + "\n"), sp.OutputString); Encoding ourEnc = Console.OutputEncoding; Assert.AreEqual(ourEnc.WebName, ourEncWebName); // has not changed to UTF-8 Assert.AreNotEqual("utf-8", ourEnc.WebName); // error sp = new SubProcess(echoArgs, "--utf8", "--error", waterTrebbleClef); sp.Wait(); Assert.AreEqual(_(waterTrebbleClef + "\n"), sp.ErrorString); // input //sp = new SubProcess(echoArgs, "--utf8", "--input-code-units", "--wait", "9999") sp = new SubProcess(echoArgs, "--utf8", "--input"); sp.Start(); sp.WriteLine(waterTrebbleClef); sp.Wait(); Assert.AreEqual(_(waterTrebbleClef + "\n"), sp.OutputString); }
public static void Test_SubProcess_input_file() { string line = "a line of input"; string filename = Path.Combine(Path.GetTempPath(), "Test_SubProcess-" + Path.GetRandomFileName() + ".tmp"); Stream fs = new FileStream(filename, FileMode.CreateNew); StreamWriter sw = new StreamWriter(fs); for (int i = 0; i < 20; ++i) { sw.WriteLine(line); } sw.Close(); fs.Close(); fs = new FileStream(filename, FileMode.Open); byte[] buffer = new byte[17]; fs.Read(buffer, 0, 17); SubProcess sp = new SubProcess(echoArgs, "--input") { In = fs }; sp.Wait(); Assert.AreEqual(_(line.ToUpper() + "\n"), sp.OutputString); fs.Close(); File.Delete(filename); }
public static void Test_SubProcess_no_tasks() { // internally no Tasks for redirecting stdout and stderr SubProcess sp = new SubProcess(echoArgs, "hello") { Out = SubProcess.Through, Error = SubProcess.Through }; sp.Wait(); // timeout version sp = new SubProcess(echoArgs, "hello") { Out = SubProcess.Through, Error = SubProcess.Through, Timeout = 99999 }; sp.Wait(); // wait for a while in the task sp = new SubProcess(echoArgs, "hello", "--wait", "0.1") { Out = SubProcess.Through, Error = SubProcess.Through }; sp.Wait(); }
public static void Test_SubProcess_no_timeout() { SubProcess sp = new SubProcess(echoArgs, "a") { Timeout = 99999 }; sp.Wait(); }
public static void Test_SubProcess_timeout() { using (SubProcess sp = new SubProcess(echoArgs, "--wait", "100") { Timeout = 0.1 }) { Assert.Throws <SubProcess.TimeoutException>(() => sp.Wait()); } }
public static void Test_SubProcess_timeout_close() { // although echo-args closes stdout and stderr the read stdout and stderr // tasks do not complete before the timeout, as with non --close case using (SubProcess sp = new SubProcess(echoArgs, "--close", "--wait", "100") { Timeout = 0.1 }) { Assert.Throws <SubProcess.TimeoutException>(() => sp.Wait()); } }
public static void Test_SubProcess_IDisposable_kills() { SubProcess sp; using (sp = new SubProcess(echoArgs, "--wait")) { sp.Start(); Assert.IsTrue(sp.IsAlive); } sp.Wait(); Assert.IsFalse(sp.IsAlive); }
public static void Test_SubProcess_input_pipe_writeline() { string line = "some input"; SubProcess sp = new SubProcess(echoArgs, "--input") { In = SubProcess.Pipe }; sp.Start(); sp.WriteLine(line); sp.Wait(); Assert.AreEqual(_(line.ToUpper() + "\n"), sp.OutputString); }
public static void Test_SubProcess_directory() { string cwd = Directory.GetCurrentDirectory(); Directory.SetCurrentDirectory(".."); string parentDir = Directory.GetCurrentDirectory(); Directory.SetCurrentDirectory(cwd); Assert.AreEqual(cwd, Directory.GetCurrentDirectory()); SubProcess sp = new SubProcess(echoArgs, "--pwd") { Directory = parentDir }; sp.Wait(); Assert.AreEqual(sp.OutputString, _(parentDir + "\n")); }
public static void Test_SubProcess_input_pipe_writeBinary() { string line = "some input"; MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.WriteLine(line); sw.Flush(); byte[] bytes = ms.GetBuffer(); SubProcess sp = new SubProcess(echoArgs, "--input") { In = SubProcess.Pipe }; sp.Start(); sp.Write(bytes, 0, (int)ms.Length); sp.Wait(); Assert.AreEqual(_(line.ToUpper() + "\n"), sp.OutputString); }
public static void Test_UseShell() { var s = new SubProcess("ls") { In = SubProcess.Through, Out = SubProcess.Through, Error = SubProcess.Through, UseShell = true }; s.Wait(); Assert.Throws <LogicError>(() => new SubProcess("ls") { // In, Out and Errors redirected UseShell = true }.Wait()); Assert.Throws <LogicError>(() => new SubProcess("ls") { // In redirected Out = SubProcess.Through, Error = SubProcess.Through, UseShell = true }.Wait()); Assert.Throws <LogicError>(() => new SubProcess("ls") { // Out redirected In = SubProcess.Through, Error = SubProcess.Through, UseShell = true }.Wait()); Assert.Throws <LogicError>(() => new SubProcess("ls") { // Error redirected In = SubProcess.Through, Out = SubProcess.Through, UseShell = true }.Wait()); }
public static void Test_SubProcess_nonUtf8() { Process p = new Process { StartInfo = { FileName = echoArgs, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardInput = true, CreateNoWindow = true } }; p.Start(); Encoding enc = p.StandardOutput.CurrentEncoding; // Windows-1252 // but SubProcess is actually IBM-850 (capitalises ä to å (in windows-1252) which // is õ to Õ in IBM-850) enc = Encoding.GetEncoding(850); string line = "a line ä"; string line2 = "a line Ë"; SubProcess sp = new SubProcess(echoArgs, "--input", "--error", line2) { OutEncoding = enc, InEncoding = enc, ErrorEncoding = enc }; sp.Start(); sp.WriteLine(line); sp.Wait(); Assert.AreEqual(_(line.ToUpper() + "\n"), sp.OutputString); Assert.AreEqual(_(line2 + "\n"), sp.ErrorString); }
public static void Test_SubProcess_input_memoryStream() { string line = "a line of input"; MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms) { AutoFlush = true }; sw.WriteLine(line); byte[] bytes = ms.GetBuffer(); long len = ms.Length; ms = new MemoryStream(bytes, 0, (int)len); SubProcess sp = new SubProcess(echoArgs, "--input") { In = ms }; sp.Wait(); Assert.AreEqual(_(line.ToUpper() + "\n"), sp.OutputString); }
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); }