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