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

Fix compile error with single explicit assert in switch expression branch (#1845) #2033

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ include::include.adoc[]
* Fix mocking issue with the ByteBuddy MockMaker when using multiple classloaders in Java 21 spockIssue:2017[]
* Fix mocking of final classes via `@SpringBean` and `@SpringSpy` spockIssue:1960[]
* Size of data providers is no longer calculated multiple times but only once
* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount. spockPull:2031[]
* Fix exception when using `@RepeatUntilFailure` with a data provider with unknown iteration amount spockPull:2031[]
* Fix compile error with single explicit assert in switch expression branch spockIssue:1845[]

== 2.4-M4 (2024-03-21)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@
currSpecialMethodCall = NoSpecialMethodCall.INSTANCE; // unrelated closure terminates currSpecialMethodCall scope
}
try {
Statement code = expr.getCode();
if (!(code instanceof BlockStatement)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this deserves a comment.

BlockStatement block = new BlockStatement();
block.addStatement(code);
expr.setCode(block);

Check warning on line 156 in spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java

View check run for this annotation

Codecov / codecov/patch

spock-core/src/main/java/org/spockframework/compiler/AbstractDeepBlockRewriter.java#L154-L156

Added lines #L154 - L156 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange that the code block is marked as not covered, is there a test case missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, strange, shouldn't. That test below should exactly trigger this two times.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, of course.
It is covered, as the tests are compiled using the production code.
But that compilation does not have coverage tracking enabled.
To get the coverage with the current setup, it must use the embedded spec compiler so that the rewriter is triggered during test runtime, not during test compile-time.

@leonard84 do you think - generally - we should get the test compilation to also record coverage data would probably make sense? Not necessarily in this PR, but probably in a separate one. Should be something like using groovyOptions.forkOptions.jvmArgs to attach the JaCoCo agent and then also submitting that to Codecov.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken it should already do that.

tasks.named("compileTestGroovy", GroovyCompile) {
def jacocoAgent = objects.newInstance(JacocoJavaagentProvider)
jacocoAgent.jacocoAgent.fileProvider(provider { file(configurations.jacocoAgentRuntime.asPath) })
jacocoAgent.execResultFile = layout.buildDirectory.file("jacoco/compileTestGroovy.exec")
groovyOptions.forkOptions.jvmArgumentProviders.add(jacocoAgent)
}

}
doVisitClosureExpression(expr);
} finally {
currClosure = oldClosure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ class ConditionG4Spec extends Specification {
(0..<5) == [0, 1, 2, 3, 4]
(0<..<5) == [1, 2, 3, 4]
}

@Issue("https://github.com/spockframework/spock/issues/1845")
def "explicit assert in switch expression"() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding examples for the other assertions switch variants?

expect:
def b = 3
!!switch (b) {
case 3 -> assert 1 == 1
default -> assert 1 == 1
}
}
}