-
Notifications
You must be signed in to change notification settings - Fork 42
/
JaCoCo.coverage.build.ps1
494 lines (369 loc) · 16.3 KB
/
JaCoCo.coverage.build.ps1
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
param
(
# Project path
[Parameter()]
[System.String]
$ProjectPath = (property ProjectPath $BuildRoot),
[Parameter()]
# Base directory of all output (default to 'output')
[System.String]
$OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),
[Parameter()]
[System.String]
$BuiltModuleSubdirectory = (property BuiltModuleSubdirectory ''),
[Parameter()]
[System.Management.Automation.SwitchParameter]
$VersionedOutputDirectory = (property VersionedOutputDirectory $true),
[Parameter()]
[System.String]
$ProjectName = (property ProjectName ''),
[Parameter()]
[System.String]
$PesterOutputFolder = (property PesterOutputFolder 'testResults'),
[Parameter()]
[System.String]
$PesterOutputFormat = (property PesterOutputFormat ''),
[Parameter()]
[System.Object[]]
$PesterScript = (property PesterScript ''),
[Parameter()]
[System.String[]]
$PesterTag = (property PesterTag @()),
[Parameter()]
[System.String[]]
$PesterExcludeTag = (property PesterExcludeTag @()),
[Parameter()]
[System.String]
$CodeCoverageThreshold = (property CodeCoverageThreshold ''),
# Build Configuration object
[Parameter()]
[System.Collections.Hashtable]
$BuildInfo = (property BuildInfo @{ })
)
# Synopsis: Merging several code coverage files together.
task Merge_CodeCoverage_Files {
# Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable
$osShortName = Get-OperatingSystemShortName
$powerShellVersion = 'PSv.{0}' -f $PSVersionTable.PSVersion
$moduleFileName = '{0}.psm1' -f $ProjectName
$PesterOutputFolder = Get-SamplerAbsolutePath -Path $PesterOutputFolder -RelativeTo $OutputDirectory
"`tPester Output Folder = '$PesterOutputFolder'"
$GetCodeCoverageThresholdParameters = @{
RuntimeCodeCoverageThreshold = $CodeCoverageThreshold
BuildInfo = $BuildInfo
}
$CodeCoverageThreshold = Get-CodeCoverageThreshold @GetCodeCoverageThresholdParameters
if (-not $CodeCoverageThreshold)
{
$CodeCoverageThreshold = 0
}
"`tCode Coverage Threshold = '$CodeCoverageThreshold'"
if ($CodeCoverageThreshold -gt 0)
{
$getPesterOutputFileFileNameParameters = @{
ProjectName = $ProjectName
ModuleVersion = $ModuleVersion
OsShortName = $osShortName
PowerShellVersion = $powerShellVersion
}
$pesterOutputFileFileName = Get-PesterOutputFileFileName @getPesterOutputFileFileNameParameters
$getCodeCoverageOutputFile = @{
BuildInfo = $BuildInfo
PesterOutputFolder = $PesterOutputFolder
}
$CodeCoverageOutputFile = Get-SamplerCodeCoverageOutputFile @getCodeCoverageOutputFile
if (-not $CodeCoverageOutputFile)
{
$CodeCoverageOutputFile = (Join-Path -Path $PesterOutputFolder -ChildPath "CodeCov_$pesterOutputFileFileName")
}
"`tCode Coverage Output File = $CodeCoverageOutputFile"
$CodeCoverageMergedOutputFile = 'CodeCov_Merged.xml'
if ($BuildInfo.CodeCoverage.CodeCoverageMergedOutputFile)
{
$CodeCoverageMergedOutputFile = $BuildInfo.CodeCoverage.CodeCoverageMergedOutputFile
}
$CodeCoverageMergedOutputFile = Get-SamplerAbsolutePath -Path $CodeCoverageMergedOutputFile -RelativeTo $PesterOutputFolder
"`tCode Coverage Merge Output File = $CodeCoverageMergedOutputFile"
$CodeCoverageFilePattern = 'Codecov*.xml'
if ($BuildInfo.ContainsKey('CodeCoverage') -and $BuildInfo.CodeCoverage.ContainsKey('CodeCoverageFilePattern'))
{
$CodeCoverageFilePattern = $BuildInfo.CodeCoverage.CodeCoverageFilePattern
}
"`tCode Coverage File Pattern = $CodeCoverageFilePattern"
if (-not [System.String]::IsNullOrEmpty($CodeCoverageFilePattern))
{
$codecovFiles = Get-ChildItem -Path $PesterOutputFolder -Include $CodeCoverageFilePattern -Recurse
}
"`tMerging Code Coverage Files = '{0}'" -f ($codecovFiles.FullName -join ', ')
""
if (Test-Path -Path $CodeCoverageMergedOutputFile)
{
Write-Build Yellow "File $CodeCoverageMergedOutputFile found, deleting file."
Remove-Item -Path $CodeCoverageMergedOutputFile -Force
}
Write-Build White "Processing folder: $OutputDirectory"
if ($codecovFiles.Count -gt 1)
{
Write-Build DarkGray "Started merging $($codecovFiles.Count) code coverage files!"
Start-CodeCoverageMerge -Files $codecovFiles -TargetFile $CodeCoverageMergedOutputFile
Write-Build Green "Merge completed. Saved merge result to: $CodeCoverageMergedOutputFile"
}
else
{
throw "Found $($codecovFiles.Count) code coverage file. Need at least two files to merge."
}
}
else
{
Write-Build White 'Code coverage is not enabled, skipping.'
}
}
function Confirm-CodeCoverageFileFormat
{
param
(
[Parameter(Mandatory = $true)]
[System.Xml.XmlDocument]
$CodeCovFile
)
$report = ($CodeCovFile.GetEnumerator() | Where-Object -FilterScript { $_.Name -eq "Report"})
if ($null -ne $report -and $report.OuterXml -like "*JACOCO*")
{
return $true
}
return $false
}
function Start-CodeCoverageMerge
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[System.Object[]]
$Files,
[Parameter(Mandatory = $true)]
[System.String]
$TargetFile
)
$firstFile = $Files | Select-Object -First 1
$otherFiles = $Files | Select-Object -Skip 1
[xml]$targetDocument = Get-Content -Path $firstFile.FullName -Raw
if (Confirm-CodeCoverageFileFormat -CodeCovFile $targetDocument)
{
Write-Verbose "Successfully imported $($firstFile.Name) as a baseline"
$merged = 0
foreach ($file in $otherFiles)
{
[xml]$mergeDocument = Get-Content -Path $file.FullName -Raw
Write-Verbose "Merging $($file.Name) into baseline"
if (Confirm-CodeCoverageFileFormat -CodeCovFile $mergeDocument)
{
$targetDocument = Merge-JaCoCoReport -OriginalDocument $targetDocument -MergeDocument $mergeDocument
$merged++
}
else
{
Write-Verbose "The following code coverage file is not using the JaCoCo format: $($file.Name)"
}
}
Write-Verbose "Merge completed: Successfully merged $merged files into the baseline"
$targetDocument = Update-JaCoCoStatistic -Document $targetDocument
$xmlSettings = New-Object -TypeName 'System.Xml.XmlWriterSettings'
$xmlSettings.Indent = $true
$xmlSettings.Encoding = [System.Text.Encoding]::ASCII
$TargetFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($TargetFile)
$xmlWriter = [System.Xml.XmlWriter]::Create($TargetFile, $xmlSettings)
$targetDocument.Save($xmlWriter)
$xmlWriter.Close()
}
else
{
throw "The following code coverage file is not using the JaCoCo format: $($firstFile.Name)"
}
}
# Synopsis: Convert JaCoCo coverage so it supports a built module by way of ModuleBuilder.
task Convert_Pester_Coverage {
# Get the vales for task variables, see https://github.com/gaelcolas/Sampler#task-variables.
. Set-SamplerTaskVariable
$GetCodeCoverageThresholdParameters = @{
RuntimeCodeCoverageThreshold = $CodeCoverageThreshold
BuildInfo = $BuildInfo
}
$CodeCoverageThreshold = Get-CodeCoverageThreshold @GetCodeCoverageThresholdParameters
"`tCode Coverage Threshold = '$CodeCoverageThreshold'"
if (-not $CodeCoverageThreshold)
{
$CodeCoverageThreshold = 0
}
$PesterOutputFolder = Get-SamplerAbsolutePath -Path $PesterOutputFolder -RelativeTo $OutputDirectory
"`tPester Output Folder = '$PesterOutputFolder'"
$osShortName = Get-OperatingSystemShortName
$powerShellVersion = 'PSv.{0}' -f $PSVersionTable.PSVersion
$moduleFileName = '{0}.psm1' -f $ProjectName
"`tModule File Name = '$moduleFileName'"
$getPesterOutputFileFileNameParameters = @{
ProjectName = $ProjectName
ModuleVersion = $ModuleVersion
OsShortName = $osShortName
PowerShellVersion = $powerShellVersion
}
$pesterOutputFileFileName = Get-PesterOutputFileFileName @getPesterOutputFileFileNameParameters
$getCodeCoverageOutputFile = @{
BuildInfo = $BuildInfo
PesterOutputFolder = $PesterOutputFolder
}
$CodeCoverageOutputFile = Get-SamplerCodeCoverageOutputFile @getCodeCoverageOutputFile
if (-not $CodeCoverageOutputFile)
{
$CodeCoverageOutputFile = (Join-Path -Path $PesterOutputFolder -ChildPath "CodeCov_$pesterOutputFileFileName")
}
"`t"
"`tCodeCoverageOutputFile = $CodeCoverageOutputFile"
$CodeCoverageOutputFileEncoding = $BuildInfo.Pester.CodeCoverageOutputFileEncoding
if (-not $CodeCoverageOutputFileEncoding)
{
$CodeCoverageOutputFileEncoding = 'ascii'
}
"`tCodeCoverageOutputFileEncoding = $CodeCoverageOutputFileEncoding"
""
if ($CodeCoverageThreshold -eq 0)
{
Write-Build -Color 'Green' -Text 'Coverage bypassed. Nothing to convert.'
return
}
$PesterResultObjectClixml = Join-Path $PesterOutputFolder "PesterObject_$pesterOutputFileFileName"
Write-Build -Color 'White' -Text "`tPester Output Object = $PesterResultObjectClixml"
if (-not (Test-Path -Path $PesterResultObjectClixml))
{
throw 'No command were tested, nothing to convert.'
}
else
{
$pesterObject = Import-Clixml -Path $PesterResultObjectClixml
}
<#
Evaluate Pester version to use the correct properties for hit and missed commands.
The property Version does not exist on the result object from Pester 4.
#>
if ($pesterObject.Version)
{
# Pester 5
[System.Version] $pesterVersion, $null = $pesterObject.Version -split '-'
if ($pesterVersion -ge '5.0.0' -and $pesterVersion -lt '5.2.0')
{
throw 'When Pester 5 is used then to correctly support code coverage the minimum required version is v5.2.0.'
}
else
{
$originalMissedCommands = $pesterObject.CodeCoverage.CommandsMissed
$originalHitCommands = $pesterObject.CodeCoverage.CommandsExecuted
}
}
else
{
# Pester 4
$originalMissedCommands = $pesterObject.CodeCoverage.MissedCommands
$originalHitCommands = $pesterObject.CodeCoverage.HitCommands
}
# Get all missed commands that are in the main module file.
$missedCommands = $originalMissedCommands |
Where-Object -FilterScript { $_.File -match [RegEx]::Escape($moduleFileName) }
# Get all hit commands that are in the main module file.
$hitCommands = $originalHitCommands |
Where-Object -FilterScript { $_.File -match [RegEx]::Escape($moduleFileName) }
<#
The command Convert-LineNumber uses 'PassThru' very strange. It is needed
to update the content of passed in object correctly (from the pipeline in
this case). When using PassThru the command adds the properties SourceFile
and SourceLineNumber.
The command Convert-LineNumber is part of ModuleBuilder.
#>
$missedCommands | Convert-LineNumber -ErrorAction 'Stop' -PassThru | Out-Null
$hitCommands | Convert-LineNumber -ErrorAction 'Stop' -PassThru | Out-Null
# Blank line in output.
""
Write-Build -Color 'White' -Text "Missed commands in source files:"
# Output missed commands to visualize it in the pipeline output.
$allMissedCommandsInSourceFiles = $missedCommands + (
$pesterObject.CodeCoverage.MissedCommands |
Where-Object -FilterScript { $_.File -notmatch [RegEx]::Escape($moduleFileName) }
)
$allMissedCommandsInSourceFiles |
Select-Object @{
Name = 'File'
Expr = {
if ($_.SourceFile)
{
$_.SourceFile
}
else
{
$_.File
}
}
},
@{
Name = 'Line'
Expr = {
if ($_.SourceLineNumber)
{
$_.SourceLineNumber
}
else
{
$_.Line
}
}
}, Function, Command |
Out-String
# Blank line in output.
""
Write-Build -Color 'White' -Text "Converting coverage file."
<#
Cannot find a good example how package and class relate to PowerShell.
This implementation tries to mimic what Pester outputs in its coverage
file.
#>
Write-Build -Color 'DarkGray' -Text "`tBuilding new code coverage file against source."
$coverageXml = New-SamplerJaCoCoDocument -MissedCommands $missedCommands -HitCommands $hitCommands -PackageName $SourcePath -PackageDisplayName $ModuleVersionFolder
$newCoverageFilePath = Join-Path -Path $PesterOutputFolder -ChildPath 'source_coverage.xml'
Write-Build -Color 'DarkGray' -Text "`tWriting converted code coverage file to '$newCoverageFilePath'."
Out-SamplerXml -Path $newCoverageFilePath -XmlDocument $coverageXml -Encoding $CodeCoverageOutputFileEncoding
Write-Build -Color 'DarkGray' -Text "`tImporting original code coverage file '$CodeCoverageOutputFile'."
$originalXml = New-Object -TypeName 'System.Xml.XmlDocument'
<#
This need to be set on Windows PowerShell even if it is already $null
otherwise 'Load()' below will try to load the DTD. This
does not happen on PowerShell and this line is not needed it Windows
PowerShell is not used at all. Seems that setting this property changes
something internal in [System.Xml.XmlDocument].
See https://stackoverflow.com/questions/11135343/xml-documenttype-method-createdocumenttype-crashes-if-dtd-is-absent-net-c-sharp.
#>
$originalXml.XmlResolver = $null
$originalXml.Load($CodeCoverageOutputFile)
$codeCoverageOutputBackupFile = $CodeCoverageOutputFile -replace '\.xml', '.xml.bak'
$newCoverageFilePath = Join-Path -Path $PesterOutputFolder -ChildPath $codeCoverageOutputBackupFile
Write-Build -Color 'DarkGray' -Text "`tWriting a backup of original code coverage file to '$codeCoverageOutputBackupFile'."
Out-SamplerXml -Path $codeCoverageOutputBackupFile -XmlDocument $originalXml -Encoding $CodeCoverageOutputFileEncoding
Write-Build -Color 'DarkGray' -Text "`tRemoving XML node from original code coverage."
$xPath = '//package[@name="{0}"]' -f $ModuleVersionFolder
Write-Build -Color 'DarkGray' -Text "`t`tUsing XPath: '$xPath'."
$elementToRemove = Select-XML -Xml $originalXml -XPath $xPath
if ($elementToRemove)
{
$elementToRemove.Node.ParentNode.RemoveChild($elementToRemove.Node) | Out-Null
}
Write-Build -Color 'DarkGray' -Text "`tMerging temporary code coverage file with the original code coverage file."
$targetXmlDocument = Merge-JaCoCoReport -OriginalDocument $originalXml -MergeDocument $coverageXml
Write-Build -Color 'DarkGray' -Text "`tUpdating statistics in the new code coverage file."
$targetXmlDocument = Update-JaCoCoStatistic -Document $targetXmlDocument
$sourcePathFolderName = Split-Path -Path $SourcePath -Leaf
Write-Build -Color 'DarkGray' -Text ("`tUpdating path to include source folder '{0}' in the package element in the coverage file." -f $sourcePathFolderName)
Select-Xml -Xml $targetXmlDocument -XPath '//package' |
ForEach-Object -Process {
$_.Node.name = $_.Node.name -replace '^\d+\.\d+\.\d+', $sourcePathFolderName
}
Write-Build -Color 'DarkGray' -Text "`tWriting back updated code coverage file to '$CodeCoverageOutputFile'."
Out-SamplerXml -Path $CodeCoverageOutputFile -XmlDocument $targetXmlDocument -Encoding $CodeCoverageOutputFileEncoding
Write-Build -Color Green -Text 'Code Coverage successfully converted.'
}