Revert some changes which don't have proper dependencies.
[mono-project.git] / mono / tests / bug-8417.cs
blob6524707c01744a6d85f2ff57b31970d8b8a5101c
1 #define USE_REDIRECT
2 using System;
3 using System.IO;
4 using System.Text;
5 using System.Collections.Generic;
6 using System.Diagnostics;
8 namespace Example
10 public static class EntryPoint
12 public static bool RunProcess (string filename, string arguments, out int exitCode, out string stdout, bool capture_stderr = false)
14 var sb = new StringBuilder ();
15 var stdout_done = new System.Threading.ManualResetEvent (false);
16 var stderr_done = new System.Threading.ManualResetEvent (false);
17 using (var p = new Process ()) {
18 p.StartInfo.FileName = filename;
19 p.StartInfo.Arguments = arguments;
20 p.StartInfo.UseShellExecute = false;
21 p.StartInfo.RedirectStandardOutput = true;
22 p.StartInfo.RedirectStandardError = capture_stderr;
24 p.OutputDataReceived += (sender, e) => {
25 if (e.Data == null) {
26 stdout_done.Set ();
28 else {
29 lock (sb)
30 sb.AppendLine (e.Data);
33 if (capture_stderr) {
34 p.ErrorDataReceived += (sender, e) => {
35 if (e.Data == null) {
36 stderr_done.Set ();
38 else {
39 lock (sb)
40 sb.AppendLine (e.Data);
44 p.Start ();
45 p.BeginOutputReadLine ();
46 if (capture_stderr)
47 p.BeginErrorReadLine ();
48 p.WaitForExit ();
49 stdout_done.WaitOne (TimeSpan.FromSeconds (1));
50 if (capture_stderr)
51 stderr_done.WaitOne (TimeSpan.FromSeconds (1));
52 stdout = sb.ToString ();
53 exitCode = p.ExitCode;
54 return exitCode == 0;
58 static string RunRedirectOutput (Action action)
60 var existingOut = Console.Out;
61 var existingErr = Console.Error;
63 try {
64 using (StringWriter writer = new StringWriter ()) {
66 #if USE_REDIRECT
67 Console.SetOut (writer);
68 Console.SetError (writer);
69 #endif
70 action ();
71 return writer.ToString ();
74 finally {
75 Console.SetOut (existingOut);
76 Console.SetError (existingErr);
80 static void RunProcess ()
82 RunProcess ("/bin/echo", "who am i", out int exitCode, out string stdout);
83 Console.Write (stdout);
86 public static int Main ()
88 var str = RunRedirectOutput (RunProcess);
89 Console.WriteLine ("'{0}'", str);
90 return str == "who am i\n" ? 0 : 1;