-
Notifications
You must be signed in to change notification settings - Fork 103
/
DiscoveryConverter.cs
467 lines (413 loc) · 19.9 KB
/
DiscoveryConverter.cs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// ***********************************************************************
// Copyright (c) 2020-2021 Charlie Poole, Terje Sandstrom
//
// 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.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using NUnit.VisualStudio.TestAdapter.Internal;
namespace NUnit.VisualStudio.TestAdapter.NUnitEngine
{
public interface IDiscoveryConverter
{
IEnumerable<NUnitDiscoveryTestCase> AllTestCases { get; }
bool IsExplicitRun { get; }
IList<TestCase> LoadedTestCases { get; }
int NoOfLoadedTestCases { get; }
/// <summary>
/// Checks if we're running the latest Current DiscoveryMethod.
/// </summary>
bool IsDiscoveryMethodCurrent { get; }
bool NoOfLoadedTestCasesAboveLimit { get; }
IEnumerable<TestCase> CheckTestCasesExplicit(IEnumerable<TestCase> filteredTestCases);
}
public class DiscoveryConverter : IDiscoveryConverter
{
private const string ParameterizedFixture = nameof(ParameterizedFixture);
private const string TestFixture = nameof(TestFixture);
private const string GenericFixture = nameof(GenericFixture);
private const string SetUpFixture = nameof(SetUpFixture);
private const string TestSuite = nameof(TestSuite);
internal static class NUnitXmlAttributeNames
{
public const string Id = "id";
public const string Type = "type";
public const string Name = "name";
public const string Fullname = "fullname";
public const string Runstate = "runstate";
public const string Testcasecount = "testcasecount";
public const string Classname = "classname";
public const string Methodname = "methodname";
public const string Seed = "seed";
}
private ITestConverterXml converterForXml;
private ITestConverter converter;
public ITestConverterCommon TestConverterForXml => converterForXml;
public ITestConverterCommon TestConverter => converter;
public NUnitDiscoveryTestRun TestRun { get; private set; }
/// <summary>
/// Checks if we're running the latest Current DiscoveryMethod.
/// </summary>
public bool IsDiscoveryMethodCurrent => Settings.DiscoveryMethod == DiscoveryMethod.Current;
public NUnitDiscoveryTestAssembly CurrentTestAssembly => TestRun.TestAssembly;
public NUnitDiscoveryTestSuite TopLevelTestSuite => CurrentTestAssembly.TestSuites.FirstOrDefault();
public IEnumerable<NUnitDiscoveryTestCase> AllTestCases => CurrentTestAssembly.AllTestCases;
public bool IsExplicitRun => CurrentTestAssembly?.IsExplicit ?? false;
private readonly List<TestCase> loadedTestCases = new ();
public IList<TestCase> LoadedTestCases => loadedTestCases;
public int NoOfLoadedTestCases => loadedTestCases.Count;
public string AssemblyPath { get; private set; }
private IAdapterSettings Settings { get; }
private ITestLogger TestLog { get; }
public bool NoOfLoadedTestCasesAboveLimit => NoOfLoadedTestCases > Settings.AssemblySelectLimit;
public IEnumerable<TestCase> CheckTestCasesExplicit(IEnumerable<TestCase> filteredTestCases)
{
var explicitCases = new List<TestCase>();
foreach (var tc in filteredTestCases)
{
var correspondingNUnitTestCase = AllTestCases.FirstOrDefault(o => o.FullName == tc.FullyQualifiedName);
if (correspondingNUnitTestCase == null)
{
TestLog.Warning(
$"CheckExplicit: Can't locate corresponding NUnit testcase {tc.FullyQualifiedName}");
continue;
}
if (correspondingNUnitTestCase.IsExplicitReverse)
explicitCases.Add(tc);
}
return explicitCases;
}
public DiscoveryConverter(ITestLogger logger, IAdapterSettings settings)
{
Settings = settings;
TestLog = logger;
}
public IList<TestCase> Convert(NUnitResults discoveryResults, string assemblyPath)
{
if (discoveryResults == null)
return new List<TestCase>();
AssemblyPath = assemblyPath;
var timing = new TimingLogger(Settings, TestLog);
if (Settings.DiscoveryMethod != DiscoveryMethod.Legacy)
{
TestRun = ConvertXml(discoveryResults);
}
var nunitTestCases = discoveryResults.TestCases();
// As a side effect of calling TestConverter.ConvertTestCase,
// the converter's cache of all test cases is populated as well.
// All future calls to convert a test case may now use the cache.
if (Settings.DiscoveryMethod == DiscoveryMethod.Legacy)
{
converterForXml = new TestConverterForXml(TestLog, AssemblyPath, Settings);
foreach (XmlNode testNode in nunitTestCases)
loadedTestCases.Add(converterForXml.ConvertTestCase(new NUnitEventTestCase(testNode)));
TestLog.Info(
$" NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Legacy discovery mode");
}
else
{
converter = new TestConverter(TestLog, AssemblyPath, Settings, this);
var isExplicit = TestRun.IsExplicit;
var testCases = RunnableTestCases(isExplicit);
foreach (var testNode in testCases)
loadedTestCases.Add(converter.ConvertTestCase(testNode));
var msg = isExplicit ? "Explicit run" : "Non-Explicit run";
TestLog.Info(
$" NUnit3TestExecutor discovered {loadedTestCases.Count} of {nunitTestCases.Count} NUnit test cases using Current Discovery mode, {msg}");
}
timing.LogTime("Converting test cases ");
return loadedTestCases;
IEnumerable<NUnitDiscoveryTestCase> RunnableTestCases(bool isExplicit) =>
isExplicit
? TestRun.TestAssembly.AllTestCases
: TestRun.TestAssembly.RunnableTestCases;
}
public NUnitDiscoveryTestRun ConvertXml(NUnitResults discovery)
{
var doc = XDocument.Load(new XmlNodeReader(discovery.FullTopNode));
var testrun = ExtractTestRun(doc);
var anode = doc.Root.Elements("test-suite");
var assemblyNode = anode.Single(o => o.Attribute(NUnitXmlAttributeNames.Type).Value == "Assembly");
var testassembly = ExtractTestAssembly(assemblyNode, testrun);
ExtractAllFixtures(testassembly, assemblyNode);
return testrun;
}
private static NUnitDiscoveryTestSuite ExtractTestSuite(XElement node, NUnitDiscoverySuiteBase parent)
{
var b = ExtractSuiteBasePropertiesClass(node);
var ts = new NUnitDiscoveryTestSuite(b, parent);
return ts;
}
private void ExtractAllFixtures(NUnitDiscoveryCanHaveTestFixture parent, XElement node)
{
foreach (var child in node.Elements("test-suite"))
{
var type = child.Attribute(NUnitXmlAttributeNames.Type)?.Value;
if (type == null)
{
TestLog.Debug($"ETF1:Don't understand element: {child}");
continue;
}
var className = child.Attribute(NUnitXmlAttributeNames.Classname)?.Value;
switch (type)
{
case TestFixture:
var tf = ExtractTestFixture(parent, child, className);
parent.AddTestFixture(tf);
ExtractTestCases(tf, child);
ExtractParameterizedMethodsAndTheories(tf, child);
break;
case GenericFixture:
var gtf = ExtractGenericTestFixture(parent, child);
parent.AddTestGenericFixture(gtf);
ExtractTestFixtures(gtf, child);
break;
case ParameterizedFixture:
var ptf = ExtractParameterizedTestFixture(parent, child);
parent.AddParameterizedFixture(ptf);
ExtractTestFixtures(ptf, child);
break;
case SetUpFixture:
var stf = ExtractSetUpTestFixture(parent, child, className);
parent.AddSetUpFixture(stf);
ExtractTestFixtures(stf, child);
break;
case TestSuite:
var ts = ExtractTestSuite(child, parent);
parent.AddTestSuite(ts);
if (child.HasElements)
ExtractAllFixtures(ts, child);
break;
default:
throw new DiscoveryException($"Invalid type found in ExtractAllFixtures for test suite: {type}");
}
}
}
private void ExtractTestFixtures(NUnitDiscoveryCanHaveTestFixture parent, XElement node)
{
foreach (var child in node.Elements().Where(o => o.Name != "properties"))
{
var type = child.Attribute(NUnitXmlAttributeNames.Type)?.Value;
if (type == null)
{
TestLog.Debug($"ETF2:Don't understand element: {child}");
continue;
}
var className = child.Attribute(NUnitXmlAttributeNames.Classname)?.Value;
var btf = ExtractSuiteBasePropertiesClass(child);
switch (type)
{
case TestFixture:
var tf = new NUnitDiscoveryTestFixture(btf, className, parent);
parent.AddTestFixture(tf);
ExtractTestCases(tf, child);
ExtractParameterizedMethodsAndTheories(tf, child);
break;
case TestSuite:
var ts = ExtractTestSuite(child, parent);
parent.AddTestSuite(ts);
if (child.HasElements)
ExtractAllFixtures(ts, child);
break;
case SetUpFixture:
var tsf = ExtractSetUpTestFixture(parent, node, className);
parent.AddSetUpFixture(tsf);
if (child.HasElements)
{
ExtractTestFixtures(tsf, child);
}
break;
case ParameterizedFixture:
var ptf = ExtractParameterizedTestFixture(parent, node);
parent.AddParameterizedFixture(ptf);
if (child.HasElements)
{
ExtractTestFixtures(ptf, child);
}
break;
case GenericFixture:
var gf = ExtractGenericTestFixture(parent, node);
parent.AddTestGenericFixture(gf);
if (child.HasElements)
{
ExtractTestFixtures(gf, child);
}
break;
default:
throw new DiscoveryException($"Not a TestFixture, SetUpFixture, ParameterizedFixture, GenericFixture or TestSuite, but {type}");
}
}
}
private static void ExtractParameterizedMethodsAndTheories(NUnitDiscoveryTestFixture tf, XElement node)
{
const string parameterizedMethod = "ParameterizedMethod";
const string theory = "Theory";
foreach (var child in node.Elements("test-suite"))
{
var type = child.Attribute(NUnitXmlAttributeNames.Type)?.Value;
if (type != parameterizedMethod && type != "Theory" && type != "GenericMethod")
throw new DiscoveryException($"Expected ParameterizedMethod, Theory or GenericMethod, but was {type}");
var className = child.Attribute(NUnitXmlAttributeNames.Classname)?.Value;
var btf = ExtractSuiteBasePropertiesClass(child);
switch (type)
{
case parameterizedMethod:
{
var tc = new NUnitDiscoveryParameterizedMethod(btf, className, tf);
ExtractTestCases(tc, child);
tf.AddParameterizedMethod(tc);
break;
}
case theory:
{
var tc = new NUnitDiscoveryTheory(btf, className, tf);
tf.AddTheory(tc);
ExtractTestCases(tc, child);
break;
}
default:
{
var tc = new NUnitDiscoveryGenericMethod(btf, className, tf);
tf.AddGenericMethod(tc);
ExtractTestCases(tc, child);
break;
}
}
}
}
public static IEnumerable<NUnitDiscoveryTestCase> ExtractTestCases(INUnitDiscoveryCanHaveTestCases tf, XElement node)
{
foreach (var child in node.Elements("test-case"))
{
var tc = ExtractTestCase(tf, child);
tf.AddTestCase(tc);
}
return tf.TestCases;
}
/// <summary>
/// Extracts single test case, made public for testing.
/// </summary>
public static NUnitDiscoveryTestCase ExtractTestCase(INUnitDiscoveryCanHaveTestCases tf, XElement child)
{
var className = child.Attribute(NUnitXmlAttributeNames.Classname)?.Value;
var methodName = child.Attribute(NUnitXmlAttributeNames.Methodname)?.Value;
var seedAtr = child.Attribute(NUnitXmlAttributeNames.Seed)?.Value;
var seed = seedAtr != null ? long.Parse(seedAtr, CultureInfo.InvariantCulture) : 0;
var btf = ExtractSuiteBasePropertiesClass(child);
var tc = new NUnitDiscoveryTestCase(btf, tf, className, methodName, seed);
return tc;
}
public static NUnitDiscoveryTestFixture ExtractTestFixture(INUnitDiscoveryCanHaveTestFixture parent, XElement node,
string className)
{
var b = ExtractSuiteBasePropertiesClass(node);
var ts = new NUnitDiscoveryTestFixture(b, className, parent);
return ts;
}
private static NUnitDiscoveryGenericFixture ExtractGenericTestFixture(
NUnitDiscoveryCanHaveTestFixture parent,
XElement node)
{
var b = ExtractSuiteBasePropertiesClass(node);
var ts = new NUnitDiscoveryGenericFixture(b, parent);
return ts;
}
private static NUnitDiscoverySetUpFixture ExtractSetUpTestFixture(
NUnitDiscoveryCanHaveTestFixture parent,
XElement node, string className)
{
var b = ExtractSuiteBasePropertiesClass(node);
var ts = new NUnitDiscoverySetUpFixture(b, className, parent);
return ts;
}
private static NUnitDiscoveryParameterizedTestFixture ExtractParameterizedTestFixture(
NUnitDiscoveryCanHaveTestFixture parent, XElement node)
{
var b = ExtractSuiteBasePropertiesClass(node);
var ts = new NUnitDiscoveryParameterizedTestFixture(b, parent);
return ts;
}
private NUnitDiscoveryTestAssembly ExtractTestAssembly(XElement node, NUnitDiscoveryTestRun parent)
{
string dType = node.Attribute(NUnitXmlAttributeNames.Type)?.Value;
if (dType is not "Assembly")
throw new DiscoveryException("Node is not of type assembly: " + node);
var aBase = ExtractSuiteBasePropertiesClass(node);
var assembly = new NUnitDiscoveryTestAssembly(aBase, parent);
parent.AddTestAssembly(assembly);
return assembly;
}
private static BaseProperties ExtractSuiteBasePropertiesClass(XElement node)
{
string dId = node.Attribute(NUnitXmlAttributeNames.Id).Value;
string dName = node.Attribute(NUnitXmlAttributeNames.Name).Value;
string dFullname = node.Attribute(NUnitXmlAttributeNames.Fullname).Value;
var dRunstate = ExtractRunState(node);
const char apo = '\'';
var tcs = node.Attribute(NUnitXmlAttributeNames.Testcasecount)?.Value.Trim(apo);
int dTestcasecount = int.Parse(tcs ?? "1", CultureInfo.InvariantCulture);
var bp = new BaseProperties(dId, dName, dFullname, dTestcasecount, dRunstate);
foreach (var propnode in node.Elements("properties").Elements("property"))
{
var prop = new NUnitProperty(propnode);
bp.Properties.Add(prop);
}
return bp;
}
private NUnitDiscoveryTestRun ExtractTestRun(XDocument node)
{
var sb = ExtractSuiteBasePropertiesClass(node.Root);
var tr = new NUnitDiscoveryTestRun(sb);
return tr;
}
private static RunStateEnum ExtractRunState(XElement node)
{
var runState = node.Attribute(NUnitXmlAttributeNames.Runstate)?.Value switch
{
"Runnable" => RunStateEnum.Runnable,
"Explicit" => RunStateEnum.Explicit,
"NotRunnable" => RunStateEnum.NotRunnable,
_ => RunStateEnum.NA
};
return runState;
}
}
public class BaseProperties
{
public BaseProperties(string dId, string dName, string dFullname, int dTestcasecount, RunStateEnum dRunstate)
{
Id = dId;
Name = dName;
Fullname = dFullname;
TestCaseCount = dTestcasecount;
RunState = dRunstate;
}
public List<NUnitProperty> Properties { get; } = new ();
public string Id { get; }
public string Name { get; }
public string Fullname { get; }
public RunStateEnum RunState { get; }
public int TestCaseCount { get; }
}
}