Skip to content

Commit

Permalink
Add interceptors that catch assumption errors
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes authored and rtretyak committed Jun 3, 2020
1 parent dac23cd commit 0163146
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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() )
Expand All @@ -271,5 +304,4 @@ class SpecInfoListener implements IRunListener {
private static FeatureInfo dummyFeature() {
new FeatureInfo( name: '<No Feature initialized!>' )
}

}

0 comments on commit 0163146

Please sign in to comment.