From 0163146695182507aaf774d386b79018d0f8d566 Mon Sep 17 00:00:00 2001 From: renatoathaydes Date: Fri, 15 May 2020 20:04:04 +0200 Subject: [PATCH] Add interceptors that catch assumption errors Related to #89 --- .../report/SpockReportExtension.groovy | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/com/athaydes/spockframework/report/SpockReportExtension.groovy b/src/main/groovy/com/athaydes/spockframework/report/SpockReportExtension.groovy index 0f72750..7e36bdd 100644 --- a/src/main/groovy/com/athaydes/spockframework/report/SpockReportExtension.groovy +++ b/src/main/groovy/com/athaydes/spockframework/report/SpockReportExtension.groovy @@ -11,8 +11,12 @@ import com.athaydes.spockframework.report.internal.SpecProblem import com.athaydes.spockframework.report.internal.SpockReportsConfiguration import com.athaydes.spockframework.report.util.Utils import groovy.util.logging.Slf4j +import org.junit.AssumptionViolatedException +import org.opentest4j.IncompleteExecutionException import org.spockframework.runtime.IRunListener import org.spockframework.runtime.extension.IGlobalExtension +import org.spockframework.runtime.extension.IMethodInterceptor +import org.spockframework.runtime.extension.IMethodInvocation import org.spockframework.runtime.model.ErrorInfo import org.spockframework.runtime.model.FeatureInfo import org.spockframework.runtime.model.IterationInfo @@ -75,7 +79,11 @@ class SpockReportExtension implements IGlobalExtension { @Override void visitSpec( SpecInfo specInfo ) { if ( reportCreator != null ) { - specInfo.addListener createListener() + def listener = createListener() + def abortionInterceptor = new AbortionInterceptor(listener) + specInfo.addListener listener + specInfo.allFeatures*.getFeatureMethod().each { it.addInterceptor(abortionInterceptor) } + specInfo.allFixtureMethods.each { it.addInterceptor(abortionInterceptor) } } else { log.warn "Not creating report for ${ specInfo.name } as reportCreator is null" } @@ -107,6 +115,24 @@ class SpockReportExtension implements IGlobalExtension { } +class AbortionInterceptor implements IMethodInterceptor { + SpecInfoListener listener + + AbortionInterceptor(SpecInfoListener listener) { + this.listener = listener + } + + @Override + void intercept(IMethodInvocation invocation) throws Throwable { + try { + invocation.proceed() + } catch( AssumptionViolatedException | IncompleteExecutionException t ) { + listener.executionAborted new ErrorInfo( invocation.method, t ) + throw t + } + } +} + @Slf4j class SpecInfoListener implements IRunListener { @@ -248,6 +274,13 @@ class SpecInfoListener implements IRunListener { // feature already knows if it's skipped } + void executionAborted( ErrorInfo error ) { + //properly handle aborted iteration or spec depending on ${error.method.kind} + //MethodKind.FEATURE for aborted features/iterations + //MethodKind.SETUP_SPEC for aborted setupSpec + //${error.error} holds the reason of abortion + } + private FeatureRun currentRun() { if ( specData.featureRuns.empty ) { specData.featureRuns.add new FeatureRun( feature: specData.info.features?.first() ?: dummyFeature() ) @@ -271,5 +304,4 @@ class SpecInfoListener implements IRunListener { private static FeatureInfo dummyFeature() { new FeatureInfo( name: '' ) } - }