-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathObjectModelConverters.cs
247 lines (212 loc) · 11.9 KB
/
ObjectModelConverters.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
using System.Diagnostics.CodeAnalysis;
using Microsoft.Testing.Extensions.TrxReport.Abstractions;
using Microsoft.Testing.Platform;
using Microsoft.Testing.Platform.Extensions.Messages;
using Microsoft.Testing.Platform.Services;
using Microsoft.Testing.Platform.TestHost;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
namespace Microsoft.Testing.Extensions.VSTestBridge.ObjectModel;
/// <summary>
/// A set of extension methods to convert between the Microsoft Testing Platform and VSTest object models.
/// </summary>
internal static class ObjectModelConverters
{
private static readonly TestProperty OriginalExecutorUriProperty = TestProperty.Register(
VSTestTestNodeProperties.OriginalExecutorUriPropertyName, VSTestTestNodeProperties.OriginalExecutorUriPropertyName,
typeof(Uri), typeof(TestNode));
/// <summary>
/// Converts a VSTest <see cref="TestCase"/> to a Microsoft Testing Platform <see cref="TestNode"/>.
/// </summary>
public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, IClientInfo client)
{
string testNodeUid = testCase.Id.ToString();
TestNode testNode = new()
{
Uid = new TestNodeUid(testNodeUid),
DisplayName = testCase.DisplayName ?? testCase.FullyQualifiedName,
};
CopyVSTestProperties(testCase.Properties, testNode, testCase, testCase.GetPropertyValue, isTrxEnabled, client);
if (testCase.CodeFilePath is not null)
{
testNode.Properties.Add(new TestFileLocationProperty(testCase.CodeFilePath, new(new(testCase.LineNumber, -1), new(testCase.LineNumber, -1))));
}
return testNode;
}
private static void CopyVSTestProperties(IEnumerable<TestProperty> testProperties, TestNode testNode, TestCase testCase, Func<TestProperty, object?> getPropertyValue,
bool isTrxEnabled, IClientInfo client)
{
List<KeyValuePair<string, string>>? testNodeTraits = null;
foreach (TestProperty property in testProperties)
{
testNode.Properties.Add(new VSTestProperty(property, testCase));
if (isTrxEnabled)
{
// TPv2 is doing some special handling for MSTest... we should probably do the same.
// See https://github.com/microsoft/vstest/blob/main/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs#L66-L70
if (property.Id == "MSTestDiscoverer.TestCategory"
&& getPropertyValue(property) is string[] mstestCategories)
{
testNode.Properties.Add(new TrxCategoriesProperty(mstestCategories));
}
}
// Implement handling of specific vstest properties for VS/VS Code Test Explorer,
// see https://github.com/microsoft/testanywhere/blob/main/docs/design/proposed/IDE_Protocol_IDE_Integration.md#vstest-test-node
if (client.Id == WellKnownClients.VisualStudio)
{
if (property.Id == TestCaseProperties.Id.Id
&& getPropertyValue(property) is Guid testCaseId)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.Id", testCaseId.ToString()));
}
else if (property.Id == TestCaseProperties.FullyQualifiedName.Id
&& getPropertyValue(property) is string testCaseFqn)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.FullyQualifiedName", testCaseFqn));
}
else if (property.Id == OriginalExecutorUriProperty.Id
&& getPropertyValue(property) is Uri originalExecutorUri)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.original-executor-uri", originalExecutorUri.AbsoluteUri));
}
// The TP object holding the hierarchy property is defined on adapter utilities and we don't want to enforce that dependency
// so instead I use the string ID copied from TP.
else if (property.Id == "TestCase.Hierarchy"
&& getPropertyValue(property) is string[] testCaseHierarchy
&& testCaseHierarchy.Length == 4)
{
testNode.Properties.Add(new SerializableNamedArrayStringProperty("vstest.TestCase.Hierarchy", testCaseHierarchy));
}
// ID is defined on TraitCollection but is internal so again we copy the string here.
else if (property.Id == "TestObject.Traits"
&& getPropertyValue(property) is KeyValuePair<string, string>[] traits && traits.Length > 0)
{
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
testNodeTraits ??= [];
#pragma warning restore SA1010 // Opening square brackets should be spaced correctly
testNodeTraits.AddRange(traits);
}
// TPv2 is doing some special handling for MSTest... we should probably do the same.
// See https://github.com/microsoft/vstest/blob/main/src/Microsoft.TestPlatform.Extensions.TrxLogger/Utility/Converter.cs#L66-L70
else if (property.Id == "MSTestDiscoverer.TestCategory"
&& getPropertyValue(property) is string[] mstestCategories && mstestCategories.Length > 0)
{
#pragma warning disable SA1010 // Opening square brackets should be spaced correctly
testNodeTraits ??= [];
#pragma warning restore SA1010 // Opening square brackets should be spaced correctly
foreach (string category in mstestCategories)
{
testNodeTraits.Add(new(category, string.Empty));
}
}
}
}
// Add all the traits and categories we collected from different properties.
if (testNodeTraits != null)
{
testNode.Properties.Add(new SerializableNamedKeyValuePairsStringProperty("traits", testNodeTraits.ToArray()));
}
}
/// <summary>
/// Converts a VSTest <see cref="TestResult"/> to a Microsoft Testing Platform <see cref="TestNode"/>.
/// </summary>
public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled, IClientInfo client)
{
var testNode = testResult.TestCase.ToTestNode(isTrxEnabled, client);
CopyVSTestProperties(testResult.Properties, testNode, testResult.TestCase, testResult.GetPropertyValue, isTrxEnabled, client);
testNode.AddOutcome(testResult);
if (isTrxEnabled)
{
testNode.Properties.Add(new TrxExceptionProperty(testResult.ErrorMessage, testResult.ErrorStackTrace));
if (TryParseFullyQualifiedType(testResult.TestCase.FullyQualifiedName, out string? fullyQualifiedType))
{
testNode.Properties.Add(new TrxFullyQualifiedTypeNameProperty(fullyQualifiedType));
}
else
{
throw new InvalidOperationException("Unable to parse fully qualified type name from test case: " + testResult.TestCase.FullyQualifiedName);
}
testNode.Properties.Add(new TrxMessagesProperty(testResult.Messages
.Select(msg =>
msg.Category switch
{
string x when x == TestResultMessage.StandardErrorCategory => new StandardErrorTrxMessage(msg.Text),
string x when x == TestResultMessage.StandardOutCategory => new StandardOutputTrxMessage(msg.Text),
string x when x == TestResultMessage.DebugTraceCategory => new DebugOrTraceTrxMessage(msg.Text),
_ => new TrxMessage(msg.Text),
})
.ToArray()));
}
testNode.Properties.Add(new TimingProperty(new(testResult.StartTime, testResult.EndTime, testResult.Duration), []));
foreach (TestResultMessage testResultMessage in testResult.Messages)
{
if (testResultMessage.Category == TestResultMessage.StandardErrorCategory)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardError", testResultMessage.Text ?? string.Empty));
testNode.Properties.Add(new StandardErrorProperty(testResultMessage.Text ?? string.Empty));
}
if (testResultMessage.Category == TestResultMessage.StandardOutCategory)
{
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.StandardOutput", testResultMessage.Text ?? string.Empty));
testNode.Properties.Add(new StandardOutputProperty(testResultMessage.Text ?? string.Empty));
}
}
return testNode;
}
private static void AddOutcome(this TestNode testNode, TestResult testResult)
{
switch (testResult.Outcome)
{
case TestOutcome.Passed:
testNode.Properties.Add(PassedTestNodeStateProperty.CachedInstance);
break;
case TestOutcome.NotFound:
testNode.Properties.Add(new ErrorTestNodeStateProperty(new VSTestException(testResult.ErrorMessage ?? "Not found", testResult.ErrorStackTrace)));
break;
case TestOutcome.Failed:
testNode.Properties.Add(new FailedTestNodeStateProperty(new VSTestException(testResult.ErrorMessage, testResult.ErrorStackTrace)));
break;
// It seems that NUnit inconclusive tests are reported as None which should be considered as Skipped.
case TestOutcome.None:
case TestOutcome.Skipped:
testNode.Properties.Add(SkippedTestNodeStateProperty.CachedInstance);
break;
default:
throw new NotSupportedException($"Unsupported test outcome value '{testResult.Outcome}'");
}
}
internal static void FixUpTestCase(this TestCase testCase, string? testAssemblyPath = null)
{
// To help framework authors using code generator, we replace the Source property of the test case with the
// test assembly path.
if (RoslynString.IsNullOrEmpty(testCase.Source) && !RoslynString.IsNullOrEmpty(testAssemblyPath))
{
testCase.Source = testAssemblyPath;
}
// Because this project is the actually registered test adapter, we need to replace test framework executor
// URI by ours.
if (!testCase.Properties.Any(x => x.Id == OriginalExecutorUriProperty.Id))
{
testCase.SetPropertyValue(OriginalExecutorUriProperty, testCase.ExecutorUri);
}
testCase.ExecutorUri = new(Constants.ExecutorUri);
}
private static bool TryParseFullyQualifiedType(string fullyQualifiedName, [NotNullWhen(true)] out string? fullyQualifiedType)
{
fullyQualifiedType = null;
// Some test frameworks display arguments in the fully qualified type name, so we need to exclude them
// before looking at the last dot.
int openBracketIndex = fullyQualifiedName.IndexOf('(');
int lastDotIndexBeforeOpenBracket = openBracketIndex <= 0
? fullyQualifiedName.LastIndexOf('.')
: fullyQualifiedName.LastIndexOf('.', openBracketIndex - 1);
if (lastDotIndexBeforeOpenBracket <= 0)
{
return false;
}
fullyQualifiedType = fullyQualifiedName[..lastDotIndexBeforeOpenBracket];
return true;
}
}