Skip to content

Commit

Permalink
Add interceptors that catch assumption errors
Browse files Browse the repository at this point in the history
Related to #89
  • Loading branch information
renatoathaydes authored and rtretyak committed Jun 3, 2020
1 parent e09e2e1 commit 052d826
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ To enable this Spock extension, you only need to declare a dependency to it (if

Spock-reports is available on Maven Central and on JCenter.

> Since version 1.3.2, Spock version 1.1+ is required.
> From version 1.7.0, Spock 1.2-groovy-2.5 or newer should be used.
> If you use Java 9+, use the latest versions of both Spock and spock-reports.
> The versions below work with the new Spock 2 / Groovy 3 versions.
> If you want to stay with Spock 1 and Groovy 2.5.x, use spock-reports version 1.7.1.
> Spock-reports should work fine with Java 9+ since version 1.7.1.
If you want to add information to your Spock-reports programmatically, since version 1.4.0, you can use the following
`Specification` class' extension methods which are added by Spock Reports:
Expand All @@ -60,10 +60,6 @@ def "My feature"() {

### If you are using Maven

> NOTE about Spock/Groovy versions:
> The versions below work with the new Spock 2 / Groovy 3 versions.
> If you want to stay with Spock 1 and Groovy 2.5.x, use spock-reports version 1.7.1.
Add ``spock-reports`` to your ``<dependencies>``:

```xml
Expand Down
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 052d826

Please sign in to comment.