Skip to content

Commit 0e87d5a

Browse files
authored
Cleanup and simplify ObjectModelConverters (#5525)
1 parent 5bcd432 commit 0e87d5a

File tree

7 files changed

+27
-102
lines changed

7 files changed

+27
-102
lines changed

src/Platform/Microsoft.Testing.Extensions.VSTestBridge/ObjectModel/ObjectModelConverters.cs

+15-37
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using Microsoft.Testing.Platform.CommandLine;
1010
using Microsoft.Testing.Platform.Extensions.Messages;
1111
using Microsoft.Testing.Platform.ServerMode;
12-
using Microsoft.TestPlatform.AdapterUtilities;
1312
using Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities;
1413
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
1514

@@ -71,7 +70,7 @@ public static TestNode ToTestNode(this TestCase testCase, bool isTrxEnabled, INa
7170

7271
if (ShouldAddVSTestProviderProperties(namedFeatureCapability, commandLineOptions))
7372
{
74-
CopyVSTestProviderProperties(testCase.Properties, testNode, testCase.GetPropertyValue);
73+
CopyVSTestProviderProperties(testNode, testCase);
7574
}
7675

7776
if (testCase.CodeFilePath is not null)
@@ -109,34 +108,21 @@ private static void CopyCategoryAndTraits(TestObject testCaseOrResult, TestNode
109108
}
110109
}
111110

112-
private static void CopyVSTestProviderProperties(IEnumerable<TestProperty> testProperties, TestNode testNode, Func<TestProperty, object?> getPropertyValue)
111+
private static void CopyVSTestProviderProperties(TestNode testNode, TestCase testCase)
113112
{
114-
foreach (TestProperty property in testProperties)
113+
if (testCase.Id is Guid testCaseId)
115114
{
116-
// If vstestProvider is enabled (only known to be true for NUnit and Expecto so far), and we are running server mode in IDE (not dotnet test),
117-
// we add these stuff.
118-
// Once NUnit and Expecto allow us to move forward and remove vstestProvider, we can remove this logic and get rid of the whole vstestProvider capability.
119-
if (property.Id == TestCaseProperties.Id.Id
120-
&& getPropertyValue(property) is Guid testCaseId)
121-
{
122-
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.Id", testCaseId.ToString()));
123-
}
124-
else if (property.Id == TestCaseProperties.FullyQualifiedName.Id
125-
&& getPropertyValue(property) is string testCaseFqn)
126-
{
127-
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.FullyQualifiedName", testCaseFqn));
128-
}
129-
else if (property.Id == OriginalExecutorUriProperty.Id
130-
&& getPropertyValue(property) is Uri originalExecutorUri)
131-
{
132-
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.original-executor-uri", originalExecutorUri.AbsoluteUri));
133-
}
134-
else if (property.Id == HierarchyConstants.HierarchyPropertyId
135-
&& getPropertyValue(property) is string[] testCaseHierarchy
136-
&& testCaseHierarchy.Length == 4)
137-
{
138-
testNode.Properties.Add(new SerializableNamedArrayStringProperty("vstest.TestCase.Hierarchy", testCaseHierarchy));
139-
}
115+
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.Id", testCaseId.ToString()));
116+
}
117+
118+
if (testCase.FullyQualifiedName is string testCaseFqn)
119+
{
120+
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.TestCase.FullyQualifiedName", testCaseFqn));
121+
}
122+
123+
if (testCase.GetPropertyValue(OriginalExecutorUriProperty) is Uri originalExecutorUri)
124+
{
125+
testNode.Properties.Add(new SerializableKeyValuePairStringProperty("vstest.original-executor-uri", originalExecutorUri.AbsoluteUri));
140126
}
141127
}
142128

@@ -149,15 +135,6 @@ public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled,
149135

150136
CopyCategoryAndTraits(testResult, testNode, isTrxEnabled);
151137

152-
bool addVSTestProviderProperties = ShouldAddVSTestProviderProperties(namedFeatureCapability, commandLineOptions);
153-
if (addVSTestProviderProperties)
154-
{
155-
// TODO: This call might be unnecessary.
156-
// All the relevant properties should be on TestCase, not TestResult.
157-
// And properties on TestCase were already copied as part of ToTestNode call above.
158-
CopyVSTestProviderProperties(testResult.Properties, testNode, testResult.GetPropertyValue);
159-
}
160-
161138
testNode.AddOutcome(testResult);
162139

163140
if (isTrxEnabled)
@@ -189,6 +166,7 @@ public static TestNode ToTestNode(this TestResult testResult, bool isTrxEnabled,
189166

190167
var standardErrorMessages = new List<string>();
191168
var standardOutputMessages = new List<string>();
169+
bool addVSTestProviderProperties = ShouldAddVSTestProviderProperties(namedFeatureCapability, commandLineOptions);
192170
foreach (TestResultMessage testResultMessage in testResult.Messages)
193171
{
194172
if (testResultMessage.Category == TestResultMessage.StandardErrorCategory)

src/Platform/Microsoft.Testing.Platform/Messages/TestNodeProperties.cs

-14
Original file line numberDiff line numberDiff line change
@@ -381,17 +381,3 @@ public record StandardErrorProperty(string StandardError) : IProperty;
381381
public record FileArtifactProperty(FileInfo FileInfo, string DisplayName, string? Description = null) : IProperty;
382382

383383
internal sealed record SerializableKeyValuePairStringProperty(string Key, string Value) : KeyValuePairStringProperty(Key, Value);
384-
385-
internal sealed record SerializableNamedArrayStringProperty(string Name, string[] Values) : IProperty
386-
{
387-
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members", Justification = "https://github.com/dotnet/roslyn/issues/52421")]
388-
private bool PrintMembers(StringBuilder builder)
389-
{
390-
builder.Append("Name = ");
391-
builder.Append(Name);
392-
builder.Append(", Values = [");
393-
builder.AppendJoin(", ", Values);
394-
builder.Append(']');
395-
return true;
396-
}
397-
}

src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/Json/Json.cs

-6
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,6 @@ public Json(Dictionary<Type, JsonSerializer>? serializers = null, Dictionary<Typ
147147
continue;
148148
}
149149

150-
if (property is SerializableNamedArrayStringProperty namedArrayStringProperty)
151-
{
152-
properties.Add((namedArrayStringProperty.Name, namedArrayStringProperty.Values));
153-
continue;
154-
}
155-
156150
if (property is TestFileLocationProperty fileLocationProperty)
157151
{
158152
properties.Add(("location.file", fileLocationProperty.FilePath));

src/Platform/Microsoft.Testing.Platform/ServerMode/JsonRpc/SerializerUtilities.cs

-6
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,6 @@ static SerializerUtilities()
209209
continue;
210210
}
211211

212-
if (property is SerializableNamedArrayStringProperty namedArrayStringProperty)
213-
{
214-
properties[namedArrayStringProperty.Name] = namedArrayStringProperty.Values;
215-
continue;
216-
}
217-
218212
if (property is TestFileLocationProperty fileLocationProperty)
219213
{
220214
properties["location.file"] = fileLocationProperty.FilePath;

test/UnitTests/Microsoft.Testing.Extensions.VSTestBridge.UnitTests/ObjectModel/ObjectModelConvertersTests.cs

+10-30
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,15 @@ public void ToTestNode_WhenTestResultHasMSTestDiscovererTestCategoryTestProperty
9999
}
100100

101101
[TestMethod]
102-
public void ToTestNode_WhenTestResultHasTestCaseHierarchyTestProperty_TestNodePropertiesContainItInSerializableNamedArrayStringProperty()
102+
public void ToTestNode_WhenTestCaseHasOriginalExecutorUriProperty_TestNodePropertiesContainItInSerializableKeyValuePairStringProperty()
103103
{
104-
TestResult testResult = new(new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"));
105-
var testCaseHierarchy = TestProperty.Register("TestCase.Hierarchy", "Label", typeof(string[]), TestPropertyAttributes.None, typeof(TestCase));
106-
testResult.SetPropertyValue<string[]>(testCaseHierarchy, ["assembly", "class", "category", "test"]);
107-
108-
var testNode = testResult.ToTestNode(false, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions());
109-
110-
SerializableNamedArrayStringProperty[] trxCategoriesProperty = testNode.Properties.OfType<SerializableNamedArrayStringProperty>().ToArray();
111-
Assert.AreEqual(1, trxCategoriesProperty.Length);
112-
Assert.AreEqual("assembly", trxCategoriesProperty[0].Values[0]);
113-
Assert.AreEqual("class", trxCategoriesProperty[0].Values[1]);
114-
Assert.AreEqual("category", trxCategoriesProperty[0].Values[2]);
115-
Assert.AreEqual("test", trxCategoriesProperty[0].Values[3]);
116-
}
117-
118-
[TestMethod]
119-
public void ToTestNode_WhenTestResultHasOriginalExecutorUriProperty_TestNodePropertiesContainItInSerializableKeyValuePairStringProperty()
120-
{
121-
TestResult testResult = new(new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"));
104+
var testCase = new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs");
122105
var originalExecutorUriProperty = TestProperty.Register(
123106
VSTestTestNodeProperties.OriginalExecutorUriPropertyName, VSTestTestNodeProperties.OriginalExecutorUriPropertyName, typeof(Uri), typeof(TestCase));
124107

125-
testResult.SetPropertyValue<Uri>(originalExecutorUriProperty, new Uri("https://vs.com/"));
108+
testCase.SetPropertyValue<Uri>(originalExecutorUriProperty, new Uri("https://vs.com/"));
126109

127-
var testNode = testResult.ToTestNode(false, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions());
110+
var testNode = testCase.ToTestNode(false, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions());
128111

129112
SerializableKeyValuePairStringProperty[] serializableKeyValuePairStringProperty = testNode.Properties.OfType<SerializableKeyValuePairStringProperty>().ToArray();
130113
Assert.AreEqual(3, serializableKeyValuePairStringProperty.Length);
@@ -229,20 +212,17 @@ public void ToTestNode_WhenTestResultOutcomeIsPassed_TestNodePropertiesContainPa
229212
}
230213

231214
[TestMethod]
232-
public void ToTestNode_WhenTestResultHasUidAndDisplayNameWithWellKnownClient_TestNodePropertiesContainSerializableKeyValuePairStringPropertyTwice()
215+
public void ToTestNode_WhenTestCaseHasUidAndDisplayNameWithWellKnownClient_TestNodePropertiesContainSerializableKeyValuePairStringPropertyTwice()
233216
{
234-
TestResult testResult = new(new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs"))
235-
{
236-
DisplayName = "TestName",
237-
};
217+
var testCase = new TestCase("SomeFqn", new("executor://uri", UriKind.Absolute), "source.cs");
238218

239-
var testNode = testResult.ToTestNode(false, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions());
219+
var testNode = testCase.ToTestNode(false, new NamedFeatureCapabilityWithVSTestProvider(), new ServerModeCommandLineOptions());
240220

241221
SerializableKeyValuePairStringProperty[] errorTestNodeStateProperties = testNode.Properties.OfType<SerializableKeyValuePairStringProperty>().ToArray();
242222
Assert.AreEqual(2, errorTestNodeStateProperties.Length, "Expected 2 SerializableKeyValuePairStringProperty");
243-
Assert.AreEqual("vstest.TestCase.Id", errorTestNodeStateProperties[0].Key);
244-
Assert.AreEqual("vstest.TestCase.FullyQualifiedName", errorTestNodeStateProperties[1].Key);
245-
Assert.AreEqual("SomeFqn", errorTestNodeStateProperties[1].Value);
223+
Assert.AreEqual("vstest.TestCase.FullyQualifiedName", errorTestNodeStateProperties[0].Key);
224+
Assert.AreEqual("SomeFqn", errorTestNodeStateProperties[0].Value);
225+
Assert.AreEqual("vstest.TestCase.Id", errorTestNodeStateProperties[1].Key);
246226
}
247227

248228
[TestMethod]

test/UnitTests/Microsoft.Testing.Platform.UnitTests/Messages/TestNodePropertiesTests.cs

-6
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,4 @@ public void TestMetadataProperty_ToStringIsCorrect()
213213
=> Assert.AreEqual(
214214
"TestMetadataProperty { Key = some name, Value = some value }",
215215
new TestMetadataProperty("some name", "some value").ToString());
216-
217-
[TestMethod]
218-
public void SerializableNamedArrayStringProperty_ToStringIsCorrect()
219-
=> Assert.AreEqual(
220-
"SerializableNamedArrayStringProperty { Name = some name, Values = [value] }",
221-
new SerializableNamedArrayStringProperty("some name", ["value"]).ToString());
222216
}

test/UnitTests/Microsoft.Testing.Platform.UnitTests/ServerMode/FormatterUtilitiesTests.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private static void AssertSerialize(Type type, string instanceSerialized)
176176

177177
if (type == typeof(TestNode))
178178
{
179-
Assert.AreEqual("""{"uid":"uid","display-name":"DisplayName","traits":[{"testmetadata-key":"testmetadata-value"}],"array-key":["1","2"],"standardError":"textProperty2","standardOutput":"textProperty","time.start-utc":"2023-01-01T01:01:01.0000000+00:00","time.stop-utc":"2023-01-01T01:01:01.0000000+00:00","time.duration-ms":0,"location.type":"namespace.typeName","location.method":"methodName(param1,param2)","location.file":"filePath","location.line-start":1,"location.line-end":2,"key":"value","node-type":"action","execution-state":"failed","error.message":"sample","error.stacktrace":"","assert.actual":"","assert.expected":""}""".Replace(" ", string.Empty), instanceSerialized, because);
179+
Assert.AreEqual("""{"uid":"uid","display-name":"DisplayName","traits":[{"testmetadata-key":"testmetadata-value"}],"standardError":"textProperty2","standardOutput":"textProperty","time.start-utc":"2023-01-01T01:01:01.0000000+00:00","time.stop-utc":"2023-01-01T01:01:01.0000000+00:00","time.duration-ms":0,"location.type":"namespace.typeName","location.method":"methodName(param1,param2)","location.file":"filePath","location.line-start":1,"location.line-end":2,"key":"value","node-type":"action","execution-state":"failed","error.message":"sample","error.stacktrace":"","assert.actual":"","assert.expected":""}""".Replace(" ", string.Empty), instanceSerialized, because);
180180
return;
181181
}
182182

@@ -188,7 +188,7 @@ private static void AssertSerialize(Type type, string instanceSerialized)
188188

189189
if (type == typeof(TestNodeUpdateMessage))
190190
{
191-
Assert.AreEqual("""{"node":{"uid":"uid","display-name":"DisplayName","traits":[{"testmetadata-key":"testmetadata-value"}],"array-key":["1","2"],"standardError":"textProperty2","standardOutput":"textProperty","time.start-utc":"2023-01-01T01:01:01.0000000+00:00","time.stop-utc":"2023-01-01T01:01:01.0000000+00:00","time.duration-ms":0,"location.type":"namespace.typeName","location.method":"methodName(param1,param2)","location.file":"filePath","location.line-start":1,"location.line-end":2,"key":"value","node-type":"action","execution-state":"failed","error.message":"sample","error.stacktrace":"","assert.actual":"","assert.expected":""},"parent":"parent-uid"}""".Replace(" ", string.Empty), instanceSerialized, because);
191+
Assert.AreEqual("""{"node":{"uid":"uid","display-name":"DisplayName","traits":[{"testmetadata-key":"testmetadata-value"}],"standardError":"textProperty2","standardOutput":"textProperty","time.start-utc":"2023-01-01T01:01:01.0000000+00:00","time.stop-utc":"2023-01-01T01:01:01.0000000+00:00","time.duration-ms":0,"location.type":"namespace.typeName","location.method":"methodName(param1,param2)","location.file":"filePath","location.line-start":1,"location.line-end":2,"key":"value","node-type":"action","execution-state":"failed","error.message":"sample","error.stacktrace":"","assert.actual":"","assert.expected":""},"parent":"parent-uid"}""".Replace(" ", string.Empty), instanceSerialized, because);
192192
return;
193193
}
194194

@@ -462,7 +462,6 @@ static TestNode GetSampleTestNode()
462462
testNode.Properties.Add(new FailedTestNodeStateProperty(new InvalidOperationException("sample")));
463463
testNode.Properties.Add(new StandardOutputProperty("textProperty"));
464464
testNode.Properties.Add(new StandardErrorProperty("textProperty2"));
465-
testNode.Properties.Add(new SerializableNamedArrayStringProperty("array-key", ["1", "2"]));
466465
testNode.Properties.Add(new TestMetadataProperty("testmetadata-key", "testmetadata-value"));
467466

468467
return testNode;

0 commit comments

Comments
 (0)