Skip to content

Commit

Permalink
convert some properties to fields (#3752)
Browse files Browse the repository at this point in the history
Co-authored-by: Jakub Jareš <me@jakubjares.com>
  • Loading branch information
SimonCropp and nohwnd committed Sep 9, 2024
1 parent 5cf2737 commit 6e86d38
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
11 changes: 4 additions & 7 deletions src/Adapter/MSTest.TestAdapter/Discovery/UnitTestDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ internal class UnitTestDiscoverer
internal UnitTestDiscoverer()
{
_assemblyEnumeratorWrapper = new AssemblyEnumeratorWrapper();
TestMethodFilter = new TestMethodFilter();
_testMethodFilter = new TestMethodFilter();
}

/// <summary>
/// Gets or sets method filter for filtering tests.
/// </summary>
private TestMethodFilter TestMethodFilter { get; set; }
private readonly TestMethodFilter _testMethodFilter;

/// <summary>
/// Discovers the tests available from the provided sources.
Expand Down Expand Up @@ -103,7 +100,7 @@ internal void SendTestCases(string source, IEnumerable<UnitTestElement> testElem
}

// Get filter expression and skip discovery in case filter expression has parsing error.
ITestCaseFilterExpression? filterExpression = TestMethodFilter.GetFilterExpression(discoveryContext, logger, out bool filterHasError);
ITestCaseFilterExpression? filterExpression = _testMethodFilter.GetFilterExpression(discoveryContext, logger, out bool filterHasError);
if (filterHasError)
{
return;
Expand All @@ -115,7 +112,7 @@ internal void SendTestCases(string source, IEnumerable<UnitTestElement> testElem
bool hasFixtureTraits = testCase.Traits.Any(t => t.Name == Constants.FixturesTestTrait);

// Filter tests based on test case filters
if (filterExpression != null && !filterExpression.MatchTestCase(testCase, (p) => TestMethodFilter.PropertyValueProvider(testCase, p)))
if (filterExpression != null && !filterExpression.MatchTestCase(testCase, (p) => _testMethodFilter.PropertyValueProvider(testCase, p)))
{
// If test is a fixture test, add it to the list of fixture tests.
if (hasFixtureTraits)
Expand Down
16 changes: 8 additions & 8 deletions src/Adapter/MSTest.TestAdapter/Execution/TestExecutionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public TestExecutionManager()

internal TestExecutionManager(IEnvironment environment, Func<Action, Task>? taskFactory = null)
{
TestMethodFilter = new TestMethodFilter();
_testMethodFilter = new TestMethodFilter();
_sessionParameters = new Dictionary<string, object>();
_environment = environment;
_taskFactory = taskFactory ?? DefaultFactoryAsync;
Expand Down Expand Up @@ -78,12 +78,12 @@ private static Task DefaultFactoryAsync(Action action)
/// <summary>
/// Gets or sets method filter for filtering tests.
/// </summary>
private TestMethodFilter TestMethodFilter { get; set; }
private readonly TestMethodFilter _testMethodFilter;

/// <summary>
/// Gets or sets a value indicating whether any test executed has failed.
/// </summary>
private bool HasAnyTestFailed { get; set; }
private bool _hasAnyTestFailed;

/// <summary>
/// Runs the tests.
Expand All @@ -110,7 +110,7 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext? runContext, IFram
// Execute the tests
ExecuteTests(tests, runContext, frameworkHandle, isDeploymentDone);

if (!HasAnyTestFailed)
if (!_hasAnyTestFailed)
{
PlatformServiceProvider.Instance.TestDeployment.Cleanup();
}
Expand Down Expand Up @@ -148,7 +148,7 @@ public void RunTests(IEnumerable<string> sources, IRunContext? runContext, IFram
// Run tests.
ExecuteTests(tests, runContext, frameworkHandle, isDeploymentDone);

if (!HasAnyTestFailed)
if (!_hasAnyTestFailed)
{
PlatformServiceProvider.Instance.TestDeployment.Cleanup();
}
Expand Down Expand Up @@ -200,7 +200,7 @@ internal void SendTestResults(TestCase test, IEnumerable<UnitTestResult> unitTes
if (testResult.Outcome == TestOutcome.Failed)
{
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("MSTestExecutor:Test {0} failed. ErrorMessage:{1}, ErrorStackTrace:{2}.", testResult.TestCase.FullyQualifiedName, testResult.ErrorMessage, testResult.ErrorStackTrace);
HasAnyTestFailed = true;
_hasAnyTestFailed = true;
}

try
Expand Down Expand Up @@ -258,7 +258,7 @@ private void ExecuteTestsInSource(IEnumerable<TestCase> tests, IRunContext? runC
PlatformServiceProvider.Instance.AdapterTraceLogger.LogInfo("Created unit-test runner {0}", source);

// Default test set is filtered tests based on user provided filter criteria
ITestCaseFilterExpression? filterExpression = TestMethodFilter.GetFilterExpression(runContext, frameworkHandle, out bool filterHasError);
ITestCaseFilterExpression? filterExpression = _testMethodFilter.GetFilterExpression(runContext, frameworkHandle, out bool filterHasError);
if (filterHasError)
{
// Bail out without processing everything else below.
Expand Down Expand Up @@ -292,7 +292,7 @@ private void ExecuteTestsInSource(IEnumerable<TestCase> tests, IRunContext? runC
TestAssemblySettings sourceSettings = (sourceSettingsProvider != null) ? TestAssemblySettingsProvider.GetSettings(source) : new TestAssemblySettings();
int parallelWorkers = sourceSettings.Workers;
ExecutionScope parallelScope = sourceSettings.Scope;
TestCase[] testsToRun = tests.Where(t => MatchTestFilter(filterExpression, t, TestMethodFilter)).ToArray();
TestCase[] testsToRun = tests.Where(t => MatchTestFilter(filterExpression, t, _testMethodFilter)).ToArray();
UnitTestElement[] unitTestElements = testsToRun.Select(e => e.ToUnitTestElement(source)).ToArray();
// Create an instance of a type defined in adapter so that adapter gets loaded in the child app domain
var testRunner = (UnitTestRunner)isolationHost.CreateInstanceForType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.UnitTests.Execution;

public class TestMethodFilterTests : TestContainer
{
public TestMethodFilterTests() => TestMethodFilter = new TestMethodFilter();
private readonly TestMethodFilter _testMethodFilter;

private TestMethodFilter TestMethodFilter { get; set; }
public TestMethodFilterTests() => _testMethodFilter = new TestMethodFilter();

public void PropertyProviderForFullyQualifiedNamePropertyReturnFullyQualifiedNameTestProperty()
{
TestProperty property = TestMethodFilter.PropertyProvider("FullyQualifiedName");
TestProperty property = _testMethodFilter.PropertyProvider("FullyQualifiedName");
Verify(property.Label == "FullyQualifiedName");
}

public void PropertyProviderForClassNamePropertyReturnClassNameTestProperty()
{
TestProperty property = TestMethodFilter.PropertyProvider("ClassName");
TestProperty property = _testMethodFilter.PropertyProvider("ClassName");
Verify(property.Label == "ClassName");
}

public void PropertyProviderForNamePropertyReturnNameTestProperty()
{
TestProperty property = TestMethodFilter.PropertyProvider("Name");
TestProperty property = _testMethodFilter.PropertyProvider("Name");
Verify(property.Label == "Name");
}

public void PropertyProviderForTestCategoryPropertyReturnTestCategoryTestProperty()
{
TestProperty property = TestMethodFilter.PropertyProvider("TestCategory");
TestProperty property = _testMethodFilter.PropertyProvider("TestCategory");
Verify(property.Label == "TestCategory");
}

public void PropertyProviderForPriorityPropertyReturnPriorityTestProperty()
{
TestProperty property = TestMethodFilter.PropertyProvider("Priority");
TestProperty property = _testMethodFilter.PropertyProvider("Priority");
Verify(property.Label == "Priority");
}

public void PropertyProviderValueForInvalidTestCaseReturnsNull()
{
object result = TestMethodFilter.PropertyValueProvider(null, "Hello");
object result = _testMethodFilter.PropertyValueProvider(null, "Hello");
Verify(result is null);
}

Expand All @@ -63,7 +63,7 @@ public void PropertyProviderValueForInvalidPropertyNameReturnsNull()
string fullName = $"{type.FullName}.{"TestMethod"}";
TestCase testCase = new(fullName, MSTest.TestAdapter.Constants.ExecutorUri, Assembly.GetExecutingAssembly().FullName);

object result = TestMethodFilter.PropertyValueProvider(testCase, null);
object result = _testMethodFilter.PropertyValueProvider(testCase, null);
Verify(result is null);
}

Expand All @@ -73,7 +73,7 @@ public void PropertyProviderValueForSupportedPropertyNameWhichIsNotSetReturnsNul
string fullName = $"{type.FullName}.{"TestMethod"}";

TestCase testCase = new(fullName, MSTest.TestAdapter.Constants.ExecutorUri, Assembly.GetExecutingAssembly().FullName);
object result = TestMethodFilter.PropertyValueProvider(testCase, "Priority");
object result = _testMethodFilter.PropertyValueProvider(testCase, "Priority");
Verify(result is null);
}

Expand All @@ -84,14 +84,14 @@ public void PropertyProviderValueForValidTestAndSupportedPropertyNameReturnsValu

TestCase testCase = new(fullName, MSTest.TestAdapter.Constants.ExecutorUri, Assembly.GetExecutingAssembly().FullName);

object result = TestMethodFilter.PropertyValueProvider(testCase, "FullyQualifiedName");
object result = _testMethodFilter.PropertyValueProvider(testCase, "FullyQualifiedName");
Verify(fullName.Equals(result));
}

public void GetFilterExpressionForNullRunContextReturnsNull()
{
TestableTestExecutionRecorder recorder = new();
ITestCaseFilterExpression filterExpression = TestMethodFilter.GetFilterExpression(null, recorder, out bool filterHasError);
ITestCaseFilterExpression filterExpression = _testMethodFilter.GetFilterExpression(null, recorder, out bool filterHasError);

Verify(filterExpression is null);
Verify(!filterHasError);
Expand All @@ -102,7 +102,7 @@ public void GetFilterExpressionForValidRunContextReturnsValidTestCaseFilterExpre
TestableTestExecutionRecorder recorder = new();
var dummyFilterExpression = new TestableTestCaseFilterExpression();
TestableRunContext runContext = new(() => dummyFilterExpression);
ITestCaseFilterExpression filterExpression = TestMethodFilter.GetFilterExpression(runContext, recorder, out bool filterHasError);
ITestCaseFilterExpression filterExpression = _testMethodFilter.GetFilterExpression(runContext, recorder, out bool filterHasError);

Verify(dummyFilterExpression == filterExpression);
Verify(!filterHasError);
Expand All @@ -116,7 +116,7 @@ public void GetFilterExpressionForDiscoveryContextWithGetTestCaseFilterReturnsVa
TestableTestExecutionRecorder recorder = new();
var dummyFilterExpression = new TestableTestCaseFilterExpression();
TestableDiscoveryContextWithGetTestCaseFilter discoveryContext = new(() => dummyFilterExpression);
ITestCaseFilterExpression filterExpression = TestMethodFilter.GetFilterExpression(discoveryContext, recorder, out bool filterHasError);
ITestCaseFilterExpression filterExpression = _testMethodFilter.GetFilterExpression(discoveryContext, recorder, out bool filterHasError);

Verify(dummyFilterExpression == filterExpression);
Verify(!filterHasError);
Expand All @@ -129,7 +129,7 @@ public void GetFilterExpressionForDiscoveryContextWithoutGetTestCaseFilterReturn
{
TestableTestExecutionRecorder recorder = new();
TestableDiscoveryContextWithoutGetTestCaseFilter discoveryContext = new();
ITestCaseFilterExpression filterExpression = TestMethodFilter.GetFilterExpression(discoveryContext, recorder, out bool filterHasError);
ITestCaseFilterExpression filterExpression = _testMethodFilter.GetFilterExpression(discoveryContext, recorder, out bool filterHasError);

Verify(filterExpression is null);
Verify(!filterHasError);
Expand All @@ -139,7 +139,7 @@ public void GetFilterExpressionForRunContextGetTestCaseFilterThrowingExceptionRe
{
TestableTestExecutionRecorder recorder = new();
TestableRunContext runContext = new(() => throw new TestPlatformFormatException("DummyException"));
ITestCaseFilterExpression filterExpression = TestMethodFilter.GetFilterExpression(runContext, recorder, out bool filterHasError);
ITestCaseFilterExpression filterExpression = _testMethodFilter.GetFilterExpression(runContext, recorder, out bool filterHasError);

Verify(filterExpression is null);
Verify(filterHasError);
Expand All @@ -154,7 +154,7 @@ public void GetFilterExpressionForDiscoveryContextWithGetTestCaseFilterThrowingE
{
TestableTestExecutionRecorder recorder = new();
TestableDiscoveryContextWithGetTestCaseFilter discoveryContext = new(() => throw new TestPlatformFormatException("DummyException"));
ITestCaseFilterExpression filterExpression = TestMethodFilter.GetFilterExpression(discoveryContext, recorder, out bool filterHasError);
ITestCaseFilterExpression filterExpression = _testMethodFilter.GetFilterExpression(discoveryContext, recorder, out bool filterHasError);

Verify(filterExpression is null);
Verify(filterHasError);
Expand Down

0 comments on commit 6e86d38

Please sign in to comment.