Skip to content

Commit 5ebfa56

Browse files
Fixed OpenCover module and CodeCoverage launcher script
* Changes to layout of package caused some changes to package path. * Added Test modules from tests\tools\modules * Fixed Get-ChildItem test * Added convertor for converting OpenCover output file to JSON. * Updated how the file is uploaded to CodeCov.io
1 parent 2ae5d07 commit 5ebfa56

File tree

3 files changed

+121
-29
lines changed

3 files changed

+121
-29
lines changed

test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ Describe "Get-ChildItem" -Tags "CI" {
6060
$files.Count | Should be 1
6161
$files[0].Name | Should Be $item_F
6262
}
63-
It "Should give .sys file if the fullpath is specified with hidden and force parameter" -Skip:(!$IsWindows){
64-
$file = Get-ChildItem -path "$env:SystemDrive\\pagefile.sys" -Hidden
63+
It "Should find the hidden file if specified with hidden switch" -Skip:(!$IsWindows){
64+
$file = Get-ChildItem -Path (Join-Path $TestDrive $item_F) -Hidden
6565
$file | Should not be $null
6666
$file.Count | Should be 1
67-
$file.Name | Should be "pagefile.sys"
67+
$file.Name | Should be $item_F
6868
}
6969
It "Should continue enumerating a directory when a contained item is deleted" {
7070
$Error.Clear()

test/tools/CodeCoverageAutomation/Start-CodeCoverageRun.ps1

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,97 @@
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+
798
function 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

78159
try
79160
{
@@ -86,7 +167,7 @@ try
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
@@ -165,7 +246,8 @@ try
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
}

test/tools/OpenCover/OpenCover.psm1

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ function Invoke-OpenCover
455455

456456
# create the arguments for OpenCover
457457

458-
$startupArgs = '$env:PSModulePath = "${env:PSModulePath};$TestToolsPath";Set-ExecutionPolicy Bypass -Force -Scope Process;'
458+
$startupArgs = 'Set-ExecutionPolicy Bypass -Force -Scope Process;'
459459
$targetArgs = "-c","${startupArgs}", "Invoke-Pester","${TestDirectory}"
460460

461461
if ( $CIOnly )
@@ -486,10 +486,16 @@ function Invoke-OpenCover
486486
# check to be sure that the module path is present
487487
# this isn't done earlier because there's no need to change env:PSModulePath unless we're going to really run tests
488488
$saveModPath = $env:PSModulePath
489-
$env:PSModulePath = "${PowerShellExeDirectory}\Modules"
490-
if ( ! (test-path $env:PSModulePath) )
489+
$env:PSModulePath = "${PowerShellExeDirectory}\Modules;$TestToolsModulesPath"
490+
491+
$modulePathParts = $env:PSModulePath -split ';'
492+
493+
foreach($part in $modulePathParts)
491494
{
492-
throw "${env:PSModulePath} does not exist"
495+
if ( ! (test-path $part) )
496+
{
497+
throw "${part} does not exist"
498+
}
493499
}
494500

495501
# invoke OpenCover elevated
@@ -500,12 +506,12 @@ function Invoke-OpenCover
500506
runas.exe /trustlevel:0x20000 "powershell.exe -file $env:temp\unelevated.ps1"
501507
# wait for process to start
502508
Start-Sleep -Seconds 5
503-
# poll for process exit every 30 seconds
504-
# timeout of 3 hours
505-
$timeOut = ([datetime]::Now).AddHours(3)
509+
# poll for process exit every 60 seconds
510+
# timeout of 6 hours
511+
$timeOut = ([datetime]::Now).AddHours(6)
506512
while([datetime]::Now -lt $timeOut)
507513
{
508-
Start-Sleep -Seconds 30
514+
Start-Sleep -Seconds 60
509515
$openCoverProcess = Get-Process "OpenCover.Console" -ErrorAction SilentlyContinue
510516

511517
if(-not $openCoverProcess)

0 commit comments

Comments
 (0)