Skip to content

Commit

Permalink
Properly call IRunListener.specSkipped and .featureSkipped (#1811)
Browse files Browse the repository at this point in the history
fixes #1662
  • Loading branch information
leonard84 authored Oct 12, 2023
1 parent 03b1d6c commit 538f3e7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public SkipResult shouldBeSkipped(SpockExecutionContext context) throws Exceptio
return shouldBeSkipped(getNodeInfo());
}

@Override
public void nodeSkipped(SpockExecutionContext context, TestDescriptor testDescriptor, SkipResult result) {
context.getRunner().supervisor.featureSkipped(getNodeInfo());
}

@Override
public void around(SpockExecutionContext context, Invocation<SpockExecutionContext> invocation) {
ErrorInfoCollector errorInfoCollector = new ErrorInfoCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ public void runFeature(SpockExecutionContext context, Runnable feature) {
if (currentFeature.isExcluded()) return;

if (currentFeature.isSkipped()) {
supervisor.featureSkipped(currentFeature); // todo notify in node isSkipped
return;
throw new InternalSpockError("Invalid state, feature is executed although it should have been skipped");
}

supervisor.beforeFeature(currentFeature);
Expand Down
13 changes: 10 additions & 3 deletions spock-core/src/main/java/org/spockframework/runtime/SpecNode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.spockframework.runtime;

import org.junit.platform.engine.TestDescriptor;
import org.spockframework.runtime.model.SpecInfo;
import spock.config.RunnerConfiguration;

Expand All @@ -21,14 +22,15 @@ public Type getType() {

@Override
public SpockExecutionContext prepare(SpockExecutionContext context) throws Exception {
PlatformParameterizedSpecRunner specRunner = context.getRunContext().createSpecRunner(getNodeInfo());
context = context.withRunner(specRunner).withSpec(getNodeInfo());
if (getNodeInfo().isSkipped()) {
// Node.prepare is called before Node.shouldBeSkipped so we just skip the shared spec initialization
// Node.prepare is called before Node.shouldBeSkipped, so we just skip the shared spec initialization.
// We still set the runner, as we need it in the nodeSkipped callback.
return context;
}
PlatformParameterizedSpecRunner specRunner = context.getRunContext().createSpecRunner(getNodeInfo());
ErrorInfoCollector errorInfoCollector = new ErrorInfoCollector();
context = context.withErrorInfoCollector(errorInfoCollector);
context = context.withRunner(specRunner).withSpec(getNodeInfo());
context = specRunner.runSharedSpec(context);
errorInfoCollector.assertEmpty();
return context;
Expand All @@ -39,6 +41,11 @@ public SkipResult shouldBeSkipped(SpockExecutionContext context) throws Exceptio
return shouldBeSkipped(getNodeInfo());
}

@Override
public void nodeSkipped(SpockExecutionContext context, TestDescriptor testDescriptor, SkipResult result) {
context.getRunner().supervisor.specSkipped(getNodeInfo());
}

@Override
public SpockExecutionContext before(SpockExecutionContext context) throws Exception {
ErrorInfoCollector errorInfoCollector = new ErrorInfoCollector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ package org.spockframework.runtime
import org.spockframework.EmbeddedSpecification
import org.spockframework.runtime.extension.*
import org.spockframework.runtime.model.SpecInfo
import org.spockframework.runtime.model.parallel.ExecutionMode
import spock.lang.Execution
import spock.lang.Ignore
import spock.lang.Issue
import spock.lang.Specification

import java.lang.annotation.*

@Execution(
value = ExecutionMode.SAME_THREAD,
reason = "Uses a shared field on a delegate"
)
class RunListenerSpec extends EmbeddedSpecification {

IRunListener runListener = Mock()
Expand Down Expand Up @@ -45,6 +53,73 @@ class ASpec extends Specification {
cleanup:
RunListenerDelegate.delegate = null
}

def "IRunListener is called for skipped features"() {
given:
RunListenerDelegate.delegate = runListener
runner.addPackageImport(Specification.package)
runner.addClassImport(RegisterRunListener)
runner.addClassImport(Ignore)

when:
runner.runWithImports '''
@RegisterRunListener
class ASpec extends Specification {
@Ignore
def "a test"() {
expect: true
}
@Ignore
def "data driven test"() {
expect: true
where:
i << [1, 2]
}
}
'''


then:
1 * runListener.beforeSpec(_)
then:
2 * runListener.featureSkipped(_)
then:
1 * runListener.afterSpec(_)
then:
0 * runListener._

cleanup:
RunListenerDelegate.delegate = null
}

@Issue("https://github.com/spockframework/spock/issues/1662")
def "IRunListener is called for skipped specs"() {
given:
RunListenerDelegate.delegate = runListener
runner.addPackageImport(Specification.package)
runner.addClassImport(RegisterRunListener)
runner.addClassImport(Ignore)

when:
runner.runWithImports '''
@RegisterRunListener
@Ignore
class ASpec extends Specification {
def "a test"() {
expect: true
}
}
'''

then:
1 * runListener.specSkipped(_)
then:
0 * runListener._

cleanup:
RunListenerDelegate.delegate = null
}
}

@Target(ElementType.TYPE)
Expand Down

0 comments on commit 538f3e7

Please sign in to comment.