-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCoveralls.ps1
218 lines (191 loc) · 6.36 KB
/
Coveralls.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
function Add-CoverageInfo {
[CmdletBinding()]
param
(
[parameter(Position=1)]
$CoverageResultSet,
[parameter(Mandatory = $true,Position=2)]
$Value
)
foreach ($coverageResult in $CoverageResultSet) {
$coverageResult | Add-Member -MemberType NoteProperty -Name CoverageResult -Value $Value
}
}
function Merge-CoverageResult {
[CmdletBinding()]
param
(
[parameter(Position=1)]
$HitCommands,
[parameter(Position=2)]
$MissedCommands,
[parameter(Mandatory = $true,Position=3)]
$File
)
# check what has been analyzed and provide context
Add-CoverageInfo -CoverageResultSet $HitCommands -Value 1
Add-CoverageInfo -CoverageResultSet $MissedCommands -Value 0
# add both arrays for easier enumeration
$allCommandsArray = @();
$allCommandsArray += $HitCommands
$allCommandsArray += $MissedCommands
return $allCommandsArray
}
function Get-CoverageArray {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true,Position=1)]
$CoverageResultArray,
[parameter(Mandatory = $true,Position=2)]
$File
)
# count the lines
$lineCount = (Get-Content $File).Count
$coverageArray = @()
# loop the lines and math with the provided results
for($line=1; $line -lt ($lineCount + 1); $line++){
$processedLine = $CoverageResultArray | Where-Object {$_.Line -eq $line}
$coverageArray += if ($processedLine) {
# make sure got the hit count correct (hate the indentation)
$firstobject = $processedLine | Select-Object -First 1
if ($processedLine.count -gt 1 -and $firstobject.CoverageResult -eq 1) {
$processedLine.count
} else {
$firstobject.CoverageResult
}
} else {
$null
}
}
return $coverageArray
}
function Format-FileCoverage {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true,Position=1)]
$CoverageArray,
[parameter(Mandatory = $true,Position=2)]
$File,
[parameter(Mandatory = $true,Position=3)]
$RootFolder
)
$fileHash = Get-FileHash $File -Algorithm MD5
$root = (Get-Item $RootFolder).FullName
$fileName = (Get-Item $File).FullName.Replace($root, '').Replace('\','/')
if ($fileName.StartsWith('/')) {
$fileName = $fileName.Remove(0,1)
}
return New-Object -TypeName PSObject -Property @{
name = $fileName
source_digest = $fileHash.Hash
coverage = $CoverageArray
}
}
function Get-CommandsForFile {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true,Position=1)]
$Commands,
[parameter(Mandatory = $true,Position=2)]
$File
)
$fullName = (Get-Item $File).FullName
$matchedCommands = $Commands | Where-Object {
$_.File -eq $fullName
}
return $matchedCommands
}
function Get-GitInfo {
[CmdletBinding()]
param(
[string]
$BranchName
)
if (!$BranchName) {
$BranchName = (git rev-parse --abbrev-ref HEAD)
}
return New-Object -TypeName PSObject -Property @{
head = New-Object -TypeName PSObject -Property @{
id = (git log --format="%H" HEAD -1)
author_name = (git log --format="%an" HEAD -1)
author_email = (git log --format="%ae" HEAD -1)
committer_name = (git log --format="%cn" HEAD -1)
committer_email = (git log --format="%ce" HEAD -1)
message = (git log --format="%s" HEAD -1)
}
branch = $BranchName
}
}
function Format-Coverage {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true, Position = 0, ParameterSetName = "PesterResults")]
$PesterResults,
[parameter(Mandatory = $true, Position = 1, ParameterSetName = "Include")]
$Include,
[parameter(Mandatory = $true,Position=2)]
[string]
$CoverallsApiToken,
$BranchName,
$RootFolder = $pwd
)
$fileCoverageArray = @()
if (!$pesterResults) {
$pesterResults = Invoke-Pester -CodeCoverage $Include -Quiet -PassThru
}
if (!$pesterResults.CodeCoverage) {
Write-Error 'Please provide pester results with code coverage using the -CodeCoverage parameter'
return;
}
foreach ($file in $pesterResults.CodeCoverage.AnalyzedFiles) {
$hitcommands = Get-CommandsForFile -Commands $pesterResults.CodeCoverage.HitCommands -File $file
$missedCommands = Get-CommandsForFile -Commands $pesterResults.CodeCoverage.MissedCommands -File $file
$coverageResult = Merge-CoverageResult -HitCommands $hitcommands -MissedCommands $missedCommands -File $file
$coverageArray = Get-CoverageArray -CoverageResultArray $coverageResult -File $file
$fileCoverageArray += (Format-FileCoverage -CoverageArray $coverageArray -File $file -RootFolder $RootFolder)
}
$git = Get-GitInfo -BranchName $BranchName
return New-Object -TypeName PSObject -Property @{
repo_token = $CoverallsApiToken
commit_sha = (git log --format="%H" HEAD -1)
git = $git
service_name = 'appveyor'
source_files = $fileCoverageArray
}
}
function Publish-Coverage {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true,Position=1)]
$Coverage
)
Add-Type -AssemblyName System.Net.Http
$stringContent = New-Object System.Net.Http.StringContent (ConvertTo-Json $Coverage -Depth 3)
$httpClient = New-Object System.Net.Http.Httpclient
$formdata = New-Object System.Net.Http.MultipartFormDataContent
$formData.Add($stringContent, "json_file", "coverage.json")
$result = $httpClient.PostAsync('https://coveralls.io/api/v1/jobs', $formData).Result;
$content = $result.Content.ReadAsStringAsync()
$coverageResult = ConvertFrom-Json $content.Result
return $coverageResult.url
}
function Get-CoveragePercentage {
[CmdletBinding()]
param
(
[parameter(Mandatory = $true,Position=1)]
$RepositoryLink
)
try {
$response = Invoke-WebRequest -UseBasicParsing "$RepositoryLink.json"
$coverageInfo = ConvertFrom-Json $response.Content
return $coverageInfo.covered_percent
} catch {
Write-Error 'We could not complete your request.'
}
}