56490556c8140fb3d81162774362f069e69a13be
[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 using NUnit.Framework;\r
7 \r
8 namespace JetBrains.TeamCity.NuGet.Tests\r
9 {\r
10   public static class ProcessExecutor\r
11   {\r
12     public static Result ExecuteProcess(string exe, params string[] args)\r
13     {\r
14       var pi = new ProcessStartInfo\r
15                  {\r
16                    FileName = exe,\r
17                    Arguments = string.Join(" ", args),\r
18                    RedirectStandardError = true,\r
19                    RedirectStandardOutput = true,\r
20                    RedirectStandardInput = true,\r
21                    UseShellExecute = false\r
22                  };\r
23 \r
24       var process = Process.Start(pi);\r
25       process.StandardInput.Close();\r
26       Func<StreamReader, string> readOutput = stream =>\r
27                                                 {\r
28                                                   var result = "";\r
29                                                   var th = new Thread(delegate()\r
30                                                                         {\r
31                                                                           var sb = new StringBuilder();\r
32                                                                           int i;\r
33                                                                           while ((i = stream.Read()) >= 0)\r
34                                                                             sb.Append((char) i);\r
35                                                                           result = sb.ToString();\r
36                                                                         })\r
37                                                              {Name = "Process output reader " + process.Id};\r
38                                                   th.Start();\r
39                                                   th.Join(TimeSpan.FromMinutes(10));\r
40                                                   return result;\r
41                                                 };\r
42 \r
43       string output = readOutput(process.StandardOutput);\r
44       string error = readOutput(process.StandardError);\r
45       process.WaitForExit();\r
46 \r
47       return new Result(output, error, process.ExitCode);\r
48     }\r
49 \r
50     public class Result\r
51     {\r
52       public string Output { get; private set; }\r
53       public string Error { get; private set; }\r
54       public int ExitCode { get; private set; }\r
55 \r
56       public Result(string output, string error, int exitCode)\r
57       {\r
58         Output = output;\r
59         Error = error;\r
60         ExitCode = exitCode;\r
61       }\r
62 \r
63       public Result AssertOutputContains(params string[] text)\r
64       {\r
65         foreach (var _ in text)\r
66         {\r
67           var s = _.Trim();\r
68           Assert.IsTrue(Output.Contains(s), "Process Output must contain {0}. Output: {1}", s, Output);\r
69         }\r
70         return this;\r
71       }\r
72 \r
73       public Result AssertNoErrorOutput()\r
74       {\r
75         Assert.IsTrue(string.IsNullOrWhiteSpace(Error));\r
76         return this;\r
77       }\r
78 \r
79       public Result AssertExitedSuccessfully()\r
80       {\r
81         Assert.That(ExitCode, Is.EqualTo(0));\r
82         return this;\r
83       }\r
84 \r
85       public Result Dump()\r
86       {\r
87         Console.Out.WriteLine(this);\r
88         return this;\r
89       }\r
90 \r
91       public override string ToString()\r
92       {\r
93         var sb = new StringBuilder();\r
94         sb.AppendFormat("ExitCode: {0}\r\n", ExitCode);\r
95         if (!string.IsNullOrWhiteSpace(Output))\r
96         {\r
97           sb\r
98             .Append("Output:\n")\r
99             .Append(Output)\r
100             .Append("\n");\r
101         }\r
102         if (!string.IsNullOrWhiteSpace(Error))\r
103         {\r
104           sb\r
105             .Append("Error:\n")\r
106             .Append(Error);\r
107         }\r
108         return sb.ToString();\r
109       }\r
110     }\r
111   }\r
112 }