44 [Parameter (Position = 2 )] $azureLogDrive = " L:\"
55)
66
7+ # Read the XML and create a dictionary for FileUID -> file full path.
8+ function GetFileTable ()
9+ {
10+ $files = $script :covData | Select-Xml ' .//File'
11+ foreach ($file in $files )
12+ {
13+ $script :fileTable [$file.Node.uid ] = $file.Node.fullPath
14+ }
15+ }
16+
17+ # Get sequence points for a particular file
18+ function GetSequencePointsForFile ([string ] $fileId )
19+ {
20+ $lineCoverage = @ {}
21+ $sequencePoints = $script :covData | Select-Xml " .//SequencePoint[@fileid = `" $fileId `" ]"
22+
23+ if ($sequencePoints.Count -gt 0 )
24+ {
25+ foreach ($sp in $sequencePoints )
26+ {
27+ $visitedCount = $sp.Node.vc
28+ $lineNumber = $sp.Node.sl
29+
30+ # #If this line has already been hit, add the hit count.
31+ if ($lineCoverage.Contains ($lineNumber ))
32+ {
33+ $lineCoverage [$lineNumber ] += [int ]::Parse($visitedCount )
34+ }
35+ else # # This line has been hit for the first time, ceate an entry in $lineCoverage
36+ {
37+ $lineCoverage.Add ($lineNumber , [int ]::Parse($visitedCount ))
38+ }
39+ }
40+
41+ return $lineCoverage
42+ }
43+ }
44+
45+ # ### Convert the OpenCover XML output for CodeCov.io JSON format as it is smaller.
46+ function ConvertTo-CodeCovJson
47+ {
48+ param (
49+ [string ] $Path ,
50+ [string ] $DestinationPath
51+ )
52+
53+ $Script :fileTable = [ordered ]@ {}
54+ $Script :covData = [xml ] (Get-Content - ReadCount 0 - Raw - Path $Path )
55+ $totalCoverage = [PSCustomObject ]::new()
56+ $totalCoverage | Add-Member - MemberType NoteProperty - Name " coverage" - Value ([PSCustomObject ]::new())
57+
58+ # # Populate the dictionary with file uid and file names.
59+ GetFileTable
60+ $keys = $Script :fileTable.Keys
61+ $progress = 0
62+ foreach ($f in $keys )
63+ {
64+ Write-Progress - Id 1 - Activity " Converting to JSON" - Status ' Converting' - PercentComplete ($progress * 100 / $keys.Count )
65+ $fileCoverage = GetSequencePointsForFile - fileId $f
66+ $fileName = $Script :fileTable [$f ]
67+ $previousFileCoverage = $totalCoverage.coverage .${fileName}
68+
69+ # #Update the values for the lines in the file.
70+ if ($previousFileCoverage -ne $null )
71+ {
72+ foreach ($lineNumber in $fileCoverage.Keys )
73+ {
74+ if ($previousFileCoverage.contains ($lineNumber ))
75+ {
76+ # # if this line has been hit before add the new hit count.
77+ $previousFileCoverage [$lineNumber ] += [int ]::Parse($fileCoverage [$lineNumber ])
78+ }
79+ else
80+ {
81+ $previousFileCoverage [$lineNumber ] = [int ]::Parse($fileCoverage [$lineNumber ])
82+ }
83+ }
84+ }
85+ else # # the file is new, so add the values as a new NoteProperty.
86+ {
87+ $totalCoverage.coverage | Add-Member - MemberType NoteProperty - Value $fileCoverage - Name $fileName
88+ }
89+
90+ $progress ++
91+ }
92+
93+ Write-Progress - Id 1 - Completed - Activity " Converting to JSON"
94+
95+ $totalCoverage | ConvertTo-Json - Depth 5 - Compress | out-file $DestinationPath - Encoding ascii
96+ }
97+
798function Write-LogPassThru
899{
9100 Param (
@@ -28,19 +119,9 @@ function Push-CodeCovData
28119 $url = " https://codecov.io"
29120
30121 $query = " package=bash-${VERSION} &token=${token} &branch=${Branch} &commit=${CommitID} &build=&build_url=&tag=&slug=&yaml=&service=&flags=&pr=&job="
122+ $uri = " $url /upload/v2?${query} "
123+ $response = Invoke-WebRequest - Method Post - InFile $file - Uri $uri
31124
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 ]
41-
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
44125 if ( $response.StatusCode -ne 200 )
45126 {
46127 Write-LogPassThru - Message " Upload failed for upload uri: $uploaduri "
@@ -65,15 +146,15 @@ $outputBaseFolder = "$env:Temp\CC"
65146$null = New-Item - ItemType Directory - Path $outputBaseFolder - Force
66147
67148$openCoverPath = " $outputBaseFolder \OpenCover"
68- $testPath = " $outputBaseFolder \tests"
149+ $testRootPath = " $outputBaseFolder \tests"
150+ $testPath = " $testRootPath \powershell"
69151$psBinPath = " $outputBaseFolder \PSCodeCoverage"
70152$openCoverTargetDirectory = " $outputBaseFolder \OpenCoverToolset"
71153$outputLog = " $outputBaseFolder \CodeCoverageOutput.xml"
72- $psCodeZip = " $outputBaseFolder \PSCode.zip"
73- $psCodePath = " $outputBaseFolder \PSCode"
74154$elevatedLogs = " $outputBaseFolder \TestResults_Elevated.xml"
75155$unelevatedLogs = " $outputBaseFolder \TestResults_Unelevated.xml"
76- $testToolsPath = " $testPath \tools"
156+ $testToolsPath = " $testRootPath \tools"
157+ $jsonFile = " $outputBaseFolder \CC.json"
77158
78159try
79160{
86167 Write-LogPassThru - Message " Downloads complete. Starting expansion"
87168
88169 Expand-Archive - path " $outputBaseFolder \PSCodeCoverage.zip" - destinationpath " $psBinPath " - Force
89- Expand-Archive - path " $outputBaseFolder \tests.zip" - destinationpath $testPath - Force
170+ Expand-Archive - path " $outputBaseFolder \tests.zip" - destinationpath $testRootPath - Force
90171 Expand-Archive - path " $outputBaseFolder \OpenCover.zip" - destinationpath $openCoverPath - Force
91172
92173 # # Download Coveralls.net uploader
165246 & $coverallsExe " "" $coverallsParams "" "
166247
167248 Write-LogPassThru - Message " Uploading to CodeCov"
168- Push-CodeCovData - file $outputLog - CommitID $commitId - token $codecovToken - Branch ' master'
249+ ConvertTo-CodeCovJson - Path $outputLog - DestinationPath $jsonFile
250+ Push-CodeCovData - file $jsonFile - CommitID $commitId - token $codecovToken - Branch ' master'
169251
170252 Write-LogPassThru - Message " Upload complete."
171253}
@@ -192,5 +274,9 @@ finally
192274
193275 # # Disable the cleanup till we stabilize.
194276 # Remove-Item -recurse -force -path $outputBaseFolder
277+
278+ # # This is required so we do not keep on merging coverage reports.
279+ Remove-Item $outputLog - Force - ErrorAction SilentlyContinue
280+
195281 $ErrorActionPreference = $oldErrorActionPreference
196282}
0 commit comments