Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
Expand All @@ -13,18 +13,18 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
[SuppressMessage("Performance", "CA1852: Seal internal types", Justification = "Overrides required for testability")]
internal class TestMethodValidator
{
private readonly ReflectHelper _reflectHelper;
private readonly IReflectionOperations _reflectionOperation;
private readonly bool _discoverInternals;

/// <summary>
/// Initializes a new instance of the <see cref="TestMethodValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
/// <param name="reflectionOperation">An instance to reflection helper for type information.</param>
/// <param name="discoverInternals">True to discover methods which are declared internal in addition to methods
/// which are declared public.</param>
internal TestMethodValidator(ReflectHelper reflectHelper, bool discoverInternals)
internal TestMethodValidator(IReflectionOperations reflectionOperation, bool discoverInternals)
{
_reflectHelper = reflectHelper;
_reflectionOperation = reflectionOperation;
_discoverInternals = discoverInternals;
}

Expand All @@ -44,7 +44,7 @@ internal virtual bool IsValidTestMethod(MethodInfo testMethodInfo, Type type, IC
// but the difference is quite small, and we don't expect a huge amount of non-test methods in the assembly.
//
// Also skip all methods coming from object, because they cannot be tests.
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectHelper.IsAttributeDefined<TestMethodAttribute>(testMethodInfo))
if (testMethodInfo.DeclaringType == typeof(object) || !_reflectionOperation.IsAttributeDefined<TestMethodAttribute>(testMethodInfo))
{
return false;
}
Expand All @@ -55,7 +55,7 @@ internal virtual bool IsValidTestMethod(MethodInfo testMethodInfo, Type type, IC
// Todo: Decide whether parameter count matters.
bool isValidTestMethod = isAccessible &&
testMethodInfo is { IsAbstract: false, IsStatic: false } &&
testMethodInfo.IsValidReturnType(_reflectHelper);
testMethodInfo.IsValidReturnType(_reflectionOperation);

if (!isValidTestMethod)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Discovery;
Expand All @@ -15,27 +15,27 @@ internal class TypeValidator
// Setting this to a string representation instead of a typeof(TestContext).FullName
// since the later would require a load of the Test Framework extension assembly at this point.
private const string TestContextFullName = "Microsoft.VisualStudio.TestTools.UnitTesting.TestContext";
private readonly ReflectHelper _reflectHelper;
private readonly IReflectionOperations _reflectionOperation;
private readonly bool _discoverInternals;

/// <summary>
/// Initializes a new instance of the <see cref="TypeValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
internal TypeValidator(ReflectHelper reflectHelper)
: this(reflectHelper, false)
/// <param name="reflectionOperation">An instance to reflection helper for type information.</param>
internal TypeValidator(IReflectionOperations reflectionOperation)
: this(reflectionOperation, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="TypeValidator"/> class.
/// </summary>
/// <param name="reflectHelper">An instance to reflection helper for type information.</param>
/// <param name="reflectionOperation">An instance to reflection helper for type information.</param>
/// <param name="discoverInternals">True to discover test classes which are declared internal in
/// addition to test classes which are declared public.</param>
internal TypeValidator(ReflectHelper reflectHelper, bool discoverInternals)
internal TypeValidator(IReflectionOperations reflectionOperation, bool discoverInternals)
{
_reflectHelper = reflectHelper;
_reflectionOperation = reflectionOperation;
_discoverInternals = discoverInternals;
}

Expand All @@ -52,7 +52,7 @@ internal virtual bool IsValidTestClass(Type type, List<string> warnings)
// gives us a better performance.
// It would be possible to use non-caching reflection here if we knew that we are only doing discovery that won't be followed by run,
// but the difference is quite small, and we don't expect a huge amount of non-test classes in the assembly.
if (!type.IsClass || !_reflectHelper.IsAttributeDefined<TestClassAttribute>(type))
if (!type.IsClass || !_reflectionOperation.IsAttributeDefined<TestClassAttribute>(type))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Security;

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution;
Expand Down Expand Up @@ -34,7 +34,10 @@ internal TestAssemblySettings GetSettings(string source)
// Load the source.
Assembly testAssembly = PlatformServiceProvider.Instance.FileOperations.LoadAssembly(source);

ParallelizeAttribute? parallelizeAttribute = ReflectHelper.GetParallelizeAttribute(testAssembly);
IReflectionOperations reflectionOperations = PlatformServiceProvider.Instance.ReflectionOperations;
ParallelizeAttribute? parallelizeAttribute = reflectionOperations.GetCustomAttributes(testAssembly, typeof(ParallelizeAttribute))
.OfType<ParallelizeAttribute>()
.FirstOrDefault();

if (parallelizeAttribute != null)
{
Expand All @@ -47,7 +50,7 @@ internal TestAssemblySettings GetSettings(string source)
}
}

testAssemblySettings.CanParallelizeAssembly = !ReflectHelper.IsDoNotParallelizeSet(testAssembly);
testAssemblySettings.CanParallelizeAssembly = reflectionOperations.GetCustomAttributes(testAssembly, typeof(DoNotParallelizeAttribute)).Length == 0;

return testAssemblySettings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal TestMethodInfo(MethodInfo testMethod, TestClassInfo parent)
/// </summary>
/// <returns>An array of the attributes.</returns>
public Attribute[]? GetAllAttributes()
=> [.. ReflectHelper.Instance.GetAttributes<Attribute>(MethodInfo)];
=> [.. PlatformServiceProvider.Instance.ReflectionOperations.GetAttributes<Attribute>(MethodInfo)];

/// <summary>
/// Gets all attributes of the test method.
Expand All @@ -111,7 +111,7 @@ internal TestMethodInfo(MethodInfo testMethod, TestClassInfo parent)
/// <returns>An array of the attributes.</returns>
public TAttributeType[] GetAttributes<TAttributeType>()
where TAttributeType : Attribute
=> [.. ReflectHelper.Instance.GetAttributes<TAttributeType>(MethodInfo)];
=> [.. PlatformServiceProvider.Instance.ReflectionOperations.GetAttributes<TAttributeType>(MethodInfo)];

/// <summary>
/// Execute test method. Capture failures, handle async and return result.
Expand Down Expand Up @@ -246,7 +246,7 @@ public virtual async Task<TestResult> InvokeAsync(object?[]? arguments)
private TimeoutInfo GetTestTimeout()
{
DebugEx.Assert(MethodInfo != null, "TestMethod should be non-null");
TimeoutAttribute? timeoutAttribute = ReflectHelper.Instance.GetFirstAttributeOrDefault<TimeoutAttribute>(MethodInfo);
TimeoutAttribute? timeoutAttribute = PlatformServiceProvider.Instance.ReflectionOperations.GetFirstAttributeOrDefault<TimeoutAttribute>(MethodInfo);
if (timeoutAttribute is null)
{
return TimeoutInfo.FromTestTimeoutSettings();
Expand All @@ -269,7 +269,7 @@ private TestMethodAttribute GetTestMethodAttribute()
{
// Get the derived TestMethod attribute from reflection.
// It should be non-null as it was already validated by IsValidTestMethod.
TestMethodAttribute testMethodAttribute = ReflectHelper.Instance.GetSingleAttributeOrDefault<TestMethodAttribute>(MethodInfo)!;
TestMethodAttribute testMethodAttribute = PlatformServiceProvider.Instance.ReflectionOperations.GetSingleAttributeOrDefault<TestMethodAttribute>(MethodInfo)!;

// Get the derived TestMethod attribute from Extended TestClass Attribute
// If the extended TestClass Attribute doesn't have extended TestMethod attribute then base class returns back the original testMethod Attribute
Expand All @@ -285,7 +285,7 @@ private TestMethodAttribute GetTestMethodAttribute()
/// </returns>
private RetryBaseAttribute? GetRetryAttribute()
{
IEnumerable<RetryBaseAttribute> attributes = ReflectHelper.Instance.GetAttributes<RetryBaseAttribute>(MethodInfo);
IEnumerable<RetryBaseAttribute> attributes = PlatformServiceProvider.Instance.ReflectionOperations.GetAttributes<RetryBaseAttribute>(MethodInfo);
using IEnumerator<RetryBaseAttribute> enumerator = attributes.GetEnumerator();
if (!enumerator.MoveNext())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Extensions;

using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers;
using Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.ObjectModel;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices;
Expand Down Expand Up @@ -191,7 +190,7 @@ private async Task<bool> TryExecuteDataSourceBasedTestsAsync(List<TestResult> re
private async Task<bool> TryExecuteFoldedDataDrivenTestsAsync(List<TestResult> results)
{
bool hasTestDataSource = false;
foreach (Attribute attribute in ReflectHelper.Instance.GetCustomAttributesCached(_testMethodInfo.MethodInfo))
foreach (Attribute attribute in PlatformServiceProvider.Instance.ReflectionOperations.GetCustomAttributesCached(_testMethodInfo.MethodInfo))
{
if (attribute is not UTF.ITestDataSource testDataSource)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/// <param name="settings"> Specifies adapter settings that need to be instantiated in the domain running these tests. </param>
/// <param name="testsToRun"> The tests to run. </param>
public UnitTestRunner(MSTestSettings settings, UnitTestElement[] testsToRun)
: this(settings, testsToRun, ReflectHelper.Instance)
: this(settings, testsToRun, PlatformServiceProvider.Instance.ReflectionOperations)
{
}

Expand All @@ -45,8 +45,8 @@
/// </summary>
/// <param name="settings"> Specifies adapter settings. </param>
/// <param name="testsToRun"> The tests to run. </param>
/// <param name="reflectHelper"> The reflect Helper. </param>
internal UnitTestRunner(MSTestSettings settings, UnitTestElement[] testsToRun, ReflectHelper reflectHelper)
/// <param name="reflectionOperation"> The reflect Helper. </param>
internal UnitTestRunner(MSTestSettings settings, UnitTestElement[] testsToRun, IReflectionOperations reflectionOperation)
{
// Populate the settings into the domain(Desktop workflow) performing discovery.
// This would just be resetting the settings to itself in non desktop workflows.
Expand All @@ -65,7 +65,7 @@
}

PlatformServiceProvider.Instance.TestRunCancellationToken ??= new TestRunCancellationToken();
_typeCache = new TypeCache(reflectHelper);
_typeCache = new TypeCache(reflectionOperation);

Check failure on line 68 in src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Release)

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs#L68

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs(68,36): error CS1503: (NETCORE_ENGINEERING_TELEMETRY=Build) Argument 1: cannot convert from 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.IReflectionOperations' to 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers.ReflectHelper'

Check failure on line 68 in src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build Linux Debug)

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs#L68

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs(68,36): error CS1503: (NETCORE_ENGINEERING_TELEMETRY=Build) Argument 1: cannot convert from 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.IReflectionOperations' to 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers.ReflectHelper'

Check failure on line 68 in src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Debug)

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs#L68

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs(68,36): error CS1503: (NETCORE_ENGINEERING_TELEMETRY=Build) Argument 1: cannot convert from 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.IReflectionOperations' to 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers.ReflectHelper'

Check failure on line 68 in src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx (Build MacOS Release)

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs#L68

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs(68,36): error CS1503: (NETCORE_ENGINEERING_TELEMETRY=Build) Argument 1: cannot convert from 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.IReflectionOperations' to 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers.ReflectHelper'

Check failure on line 68 in src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs

View check run for this annotation

Azure Pipelines / microsoft.testfx

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs#L68

src/Adapter/MSTestAdapter.PlatformServices/Execution/UnitTestRunner.cs(68,36): error CS1503: (NETCORE_ENGINEERING_TELEMETRY=Build) Argument 1: cannot convert from 'Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Interface.IReflectionOperations' to 'Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Helpers.ReflectHelper'

_classCleanupManager = new ClassCleanupManager(testsToRun);
}
Expand Down
Loading
Loading