Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle case when underlying step returns Task<T> masked as non-generic Task #343

Merged
merged 1 commit into from
May 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions Allure.NUnit/Core/Steps/AllureStepAspect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class AllureStepAspect
private static readonly MethodInfo AsyncHandler =
typeof(AllureStepAspect).GetMethod(nameof(WrapAsync), BindingFlags.NonPublic | BindingFlags.Static);

private static readonly MethodInfo AsyncGenericHandler =
typeof(AllureStepAspect).GetMethod(nameof(WrapAsyncGeneric), BindingFlags.NonPublic | BindingFlags.Static);

private static readonly MethodInfo SyncHandler =
typeof(AllureStepAspect).GetMethod(nameof(WrapSync), BindingFlags.NonPublic | BindingFlags.Static);

Expand All @@ -42,10 +45,15 @@ public object Around(

if (_typeTask.IsAssignableFrom(returnType))
{
if (returnType == _typeTask)
{
return AsyncHandler.Invoke(this, new object[] { target, args, metadata, stepName, stepParameters });
}

var syncResultType = returnType.IsConstructedGenericType
? returnType.GenericTypeArguments[0]
: _typeVoidTaskResult;
return AsyncHandler.MakeGenericMethod(syncResultType)
return AsyncGenericHandler.MakeGenericMethod(syncResultType)
.Invoke(this, new object[] { target, args, metadata, stepName, stepParameters });
}
else if (_typeVoid.IsAssignableFrom(returnType))
Expand Down Expand Up @@ -244,7 +252,30 @@ List<Parameter> stepParameters
}
}

private static async Task<T> WrapAsync<T>(
private static async Task WrapAsync(
Func<object[], object> target,
object[] args,
MethodBase metadata,
string stepName,
List<Parameter> stepParameters
)
{
string stepUuid = null;

try
{
stepUuid = BeforeTargetInvoke(metadata, stepName, stepParameters);
await ((Task)target(args)).ConfigureAwait(false);
AfterTargetInvoke(stepUuid, metadata);
}
catch (Exception e)
{
OnTargetInvokeException(stepUuid, metadata, e);
throw;
}
}

private static async Task<T> WrapAsyncGeneric<T>(
Func<object[], object> target,
object[] args,
MethodBase metadata,
Expand Down
Loading