forked from gaelcolas/Sampler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge-CodeCoverageFiles.pester.build.ps1
124 lines (103 loc) · 3.81 KB
/
Merge-CodeCoverageFiles.pester.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
Param (
# Project path
[Parameter()]
[string]
$ProjectPath = (property ProjectPath $BuildRoot),
[Parameter()]
# Base directory of all output (default to 'output')
[string]
$OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),
# Build Configuration object
[Parameter()]
$BuildInfo = (property BuildInfo @{ })
)
Import-Module -Name "$PSScriptRoot/Common.Functions.psm1"
# Synopsis: Making sure the Module meets some quality standard (help, tests).
task Merge_CodeCoverage_Files {
if (!(Split-Path -isAbsolute $OutputDirectory))
{
$OutputDirectory = Join-Path -Path $ProjectPath -ChildPath $OutputDirectory
Write-Build Yellow "Absolute path to Output Directory is $OutputDirectory"
}
$CodeCovOutputFile = "CodeCov_Merged.xml"
if ($BuildInfo.ContainsKey("Pester") -eq $true -and
$BuildInfo.Pester.ContainsKey("CodeCoverageMergedOutputFile") -eq $true)
{
$CodeCovOutputFile = $BuildInfo.Pester.CodeCoverageMergedOutputFile
}
$targetFile = Join-Path -Path $OutputDirectory -ChildPath $CodeCovOutputFile
if (Test-Path -Path $targetFile)
{
Write-Build Yellow "File $targetFile found, deleting file"
Remove-Item -Path $targetFile -Force
}
Write-Build White "Processing folder: $OutputDirectory"
$codecovFiles = Get-ChildItem -Path $OutputDirectory -Include 'codecov*.xml' -Recurse
if ($codecovFiles.Count -gt 1)
{
Write-Build DarkGray "Started merging $($codecovFiles.Count) code coverage files!"
Start-CodeCoverageMerge -Files $codecovFiles -TargetFile $targetFile
Write-Build DarkGray "Merge completed. Saved merge result to: $targetFile"
}
else
{
throw "Found $($codecovFiles.Count) code coverage file. Need at least two files to merge."
}
}
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-JaCoCoReports -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-JaCoCoStatistics -Document $targetDocument
$fullTargetFilePath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($TargetFile)
$targetDocument.Save($fullTargetFilePath)
}
else
{
throw "The following code coverage file is not using the JaCoCo format: $($firstFile.Name)"
}
}