Skip to content

Commit

Permalink
Merge pull request #239 from renatoathaydes/dev
Browse files Browse the repository at this point in the history
2.3.2-groovy-3.0
  • Loading branch information
renatoathaydes authored Nov 14, 2022
2 parents 0a92f4d + fa311c8 commit 13ef599
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Version compatibility:

| Java | Groovy | Spock | spock-reports |
|-------|--------|----------------|-------------------------|
| 17+ | 3.0.9+ | 2.1-groovy-3.0 | <b>2.3.1-groovy-3.0</b> |
| 17+ | 3.0.9+ | 2.1-groovy-3.0 | <b>2.3.2-groovy-3.0</b> |
| 11-16 | 3.0+ | 2.0-groovy-3.0 | <b>2.1.1-groovy-3.0</b> |
| 8+ | 2.5+ | 2.0-groovy-2.5 | <b>2.0-groovy-2.5</b> |
| 7, 8 | 2.4+ | 1.3+ | <b>1.8.0</b> |
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
defaultTasks 'check'

group = 'com.athaydes'
version = "2.3.1-groovy-3.0"
version = "2.3.2-groovy-3.0"
description = 'This project is a global extension for Spock to create test (or, in Spock terms, Specifications) reports.'

sourceCompatibility = '1.8'
Expand Down
4 changes: 4 additions & 0 deletions news.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

> Most recent first
* `14th of November 2022`

A couple of nice improvements to "vivid" reports (that include source code).

* `25th of August 2022`

Minor bug fixes release. See release notes.
Expand Down
5 changes: 5 additions & 0 deletions releases/Release_Notes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.3.2-groovy-3.0 - 2022 Nov 14

* #235 fixed indentation of source code in vivid reports.
* #237 keep `where` block source code available for inclusion in template reports (thanks to @Gleethos).

2.3.1-groovy-3.0 - 2022 Aug 25

* #231 fixed disappearing blocks in vivid reports when spec uses annotations (thanks to @ArkiMargust).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class HtmlReportCreator extends AbstractHtmlCreator<SpecData>

private writeBlockRowsFromCode( MarkupBuilder builder, cssClass, blockKind,
BlockCode code, text, int failureLineNumber ) {
def statements = code.statements
def statements = ( blockKind == 'where' ? [] : code.statements )
def lineNumbers = code.lineNumbers

if ( text ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ class SpecSourceCode {

static String removeIndent( String code ) {
def lines = code.readLines()
if ( lines.size() < 2 ) {
return code
}

// do not use the first line because the first line never gets any indentation
def firstTextIndexes = lines[ 1..-1 ].collect { String line -> line.findIndexOf { it != ' ' } }
def firstTextIndexes = lines.collect { String line -> line.findIndexOf { it != ' ' } }
def minIndent = firstTextIndexes.min()

if ( minIndent > 0 ) {
def resultLines = [ lines[ 0 ] ] + lines[ 1..-1 ].collect { String line -> line.substring( minIndent ) }
def resultLines = lines.collect { String line -> trimLine( line, minIndent ) }
return resultLines.join( '\n' )
} else {
return code
}
}

private static String trimLine( String line, int minIndent ) {
def lastIndex = line.findLastIndexOf { it != ' ' }
line[minIndent..lastIndex]
}

}

@ToString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import org.codehaus.groovy.ast.ModuleNode
import org.codehaus.groovy.ast.expr.ConstantExpression
import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.control.Janitor
import org.codehaus.groovy.control.SourceUnit
import org.spockframework.compiler.SourceLookup
import org.spockframework.util.Nullable

import java.util.concurrent.ConcurrentHashMap

@Slf4j
@CompileStatic
class SpecSourceCodeCollector {
class SpecSourceCodeCollector implements AutoCloseable {

private final SourceUnit sourceUnit
private final Janitor janitor = new Janitor()

final SourceLookup sourceLookup
final ModuleNode module

private static final Map<String, SpecSourceCode> specSourceCodeByClassName = [ : ] as ConcurrentHashMap
Expand All @@ -29,7 +31,7 @@ class SpecSourceCodeCollector {
private MethodNode method

SpecSourceCodeCollector( SourceUnit sourceUnit ) {
this.sourceLookup = new SourceLookup( sourceUnit )
this.sourceUnit = sourceUnit
this.module = sourceUnit.AST
}

Expand Down Expand Up @@ -62,7 +64,7 @@ class SpecSourceCodeCollector {
assert className && method

def label = statement.statementLabel
def code = sourceLookup.lookup( statement )
def code = lookupCode( statement )
def specCode = specSourceCodeByClassName[ className ]

if ( !specCode ) {
Expand All @@ -79,10 +81,17 @@ class SpecSourceCodeCollector {
return // don't add the text to the code
}
}
// All statements must be added to the code in the report
specCode.addStatement( method, code, statement.lineNumber )
}

if ( label != 'where' ) { // the where statement must not be added to the code in the report
specCode.addStatement( method, code, statement.lineNumber )
private String lookupCode( Statement statement) {
def text = new StringBuilder()
for (int i = statement.getLineNumber(); i <= statement.getLastLineNumber(); i++) {
def line = sourceUnit.getSample( i, 0, janitor )
text.append( line ).append( '\n' )
}
text.toString()
}

@Nullable
Expand All @@ -97,4 +106,8 @@ class SpecSourceCodeCollector {
null
}

@Override
void close() {
janitor.cleanup()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ class VividAstInspector {
final visitor = new VividASTVisitor( codeCollector )
final module = codeCollector.module

visitor.visitBlockStatement( module.statementBlock )
codeCollector.withCloseable {
visitor.visitBlockStatement( module.statementBlock )

for ( MethodNode method in module.methods ) {
visitor.visitMethod( method )
}
for ( MethodNode method in module.methods ) {
visitor.visitMethod( method )
}

for ( ClassNode clazz in module.classes ) {
visitor.visitClass( clazz )
}
for ( ClassNode clazz in module.classes ) {
visitor.visitClass( clazz )
}

codeCollector
codeCollector
}
}

private class VividClassLoader extends GroovyClassLoader {
Expand Down Expand Up @@ -168,23 +170,12 @@ class VividASTVisitor extends ClassCodeVisitorSupport {
void visitStatement( Statement node ) {
if ( visitStatements && node instanceof BlockStatement ) {
def stmts = ( node as BlockStatement ).statements
def waitForNextBlock = false
if ( stmts ) for ( statement in stmts ) {
if ( waitForNextBlock && !statement.statementLabel ) {
continue // skip statements in this block
} else {
waitForNextBlock = false
if ( stmts )
for ( statement in stmts ) {
codeCollector.add( statement )
}

codeCollector.add( statement )

if ( statement.statementLabel == 'where' ) {
waitForNextBlock = true
}
}
visitStatements = false
}

super.visitStatement( node )
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templateReportCreator/spec-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Time: $totalTime
%>
* ${block.kind} ${block.text}
<%
if ( block.sourceCode ) {
if ( block.sourceCode && block.kind != 'Where:' ) {
out << "\n```\n"
block.sourceCode.each { codeLine ->
out << codeLine << '\n'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class VividAstInspectorSpec extends Specification {
blocks[ 1 ].statements.isEmpty()
blocks[ 2 ].statements.isEmpty()
blocks[ 3 ].statements == [ 'x == y' ]
blocks[ 4 ].statements.isEmpty() // where statements are not captured
blocks[ 4 ].statements == [ "x | y", "'a' | 'a'", "'b' | 'c'" ]

and: 'The blocks should have the expected label and text'
blocks[ 0 ].label == 'given'
Expand Down Expand Up @@ -229,7 +229,7 @@ class VividAstInspectorSpec extends Specification {
blocks.size() == 4

and: 'The inspector should be able to provide the source code for each block'
blocks[ 0 ].statements == [ 'def x = 10 +', '20 +', '30' ]
blocks[ 0 ].statements == [ 'def x = 10 +', ' 20 +', ' 30' ]
blocks[ 1 ].statements == [ 'def y = """',
' hello',
' world',
Expand Down Expand Up @@ -282,8 +282,8 @@ class VividAstInspectorSpec extends Specification {
and: 'The inspector should be able to provide the source code for each block'
blocks[ 0 ].statements == [ 'x < y' ]

and: 'The where block is not captured'
blocks[ 1 ].statements.isEmpty()
and: 'The where block is captured'
blocks[ 1 ].statements == ['x | y', '20 | 30', '100 | 2000']

and: 'The cleanup block is captured'
blocks[ 2 ].statements == [ 'x = null' ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ class SpecSourceCodeSpec extends Specification {
| println 2 * i
|}'''.stripMargin() | 'for (i in x) {\n println i\n println 2 * i\n}'

'''for (i in x) {
'''\
for (i in x) {
println i
println 2 * i
}''' | 'for (i in x) {\n println i\n println 2 * i\n}'


}

}

0 comments on commit 13ef599

Please sign in to comment.