renatoathaydes / spock-reports

This project creates a global extension to Spock to create test reports.
Apache License 2.0
273 stars 68 forks source link

Code blocks are not shown in the Report for Features with @Requires Annotation #231

Closed ArkiMargust closed 1 year ago

ArkiMargust commented 2 years ago

Dear @renatoathaydes,

Hoping that you could look into this and for it to be an easy fix.

To add, regardless whether the condition for the @Requires Annotation is met (Feature is executed/ignored), the code blocks are not shown in the report.

Thank you!

ArkiMargust commented 2 years ago

I've tested with @IgnoreIf and it's the same issue. With @Ignore the Code blocks are shown.

Gleethos commented 2 years ago

I experience the same issue where @Ignore and @IgnoreIf don't have code! This even happens when using custom templates (like this json template I wrote for example).

Also there seems to be a related ~bug where sometimes there is "----" instead of the actual (empty) block text:

      "blocks":[
        {"kind":"given","text":"----","code":[]},
        {"kind":"expect","text":"----","code":[]},
        {"kind":"when","text":"----","code":[]},
        {"kind":"then","text":"----","code":[]},
        {"kind":"when","text":"----","code":[]},
        {"kind":"then","text":"----","code":[]},
        {"kind":"when","text":"----","code":[]},
        {"kind":"then","text":"----","code":[]},
        {"kind":"where","text":"----","code":[]}
      ],

This seems to be related to this source code I found in report/template/TemplateReportCreator.groovy

        // as we don't have the AST, we need to use the old way to get the block text from Spock's API
        feature.blocks.collect { BlockInfo block ->
            List<String> blockTexts = block.texts

            if ( !Utils.isEmptyOrContainsOnlyEmptyStrings( blockTexts ) ) {
                int index = 0
                blockTexts.collect { blockText ->
                    if ( iteration ) {
                        blockText = stringProcessor.process( blockText, feature.dataVariables, iteration )
                    }
                    [ kind      : Utils.block2String[ ( index++ ) == 0 ? block.kind : 'and' ],
                      text      : blockText,
                      sourceCode: emptyList() ]
                }
            } else if ( !hideEmptyBlocks ) {
                [ [ kind      : Utils.block2String[ block.kind ],
                    text      : '----',
                    sourceCode: emptyList() ] ]
            } else {
                [ [ : ] ]
            }
        }.flatten().findAll { Map item -> !item.isEmpty() }
    }

I have the feeling that this might be a Spock bug, does Spock maybe not record this kind of data if features are ignored?

Gleethos commented 2 years ago

I forked and fixed it by preventing the AST traversal of annotations, + added a unit test. I let the forked and modified version run over the 1200+ tests in https://github.com/Gleethos/neureka and it worked perfectly.

renatoathaydes commented 2 years ago

Thanks for the PR @Gleethos . I will release a fix as soon as possible. Sorry the delay.

Gleethos commented 2 years ago

I am glad my PR helps! Thank you for creating and maintaining this powerful project @renatoathaydes !

renatoathaydes commented 1 year ago

Sorry the delay. Release on versions 2.4.0-groovy-4.0 and 2.3.2-groovy-4.0.