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

Add interceptors that catch assumption errors #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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!>' )
}

}