add more tests on native part
[teamcity/dotNetPackagesSupport.git] / nuget-extensions / nuget-tests / src / ProcessExecutor.cs
1 using System;\r
2 using System.Diagnostics;\r
3 using System.IO;\r
4 using System.Text;\r
5 using System.Threading;\r
6 \r
7 namespace JetBrains.TeamCity.NuGet.Tests\r
8 {\r
9   public static class ProcessExecutor\r
10   {\r
11     public static Result ExecuteProcess(string exe, params string[] args)\r
12     {\r
13       var pi = new ProcessStartInfo\r
14                  {\r
15                    FileName = exe,\r
16                    Arguments = string.Join(" ", args),\r
17                    RedirectStandardError = true,\r
18                    RedirectStandardOutput = true,\r
19                    RedirectStandardInput = true,\r
20                    UseShellExecute = false\r
21                  };\r
22 \r
23       var process = Process.Start(pi);\r
24       process.StandardInput.Close();\r
25       Func<StreamReader, string> readOutput = stream =>\r
26                                                 {\r
27                                                   var result = "";\r
28                                                   var th = new Thread(delegate()\r
29                                                                         {\r
30                                                                           var sb = new StringBuilder();\r
31                                                                           int i;\r
32                                                                           while ((i = stream.Read()) >= 0)\r
33                                                                             sb.Append((char) i);\r
34                                                                           result = sb.ToString();\r
35                                                                         })\r
36                                                              {Name = "Process output reader " + process.Id};\r
37                                                   th.Start();\r
38                                                   th.Join(TimeSpan.FromMinutes(10));\r
39                                                   return result;\r
40                                                 };\r
41 \r
42       string output = readOutput(process.StandardOutput);\r
43       string error = readOutput(process.StandardError);\r
44       process.WaitForExit();\r
45 \r
46       return new Result(output, error, process.ExitCode);\r
47     }\r
48 \r
49     public class Result\r
50     {\r
51       public string Output { get; private set; }\r
52       public string Error { get; private set; }\r
53       public int ExitCode { get; private set; }\r
54 \r
55       public Result(string output, string error, int exitCode)\r
56       {\r
57         Output = output;\r
58         Error = error;\r
59         ExitCode = exitCode;\r
60       }\r
61 \r
62       public override string ToString()\r
63       {\r
64         return new StringBuilder()\r
65           .AppendFormat("ExitCode: {0}\r\n", ExitCode)\r
66           .Append("Output:\n")\r
67           .Append(Output)\r
68           .Append("\n")\r
69           .Append("Error:\n")\r
70           .Append(Error)\r
71           .ToString();\r
72       }\r
73     }\r
74   }\r
75 }