-
Notifications
You must be signed in to change notification settings - Fork 68
/
TemplateReportCreator.groovy
218 lines (185 loc) · 8.35 KB
/
TemplateReportCreator.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.athaydes.spockframework.report.template
import com.athaydes.spockframework.report.IReportCreator
import com.athaydes.spockframework.report.internal.FeatureRun
import com.athaydes.spockframework.report.internal.SpecData
import com.athaydes.spockframework.report.internal.StringFormatHelper
import com.athaydes.spockframework.report.internal.StringTemplateProcessor
import com.athaydes.spockframework.report.util.Utils
import com.athaydes.spockframework.report.vivid.BlockCode
import com.athaydes.spockframework.report.vivid.SpecSourceCodeReader
import groovy.text.GStringTemplateEngine
import groovy.util.logging.Slf4j
import org.spockframework.runtime.model.BlockInfo
import org.spockframework.runtime.model.FeatureInfo
import org.spockframework.runtime.model.IterationInfo
import static java.util.Collections.emptyList
import static java.util.Collections.emptyMap
/**
* IReportCreator which uses a user-provided template to generate spock-reports.
*/
@Slf4j
class TemplateReportCreator implements IReportCreator {
static final reportAggregator = new TemplateReportAggregator()
final stringProcessor = new StringTemplateProcessor()
// IReportCreator shared properties
String outputDir
boolean hideEmptyBlocks
boolean showCodeBlocks
// TemplateReportCreator properties
String specTemplateFile
String reportFileExtension
String summaryTemplateFile
String summaryFileName
boolean enabled = true
private final SpecSourceCodeReader codeReader = new SpecSourceCodeReader()
@Override
void setTestSourceRoots( roots ) {
if ( roots ) {
codeReader.testSourceRoots = roots
}
}
void setEnabled( String enabled ) {
try {
this.@enabled = Boolean.parseBoolean( enabled )
} catch ( e ) {
log.warn( "Problem parsing 'enabled' property, invalid value: $enabled", e )
}
}
@Override
void setProjectName( String projectName ) {
reportAggregator?.projectName = projectName
}
@Override
void setProjectVersion( String projectVersion ) {
reportAggregator?.projectVersion = projectVersion
}
@Override
void setAggregatedJsonReportDir( String path ) {
reportAggregator?.aggregatedJsonReportDir = path
}
void done() {
if ( !enabled ) {
return
}
def reportsDir = Utils.createDir( outputDir )
reportAggregator?.writeOut(
new File( reportsDir, summaryFileName ),
summaryTemplateFile )
}
@Override
void createReportFor( SpecData data ) {
if ( !enabled ) {
return
}
def specClassName = Utils.getSpecClassName( data )
def reportsDir = Utils.createDir( outputDir )
def reportFile = new File( reportsDir, specClassName + '.' + reportFileExtension )
reportFile.delete()
try {
if ( reportsDir.isDirectory() ) {
log.debug( "Writing report to file: {}", reportFile )
reportFile.write( reportFor( data ), 'UTF-8' )
reportAggregator.addData( data )
} else {
log.warn "${this.class.name} cannot create output directory: ${reportsDir.absolutePath}"
}
} catch ( e ) {
log.warn "Unexpected error creating report", e
}
}
String reportFor( SpecData data ) {
def templateFileUrl = this.class.getResource( specTemplateFile )
if ( !templateFileUrl ) {
throw new RuntimeException( "Template File does not exist: $specTemplateFile" )
}
def engine = new GStringTemplateEngine()
def featuresCallback = createFeaturesCallback data
if ( showCodeBlocks ) {
codeReader.read( data )
}
engine.createTemplate( templateFileUrl )
.make( [ reportCreator: this,
'utils' : Utils,
'fmt' : new StringFormatHelper(),
data : data,
features : featuresCallback ] )
.toString()
}
def createFeaturesCallback( SpecData data ) {
return [ eachFeature: { Closure callback ->
for ( feature in data.info.allFeaturesInExecutionOrder ) {
callback.delegate = feature
FeatureRun run = data.withFeatureRuns { it.find { it.feature == feature } }
if ( run && Utils.isUnrolled( feature ) ) {
handleUnrolledFeature( run, feature, callback )
} else {
handleRegularFeature( run, callback, feature )
}
}
} ]
}
protected void handleRegularFeature( FeatureRun run, Closure callback, FeatureInfo feature ) {
final failures = run ? Utils.countProblems( [ run ], Utils.&isFailure ) : 0
final errors = run ? Utils.countProblems( [ run ], Utils.&isError ) : 0
final isSkipped = !run || Utils.isSkipped( feature )
final result = errors ? 'ERROR' : failures ? 'FAIL' : isSkipped ? 'IGNORED' : 'PASS'
final problemsByIteration = run ? Utils.iterationData( run.copyFailuresByIteration(), run.copyTimeByIteration() ) : [ : ]
callback.call( feature.name, result, processedBlocks( feature ), problemsByIteration, feature.parameterNames )
}
protected void handleUnrolledFeature( FeatureRun run, FeatureInfo feature, Closure callback ) {
def iterations = run.copyFailuresByIteration()
def multipleIterations = iterations.size() > 1
iterations.eachWithIndex { iteration, problems, index ->
final name = Utils.featureNameFrom( feature, iteration, index, multipleIterations )
final result = problems.any( Utils.&isError ) ? 'ERROR' :
problems.any( Utils.&isFailure ) ? 'FAIL' :
Utils.isSkipped( feature ) ? 'IGNORED' : 'PASS'
final time = run.timeByIteration.get( iteration, 0L )
final problemsByIteration = Utils.iterationData( [ ( iteration ): problems ], [ ( iteration ): time ] )
callback.call( name, result, processedBlocks( feature, iteration ), problemsByIteration, feature.parameterNames )
}
}
protected List processedBlocks( FeatureInfo feature, IterationInfo iteration = null ) {
if ( showCodeBlocks ) {
def result = processedBlocksFromCode( feature, iteration )
if ( result ) { // only return if we found something, otherwise, run the conventional procedure
return result
}
}
// 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() }
}
private List processedBlocksFromCode( FeatureInfo feature, IterationInfo iteration ) {
def blocks = codeReader.getBlocks( feature )
blocks.collect { BlockCode block ->
def blockText = iteration && block.text ?
stringProcessor.process( block.text, feature.dataVariables, iteration ) :
( block.text ?: '' )
def blockKind = Utils.block2String[ block.label ] ?: 'Block:' // use an emergency label if something goes wrong
if ( blockText || block.statements || !hideEmptyBlocks ) {
[ kind: blockKind, text: blockText, sourceCode: block.statements ]
} else {
emptyMap()
}
}.findAll { Map map -> !map.isEmpty() }
}
}