Skip to content

Commit 726423b

Browse files
authored
First step removing ReflectionUtility (#7361)
2 parents a12533a + bb0f857 commit 726423b

9 files changed

Lines changed: 49 additions & 51 deletions

File tree

src/Adapter/MSTestAdapter.PlatformServices/Services/TestDeployment.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if !WINDOWS_UWP && !WIN_UI
55

66
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
78
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
89
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
910
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
@@ -34,7 +35,7 @@ internal sealed class TestDeployment : ITestDeployment
3435
/// Initializes a new instance of the <see cref="TestDeployment"/> class.
3536
/// </summary>
3637
public TestDeployment()
37-
: this(new DeploymentItemUtility(new ReflectionUtility()), new DeploymentUtility(), new FileUtility())
38+
: this(new DeploymentItemUtility(ReflectHelper.Instance), new DeploymentUtility(), new FileUtility())
3839
{
3940
}
4041

src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentItemUtility.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#if !WINDOWS_UWP && !WIN_UI
55

6+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
67
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
78
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
89
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -14,8 +15,7 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Uti
1415
/// </summary>
1516
internal sealed class DeploymentItemUtility
1617
{
17-
// REVIEW: it would be better if this was a ReflectionHelper, because helper is able to cache. But we don't have reflection helper here, because this is platform services dll.
18-
private readonly ReflectionUtility _reflectionUtility;
18+
private readonly ReflectHelper _reflectHelper;
1919

2020
/// <summary>
2121
/// A cache for class level deployment items.
@@ -25,10 +25,10 @@ internal sealed class DeploymentItemUtility
2525
/// <summary>
2626
/// Initializes a new instance of the <see cref="DeploymentItemUtility"/> class.
2727
/// </summary>
28-
/// <param name="reflectionUtility"> The reflect helper. </param>
29-
internal DeploymentItemUtility(ReflectionUtility reflectionUtility)
28+
/// <param name="reflectHelper"> The reflect helper. </param>
29+
internal DeploymentItemUtility(ReflectHelper reflectHelper)
3030
{
31-
_reflectionUtility = reflectionUtility;
31+
_reflectHelper = reflectHelper;
3232
_classLevelDeploymentItems = [];
3333
}
3434

@@ -42,9 +42,7 @@ internal IList<DeploymentItem> GetClassLevelDeploymentItems(Type type, ICollecti
4242
{
4343
if (!_classLevelDeploymentItems.TryGetValue(type, out IList<DeploymentItem>? value))
4444
{
45-
IReadOnlyList<object> deploymentItemAttributes = _reflectionUtility.GetCustomAttributes(
46-
type,
47-
typeof(DeploymentItemAttribute));
45+
IEnumerable<DeploymentItemAttribute> deploymentItemAttributes = _reflectHelper.GetAttributes<DeploymentItemAttribute>(type);
4846
value = GetDeploymentItems(deploymentItemAttributes, warnings);
4947
_classLevelDeploymentItems[type] = value;
5048
}
@@ -61,7 +59,7 @@ internal IList<DeploymentItem> GetClassLevelDeploymentItems(Type type, ICollecti
6159
internal KeyValuePair<string, string>[]? GetDeploymentItems(MethodInfo method, IEnumerable<DeploymentItem> classLevelDeploymentItems,
6260
ICollection<string> warnings)
6361
{
64-
List<DeploymentItem> testLevelDeploymentItems = GetDeploymentItems(_reflectionUtility.GetCustomAttributes(method, typeof(DeploymentItemAttribute)), warnings);
62+
List<DeploymentItem> testLevelDeploymentItems = GetDeploymentItems(_reflectHelper.GetAttributes<DeploymentItemAttribute>(method), warnings);
6563

6664
return ToKeyValuePairs(Concat(testLevelDeploymentItems, classLevelDeploymentItems));
6765
}
@@ -174,11 +172,11 @@ private static bool IsInvalidPath(string path)
174172
return false;
175173
}
176174

177-
private static List<DeploymentItem> GetDeploymentItems(IEnumerable deploymentItemAttributes, ICollection<string> warnings)
175+
private static List<DeploymentItem> GetDeploymentItems(IEnumerable<DeploymentItemAttribute> deploymentItemAttributes, ICollection<string> warnings)
178176
{
179177
var deploymentItems = new List<DeploymentItem>();
180178

181-
foreach (DeploymentItemAttribute deploymentItemAttribute in deploymentItemAttributes.Cast<DeploymentItemAttribute>())
179+
foreach (DeploymentItemAttribute deploymentItemAttribute in deploymentItemAttributes)
182180
{
183181
if (IsValidDeploymentItem(deploymentItemAttribute.Path, deploymentItemAttribute.OutputDirectory, out string? warning))
184182
{

src/Adapter/MSTestAdapter.PlatformServices/Utilities/DeploymentUtilityBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if !WINDOWS_UWP && !WIN_UI
55

66
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter;
7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
78
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
89
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Extensions;
910

@@ -25,7 +26,7 @@ internal abstract class DeploymentUtilityBase
2526
protected const string DeploymentFolderPrefix = "Deploy";
2627

2728
public DeploymentUtilityBase()
28-
: this(new DeploymentItemUtility(new ReflectionUtility()), new AssemblyUtility(), new FileUtility())
29+
: this(new DeploymentItemUtility(ReflectHelper.Instance), new AssemblyUtility(), new FileUtility())
2930
{
3031
}
3132

test/IntegrationTests/PlatformServices.Desktop.IntegrationTests/ReflectionUtilityTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using AwesomeAssertions;
55

6+
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
67
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
78

89
using SampleFrameworkExtensions;
@@ -39,7 +40,7 @@ public void GetCustomAttributesShouldReturnAllAttributes()
3940
{
4041
MethodInfo methodInfo = _testAsset.GetType("TestProjectForDiscovery.AttributeTestBaseClass").GetMethod("DummyVTestMethod1");
4142

42-
IReadOnlyList<object> attributes = ReflectionUtility.GetCustomAttributes(methodInfo);
43+
IReadOnlyList<object> attributes = new ReflectionOperations().GetCustomAttributes(methodInfo);
4344

4445
attributes.Should().NotBeNull();
4546
attributes.Should().HaveCount(2);
@@ -52,7 +53,7 @@ public void GetCustomAttributesShouldReturnAllAttributesWithBaseInheritance()
5253
{
5354
MethodInfo methodInfo = _testAsset.GetType("TestProjectForDiscovery.AttributeTestClass").GetMethod("DummyVTestMethod1");
5455

55-
IReadOnlyList<object> attributes = ReflectionUtility.GetCustomAttributes(methodInfo);
56+
IReadOnlyList<object> attributes = new ReflectionOperations().GetCustomAttributes(methodInfo);
5657

5758
attributes.Should().NotBeNull();
5859
attributes.Should().HaveCount(3);
@@ -66,7 +67,7 @@ public void GetCustomAttributesOnTypeShouldReturnAllAttributes()
6667
{
6768
Type type = _testAsset.GetType("TestProjectForDiscovery.AttributeTestBaseClass");
6869

69-
IReadOnlyList<object> attributes = ReflectionUtility.GetCustomAttributes(type);
70+
IReadOnlyList<object> attributes = new ReflectionOperations().GetCustomAttributes(type);
7071

7172
attributes.Should().NotBeNull();
7273
attributes.Should().HaveCount(1);
@@ -79,7 +80,7 @@ public void GetCustomAttributesOnTypeShouldReturnAllAttributesWithBaseInheritanc
7980
{
8081
Type type = _testAsset.GetType("TestProjectForDiscovery.AttributeTestClass");
8182

82-
IReadOnlyList<object> attributes = ReflectionUtility.GetCustomAttributes(type);
83+
IReadOnlyList<object> attributes = new ReflectionOperations().GetCustomAttributes(type);
8384

8485
attributes.Should().NotBeNull();
8586
attributes.Should().HaveCount(2);

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/DesktopTestDeploymentTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if NETFRAMEWORK
55
using AwesomeAssertions;
66

7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
78
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
89
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
910
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
@@ -23,7 +24,6 @@ public class DesktopTestDeploymentTests : TestContainer
2324
private const string DefaultDeploymentItemPath = @"c:\temp";
2425
private const string DefaultDeploymentItemOutputDirectory = "out";
2526

26-
private readonly Mock<ReflectionUtility> _mockReflectionUtility;
2727
private readonly Mock<FileUtility> _mockFileUtility;
2828

2929
#pragma warning disable IDE0052 // Remove unread private members
@@ -32,7 +32,6 @@ public class DesktopTestDeploymentTests : TestContainer
3232

3333
public DesktopTestDeploymentTests()
3434
{
35-
_mockReflectionUtility = new Mock<ReflectionUtility>();
3635
_mockFileUtility = new Mock<FileUtility>();
3736
_warnings = [];
3837

@@ -149,7 +148,7 @@ private TestDeployment CreateAndSetupDeploymentRelatedUtilities(out TestRunDirec
149148
_mockFileUtility.Setup(fu => fu.GetNextIterationDirectoryName(It.IsAny<string>(), It.IsAny<string>()))
150149
.Returns(testRunDirectories.RootDeploymentDirectory);
151150

152-
var deploymentItemUtility = new DeploymentItemUtility(_mockReflectionUtility.Object);
151+
var deploymentItemUtility = new DeploymentItemUtility(new ReflectHelper());
153152

154153
return new TestDeployment(
155154
deploymentItemUtility,

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Services/TestDeploymentTests.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if !WINDOWS_UWP && !WIN_UI
55
using AwesomeAssertions;
66

7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
78
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
89
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
910
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
@@ -24,7 +25,7 @@ public class TestDeploymentTests : TestContainer
2425
private const string DefaultDeploymentItemPath = @"c:\temp";
2526
private const string DefaultDeploymentItemOutputDirectory = "out";
2627

27-
private readonly Mock<ReflectionUtility> _mockReflectionUtility;
28+
private readonly Mock<ReflectHelper> _mockReflectHelper;
2829
private readonly Mock<FileUtility> _mockFileUtility;
2930

3031
#pragma warning disable IDE0044 // Add readonly modifier
@@ -33,7 +34,7 @@ public class TestDeploymentTests : TestContainer
3334

3435
public TestDeploymentTests()
3536
{
36-
_mockReflectionUtility = new Mock<ReflectionUtility>();
37+
_mockReflectHelper = new Mock<ReflectHelper>();
3738
_mockFileUtility = new Mock<FileUtility>();
3839
_warnings = [];
3940

@@ -52,7 +53,7 @@ public void GetDeploymentItemsReturnsNullWhenNoDeploymentItems()
5253
public void GetDeploymentItemsReturnsDeploymentItems()
5354
{
5455
// Arrange.
55-
var testDeployment = new TestDeployment(new DeploymentItemUtility(_mockReflectionUtility.Object), null!, null!);
56+
var testDeployment = new TestDeployment(new DeploymentItemUtility(_mockReflectHelper.Object), null!, null!);
5657

5758
// setup mocks
5859
KeyValuePair<string, string>[] methodLevelDeploymentItems =
@@ -181,7 +182,7 @@ public void DeployShouldReturnFalseWhenDeploymentEnabledSetToFalseButHasDeployme
181182
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, kvpArray);
182183

183184
var testDeployment = new TestDeployment(
184-
new DeploymentItemUtility(_mockReflectionUtility.Object),
185+
new DeploymentItemUtility(_mockReflectHelper.Object),
185186
new DeploymentUtility(),
186187
_mockFileUtility.Object);
187188

@@ -204,7 +205,7 @@ public void DeployShouldReturnFalseWhenDeploymentEnabledSetToFalseAndHasNoDeploy
204205
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "path/to/asm.dll");
205206
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, null);
206207
var testDeployment = new TestDeployment(
207-
new DeploymentItemUtility(_mockReflectionUtility.Object),
208+
new DeploymentItemUtility(_mockReflectHelper.Object),
208209
new DeploymentUtility(),
209210
_mockFileUtility.Object);
210211

@@ -227,7 +228,7 @@ public void DeployShouldReturnFalseWhenDeploymentEnabledSetToTrueButHasNoDeploym
227228
var testCase = new TestCase("A.C.M", new Uri("executor://testExecutor"), "path/to/asm.dll");
228229
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, null);
229230
var testDeployment = new TestDeployment(
230-
new DeploymentItemUtility(_mockReflectionUtility.Object),
231+
new DeploymentItemUtility(_mockReflectHelper.Object),
231232
new DeploymentUtility(),
232233
_mockFileUtility.Object);
233234

@@ -257,7 +258,7 @@ internal void DeployShouldReturnTrueWhenDeploymentEnabledSetToTrueAndHasDeployme
257258
];
258259
testCase.SetPropertyValue(DeploymentItemUtilityTests.DeploymentItemsProperty, kvpArray);
259260
var testDeployment = new TestDeployment(
260-
new DeploymentItemUtility(_mockReflectionUtility.Object),
261+
new DeploymentItemUtility(_mockReflectHelper.Object),
261262
new DeploymentUtility(),
262263
_mockFileUtility.Object);
263264

@@ -370,11 +371,9 @@ private void SetupDeploymentItems(MemberInfo memberInfo, KeyValuePair<string, st
370371
deploymentItemAttributes.Add(new DeploymentItemAttribute(deploymentItem.Key, deploymentItem.Value));
371372
}
372373

373-
_mockReflectionUtility.Setup(
374-
ru =>
375-
ru.GetCustomAttributes(
376-
memberInfo,
377-
typeof(DeploymentItemAttribute))).Returns(deploymentItemAttributes.ToArray());
374+
_mockReflectHelper
375+
.Setup(ru => ru.GetAttributes<DeploymentItemAttribute>(memberInfo))
376+
.Returns(deploymentItemAttributes.ToArray());
378377
}
379378

380379
private static TestCase GetTestCase(string source)
@@ -418,7 +417,7 @@ private TestDeployment CreateAndSetupDeploymentRelatedUtilities(out TestRunDirec
418417
_mockFileUtility.Setup(fu => fu.GetNextIterationDirectoryName(It.IsAny<string>(), It.IsAny<string>()))
419418
.Returns(testRunDirectories.RootDeploymentDirectory);
420419

421-
var deploymentItemUtility = new DeploymentItemUtility(_mockReflectionUtility.Object);
420+
var deploymentItemUtility = new DeploymentItemUtility(_mockReflectHelper.Object);
422421

423422
return new TestDeployment(
424423
deploymentItemUtility,

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentItemUtilityTests.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if !WINDOWS_UWP && !WIN_UI
55
using AwesomeAssertions;
66

7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
78
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
89
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Resources;
910
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
@@ -24,7 +25,7 @@ public class DeploymentItemUtilityTests : TestContainer
2425
TestPropertyAttributes.Hidden,
2526
typeof(TestCase));
2627

27-
private readonly Mock<ReflectionUtility> _mockReflectionUtility;
28+
private readonly Mock<ReflectHelper> _mockReflectHelper;
2829
private readonly DeploymentItemUtility _deploymentItemUtility;
2930
private readonly ICollection<string> _warnings;
3031

@@ -33,16 +34,16 @@ public class DeploymentItemUtilityTests : TestContainer
3334

3435
public DeploymentItemUtilityTests()
3536
{
36-
_mockReflectionUtility = new Mock<ReflectionUtility>();
37-
_deploymentItemUtility = new DeploymentItemUtility(_mockReflectionUtility.Object);
37+
_mockReflectHelper = new Mock<ReflectHelper>();
38+
_deploymentItemUtility = new DeploymentItemUtility(_mockReflectHelper.Object);
3839
_warnings = [];
3940
}
4041

4142
#region GetClassLevelDeploymentItems tests
4243

4344
public void GetClassLevelDeploymentItemsShouldReturnEmptyListWhenNoDeploymentItems()
4445
{
45-
_mockReflectionUtility.Setup(x => x.GetCustomAttributes(typeof(DeploymentItemUtilityTests), typeof(DeploymentItemAttribute)))
46+
_mockReflectHelper.Setup(x => x.GetAttributes<DeploymentItemAttribute>(typeof(DeploymentItemUtilityTests)))
4647
.Returns([]);
4748
IList<DeploymentItem> deploymentItems = _deploymentItemUtility.GetClassLevelDeploymentItems(typeof(DeploymentItemUtilityTests), _warnings);
4849

@@ -163,7 +164,7 @@ public void GetClassLevelDeploymentItemsShouldReportWarningsForInvalidDeployment
163164
public void GetDeploymentItemsShouldReturnNullOnNoDeploymentItems()
164165
{
165166
MethodInfo method = typeof(DeploymentItemUtilityTests).GetMethod("GetDeploymentItemsShouldReturnNullOnNoDeploymentItems")!;
166-
_mockReflectionUtility.Setup(x => x.GetCustomAttributes(method, typeof(DeploymentItemAttribute)))
167+
_mockReflectHelper.Setup(x => x.GetAttributes<DeploymentItemAttribute>(method))
167168
.Returns([]);
168169

169170
_deploymentItemUtility.GetDeploymentItems(method, null!, _warnings).Should().BeNull();
@@ -208,7 +209,7 @@ public void GetDeploymentItemsShouldReturnClassLevelDeploymentItemsOnly()
208209
};
209210

210211
MethodInfo method = typeof(DeploymentItemUtilityTests).GetMethod("GetDeploymentItemsShouldReturnNullOnNoDeploymentItems")!;
211-
_mockReflectionUtility.Setup(x => x.GetCustomAttributes(method, typeof(DeploymentItemAttribute)))
212+
_mockReflectHelper.Setup(x => x.GetAttributes<DeploymentItemAttribute>(method))
212213
.Returns([]);
213214

214215
// Act.
@@ -422,11 +423,9 @@ private void SetupDeploymentItems(MemberInfo memberInfo, KeyValuePair<string, st
422423
deploymentItemAttributes.Add(new DeploymentItemAttribute(deploymentItem.Key, deploymentItem.Value));
423424
}
424425

425-
_mockReflectionUtility.Setup(
426-
ru =>
427-
ru.GetCustomAttributes(
428-
memberInfo,
429-
typeof(DeploymentItemAttribute))).Returns(deploymentItemAttributes.ToArray());
426+
_mockReflectHelper
427+
.Setup(ru => ru.GetAttributes<DeploymentItemAttribute>(memberInfo))
428+
.Returns(deploymentItemAttributes.ToArray());
430429
}
431430

432431
#endregion

test/UnitTests/MSTestAdapter.PlatformServices.UnitTests/Utilities/DeploymentUtilityTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if !WINDOWS_UWP && !WIN_UI
55
using AwesomeAssertions;
66

7+
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
78
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Deployment;
89
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Resources;
910
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Utilities;
@@ -24,7 +25,6 @@ public class DeploymentUtilityTests : TestContainer
2425
private const string DefaultDeploymentItemPath = @"c:\temp";
2526
private const string DefaultDeploymentItemOutputDirectory = "out";
2627

27-
private readonly Mock<ReflectionUtility> _mockReflectionUtility;
2828
private readonly Mock<FileUtility> _mockFileUtility;
2929
private readonly Mock<AssemblyUtility> _mockAssemblyUtility;
3030
private readonly Mock<IRunContext> _mockRunContext;
@@ -40,13 +40,12 @@ public class DeploymentUtilityTests : TestContainer
4040

4141
public DeploymentUtilityTests()
4242
{
43-
_mockReflectionUtility = new Mock<ReflectionUtility>();
4443
_mockFileUtility = new Mock<FileUtility>();
4544
_mockAssemblyUtility = new Mock<AssemblyUtility>();
4645
_warnings = [];
4746

4847
_deploymentUtility = new DeploymentUtility(
49-
new DeploymentItemUtility(_mockReflectionUtility.Object),
48+
new DeploymentItemUtility(new ReflectHelper()),
5049
_mockAssemblyUtility.Object,
5150
_mockFileUtility.Object);
5251

0 commit comments

Comments
 (0)