Skip to content

Commit

Permalink
Fix tests and PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
nohwnd committed Nov 20, 2024
1 parent 0b81e85 commit 897adc4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Adapter/MSTest.TestAdapter/Execution/TypeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private static bool TryGetUnescapedManagedTypeName(TestMethod testMethod, [NotNu
private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)
{
IEnumerable<ConstructorInfo> constructors = PlatformServiceProvider.Instance.ReflectionOperations.GetDeclaredConstructors(classType);
(ConstructorInfo? CtorInfo, bool IsParameterless)? selectedConstructor = null;
(ConstructorInfo CtorInfo, bool IsParameterless)? selectedConstructor = null;

foreach (ConstructorInfo ctor in constructors)
{
Expand All @@ -272,19 +272,20 @@ private TestClassInfo CreateClassInfo(Type classType, TestMethod testMethod)
ParameterInfo[] parameters = ctor.GetParameters();

// There are just 2 ctor shapes that we know, so the code is quite simple,
// but if we add more, add a priority to the search, and shortcircuit this search so we only iterate
// but if we add more, add a priority to the search, and short-circuit this search so we only iterate
// through the collection once, to avoid re-allocating GetParameters multiple times.
if (parameters.Length == 1 && parameters[0].ParameterType == typeof(TestContext))
{
selectedConstructor ??= (ctor, IsParameterless: false);
selectedConstructor = (ctor, IsParameterless: false);

// This is the preferred constructor, no point in searching for more.
break;
}

if (parameters.Length == 0)
{
selectedConstructor = (ctor, IsParameterless: true);
// Otherwise take the first parameterless constructor we can find.
selectedConstructor ??= (ctor, IsParameterless: true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ public void RunTestsForTestShouldNotRunTestsInParallelWhenDisabledFromRunsetting
var originalReflectionOperation = new ReflectionOperations2();
var originalFileOperation = new FileOperations();

testablePlatformService.MockReflectionOperations.Setup(ro => ro.GetDeclaredConstructors(It.IsAny<Type>()))
.Returns((Type classType) => originalReflectionOperation.GetDeclaredConstructors(classType));

testablePlatformService.MockReflectionOperations.Setup(
ro => ro.GetCustomAttributes(It.IsAny<Assembly>(), It.IsAny<Type>())).
Returns((Assembly asm, Type type) => type.FullName.Equals(typeof(ParallelizeAttribute).FullName, StringComparison.Ordinal)
Expand Down Expand Up @@ -632,6 +635,9 @@ public void RunTestsForTestShouldNotRunTestsInParallelWhenDisabledFromSource()
var originalReflectionOperation = new ReflectionOperations2();
var originalFileOperation = new FileOperations();

testablePlatformService.MockReflectionOperations.Setup(ro => ro.GetDeclaredConstructors(It.IsAny<Type>()))
.Returns((Type classType) => originalReflectionOperation.GetDeclaredConstructors(classType));

testablePlatformService.MockReflectionOperations.Setup(
ro => ro.GetCustomAttributes(It.IsAny<Assembly>(), It.IsAny<Type>())).
Returns((Assembly asm, Type type) => type.FullName.Equals(typeof(DoNotParallelizeAttribute).FullName, StringComparison.Ordinal)
Expand Down Expand Up @@ -730,6 +736,9 @@ public void RunTestsForTestShouldPreferParallelSettingsFromRunSettingsOverAssemb
var originalReflectionOperation = new ReflectionOperations2();
var originalFileOperation = new FileOperations();

testablePlatformService.MockReflectionOperations.Setup(ro => ro.GetDeclaredConstructors(It.IsAny<Type>()))
.Returns((Type classType) => originalReflectionOperation.GetDeclaredConstructors(classType));

testablePlatformService.MockReflectionOperations.Setup(
ro => ro.GetCustomAttributes(It.IsAny<Assembly>(), It.IsAny<Type>())).
Returns((Assembly asm, Type type) => type.FullName.Equals(typeof(ParallelizeAttribute).FullName, StringComparison.Ordinal)
Expand Down

0 comments on commit 897adc4

Please sign in to comment.