Last active
December 19, 2018 10:09
-
-
Save lordmilko/5291d64509dab5bd6c2d4556df988371 to your computer and use it in GitHub Desktop.
PrtgAPI Appveyor Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: 'Build #{build}' | |
image: WMF 5 | |
configuration: Release | |
install: | |
- ps: choco upgrade chocolatey --limitoutput --no-progress | |
- ps: cinst pester --version 3.4.6 --no-progress --limitoutput | |
- ps: cinst codecov --no-progress --limitoutput | |
- ps: cinst opencover.portable --no-progress --limitoutput | |
- ps: cinst reportgenerator.portable --no-progress --limitoutput | |
- ps: Install-PackageProvider -Name NuGet -MinimumVersion '2.8.5.201' -Force | |
before_build: | |
- cmd: nuget restore | |
build: | |
verbosity: minimal | |
after_build: | |
- ps: | | |
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 | |
Invoke-WebRequest https://gist.github.com/lordmilko/5291d64509dab5bd6c2d4556df988371/raw/build.psm1 -OutFile .\build.psm1 | |
Import-Module .\build.psm1 | |
Set-PrtgVersion | |
before_test: | |
- ps: Start-PrtgNuGet | |
test_script: | |
- ps: >- | |
Invoke-Pester -Path .\PrtgAPI.Tests.UnitTests\PowerShell -EnableExit | |
try | |
{ | |
$ErrorActionPreference = "Continue" | |
vstest.console /logger:Appveyor /TestCaseFilter:TestCategory!=SkipCI "$env:APPVEYOR_BUILD_FOLDER\PrtgAPI.Tests.UnitTests\bin\$env:CONFIGURATION\PrtgAPI.Tests.UnitTests.dll" | |
} | |
finally | |
{ | |
$ErrorActionPreference = "Stop" | |
} | |
after_test: | |
- ps: Start-PrtgCoverage | |
artifacts: | |
- path: PrtgAPI\bin\Release\PrtgAPI | |
- path: '*.nupkg' | |
skip_commits: | |
files: | |
- README.md | |
- '**/*.nuspec' | |
- assets/* | |
skip_tags: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ProgressPreference = "SilentlyContinue" | |
$ErrorActionPreference = "Stop" | |
$localProjectFolder = $null # Replace this with the path to the PrtgAPI project under the repo when testing locally | |
function Main | |
{ | |
try | |
{ | |
Write-Host -ForegroundColor Cyan "Building NuGet Package" | |
$repoLocation = "$env:TEMP\TempRepository" | |
$config = [PSCustomObject]@{ | |
SolutionRoot = $null | |
CSharpProjectRoot = $null | |
CSharpOutputDir = $null | |
PowerShellProjectRoot = $null | |
PowerShellOutputDir = $null | |
Fake = $false | |
} | |
$config.SolutionRoot = "$env:TEMP\PrtgAPI" | |
$config.CSharpProjectRoot = "$($config.SolutionRoot)\PrtgAPI" | |
$config.CSharpOutputDir = "$($config.CSharpProjectRoot)\bin\Debug" | |
$config.PowerShellProjectRoot = "$($config.SolutionRoot)\PrtgAPI.PowerShell" | |
$config.PowerShellOutputDir = "$($config.PowerShellProjectRoot)\bin\Debug\PrtgAPI" | |
if($env:APPVEYOR) | |
{ | |
$config.SolutionRoot = "$env:APPVEYOR_BUILD_FOLDER\PrtgAPI" | |
$config.CSharpProjectRoot = "$($config.SolutionRoot)\PrtgAPI" | |
$config.CSharpOutputDir = "$($config.CSharpProjectRoot)\bin\$env:CONFIGURATION" | |
$config.PowerShellProjectRoot = "$($config.SolutionRoot)\PrtgAPI.PowerShell" | |
$config.PowerShellOutputDir = "$($config.PowerShellProjectRoot)\bin\$env:CONFIGURATION\PrtgAPI" | |
} | |
if(!$env:APPVEYOR) | |
{ | |
$config.Fake = $true | |
GitClone $config | |
FakeAppveyor $config | |
} | |
InstallProvider | |
CreateRepo $repoLocation $config | |
CreateCSharp $config $repoLocation | |
CreatePowerShell $config $repoLocation | |
Write-Host "Unregistering temp repository" | |
Unregister-PSRepository TempRepository | |
Write-Host "Removing temp repository folder" | |
remove-item -Recurse $repoLocation -Force | |
if(!$env:APPVEYOR) | |
{ | |
Write-Host "Removing git clone" | |
remove-item -Recurse $config.SolutionRoot -Force | |
} | |
} | |
finally | |
{ | |
if($config.Fake) | |
{ | |
$env:APPVEYOR = $null | |
} | |
} | |
} | |
#region Setup | |
function GitClone($config) | |
{ | |
Write-Host "GitClone" | |
if(Test-Path $config.SolutionRoot) | |
{ | |
Write-Host -ForegroundColor Red "`tRemoving git clone left over from previous run" | |
remove-item -Recurse $config.SolutionRoot -Force | |
} | |
Write-Host "`tCloning into $($config.SolutionRoot)" | |
Copy-Item -Recurse "$localSolutionFolder\PrtgAPI" $config.CSharpProjectRoot | |
Copy-Item -Recurse "$localSolutionFolder\PrtgAPI.PowerShell" $config.PowerShellProjectRoot | |
Get-Childitem $localSolutionFolder -File | foreach { Copy-Item $_.FullName $config.SolutionRoot } | |
} | |
function FakeAppveyor($config) | |
{ | |
$env:APPVEYOR = $true | |
$env:CONFIGURATION = "Debug" | |
$env:APPVEYOR_BUILD_VERSION = GetBuildVersion $config.CSharpProjectRoot | |
$env:APPVEYOR_BUILD_FOLDER = $config.SolutionRoot | |
$env:APPVEYOR_REPO_COMMIT_MESSAGE = 'Did some stuff' | |
$env:APPVEYOR_REPO_COMMIT_MESSAGE_EXTENDED = 'For #4' | |
} | |
function InstallProvider | |
{ | |
if(!(get-packageprovider|where name -eq nuget)) | |
{ | |
Write-Host "Installing NuGet package provider" | |
install-packageprovider nuget -force | |
} | |
} | |
function CreateRepo($repoPath, $config) | |
{ | |
Write-Host "CreateRepo" | |
if(Test-Path $repoPath) | |
{ | |
Write-Host -ForegroundColor Red "`tRemoving repository folder left over from previous run..." | |
remove-item -Recurse $repoPath -Force | |
} | |
Write-Host "`tCreating repo folder" | |
New-Item -ItemType Directory $repoPath|Out-Null | |
if(get-psrepository|where name -eq TempRepository) | |
{ | |
Write-Host -ForegroundColor Red "`tRemoving repository left over from previous run..." | |
Unregister-PSRepository TempRepository | |
} | |
Write-Host "`tRegistering repo" | |
Register-PSRepository -Name TempRepository -SourceLocation $repoPath -PublishLocation $repoPath -InstallationPolicy Trusted | |
Update-ModuleManifest "$($config.PowerShellOutputDir)\PrtgAPI.psd1" | |
} | |
#endregion | |
#region C# | |
function CreateCSharp($config, $repoLocation) | |
{ | |
Write-Host -ForegroundColor Magenta "Creating C# nupkg" | |
PackageRunner $config $false | |
} | |
function PackageRunner($config, $powershellPkg) | |
{ | |
BackupOutputDir $config.CSharpOutputDir | |
# Create dummy package to establish dll lib path | |
CreatePackage $config.CSharpProjectRoot $false | |
UpdateTargetLib $repoLocation $config.CSharpProjectRoot $config.CSharpOutputDir | |
UpdateNuspecReleaseNotes $config.CSharpProjectRoot | |
if($powershellPkg) | |
{ | |
UpdatePsd1ReleaseNotes $config.CSharpOutputDir | |
} | |
ClearRepo $repoLocation | |
CreatePackage $config.CSharpProjectRoot $true | |
RestoreOutputDir $config.CSharpOutputDir | |
TestContent $repoLocation | |
TestPackage $powershellPkg | |
MovePackages $repoLocation $null | |
} | |
function CreatePackage($projectRoot, $createSymbols) | |
{ | |
$nugetArgs = @() | |
$nugetArgs += "pack" | |
$nugetArgs += "$projectRoot\PrtgAPI.csproj" | |
$nugetArgs += "-Exclude" | |
$nugetArgs += "**/*.tt;**/Resources/*.txt;*PrtgClient.Methods.xml" | |
$nugetArgs += "-outputdirectory" | |
$nugetArgs += "$env:TEMP\TempRepository" | |
$nugetArgs += "-NoPackageAnalysis" | |
$nugetArgs += "-version" | |
if($env:APPVEYOR) | |
{ | |
$nugetArgs += $env:APPVEYOR_BUILD_VERSION | |
} | |
else | |
{ | |
$version = GetBuildVersion $projectRoot | |
$nugetArgs += $version | |
} | |
if($createSymbols) | |
{ | |
$nugetArgs += "-symbols" | |
} | |
nuget.exe $nugetArgs | |
} | |
function GetBuildVersion($projectRoot) | |
{ | |
if(!(gcm Get-PrtgVersion -ErrorAction SilentlyContinue)) | |
{ | |
ipmo .\build.psm1 | |
} | |
$root = $projectRoot.Substring(0, $projectRoot.LastIndexOf("\")) | |
$version = Get-PrtgVersion $root | |
return $version | |
} | |
function UpdateTargetLib($repoPath, $projectRoot, $outputDir) | |
{ | |
Write-Host "Updating target library" | |
ExtractNupkg $repoPath "files.zip" | |
#update the path to the dll in psd1 | |
$targets = gci $repoPath\Extracted\lib | |
if($targets.Count -gt 1) | |
{ | |
throw "Multiple targets are not yet supported" | |
} | |
Write-Host "`tSetting target to $($targets.Name)" | |
$target = $targets.Name | |
$psd1Path = "$outputDir\PrtgAPI.psd1" | |
if(Test-Path $psd1Path) | |
{ | |
(gc $psd1Path)|foreach { | |
$_ -replace "^RootModule = 'PrtgAPI.dll'$","RootModule = 'lib\$target\PrtgAPI.dll'" | |
}|set-content $psd1Path | |
} | |
#update the path to the target in nuspec | |
[xml]$nuspec = gc "$projectRoot\PrtgAPI.nuspec" | |
foreach($file in $nuspec.package.files.file) | |
{ | |
if($file.PSObject.Properties.Name -match "Target") | |
{ | |
if($file.Target -like "lib\*") | |
{ | |
$file.target = "lib\$target" | |
} | |
} | |
} | |
$nuspec.Save("$projectRoot\PrtgAPI.nuspec") | |
} | |
function UpdateNuspecReleaseNotes($projectRoot) | |
{ | |
if($env:APPVEYOR) | |
{ | |
Write-Host "Updating nuspec release notes" | |
[xml]$nuspec = gc "$projectRoot\PrtgAPI.nuspec" | |
$new = FormatReleaseNotes $nuspec.package.metadata.releaseNotes | |
$nuspec.package.metadata.releaseNotes = $new | |
$nuspec.Save("$projectRoot\PrtgAPI.nuspec") | |
} | |
} | |
function ExtractNupkg($repoPath, $newName) | |
{ | |
Write-Host "`tRenaming temp nupkg" | |
$tempPkg = gci $repoPath -Filter *.nupkg | |
Rename-Item $tempPkg.FullName $newName | |
Write-Host "`tExtracting nupkg" | |
Expand-Archive "$repoPath\$newName" "$repoPath\Extracted" | |
} | |
#endregion | |
#region PowerShell | |
function CreatePowerShell($config, $repoLocation) | |
{ | |
Write-Host -ForegroundColor Magenta "Creating PowerShell nupkg" | |
#backup outdir | |
BackupOutputDir $config.PowerShellOutputDir | |
UpdatePsd1ReleaseNotes $config.PowerShellOutputDir | |
gci "$($config.PowerShellOutputDir)\*" -Include *.cmd,*.pdb | Remove-Item -Force | |
Write-Host "Publishing module to TempRepository" | |
Publish-Module -Path $config.PowerShellOutputDir -Repository TempRepository -WarningAction SilentlyContinue | |
RestoreOutputDir $config.PowerShellOutputDir | |
TestContent $repoLocation | |
TestPackage | |
MovePackages $repoLocation "_PowerShell" | |
} | |
#endregion | |
#region Shared | |
function BackupOutputDir($outputDir) | |
{ | |
$baseOutputFolder = $outputDir.Substring($outputDir.LastIndexOf("\") + 1) | |
$parentFolder = $baseConfigurationFolder = $outputDir.Substring(0, $outputDir.LastIndexOf("\")) | |
Write-Host "Backing up build output" | |
Copy-Item -Recurse $parentFolder\$baseOutputFolder "$baseConfigurationFolder\$($baseOutputFolder)_bak" | |
} | |
function RestoreOutputDir($outputDir) | |
{ | |
$baseOutputFolder = $outputDir.Substring($outputDir.LastIndexOf("\") + 1) | |
$parentFolder = $baseConfigurationFolder = $outputDir.Substring(0, $outputDir.LastIndexOf("\")) | |
Write-Host "Restoring build output" | |
gci "$parentFolder\$($baseOutputFolder)_bak" -Exclude *.dll |where PSIsContainer -eq $false| foreach { | |
Move-Item $_.FullName "$baseConfigurationFolder\$($baseOutputFolder)\$($_.Name)" -Force | |
} | |
Remove-Item -Recurse -Force "$baseConfigurationFolder\$($baseOutputFolder)_bak" | |
} | |
function MovePackages($repoLocation, $suffix) | |
{ | |
if($env:APPVEYOR) | |
{ | |
$pkgs = Get-ChildItem $repoLocation -Filter *.nupkg | |
foreach($pkg in $pkgs) | |
{ | |
$newName = "$($pkg.BaseName)$suffix$($pkg.Extension)" | |
$newPath = "$env:APPVEYOR_BUILD_FOLDER\$newName" | |
Write-Host "Moving package $($pkg.Name) to $newPath" | |
Move-Item $pkg.Fullname $newPath | |
} | |
} | |
else | |
{ | |
Write-Host "Clearing repo (not running under Appveyor)" | |
ClearRepo $repoLocation | |
} | |
} | |
function UpdatePsd1ReleaseNotes($outputDir) | |
{ | |
if($env:APPVEYOR) | |
{ | |
Write-Host "Updating psd1 release notes" | |
$psd1Path = "$outputDir\PrtgAPI.psd1" | |
$psd1Data = Import-PowerShellDataFile $psd1Path | |
$originalNotes = $psd1Data.PrivateData.PSData.ReleaseNotes | |
$content = gc $psd1Path -Raw | |
$newNotes = FormatReleaseNotes $originalNotes | |
$newNotes = $newNotes.Replace("'", "''") | |
$newContent = $content.Replace($originalNotes, $newNotes) | |
Set-Content $psd1Path $newContent | |
} | |
} | |
function FormatReleaseNotes($releaseNotes) | |
{ | |
$notes = @() | |
$notes += "Release Notes: https://github.com/lordmilko/PrtgAPI/releases/tag/v$env:APPVEYOR_BUILD_VERSION" | |
$formatted = [string]::Join("`r`n", $notes) + "`r`n`r`n---`r`n`r`n" + $releaseNotes | |
return $formatted | |
} | |
function TestPackage($powershellPkg) | |
{ | |
Write-Host "Testing Package" | |
if(!$powershellPkg) | |
{ | |
Write-Host "`tSkipping testing as package is for .NET" | |
return | |
} | |
Write-Host "`tInstalling Package" | |
if(!(Install-Package PrtgAPI -Source TempRepository)) | |
{ | |
throw "PrtgAPI did not install properly" | |
} | |
Write-Host "`tTesting Package Cmdlet" | |
$resultCmdlet = (powershell -command '&{ import-module PrtgAPI; try { Get-Sensor } catch [exception] { $_.exception.message }}') | |
$resultFunction = (powershell -command '&{ import-module PrtgAPI; (New-Credential a b).ToString() }') | |
Write-Host "`tUninstalling Package" | |
if(!(Uninstall-Package PrtgAPI)) | |
{ | |
throw "PrtgAPI did not uninstall properly" | |
} | |
Write-Host "`tValidating cmdlet output" | |
if($resultCmdlet -ne "You are not connected to a PRTG Server. Please connect first using Connect-PrtgServer.") | |
{ | |
throw $resultCmdlet | |
} | |
$str = [string]::Join("", $resultFunction) | |
if($resultFunction -ne "System.Management.Automation.PSCredential") | |
{ | |
throw $resultFunction | |
} | |
} | |
function TestContent($repoLocation) | |
{ | |
if($env:APPVEYOR) | |
{ | |
$extracted = "$repoLocation\Extracted" | |
Write-Host "Testing release notes" | |
ExtractNupkg $repoLocation "file.zip" | |
$nuspec = gci $extracted -Filter *.nuspec | gc -raw | |
$notes = FormatReleaseNotes "" | |
Write-Host "`tValidating nuspec" | |
if(!($nuspec.Contains($notes))) | |
{ | |
throw "Nuspec does not contain release notes" | |
} | |
$psd1Notes = $notes.Replace("'", "''") | |
Write-Host "`tValidating psd1" | |
$psd1 = gci "$repoLocation\Extracted" -Filter *.psd1 | gc -raw | |
if($psd1) | |
{ | |
if(!($psd1.Contains($psd1Notes))) | |
{ | |
throw "psd1 does not contain release notes" | |
} | |
} | |
Write-Host "Validating nupkg contents" | |
if(Test-Path "$extracted\content") | |
{ | |
throw "Found 'content' folder in nupkg; content folder is not allowed" | |
} | |
Remove-Item $extracted -Recurse -Force | |
Remove-Item "$repoLocation\file.zip" -Force | |
} | |
} | |
function ExtractNupkg($repoPath, $newName) | |
{ | |
Write-Host "`tRenaming temp nupkg" | |
$tempPkg = gci $repoPath -Filter *.nupkg | where name -NotLike "*.symbols.nupkg" | |
Copy-Item $tempPkg.FullName "$repoPath\$newName" | |
Write-Host "`tExtracting nupkg" | |
Expand-Archive "$repoPath\$newName" "$repoPath\Extracted" | |
} | |
function ClearRepo($repoPath) | |
{ | |
gci -recurse $repoPath|remove-item -Recurse -Force | |
} | |
#endregion | |
Main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-PrtgVersion | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string]$BuildFolder = $env:APPVEYOR_BUILD_FOLDER | |
) | |
$ErrorActionPreference = "Stop" | |
$assemblyFile = "$BuildFolder\PrtgAPI\Properties\AssemblyFileVersion.cs" | |
$regex = New-Object System.Text.RegularExpressions.Regex ('(AssemblyFileVersion(Attribute)?\s*\(\s*\")(.*)(\"\s*\))', | |
[System.Text.RegularExpressions.RegexOptions]::MultiLine) | |
$content = [IO.File]::ReadAllText($assemblyFile) | |
$version = $null | |
$match = $regex.Match($content) | |
if($match.Success) { | |
$version = $match.groups[3].value | |
} | |
# update assembly info | |
$content = $regex.Replace($content, '${1}' + $version + '${4}') | |
[IO.File]::WriteAllText($assemblyFile, $content) | |
# Extract version | |
$ver = New-Object Version $version | |
return $ver.ToString(3) | |
} | |
function Set-PrtgVersion | |
{ | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $false, Position = 0)] | |
[string]$BuildFolder = $env:APPVEYOR_BUILD_FOLDER | |
) | |
try | |
{ | |
Log "Calculating version" | |
$version = Get-PrtgVersion $BuildFolder | |
Log "Setting AppVeyor build to version '$version'" | |
if($env:APPVEYOR) | |
{ | |
Update-AppVeyorBuild -Version $version | |
} | |
} | |
catch | |
{ | |
$host.SetShouldExit(1) | |
} | |
} | |
function Start-PrtgNuGet | |
{ | |
DownloadScript "Build-NuGet.ps1" | |
. "$PSScriptRoot\Build-NuGet.ps1" | |
} | |
function Start-PrtgCoverage | |
{ | |
DownloadScript "Get-CodeCoverage.ps1" | |
. "$PSScriptRoot\Get-CodeCoverage.ps1" | |
Get-CodeCoverage | |
$lineCoverage = Get-LineCoverage | |
$threshold = 95.3 | |
if($lineCoverage -lt $threshold) | |
{ | |
throw "Code coverage was $lineCoverage%. Coverage must be higher than $threshold%" | |
} | |
else | |
{ | |
Log "Coverage report completed with $lineCoverage% code coverage" | |
Write-Host "Uploading coverage to codecov" | |
codecov -f "$env:temp\opencover.xml" | |
} | |
} | |
function DownloadScript($scriptName) | |
{ | |
$scriptPath = "$PSScriptRoot\$scriptName" | |
Write-Host "Searching for $scriptPath" | |
if(!(Test-Path $scriptPath)) | |
{ | |
Write-Host "Downloading $scriptName to $scriptPath" | |
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 | |
Invoke-WebRequest https://gist.github.com/lordmilko/5291d64509dab5bd6c2d4556df988371/raw/$scriptName -OutFile $scriptPath | |
} | |
else | |
{ | |
Write-Host "$scriptName is already downloaded" | |
} | |
} | |
function Log($msg) | |
{ | |
Write-Host -ForegroundColor Cyan $msg | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$opencover = "C:\ProgramData\chocolatey\bin\OpenCover.Console.exe" | |
$vstest = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe" | |
$powershellAdapter = "$env:temp\PSToolsExtracted\" | |
$opencoverOutput = "$env:temp\opencover.xml" | |
$vsixInstaller = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\VSIXInstaller.exe" | |
$vsixExtensions = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions" | |
$powershellAdapterDownload = "https://github.com/adamdriscoll/poshtools/releases/download/july-maintenance/PowerShellTools.14.0.vsix" | |
function Get-CodeCoverage | |
{ | |
[CmdletBinding()] | |
param( | |
[string]$BuildFolder = $env:APPVEYOR_BUILD_FOLDER, | |
[string]$Configuration = $env:CONFIGURATION, | |
[Switch]$TestOnly | |
) | |
if(!(gcm "opencover.console" -ErrorAction SilentlyContinue)) | |
{ | |
cinst opencover.portable --confirm --no-progress | |
} | |
else | |
{ | |
& $opencover -version | |
} | |
if(Test-Path $opencoverOutput) | |
{ | |
Remove-Item $opencoverOutput -Force | |
} | |
Get-PSCodeCoverage $BuildFolder $Configuration -TestOnly:$TestOnly | |
Get-CSharpCodeCoverage $BuildFolder $Configuration -TestOnly:$TestOnly | |
} | |
function Get-CSharpCodeCoverage | |
{ | |
[CmdletBinding()] | |
param( | |
[string]$BuildFolder = $env:APPVEYOR_BUILD_FOLDER, | |
[string]$Configuration = $env:CONFIGURATION, | |
[Switch]$TestOnly | |
) | |
Log "Calculating C# Coverage" | |
$vstestParams = "/TestCaseFilter:TestCategory!=SlowCoverage&TestCategory!=SkipCI \`"$BuildFolder\PrtgAPI.Tests.UnitTests\bin\$Configuration\PrtgAPI.Tests.UnitTests.dll\`"" | |
if($TestOnly) | |
{ | |
# Replace the cmd escaped quotes with PowerShell escaped quotes, and then add an additional quote at the end of the TestCaseFilter to separate the arguments. | |
# Trim any quotes from the end of the string, since PowerShell will add its own quote for us | |
$vstestParams = ($vstestParams -replace "\\`"","`" `"").Trim("`" `"") | |
Write-Host "Executing $vstest $vstestParams" | |
& $vstest $vstestParams | |
} | |
else | |
{ | |
$opencoverParams = (Get-OpenCoverParams $vstestParams) + "-mergeoutput" | |
& $opencover @opencoverParams | |
} | |
} | |
function Get-PSCodeCoverage | |
{ | |
[CmdletBinding()] | |
param( | |
[string]$BuildFolder = $env:APPVEYOR_BUILD_FOLDER, | |
[string]$Configuration = $env:CONFIGURATION, | |
[Switch]$TestOnly | |
) | |
Log "Calculating PowerShell Coverage" | |
$tests = gci $BuildFolder\PrtgAPI.Tests.UnitTests\PowerShell -Recurse -Filter *.Tests.ps1|foreach {"\`"$($_.FullName)\`""} | |
if($tests.Count -eq 0) | |
{ | |
throw "Couldn't find any PowerShell tests" | |
} | |
else | |
{ | |
Write-Host "Found $($tests.Count) PowerShell tests" | |
} | |
$testsStr = ($tests -join " ") | |
if(!(Test-Path "$powershellAdapter\PowerShellTools.TestAdapter.dll")) | |
{ | |
$downloadPath = "$env:temp\PowerShellTools.14.0.vsix" | |
$zip = ($downloadPath -replace ".vsix",".zip") | |
$localTools = "$BuildFolder\PrtgAPI.Tests.IntegrationTests\Tools\PowerShellTools.14.0.vsix" | |
if(Test-Path $localTools) | |
{ | |
Write-Host "Copying PowerShell Tools for Visual Studio from project" | |
Copy-Item $localTools $downloadPath | |
} | |
else | |
{ | |
Write-Host "Downloading PowerShell Tools for Visual Studio" | |
Invoke-WebRequest $powershellAdapterDownload -OutFile $downloadPath | |
} | |
Write-Host "Extracting PowerShell Tools for Visual Studio" | |
Move-Item $downloadPath $zip -Force | |
Expand-Archive $zip $powershellAdapter -Force | |
} | |
$vstestParams = "/TestAdapterPath:$powershellAdapter" | |
if($TestOnly) | |
{ | |
$testsStr = ($testsStr -replace "\\`"","`"").Trim("`"") | |
$vstestParamsFinal = @( | |
"$testsStr" | |
$vstestParams | |
) | |
& $vstest ("$testsStr " + "`" `"$vstestParams") | |
} | |
else | |
{ | |
$opencoverParams = Get-OpenCoverParams "$vstestParams $testsStr" | |
Write-Host "Executing $opencover $opencoverParams" | |
& $opencover @opencoverParams | |
} | |
} | |
function Get-OpenCoverParams($arguments) | |
{ | |
$opencoverParams = @( | |
"-target:$vstest" | |
"-targetargs:$arguments" | |
"-output:$opencoverOutput" | |
"-register:path32" | |
"-filter:+[PrtgAPI]*" | |
"-excludebyattribute:System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" | |
"-hideskipped:attribute" | |
) | |
return $opencoverParams | |
} | |
function Create-CoverageReport | |
{ | |
[CmdletBinding()] | |
param( | |
[string]$Types = "Html", | |
[string]$TargetDir = "$env:temp\report" | |
) | |
Write-Host "Generating a coverage report" | |
if(!(gcm "reportgenerator" -ErrorAction SilentlyContinue)) | |
{ | |
cinst reportgenerator.portable --confirm --no-progress | Out-Null | |
} | |
& "reportgenerator" -reports:$opencoverOutput -reporttypes:$Types "-targetdir:$TargetDir" | Out-Null | |
} | |
function Get-LineCoverage | |
{ | |
Create-CoverageReport CsvSummary | |
$csv = Import-Csv $env:temp\report\Summary.csv -Delimiter ';' -Header "Property","Value" | |
$val = ($csv|where property -eq "Line coverage:"|select -expand value).Trim("%") | |
return [double]$val | |
} | |
function Log($msg) | |
{ | |
Write-Host -ForegroundColor Cyan $msg | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment