Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TcpTestListener.cs « Services « NUnit « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf2f57ffd577d037e1a9a44362053b92d556b296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// TcpTestListener.cs
//
// Author:
//       Alan McGovern <alan.mcgovern@gmail.com>
//
// Copyright (c) 2013 Alan McGovern
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Net;
using System.Net.Sockets;
using MonoDevelop.NUnit.External;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using MonoDevelop.Core;

namespace MonoDevelop.NUnit.External
{
	class TcpTestListener : IDisposable
	{
		string testSuiteName;
		string rootTestName;

		public bool HasReceivedConnection {
			get; private set;
		}

		List<Tuple<string,UnitTestResult>> suiteStack = new List<Tuple<string, UnitTestResult>> ();
		IRemoteEventListener listener;

		TcpListener TcpListener {
			get; set;
		}

		public int Port {
			get { return ((IPEndPoint) TcpListener.LocalEndpoint).Port; }
		}


		public TcpTestListener (IRemoteEventListener listener, string suiteName)
		{
			this.testSuiteName = suiteName;
			this.listener = listener;
			bool rootSuiteStarted = false;

			TcpListener = new TcpListener (new IPEndPoint (IPAddress.Loopback, 0));
			TcpListener.Start ();
			Task.Factory.StartNew (() => {
				try {
					using (var client = TcpListener.AcceptTcpClient ())
					using (var socketStream = client.GetStream ())
					using (var reader = new StreamReader (socketStream, Encoding.UTF8)) {

						HasReceivedConnection = true;
						string line = null;
						while ((line = reader.ReadLine ()) != null) {
							var element = XElement.Parse (line);
							string testName = element.Attribute ("name").Value;
							var action = element.Name.LocalName;

							if (testSuiteName.Length == 0 && !rootSuiteStarted) {
								// Running the whole assembly
								rootTestName = testName;
								rootSuiteStarted = true;
								continue;
							}
							if (testSuiteName == testName && !rootSuiteStarted) {
								// Running a test suite
								rootTestName = testName;
								rootSuiteStarted = true;
								listener.SuiteStarted ("<root>");
								continue;
							}

							if (!rootSuiteStarted)
								continue;

							switch (action) {
							case "suite-started":
								UpdateTestSuiteStatus (testName, false); break;
							case "test-started":
								UpdateTestSuiteStatus (testName, true);
								listener.TestStarted (testName); break;
							case "test-finished":
								var res = CreateResult (element);
								AddTestResult (res);
								listener.TestFinished (testName, res); break;
							case "suite-finished":
								if (testName == rootTestName) {
									FinishSuites (0);
									listener.SuiteFinished ("<root>", CreateResult (element));
									rootSuiteStarted = false;
								}
								break;
							}
						}
					}
				} catch (Exception ex) {
					LoggingService.LogError ("Exception in test listener", ex);
				} finally {
					TcpListener.Stop ();
				}
			});
		}

		public void Dispose ()
		{
			TcpListener.Stop ();
		}

		void UpdateTestSuiteStatus (string name, bool isTest)
		{
			if (testSuiteName.Length > 0)
				name = name.Substring (testSuiteName.Length + 1);
			string[] parts = name.Split ('.');
			int len = isTest ? parts.Length - 1 : parts.Length;
			for (int n = 0; n < len; n++) {
				if (n >= suiteStack.Count) {
					StartSuite (parts[n]);
				} else if (parts [n] != suiteStack [n].Item1) {
					FinishSuites (n);
					StartSuite (parts[n]);
				}
			}
		}

		void FinishSuites (int stackLevel)
		{
			if (stackLevel + 1 < suiteStack.Count)
				FinishSuites (stackLevel + 1);

			if (stackLevel >= suiteStack.Count)
				return;

			var tname = GetTestSuiteName (stackLevel);
			var res = suiteStack [stackLevel].Item2;

			suiteStack.RemoveAt (stackLevel);

			listener.SuiteFinished (tname, res);
		}

		void StartSuite (string name)
		{
			suiteStack.Add (new Tuple<string, UnitTestResult> (name, new UnitTestResult ()));
			name = GetTestSuiteName (suiteStack.Count - 1);
			listener.SuiteStarted (name);
		}

		void AddTestResult (UnitTestResult res)
		{
			foreach (var r in suiteStack)
				r.Item2.Add (res);
		}

		string GetTestSuiteName (int stackLevel)
		{
			var info = suiteStack [stackLevel];

			string name;
			if (stackLevel > 0) {
				var prefix = string.Join (".", suiteStack.Select (s => s.Item1).Take (stackLevel));
				name = prefix + "." + info.Item1;
			} else
				name = info.Item1;

			if (testSuiteName.Length > 0)
				name = testSuiteName + "." + name;
			return name;
		}

		UnitTestResult CreateResult (XElement element)
		{
			var result = (ResultStatus)Enum.Parse (typeof(ResultStatus), element.Attribute ("result").Value);
			var passed = int.Parse (element.Attribute ("passed").Value);
			var failures = int.Parse (element.Attribute ("failures").Value);
			var ignored = int.Parse (element.Attribute ("ignored").Value);
			var inconclusive = int.Parse (element.Attribute ("inconclusive").Value);

			var message = (string)element.Attribute ("message");
			var stackTrace = (string)element.Attribute ("stack-trace");

			return new UnitTestResult {
				Status = result,
				Passed = passed,
				Failures = failures,
				Ignored = ignored,
				Inconclusive = inconclusive,
				Message = message,
				StackTrace = stackTrace
			};
		}
	}
}