Skip to content

Commit 538f3e7

Browse files
authored
Properly call IRunListener.specSkipped and .featureSkipped (#1811)
fixes #1662
1 parent 03b1d6c commit 538f3e7

File tree

4 files changed

+91
-5
lines changed

4 files changed

+91
-5
lines changed

spock-core/src/main/java/org/spockframework/runtime/FeatureNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public SkipResult shouldBeSkipped(SpockExecutionContext context) throws Exceptio
3939
return shouldBeSkipped(getNodeInfo());
4040
}
4141

42+
@Override
43+
public void nodeSkipped(SpockExecutionContext context, TestDescriptor testDescriptor, SkipResult result) {
44+
context.getRunner().supervisor.featureSkipped(getNodeInfo());
45+
}
46+
4247
@Override
4348
public void around(SpockExecutionContext context, Invocation<SpockExecutionContext> invocation) {
4449
ErrorInfoCollector errorInfoCollector = new ErrorInfoCollector();

spock-core/src/main/java/org/spockframework/runtime/PlatformSpecRunner.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ public void runFeature(SpockExecutionContext context, Runnable feature) {
184184
if (currentFeature.isExcluded()) return;
185185

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

191190
supervisor.beforeFeature(currentFeature);

spock-core/src/main/java/org/spockframework/runtime/SpecNode.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.spockframework.runtime;
22

3+
import org.junit.platform.engine.TestDescriptor;
34
import org.spockframework.runtime.model.SpecInfo;
45
import spock.config.RunnerConfiguration;
56

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

2223
@Override
2324
public SpockExecutionContext prepare(SpockExecutionContext context) throws Exception {
25+
PlatformParameterizedSpecRunner specRunner = context.getRunContext().createSpecRunner(getNodeInfo());
26+
context = context.withRunner(specRunner).withSpec(getNodeInfo());
2427
if (getNodeInfo().isSkipped()) {
25-
// Node.prepare is called before Node.shouldBeSkipped so we just skip the shared spec initialization
28+
// Node.prepare is called before Node.shouldBeSkipped, so we just skip the shared spec initialization.
29+
// We still set the runner, as we need it in the nodeSkipped callback.
2630
return context;
2731
}
28-
PlatformParameterizedSpecRunner specRunner = context.getRunContext().createSpecRunner(getNodeInfo());
2932
ErrorInfoCollector errorInfoCollector = new ErrorInfoCollector();
3033
context = context.withErrorInfoCollector(errorInfoCollector);
31-
context = context.withRunner(specRunner).withSpec(getNodeInfo());
3234
context = specRunner.runSharedSpec(context);
3335
errorInfoCollector.assertEmpty();
3436
return context;
@@ -39,6 +41,11 @@ public SkipResult shouldBeSkipped(SpockExecutionContext context) throws Exceptio
3941
return shouldBeSkipped(getNodeInfo());
4042
}
4143

44+
@Override
45+
public void nodeSkipped(SpockExecutionContext context, TestDescriptor testDescriptor, SkipResult result) {
46+
context.getRunner().supervisor.specSkipped(getNodeInfo());
47+
}
48+
4249
@Override
4350
public SpockExecutionContext before(SpockExecutionContext context) throws Exception {
4451
ErrorInfoCollector errorInfoCollector = new ErrorInfoCollector();

spock-specs/src/test/groovy/org/spockframework/runtime/RunListenerSpec.groovy

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ package org.spockframework.runtime
33
import org.spockframework.EmbeddedSpecification
44
import org.spockframework.runtime.extension.*
55
import org.spockframework.runtime.model.SpecInfo
6+
import org.spockframework.runtime.model.parallel.ExecutionMode
7+
import spock.lang.Execution
8+
import spock.lang.Ignore
9+
import spock.lang.Issue
610
import spock.lang.Specification
711

812
import java.lang.annotation.*
913

14+
@Execution(
15+
value = ExecutionMode.SAME_THREAD,
16+
reason = "Uses a shared field on a delegate"
17+
)
1018
class RunListenerSpec extends EmbeddedSpecification {
1119

1220
IRunListener runListener = Mock()
@@ -45,6 +53,73 @@ class ASpec extends Specification {
4553
cleanup:
4654
RunListenerDelegate.delegate = null
4755
}
56+
57+
def "IRunListener is called for skipped features"() {
58+
given:
59+
RunListenerDelegate.delegate = runListener
60+
runner.addPackageImport(Specification.package)
61+
runner.addClassImport(RegisterRunListener)
62+
runner.addClassImport(Ignore)
63+
64+
when:
65+
runner.runWithImports '''
66+
@RegisterRunListener
67+
class ASpec extends Specification {
68+
@Ignore
69+
def "a test"() {
70+
expect: true
71+
}
72+
73+
@Ignore
74+
def "data driven test"() {
75+
expect: true
76+
where:
77+
i << [1, 2]
78+
}
79+
}
80+
'''
81+
82+
83+
then:
84+
1 * runListener.beforeSpec(_)
85+
then:
86+
2 * runListener.featureSkipped(_)
87+
then:
88+
1 * runListener.afterSpec(_)
89+
then:
90+
0 * runListener._
91+
92+
cleanup:
93+
RunListenerDelegate.delegate = null
94+
}
95+
96+
@Issue("https://github.com/spockframework/spock/issues/1662")
97+
def "IRunListener is called for skipped specs"() {
98+
given:
99+
RunListenerDelegate.delegate = runListener
100+
runner.addPackageImport(Specification.package)
101+
runner.addClassImport(RegisterRunListener)
102+
runner.addClassImport(Ignore)
103+
104+
when:
105+
runner.runWithImports '''
106+
@RegisterRunListener
107+
@Ignore
108+
class ASpec extends Specification {
109+
def "a test"() {
110+
expect: true
111+
}
112+
}
113+
'''
114+
115+
then:
116+
1 * runListener.specSkipped(_)
117+
then:
118+
0 * runListener._
119+
120+
cleanup:
121+
RunListenerDelegate.delegate = null
122+
}
48123
}
49124

50125
@Target(ElementType.TYPE)

0 commit comments

Comments
 (0)