public void TestStdOutput() { using (ProcessRunner runner = new ProcessRunner("cmd.exe")) { StringWriter wtr = new StringWriter(); StringWriter err = new StringWriter(); ProcessOutputEventHandler handler = delegate(object o, ProcessOutputEventArgs e) { if (e.Error) err.WriteLine(e.Data); else wtr.WriteLine(e.Data); }; runner.OutputReceived += handler; Assert.AreEqual(0, runner.Run("/C", "dir", "/b", "/on", "/ad-h-s", "c:\\")); Assert.AreEqual(String.Empty, err.ToString()); Assert.AreNotEqual(0, runner.PID); StringReader rdr = new StringReader(wtr.ToString()); List<DirectoryInfo> rootdirs = new List<DirectoryInfo>(new DirectoryInfo("C:\\").GetDirectories()); rootdirs.Sort(delegate(DirectoryInfo x, DirectoryInfo y) { return StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name); }); foreach (DirectoryInfo di in rootdirs) { if ((di.Attributes & (FileAttributes.Hidden | FileAttributes.System)) != 0) continue; Assert.AreEqual(di.Name, rdr.ReadLine()); } Assert.AreEqual(null, rdr.ReadLine()); } }
/// <summary> Runs the process and returns the exit code. </summary> public int Run(TextReader input, params string[] arguments) { if (input == null && _engine.UsesStandardInputScript) { input = new StringReader(_scriptFile.ReadAllText()); } return(_runner.Run(input, arguments)); }
private void shell(string exePath, List <string> args) { CSharpTest.Net.Processes.ProcessRunner procRunner = new CSharpTest.Net.Processes.ProcessRunner(isTrueString(_autoEscape), exePath, args.ToArray()); _cmdLine = procRunner.ToString(); // What will be spawned procRunner.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived); _retCode = procRunner.Run().ToString(); }
public void TestStdError() { ProcessRunner runner = new ProcessRunner("cmd.exe"); StringWriter wtr = new StringWriter(); StringWriter err = new StringWriter(); runner.OutputReceived += delegate(object o, ProcessOutputEventArgs e) { if (e.Error) err.WriteLine(e.Data); else wtr.WriteLine(e.Data); }; Assert.AreNotEqual(0, runner.Run("/C", "dir", "c:\\I truely hope for this tests sake that this directory doesn't exist\\")); Assert.AreEqual(String.Empty, wtr.ToString()); Assert.AreEqual("The system cannot find the file specified.", err.ToString().Trim()); }
public void TestOutputEventUnsubscribe() { ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "echo"); bool outputReceived = false; ProcessOutputEventHandler handler = delegate(object o, ProcessOutputEventArgs e) { outputReceived = true; }; runner.OutputReceived += handler; runner.OutputReceived -= handler; Assert.AreEqual(0, runner.Run()); Assert.IsFalse(outputReceived); }
public void TestExitedEventUnsubscribe() { ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "echo"); int exitCode = -1; bool receivedExit = false; ProcessExitedEventHandler handler = delegate(object o, ProcessExitedEventArgs e) { receivedExit = true; exitCode = e.ExitCode; }; runner.ProcessExited += handler; runner.ProcessExited -= handler; Assert.AreEqual(0, runner.Run()); Assert.IsFalse(receivedExit); Assert.AreEqual(-1, exitCode); }
public void TestRunWithWorkingDirectory() { using (TempDirectory dir = new TempDirectory()) using (ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "echo %CD%")) { List<string> lines = new List<string>(); runner.OutputReceived += delegate(Object o, ProcessOutputEventArgs e) { lines.Add(e.Data); }; Assert.AreNotEqual(dir.TempPath, runner.WorkingDirectory); runner.WorkingDirectory = dir.TempPath; Assert.AreEqual(dir.TempPath, runner.WorkingDirectory); int exitCode = runner.Run(); Assert.AreEqual(0, exitCode); Assert.AreEqual(dir.TempPath.TrimEnd('\\', '/'), lines[0].TrimEnd('\\', '/')); } }
public void TestRunWithInput() { using (ProcessRunner runner = new ProcessRunner("cmd.exe", "/C", "sort")) { List<string> lines = new List<string>(); runner.OutputReceived += delegate(Object o, ProcessOutputEventArgs e) { lines.Add(e.Data); }; int exitCode = runner.Run(new StringReader("Hello World\r\nWhatever!\r\nA line that goes first.")); Assert.AreEqual(0, exitCode); Assert.AreEqual("A line that goes first.", lines[0]); Assert.AreEqual("Hello World", lines[1]); Assert.AreEqual("Whatever!", lines[2]); } }