Fixed error with reflection on integration test configure builder attributes, so integration tests can be created outside of the Umbraco integration test project (#19077)

* Fixed error with reflection on integration test configure builder attributes, so integration tests can be created outside of the Umbraco integration test project.

* Fix nullability

---------

Co-authored-by: mole <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Andy Butland
2025-04-24 13:18:29 +02:00
committed by GitHub
parent cb1ec988ce
commit b4528cf963

View File

@@ -189,25 +189,48 @@ public abstract class UmbracoIntegrationTest : UmbracoIntegrationTestBase
private void ExecuteBuilderAttributes(IUmbracoBuilder builder)
{
// todo better errors
Type? testClassType = GetTestClassType()
?? throw new Exception($"Could not find test class for {TestContext.CurrentContext.Test.FullName} in order to execute builder attributes.");
// execute builder attributes defined on method
foreach (ConfigureBuilderAttribute builderAttribute in Type.GetType(TestContext.CurrentContext.Test.ClassName)
.GetMethods().First(m => m.Name == TestContext.CurrentContext.Test.MethodName)
.GetCustomAttributes(typeof(ConfigureBuilderAttribute), true))
// Execute builder attributes defined on method.
foreach (ConfigureBuilderAttribute builderAttribute in GetConfigureBuilderAttributes<ConfigureBuilderAttribute>(testClassType))
{
builderAttribute.Execute(builder);
}
// execute builder attributes defined on method with param value passtrough from testcase
foreach (ConfigureBuilderTestCaseAttribute builderAttribute in Type.GetType(TestContext.CurrentContext.Test.ClassName)
.GetMethods().First(m => m.Name == TestContext.CurrentContext.Test.MethodName)
.GetCustomAttributes(typeof(ConfigureBuilderTestCaseAttribute), true))
// Execute builder attributes defined on method with param value pass through from test case.
foreach (ConfigureBuilderTestCaseAttribute builderAttribute in GetConfigureBuilderAttributes<ConfigureBuilderTestCaseAttribute>(testClassType))
{
builderAttribute.Execute(builder);
}
}
private static Type? GetTestClassType()
{
string testClassName = TestContext.CurrentContext.Test.ClassName;
// Try resolving the type name directly (which will work for tests in this assembly).
Type testClass = Type.GetType(testClassName);
if (testClass is not null)
{
return testClass;
}
// Try scanning the loaded assemblies to see if we can find the class by full name. This will be necessary
// for integration test projects using the base classess provided by Umbraco.
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return assemblies
.SelectMany(a => a.GetTypes().Where(t => t.FullName == testClassName))
.FirstOrDefault();
}
private static IEnumerable<TAttribute> GetConfigureBuilderAttributes<TAttribute>(Type testClassType)
where TAttribute : Attribute =>
testClassType
.GetMethods().First(m => m.Name == TestContext.CurrentContext.Test.MethodName)
.GetCustomAttributes(typeof(TAttribute), true)
.Cast<TAttribute>();
/// <summary>
/// Hook for altering UmbracoBuilder setup
/// </summary>