Skip to content

Commit

Permalink
Merge pull request #190 from renatoathaydes/spock2
Browse files Browse the repository at this point in the history
Spock2
  • Loading branch information
renatoathaydes authored May 15, 2020
2 parents 42c2e8d + 80c60da commit e340596
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 68 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ def "My feature"() {

### If you are using Maven

> NOTE about Spock/Groovy versions:
> The versions below work with the new Spock 2 / Groovy 3 versions.
> If you want to stay with Spock 1 and Groovy 2.5.x, use spock-reports version 1.7.1.
Add ``spock-reports`` to your ``<dependencies>``:

```xml
<dependency>
<groupId>com.athaydes</groupId>
<artifactId>spock-reports</artifactId>
<version>1.7.1</version>
<version>2.0-RC2</version>
<scope>test</scope>
<!-- this avoids affecting your version of Groovy/Spock -->
<exclusions>
Expand All @@ -81,13 +85,13 @@ Add ``spock-reports`` to your ``<dependencies>``:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.13</version>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.13</version>
<version>1.7.30</version>
<scope>test</scope>
</dependency>
```
Expand All @@ -100,21 +104,26 @@ repositories {
jcenter()
}
test {
useJUnitPlatform()
}
dependencies {
testCompile( 'com.athaydes:spock-reports:1.7.1' ) {
// you can use testRuntimeClasspath if you don't want to use spock-report-specific features in your Specs
testImplementation( 'com.athaydes:spock-reports:2.0-RC2' ) {
transitive = false // this avoids affecting your version of Groovy/Spock
}
// if you don't already have slf4j-api and an implementation of it in the classpath, add this!
testCompile 'org.slf4j:slf4j-api:1.7.13'
testCompile 'org.slf4j:slf4j-simple:1.7.13'
testImplementation 'org.slf4j:slf4j-api:1.7.30'
testRuntimeClasspath 'org.slf4j:slf4j-simple:1.7.30'
}
```

If you prefer, you can just download the jar directly from [JCenter](http://jcenter.bintray.com/com/athaydes/spock-reports/).

The only dependencies this project has are Groovy version 2.0+ (only the
`groovy`, `groovy-templates`, `groovy-xml` and `groovy-json`
modules are required) and `Spock`, but if you're using Spock (version 0.7-groovy-2.0+) then you already have them all!
The only dependencies this project has are Groovy (only the
`groovy`, `groovy-templates`, `groovy-xml` and `groovy-json` modules are required)
and `Spock`, of course.

## Customizing spock-reports logging

Expand All @@ -135,7 +144,7 @@ To get rid of the warning, add a dependency on a logging framework that implemen
For example, to use `slf4j-simple`, add this line to your Gradle dependencies (or the equivalent XML in your Maven pom):

```groovy
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.13'
testCompile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.30'
```

To configure the logging framework itself, please check the documentation of the framework you decide to use.
Expand Down
32 changes: 17 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,36 @@ plugins {
defaultTasks 'clean', 'jar', 'test'

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

def groovyVersion = '2.5.10'
def spockVersion = '1.2-groovy-2.5'
def groovyVersion = '3.0.1'
def spockVersion = '2.0-M2-groovy-3.0'

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
compile "org.codehaus.groovy:groovy:${groovyVersion}"
compile "org.codehaus.groovy:groovy-xml:${groovyVersion}"
compile "org.codehaus.groovy:groovy-json:${groovyVersion}"
compile "org.codehaus.groovy:groovy-templates:${groovyVersion}"
compile "org.spockframework:spock-core:${spockVersion}", {
transitive = false
implementation "org.codehaus.groovy:groovy:${groovyVersion}"
implementation "org.codehaus.groovy:groovy-xml:${groovyVersion}"
implementation "org.codehaus.groovy:groovy-json:${groovyVersion}"
implementation "org.codehaus.groovy:groovy-templates:${groovyVersion}"
implementation "org.spockframework:spock-core:${spockVersion}", {
exclude group: 'org.codehaus.groovy'
}
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'junit:junit:4.12'

testCompile "org.codehaus.groovy:groovy-test:${groovyVersion}"
testCompile "cglib:cglib-nodep:2.2.2"
testCompile "org.slf4j:slf4j-simple:1.7.13"
implementation 'org.slf4j:slf4j-api:1.7.30'
testImplementation 'org.junit.platform:junit-platform-testkit:1.6.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.0'
testImplementation "org.codehaus.groovy:groovy-test:${groovyVersion}"
testImplementation "cglib:cglib-nodep:3.2.10"
testImplementation "org.slf4j:slf4j-simple:1.7.30"
}

test {
useJUnitPlatform()

maxParallelForks = 4
exclude '**/*FakeTest.class', '**/UnrolledSpec.class', '**/SpecIncludingExtraInfo.class'
systemProperty 'project.buildDir', project.buildDir
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
8 changes: 8 additions & 0 deletions news.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

> Most recent first
* `15th of May 2020`

spock-reports has caught up with the new Groovy and Spock releases!
The new version, 2.0-RC2, works with Groovy 3 and Spock 2.

This is only a release-candidate (hence RC in the version), so please give it a go and as soon as Spock 2 is stabilized,
hopefully spock-reports 2 will also have been well tested enough to update version to 2.0 itself.

* `10th of March 2020`

This release (1.7.0) does not contain code changes. It is only a dependency upgrade release that should bring
Expand Down
4 changes: 4 additions & 0 deletions releases/Release_Notes.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.0-RC2 - 2020 May 15

* Upgrade to Spock 2 and Groovy 3.

1.7.1 - 2020 March 10

* Publishing same code as 1.7.0 but compiled with Java 8. Use this version if Java 8 is required.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class SpockReportExtension implements IGlobalExtension {
void start() {
if ( !initialized.getAndSet( true ) ) {
log.info( "Got configuration from Spock: {}", configuration )
log.debug "Configuring ${this.class.name}"
log.debug "Configuring ${ this.class.name }"
def config = configLoader.loadConfig( configuration )

// Read the class report property and exit if its not set
String commaListOfReportClasses = config.remove( IReportCreator.name )
if ( !commaListOfReportClasses ) {
log.warn( "Missing property: ${IReportCreator.name} - no report classes defined" )
log.warn( "Missing property: ${ IReportCreator.name } - no report classes defined" )
return
}

Expand Down Expand Up @@ -77,7 +77,7 @@ class SpockReportExtension implements IGlobalExtension {
if ( reportCreator != null ) {
specInfo.addListener createListener()
} else {
log.warn "Not creating report for ${specInfo.name} as reportCreator is null"
log.warn "Not creating report for ${ specInfo.name } as reportCreator is null"
}
}

Expand Down Expand Up @@ -123,6 +123,10 @@ class SpecInfoListener implements IRunListener {

@Override
synchronized void beforeSpec( SpecInfo spec ) {
if ( specData != null ) {
log.debug( 'Unexpected state: Current specData is {}, not done yet and already started with {}',
specData?.info?.name, spec.name )
}
specData = new SpecData( info: spec )
log.debug( "Before spec: {}", Utils.getSpecClassName( specData ) )
startT = System.currentTimeMillis()
Expand Down Expand Up @@ -167,6 +171,10 @@ class SpecInfoListener implements IRunListener {

@Override
void afterSpec( SpecInfo spec ) {
if ( specData == null ) {
log.debug( 'Unexpected state: running afterSpec without having a specData' )
return
}
assert specData.info == spec
log.debug( "After spec: {}", Utils.getSpecClassName( specData ) )
specData.totalTime = System.currentTimeMillis() - startT
Expand All @@ -179,7 +187,7 @@ class SpecInfoListener implements IRunListener {
try {
def errorInInitialization = ( specData == null )
log.debug( "Error on spec: {}", errorInInitialization ?
"<${EmptyInitializationException.INIT_ERROR}>" :
"<${ EmptyInitializationException.INIT_ERROR }>" :
Utils.getSpecClassName( specData ) )

if ( errorInInitialization ) {
Expand Down Expand Up @@ -249,7 +257,7 @@ class SpecInfoListener implements IRunListener {

private void markWithInitializationError( FeatureInfo featureInfo ) {
def originalGetName = featureInfo.&getName
featureInfo.metaClass.getName = { "[${EmptyInitializationException.INIT_ERROR}] ${originalGetName()}" }
featureInfo.metaClass.getName = { "[${ EmptyInitializationException.INIT_ERROR }] ${ originalGetName() }" }
}

private IterationInfo dummySpecIteration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import spock.lang.Ignore
import spock.lang.PendingFeature
import spock.lang.Title

import static com.athaydes.spockframework.report.util.Utils.featureAnnotation

/**
*
* User: Renato
Expand Down Expand Up @@ -249,8 +251,8 @@ class HtmlReportCreator extends AbstractHtmlCreator<SpecData>
problems.any( Utils.&isFailure ) ? 'failure' :
Utils.isSkipped( feature ) ? 'ignored' : ''
writeFeatureDescription( builder, name, cssClass,
feature.description.getAnnotation( Ignore ),
feature.description.getAnnotation( PendingFeature ),
featureAnnotation( feature, Ignore ),
featureAnnotation( feature, PendingFeature ),
extraInfo,
run.feature )
writeFeatureBlocks( builder, feature, problems, iteration )
Expand All @@ -271,8 +273,8 @@ class HtmlReportCreator extends AbstractHtmlCreator<SpecData>
} : [ ]

writeFeatureDescription( builder, feature.name, cssClass,
feature.description.getAnnotation( Ignore ),
feature.description.getAnnotation( PendingFeature ),
featureAnnotation( feature, Ignore ),
featureAnnotation( feature, PendingFeature ),
extraInfo,
run?.feature )
def problems = run ? run.failuresByIteration.values().collectMany { it } : [ ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Utils {

static boolean isUnrolled( FeatureInfo feature ) {
feature.spec?.isAnnotationPresent( Unroll ) ||
feature.description?.annotations?.any { Annotation a -> a.annotationType() == Unroll } ?: false
featureAnnotation( feature, Unroll ) != null
}

static boolean isFailure( SpecProblem problem ) {
Expand All @@ -103,7 +103,7 @@ class Utils {

static boolean isSkipped( FeatureInfo featureInfo ) {
// pending features are not marked as skipped but they are always skipped or fail
featureInfo.skipped || featureInfo.description.getAnnotation( PendingFeature )
featureInfo.skipped || featureAnnotation( featureInfo, PendingFeature ) != null
}

static int countFeatures( List<FeatureRun> runs,
Expand Down Expand Up @@ -141,7 +141,15 @@ class Utils {
}

static <A extends Annotation> A specAnnotation( SpecData data, Class<A> annotation ) {
data.info.description?.testClass?.getAnnotation( annotation )
data.info.isAnnotationPresent( annotation )
? data.info.getAnnotation( annotation ) as A
: null
}

static <A extends Annotation> A featureAnnotation( FeatureInfo feature, Class<A> annotation ) {
feature.featureMethod.isAnnotationPresent( annotation )
? feature.featureMethod.getAnnotation( annotation ) as A
: null
}

static List nextSpecExtraInfo( SpecData data ) {
Expand Down Expand Up @@ -192,7 +200,7 @@ class Utils {
}

static String getSpecClassName( SpecInfo info ) {
info.description?.className ?: specNameFromFileName( info )
specNameFromFileName( info )
}

static List<String> getParentSpecNames( String className ) {
Expand Down Expand Up @@ -223,7 +231,7 @@ class Utils {
def lastDotInFileName = fileName.lastIndexOf( '.' )
def name = lastDotInFileName > 0 ? fileName.substring( 0, lastDotInFileName ) : fileName

return specInfo.package + '.' + name
return (specInfo.package ? specInfo.package + '.' : '') + name
}

@Nullable
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templateReportCreator/spec-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
features.eachFeature { name, result, blocks, iterations, params ->
%>
### $name
<%
writePendingFeature( description.getAnnotation( spock.lang.PendingFeature ) )
<%
writePendingFeature( featureMethod.getAnnotation( spock.lang.PendingFeature ) )
writeTagOrAttachment( delegate )
if (result != "IGNORED") {
if ( utils.isUnrolled( delegate ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.athaydes.spockframework.report.engine

import org.junit.platform.engine.DiscoverySelector
import org.junit.platform.testkit.engine.EngineExecutionResults
import org.junit.platform.testkit.engine.EngineTestKit
import org.junit.platform.testkit.engine.EventStatistics
import spock.lang.Specification

import java.util.function.Consumer

import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass

trait CanRunSpockSpecs {

void runSpec( Class<? extends Specification> specClass, Consumer<EventStatistics> assertions = null ) {
if ( assertions ) execute( selectClass( specClass ), assertions )
else execute( selectClass( specClass ) )
}

private static void execute( DiscoverySelector selector, Consumer<EventStatistics> statisticsConsumer ) {
execute( selector )
.testEvents()
.debug()
.assertStatistics( statisticsConsumer )
}

private static EngineExecutionResults execute( DiscoverySelector selector ) {
return EngineTestKit
.engine( "spock" )
.selectors( selector )
.execute()
}

}
Loading

0 comments on commit e340596

Please sign in to comment.