From b908bc567ec6a02be45988aa3820a33399453bfe Mon Sep 17 00:00:00 2001 From: Tomasz Juchniewicz Date: Sat, 16 Jul 2016 08:00:47 +0200 Subject: [PATCH] Make use of Spock Tags and Attachments Fixes gh-76 --- .../report/internal/HtmlReportCreator.groovy | 73 +++++++++++-------- .../templateReportCreator/spec-template.md | 23 +++--- .../spockframework/report/FakeTest.groovy | 4 +- .../report/VividFakeTest.groovy | 4 +- src/test/resources/FakeTest.md | 2 +- src/test/resources/VividFakeTest.md | 2 +- .../report/internal/FakeTestReport.html | 6 +- .../report/internal/VividFakeTestReport.html | 4 +- 8 files changed, 69 insertions(+), 49 deletions(-) diff --git a/src/main/groovy/com/athaydes/spockframework/report/internal/HtmlReportCreator.groovy b/src/main/groovy/com/athaydes/spockframework/report/internal/HtmlReportCreator.groovy index 877c7a7b..c22de0a2 100644 --- a/src/main/groovy/com/athaydes/spockframework/report/internal/HtmlReportCreator.groovy +++ b/src/main/groovy/com/athaydes/spockframework/report/internal/HtmlReportCreator.groovy @@ -6,9 +6,12 @@ import com.athaydes.spockframework.report.vivid.BlockCode import com.athaydes.spockframework.report.vivid.SpecSourceCodeReader import groovy.util.logging.Slf4j import groovy.xml.MarkupBuilder +import org.spockframework.runtime.model.Attachment import org.spockframework.runtime.model.BlockInfo import org.spockframework.runtime.model.FeatureInfo import org.spockframework.runtime.model.IterationInfo +import org.spockframework.runtime.model.SpecElementInfo +import org.spockframework.runtime.model.Tag import spock.lang.Ignore import spock.lang.Issue import spock.lang.PendingFeature @@ -171,22 +174,15 @@ class HtmlReportCreator extends AbstractHtmlCreator if ( narrative ) { builder.pre( 'class': 'narrative', narrative ) } - def issues = Utils.specAnnotation( data, Issue ) - if ( issues ) { - writeIssuesOrSees( builder, issues, 'Issues:' ) - } def pendingFeature = Utils.specAnnotation( data, PendingFeature ) if ( pendingFeature ) { writePendingFeature( builder, pendingFeature ) } - def sees = Utils.specAnnotation( data, See ) - if ( sees ) { - writeIssuesOrSees( builder, sees, 'See:' ) - } def headers = Utils.specHeaders( data ) if ( headers ) { writeHeaders( builder, headers ) } + writeTagOrAttachment( builder, data.info) builder.h3 "Features:" builder.table( 'class': 'features-table' ) { colgroup { @@ -246,10 +242,9 @@ class HtmlReportCreator extends AbstractHtmlCreator Utils.isSkipped( feature ) ? 'ignored' : '' writeFeatureDescription( builder, name, cssClass, feature.description.getAnnotation( Ignore ), - feature.description.getAnnotation( Issue ), - feature.description.getAnnotation( See ), feature.description.getAnnotation( PendingFeature ), - extraInfo ) + extraInfo, + run.feature ) writeFeatureBlocks( builder, feature, problems, iteration ) writeRun( builder, run, iteration ) problemWriter.writeProblemBlockForIteration( builder, iteration, problems ) @@ -268,10 +263,9 @@ class HtmlReportCreator extends AbstractHtmlCreator writeFeatureDescription( builder, feature.name, cssClass, feature.description.getAnnotation( Ignore ), - feature.description.getAnnotation( Issue ), - feature.description.getAnnotation( See ), feature.description.getAnnotation( PendingFeature ), - extraInfo ) + extraInfo, + run?.feature ) def problems = run ? run.failuresByIteration.values().collectMany { it } : [ ] writeFeatureBlocks( builder, feature, problems ) if ( run ) { @@ -502,10 +496,9 @@ class HtmlReportCreator extends AbstractHtmlCreator private void writeFeatureDescription( MarkupBuilder builder, String name, String cssClass, Ignore ignoreAnnotation, - Issue issueAnnotation, - See seeAnnotation, PendingFeature pendingFeature, - List extraInfo ) { + List extraInfo, + SpecElementInfo feature ) { def ignoreReason = '' if ( cssClass == 'ignored' && ignoreAnnotation ) { ignoreReason = ignoreAnnotation.value() @@ -522,28 +515,50 @@ class HtmlReportCreator extends AbstractHtmlCreator div() span( 'class': 'reason', ignoreReason ) } - writeIssuesOrSees builder, issueAnnotation, 'Issues:' writePendingFeature( builder, pendingFeature ) - writeIssuesOrSees builder, seeAnnotation, 'See:' writeExtraInfo( builder, extraInfo ) + if ( feature ) { + writeTagOrAttachment builder, feature + } } } } } - private void writeIssuesOrSees( MarkupBuilder builder, Annotation annotation, String description ) { - if ( annotation?.value() ) { - builder.div( 'class': 'issues' ) { - div( description ) + private void writeTagOrAttachment( MarkupBuilder builder, SpecElementInfo feature ) { + + if ( feature.attachments.isEmpty() && feature.tags.isEmpty()) { + return; + } + + builder.div( 'class': 'issues' ) { + if ( !feature.tags.isEmpty() ) { + def tagsByKey = feature.tags.groupBy( { t -> t.key } ) + tagsByKey.each { key, values -> + div( key.capitalize() + 's:' ) + ul { + for ( Tag value in values ) { + li { + if ( Utils.isUrl( value.url ) ) { + a( 'href': value.url ) { mkp.yield value.name } + } else { + span stringFormatter.escapeXml( value.name ) + } + } + } + } + } + } + + if (!feature.attachments.isEmpty()) { + div( 'See:' ) ul { - for ( String value in annotation.value() ) { + for ( Attachment value in feature.attachments ) { li { - if ( Utils.isUrl( value ) ) { - a( 'href': value ) { - mkp.yield value - } + if ( Utils.isUrl( value.url ) ) { + a( 'href': value.url ) { mkp.yield value.name } } else { - span stringFormatter.escapeXml( value ) + span stringFormatter.escapeXml( value.name ) } } } diff --git a/src/main/resources/templateReportCreator/spec-template.md b/src/main/resources/templateReportCreator/spec-template.md index 4b13e130..4d44af75 100644 --- a/src/main/resources/templateReportCreator/spec-template.md +++ b/src/main/resources/templateReportCreator/spec-template.md @@ -21,13 +21,20 @@ out << '
\n' << data.info.narrative << '\n
' } - def writeIssuesOrSees = { issues, description -> - if ( issues?.value() ) { - out << '\n#### ' << description << ':\n\n' - issues.value().each { issue -> - out << '* ' << issue << '\n' + def writeTagOrAttachment = { feature -> + def tagsByKey = feature.tags.groupBy( { t -> t.key } ) + tagsByKey.each { key, values -> + out << '\n#### ' << key.capitalize() << 's:\n\n' + values.each { tag -> + out << '* ' << tag.url << '\n' } } + if ( feature.attachments.size > 0 ) { + out << '\n#### ' << 'See:' << '\n\n' + feature.attachments.each { value -> + out << '* ' << value.url << '\n' + } + } } def writePendingFeature = { pendingFeature -> if ( pendingFeature ) { @@ -48,9 +55,8 @@ } } } - writeIssuesOrSees( utils.specAnnotation( data, spock.lang.Issue ), 'Issues' ) - writeIssuesOrSees( utils.specAnnotation( data, spock.lang.See ), 'See' ) writeHeaders( utils.specHeaders( data ) ) + writeTagOrAttachment data.info %> ## Features @@ -60,8 +66,7 @@ ### $name <% writePendingFeature( description.getAnnotation( spock.lang.PendingFeature ) ) - writeIssuesOrSees( description.getAnnotation( spock.lang.Issue ), 'Issues' ) - writeIssuesOrSees( description.getAnnotation( spock.lang.See ), 'See' ) + writeTagOrAttachment( delegate ) if ( utils.isUnrolled( delegate ) ) { writeExtraInfo( utils.nextSpecExtraInfo( data ) ) } else { diff --git a/src/test/groovy/com/athaydes/spockframework/report/FakeTest.groovy b/src/test/groovy/com/athaydes/spockframework/report/FakeTest.groovy index e84ca817..4fc254e9 100644 --- a/src/test/groovy/com/athaydes/spockframework/report/FakeTest.groovy +++ b/src/test/groovy/com/athaydes/spockframework/report/FakeTest.groovy @@ -66,7 +66,7 @@ class FakeTest extends Specification { "Nothing happens" } - @Issue( [ "http://myhost.com/issues/995", "http://myhost.com/issues/973" ] ) + @Issue( [ "http://myhost.com/issues/995", "https://myhost.com/issues/973" ] ) def "A test with an error"() { when: "An Exception is thrown" @@ -76,7 +76,7 @@ class FakeTest extends Specification { "Will never succeed" } - @See( "http://myhost.com/features/feature-234" ) + @See( [ "http://myhost.com/features/feature-234" ] ) def "A test with a failure"() { when: "Do nothing" diff --git a/src/test/groovy/com/athaydes/spockframework/report/VividFakeTest.groovy b/src/test/groovy/com/athaydes/spockframework/report/VividFakeTest.groovy index 6f367cca..458525d1 100644 --- a/src/test/groovy/com/athaydes/spockframework/report/VividFakeTest.groovy +++ b/src/test/groovy/com/athaydes/spockframework/report/VividFakeTest.groovy @@ -47,7 +47,7 @@ class VividFakeTest extends Specification { return a + b } - @Issue( [ "http://myhost.com/issues/995", "http://myhost.com/issues/973" ] ) + @Issue( [ "http://myhost.com/issues/995", "https://myhost.com/issues/973" ] ) def "A test with an error"() { when: "An Exception is thrown" @@ -57,7 +57,7 @@ class VividFakeTest extends Specification { "Will never succeed" } - @See( "http://myhost.com/features/feature-234" ) + @See( [ "http://myhost.com/features/feature-234" ] ) def "A test with a failure"() { when: "Do nothing" diff --git a/src/test/resources/FakeTest.md b/src/test/resources/FakeTest.md index 041e6f0e..ef33c038 100644 --- a/src/test/resources/FakeTest.md +++ b/src/test/resources/FakeTest.md @@ -85,7 +85,7 @@ Result: **IGNORED** #### Issues: * http://myhost.com/issues/995 -* http://myhost.com/issues/973 +* https://myhost.com/issues/973 Result: **ERROR** diff --git a/src/test/resources/VividFakeTest.md b/src/test/resources/VividFakeTest.md index bc56fde1..28a028c5 100644 --- a/src/test/resources/VividFakeTest.md +++ b/src/test/resources/VividFakeTest.md @@ -78,7 +78,7 @@ add( 1, 2 ) == 3 #### Issues: * http://myhost.com/issues/995 -* http://myhost.com/issues/973 +* https://myhost.com/issues/973 Result: **ERROR** diff --git a/src/test/resources/com/athaydes/spockframework/report/internal/FakeTestReport.html b/src/test/resources/com/athaydes/spockframework/report/internal/FakeTestReport.html index 2754a7c0..22de7742 100644 --- a/src/test/resources/com/athaydes/spockframework/report/internal/FakeTestReport.html +++ b/src/test/resources/com/athaydes/spockframework/report/internal/FakeTestReport.html @@ -220,9 +220,9 @@

Features:

diff --git a/src/test/resources/com/athaydes/spockframework/report/internal/VividFakeTestReport.html b/src/test/resources/com/athaydes/spockframework/report/internal/VividFakeTestReport.html index 43bb1e87..01d6b497 100644 --- a/src/test/resources/com/athaydes/spockframework/report/internal/VividFakeTestReport.html +++ b/src/test/resources/com/athaydes/spockframework/report/internal/VividFakeTestReport.html @@ -181,8 +181,8 @@

Features: