Skip to content

Commit 9ab8c6d

Browse files
adityapatwardhanTravisEz13
authored andcommitted
Make OpenCover merge and upload more reliable/usable (PowerShell#3078)
* Corrected the opencover option to merge the output file - Delete temporary zip file. - Remove CodeCov uploading through cygwin. - Add code to use Invoke-WebRequest to upload to CodeCov * Codecov settings - Wait for 1 build to start analysis. - Do not wait for CI. - Disable comments in PR. * Addressed code review comments
1 parent 89a5c17 commit 9ab8c6d

File tree

3 files changed

+42
-60
lines changed

3 files changed

+42
-60
lines changed

codecov.yml

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# name: codecov.yml
22
fixes:
33
- "projects/powershell-*::"
4+
codecov:
5+
notify:
6+
after_n_builds: 1
7+
wait_for_ci: no
8+
comment: off

test/tools/CodeCoverageAutomation/Start-CodeCoverageRun.ps1

+35-58
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,43 @@ function Write-LogPassThru
1616
Add-Content -Path $Path -Value $Message -PassThru -Force
1717
}
1818

19-
function Write-BashInvokerScript
19+
function Push-CodeCovData
2020
{
21-
param($path)
22-
23-
$scriptContent =
24-
@'
25-
@echo off
26-
setlocal
27-
28-
if not exist "%~dpn0.sh" echo Script "%~dpn0.sh" not found & exit 2
29-
30-
set _CYGBIN=C:\cygwin64\bin
31-
if not exist "%_CYGBIN%" echo Couldn't find Cygwin at "%_CYGBIN%" & exit 3
21+
param (
22+
[Parameter(Mandatory=$true)]$file,
23+
[Parameter(Mandatory=$true)]$CommitID,
24+
[Parameter(Mandatory=$false)]$token,
25+
[Parameter(Mandatory=$false)]$Branch = "master"
26+
)
27+
$VERSION="64c1150"
28+
$url="https://codecov.io"
3229

33-
:: Resolve ___.sh to /cygdrive based *nix path and store in %_CYGSCRIPT%
34-
for /f "delims=" %%A in ('%_CYGBIN%\cygpath.exe "%~dpn0.sh"') do set _CYGSCRIPT=%%A
30+
$query = "package=bash-${VERSION}&token=${token}&branch=${Branch}&commit=${CommitID}&build=&build_url=&tag=&slug=&yaml=&service=&flags=&pr=&job="
3531

36-
:: Throw away temporary env vars and invoke script, passing any args that were passed to us
37-
endlocal & %_CYGBIN%\bash --login "%_CYGSCRIPT%" %*
38-
'@
32+
$CodeCovHeader = @{ Accept = "text/plain" }
33+
$uri = "$url/upload/v4?${query}"
34+
$response = Invoke-WebRequest -Method POST -Uri $uri -Headers $CodeCovHeader
35+
if ( $response.StatusCode -ne 200 )
36+
{
37+
Write-LogPassThru -Message "Could not get upload url for request $uri"
38+
throw "Could not get upload url"
39+
}
40+
$uploaduri = $response.content.split("`n")[-1]
3941

40-
$scriptContent | Out-File $path -Force -Encoding ascii
42+
$UploadHeader = @{ "Content-Type" = "text/plain"; "x-amz-acl" = "public-read"; "x-amz-storage-class" = "REDUCED_REDUNDANCY" }
43+
$response = Invoke-WebRequest -Method Put -Uri $uploaduri -InFile $file -Headers $UploadHeader
44+
if ( $response.StatusCode -ne 200 )
45+
{
46+
Write-LogPassThru -Message "Upload failed for upload uri: $uploaduri"
47+
throw "upload failed"
48+
}
4149
}
4250

4351
Write-LogPassThru -Message "***** New Run *****"
4452

53+
Write-LogPassThru -Message "Forcing winrm quickconfig as it is required for remoting tests."
54+
winrm quickconfig -force
55+
4556
$codeCoverageZip = 'https://ci.appveyor.com/api/projects/PowerShell/powershell-f975h/artifacts/CodeCoverage.zip'
4657
$testContentZip = 'https://ci.appveyor.com/api/projects/PowerShell/powershell-f975h/artifacts/tests.zip'
4758
$openCoverZip = 'https://ci.appveyor.com/api/projects/PowerShell/powershell-f975h/artifacts/OpenCover.zip'
@@ -150,64 +161,30 @@ try
150161
Write-LogPassThru -Message "Uploading to CoverAlls"
151162
& $coverallsExe """$coverallsParams"""
152163

153-
$bashScriptInvoker = "$PSScriptRoot\CodecovUploader.cmd"
154-
$bashScript = "$PSScriptRoot\CodecovUploader.sh"
155-
$cygwinLocation = "$env:SystemDrive\cygwin*"
156-
157-
if($bashScript)
158-
{
159-
Remove-Item $bashScript -Force -ErrorAction SilentlyContinue
160-
}
161-
162-
Invoke-RestMethod 'https://codecov.io/bash' -OutFile $bashScript
163-
Write-BashInvokerScript -path $bashScriptInvoker
164-
165-
if((Test-Path $bashScriptInvoker) -and
166-
(Test-Path $bashScript) -and
167-
(Test-Path $cygwinLocation)
168-
)
169-
{
170-
Write-LogPassThru -Message "Uploading to CodeCov"
171-
$cygwinPath = "/cygdrive/" + $outputLog.Replace("\", "/").Replace(":","")
172-
173-
$codecovParmeters = @(
174-
"-f $cygwinPath"
175-
"-X gcov",
176-
"-B master",
177-
"-C $commitId",
178-
"-X network")
179-
180-
$codecovParmetersString = $codecovParmeters -join ' '
181-
182-
& $bashScriptInvoker $codecovParmetersString
183-
}
184-
else
185-
{
186-
Write-LogPassThru -Message "BashScript: $bashScript"
187-
Write-LogPassThru -Message "BashScriptInvoke: $bashScriptInvoker"
188-
Write-LogPassThru -Message "CygwinPath : $cygwinPath"
189-
Write-LogPassThru -Message "Cannot upload to codecov as some paths are not existent"
190-
}
164+
Write-LogPassThru -Message "Uploading to CodeCov"
165+
Push-CodeCovData -file $outputLog -CommitID $commitId -token $codecovToken -Branch 'master'
191166

192167
Write-LogPassThru -Message "Upload complete."
193168
}
194169
catch
195170
{
196-
$_
171+
Write-LogPassThru -Message $_
197172
}
198173
finally
199174
{
200175
## See if Azure log directory is mounted
201176
if(Test-Path $azureLogDrive)
202177
{
203178
##Create yyyy-dd folder
204-
$monthFolder = "{0:yyyy-mm}" -f [datetime]::Now
179+
$monthFolder = "{0:yyyy-MM}" -f [datetime]::Now
205180
$monthFolderFullPath = New-Item -Path (Join-Path $azureLogDrive $monthFolder) -ItemType Directory -Force
206181
$windowsFolderPath = New-Item (Join-Path $monthFolderFullPath "Windows") -ItemType Directory -Force
207182

208183
$destinationPath = Join-Path $env:Temp ("CodeCoverageLogs-{0:yyyy_MM_dd}-{0:hh_mm_ss}.zip" -f [datetime]::Now)
209184
Compress-Archive -Path $elevatedLogs,$unelevatedLogs,$outputLog -DestinationPath $destinationPath
210185
Copy-Item $destinationPath $windowsFolderPath -Force -ErrorAction SilentlyContinue
186+
187+
Remove-Item -Path $destinationPath -Force -ErrorAction SilentlyContinue
211188
}
212189

213190
## Disable the cleanup till we stabilize.

test/tools/OpenCover/OpenCover.psm1

+2-2
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ function Invoke-OpenCover
426426
$targetArgStringElevated = $targetArgsElevated -join " "
427427
$targetArgStringUnelevated = $targetArgsUnelevated -join " "
428428
# the order seems to be important. Always keep -targetargs as the last parameter.
429-
$openCoverArgsElevated = "-target:$target","-register:user","-output:${outputLog}","-nodefaultfilters","-oldstyle","-hideskipped:all","-mergebyhash","-targetargs:`"$targetArgStringElevated`""
430-
$openCoverArgsUnelevated = "-target:$target","-register:user","-output:${outputLog}","-nodefaultfilters","-oldstyle","-hideskipped:all", "-mergebyhash", "-targetargs:`"$targetArgStringUnelevated`""
429+
$openCoverArgsElevated = "-target:$target","-register:user","-output:${outputLog}","-nodefaultfilters","-oldstyle","-hideskipped:all","-mergeoutput","-targetargs:`"$targetArgStringElevated`""
430+
$openCoverArgsUnelevated = "-target:$target","-register:user","-output:${outputLog}","-nodefaultfilters","-oldstyle","-hideskipped:all", "-mergeoutput", "-targetargs:`"$targetArgStringUnelevated`""
431431
$openCoverArgsUnelevatedString = $openCoverArgsUnelevated -join " "
432432

433433
if ( $PSCmdlet.ShouldProcess("$OpenCoverBin $openCoverArgsUnelevated") )

0 commit comments

Comments
 (0)