spockframework / spockframework/spock

IBlockListener per block, not BlockKind / precise block logging

Open
#2,182 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

new feature
Dominant language
Java
Stars
3.6k
Forks
483
PR merge metrics
No merged PRs in 30d

Description

Is your feature request related to a problem?

Yes - precise logging of particular block's text is not possible.

Describe the solution you'd like

Example code:

Repo with working example: https://github.com/ptomaszek/spock-IBlockListener
./mvnw test

Spock test:

    def "test"() {
        given: "given 1"
        log.info("given 1 custom log ")

        and: "given 2"
        log.info("given 2 custom log ")

        expect: "expect"
        log.info("expect custom log ")
    }

Listener and extension setup:

class IBlockLoggingExtension implements IGlobalExtension {

    @Override
    void visitSpec(SpecInfo spec) {
        spec.addListener(new BlockLoggingRunListener())
    }

    class BlockLoggingRunListener extends AbstractRunListener {

        @Override
        void beforeFeature(FeatureInfo feature) {
            feature.addBlockListener(new BlockLoggingListener())
        }
    }

    class BlockLoggingListener implements IBlockListener {

        @Override
        <S extends Specification> void blockEntered(S specificationInstance, BlockInfo blockInfo) {
            def logger = LoggerFactory.getLogger(specificationInstance.class)
            logger.info("{}", blockInfo.getTexts().join("; "))
        }
    }
}

What happens:

[main] INFO org.example.IBlockListenerSpec - given 1; given 2
[main] INFO org.example.IBlockListenerSpec - given 1 custom log 
[main] INFO org.example.IBlockListenerSpec - given 2 custom log 
[main] INFO org.example.IBlockListenerSpec - expect
[main] INFO org.example.IBlockListenerSpec - expect custom log 

What I would like to happen:

[main] INFO org.example.IBlockListenerSpec - given 1
[main] INFO org.example.IBlockListenerSpec - given 1 custom log 
[main] INFO org.example.IBlockListenerSpec - given 2
[main] INFO org.example.IBlockListenerSpec - given 2 custom log 
[main] INFO org.example.IBlockListenerSpec - expect
[main] INFO org.example.IBlockListenerSpec - expect custom log 
High level proposal

Extend IBlockListener with blockPartEntered and blockPartExited methods. An implementaion similar to the following should be possible:

        @Override
        <S extends Specification> void blockPartEntered(S specificationInstance, BlockPartInfo blockPartInfo) {
            def logger = LoggerFactory.getLogger(specificationInstance.class)
            logger.info("{}", blockPartInfo.getText())
        }
Describe alternatives you've considered

I am aware of the _ workaround (def _(def message) {...}) mentioned in couple of places, but I believe this is just a workaround, not the proper solution.
If Spock distinguishes blocks, then in should support proper block listener, not only block-kind listener.

Additional context

By looking at historical PRs and issues, I believe this request is valid to be considered for precise logging and possibly other custom aspects.

References:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with IBlockListener, BlockInfo, and the example extension shown in the issue; run ./mvnw test in the linked working example to observe the current block-level callbacks. Determine where BlockPartInfo entry and exit callbacks fit, then verify that separate given, and, and expect parts produce the requested logging behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, java
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.