2020-10-02 15:48:58 +02:00
|
|
|
$Global:ProgressPreference = 'SilentlyContinue'
|
2022-06-28 08:56:33 +02:00
|
|
|
$ErrorActionPreference = 'Stop'
|
2020-10-02 15:48:58 +02:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
function Invoke-ShowEnv {
|
2020-10-02 15:48:58 +02:00
|
|
|
[CmdletBinding()]
|
2022-06-28 08:56:33 +02:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSProvideCommentHelp',
|
|
|
|
'',
|
|
|
|
Justification = 'internal function'
|
|
|
|
)]
|
|
|
|
param ()
|
2020-10-02 15:48:58 +02:00
|
|
|
|
|
|
|
process {
|
|
|
|
Get-ChildItem -Path 'Env:' | Format-Table | Out-String
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
function Invoke-InstallDependencies {
|
2020-10-02 15:48:58 +02:00
|
|
|
[CmdletBinding()]
|
2022-06-28 08:56:33 +02:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSProvideCommentHelp',
|
|
|
|
'',
|
|
|
|
Justification = 'internal function'
|
|
|
|
)]
|
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSUseSingularNouns',
|
|
|
|
'',
|
|
|
|
Justification = 'internal function'
|
|
|
|
)]
|
|
|
|
param ()
|
2020-10-02 15:48:58 +02:00
|
|
|
|
|
|
|
process {
|
2022-06-28 08:56:33 +02:00
|
|
|
$ErrorActionPreference = 'Stop'
|
2020-10-02 15:48:58 +02:00
|
|
|
try {
|
2022-06-28 08:56:33 +02:00
|
|
|
$ParamsPSScript = @{
|
2022-06-28 09:41:53 +02:00
|
|
|
Name = 'PSScriptAnalyzer'
|
|
|
|
Scope = 'CurrentUser'
|
|
|
|
RequiredVersion = '1.20.0'
|
|
|
|
Force = $true
|
2022-06-28 08:56:33 +02:00
|
|
|
SkipPublisherCheck = $true
|
2022-06-28 09:41:53 +02:00
|
|
|
AllowClobber = $true
|
|
|
|
Verbose = $VerbosePreference
|
2022-06-28 08:56:33 +02:00
|
|
|
}
|
|
|
|
Install-Module @ParamsPSScript
|
|
|
|
|
|
|
|
$ParamsPester = @{
|
2022-06-28 09:41:53 +02:00
|
|
|
Name = 'Pester'
|
|
|
|
Scope = 'CurrentUser'
|
|
|
|
RequiredVersion = '5.3.3'
|
|
|
|
Force = $true
|
2022-06-28 08:56:33 +02:00
|
|
|
SkipPublisherCheck = $true
|
2022-06-28 09:41:53 +02:00
|
|
|
AllowClobber = $true
|
|
|
|
Verbose = $VerbosePreference
|
2022-06-28 08:56:33 +02:00
|
|
|
}
|
|
|
|
Install-Module @ParamsPester
|
|
|
|
|
|
|
|
$ParamsPosh = @{
|
2022-06-28 09:41:53 +02:00
|
|
|
Name = 'posh-git'
|
|
|
|
Scope = 'CurrentUser'
|
|
|
|
RequiredVersion = '1.1.0'
|
|
|
|
Force = $true
|
2022-06-28 08:56:33 +02:00
|
|
|
SkipPublisherCheck = $true
|
2022-06-28 09:41:53 +02:00
|
|
|
AllowClobber = $true
|
|
|
|
Verbose = $VerbosePreference
|
2022-06-28 08:56:33 +02:00
|
|
|
#ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Install-Module @ParamsPosh
|
2020-10-02 15:48:58 +02:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
$ExceParams = @{
|
|
|
|
Exception = [System.Exception]::new(
|
|
|
|
'Could not install required build dependencies!',
|
|
|
|
$PSItem.Exception
|
|
|
|
)
|
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
|
|
|
Write-Error @ExceParams
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 13:37:12 +02:00
|
|
|
|
|
|
|
function Start-PSScriptAnalyzer {
|
2020-10-02 15:48:58 +02:00
|
|
|
[CmdletBinding()]
|
2022-07-14 13:37:12 +02:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSUseShouldProcessForStateChangingFunctions',
|
|
|
|
'',
|
|
|
|
Justification = 'justification'
|
|
|
|
)]
|
2022-06-28 08:56:33 +02:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSProvideCommentHelp',
|
|
|
|
'',
|
|
|
|
Justification = 'internal function'
|
|
|
|
)]
|
|
|
|
param ()
|
2020-10-02 15:48:58 +02:00
|
|
|
|
|
|
|
process {
|
2022-06-28 08:56:33 +02:00
|
|
|
$AnalyzerSettings = @{
|
|
|
|
Path = './src/'
|
|
|
|
Recurse = $true
|
|
|
|
Settings = './tools/PSScriptAnalyzerSettings.psd1'
|
|
|
|
ReportSummary = $true
|
2022-06-28 09:41:53 +02:00
|
|
|
ErrorAction = 'Stop'
|
|
|
|
}
|
2022-07-14 13:37:12 +02:00
|
|
|
$AnalyzerResults = Invoke-ScriptAnalyzer @AnalyzerSettings
|
|
|
|
if ( $AnalyzerResults ) {
|
|
|
|
Write-Output -InputObject $AnalyzerResults
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function Invoke-Linter {
|
|
|
|
[CmdletBinding()]
|
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSProvideCommentHelp',
|
|
|
|
'',
|
|
|
|
Justification = 'internal function'
|
|
|
|
)]
|
|
|
|
param ()
|
|
|
|
|
|
|
|
process {
|
2022-06-28 09:41:53 +02:00
|
|
|
try {
|
2022-07-14 13:37:12 +02:00
|
|
|
$AnalyzerResults = Start-PSScriptAnalyzer
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Write-Debug -Message $_.Exception.Message -Debug
|
|
|
|
if ($_.Exception.Message -match 'Object reference not set') {
|
|
|
|
Write-Debug -Message 'ReRun PSScriptAnalyzer' -Debug
|
|
|
|
$AnalyzerResults = Start-PSScriptAnalyzer
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Write-Error -Message 'PSScriptAnalyzer failer'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally {
|
2022-06-28 09:41:53 +02:00
|
|
|
if ( $AnalyzerResults ) {
|
|
|
|
$AnalyzerResults | Sort-Object -Property @(
|
|
|
|
"ScriptName",
|
|
|
|
"Line"
|
|
|
|
) | Format-Table @(
|
|
|
|
"Severity",
|
|
|
|
"ScriptName",
|
|
|
|
"Line",
|
|
|
|
"RuleName",
|
|
|
|
"Message"
|
|
|
|
) -AutoSize | Out-String | Write-Verbose -Verbose
|
2022-07-14 13:37:12 +02:00
|
|
|
Update-BuildStateFile
|
|
|
|
throw 'PS Script Analyzer failed!'
|
2022-06-28 09:41:53 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-02 15:48:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
function Invoke-UnitTest {
|
2020-10-02 15:48:58 +02:00
|
|
|
[CmdletBinding()]
|
2022-06-28 08:56:33 +02:00
|
|
|
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
|
|
|
|
'PSProvideCommentHelp',
|
|
|
|
'',
|
|
|
|
Justification = 'internal function'
|
|
|
|
)]
|
2020-10-02 15:48:58 +02:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
param (
|
|
|
|
[Parameter( Mandatory = $false )]
|
|
|
|
[ValidateSet('JaCoCo', 'CoverageGutters')]
|
|
|
|
[string]$CoverageFormat = 'JaCoCo',
|
2020-10-02 15:48:58 +02:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[ValidateSet('None', 'Normal', 'Detailed', 'Diagnostic')]
|
|
|
|
[string]$Verbosity = 'Normal',
|
|
|
|
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[switch]$PassThru,
|
2020-10-02 15:48:58 +02:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string[]]$Tag,
|
2020-10-02 15:48:58 +02:00
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string[]]$ExcludeTag
|
|
|
|
)
|
|
|
|
|
|
|
|
process {
|
|
|
|
Write-Verbose -Message '===== Running Pester =====' -Verbose:$VerbosePreference
|
|
|
|
$PesterConf = New-PesterConfiguration
|
|
|
|
$PesterConf.Run.Path = (Resolve-Path -Path './src').Path
|
|
|
|
$PesterConf.Run.Exit = $false
|
|
|
|
$PesterConf.Run.PassThru = $true
|
|
|
|
$PesterConf.CodeCoverage.Enabled = $true
|
|
|
|
$PesterConf.CodeCoverage.OutputFormat = $CoverageFormat
|
|
|
|
$PesterConf.TestResult.Enabled = $true
|
|
|
|
$CovFiles = Get-ChildItem -Path "./src/*.ps1" -Recurse | Where-Object {
|
|
|
|
$_.BaseName -notmatch '.Tests'
|
|
|
|
} | Select-Object -ExpandProperty 'FullName'
|
|
|
|
$PesterConf.CodeCoverage.Path = $CovFiles
|
|
|
|
$PesterConf.Output.Verbosity = $Verbosity
|
|
|
|
# Set Tags if given
|
|
|
|
if ($Tag) {
|
|
|
|
$PesterConf.Filter.Tag = $Tag
|
|
|
|
}
|
|
|
|
if ($ExcludeTag) {
|
|
|
|
$PesterConf.Filter.ExcludeTag = $ExcludeTag
|
|
|
|
}
|
|
|
|
$TestResults = Invoke-Pester -Configuration $PesterConf -ErrorAction 'Stop'
|
2022-07-14 13:37:12 +02:00
|
|
|
|
|
|
|
if ($TestResults.FailedCount -gt 0) {
|
|
|
|
Update-BuildStateFile
|
|
|
|
throw ('{0} tests failed!' -f $TestResults.FailedCount)
|
|
|
|
}
|
|
|
|
|
2022-06-28 08:56:33 +02:00
|
|
|
if ($PassThru.IsPresent) {
|
|
|
|
Write-Output -InputObject $TestResults
|
2020-10-02 15:48:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 13:37:12 +02:00
|
|
|
|
|
|
|
function Update-BuildStateFile {
|
|
|
|
[CmdletBinding()]
|
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $false)]
|
|
|
|
[ValidateNotNullOrEmpty()]
|
|
|
|
[string]$StepName = $Env:DRONE_FAILED_STEPS
|
|
|
|
)
|
|
|
|
|
|
|
|
process {
|
|
|
|
$StateFilePath = Join-Path -Path $PWD -ChildPath './STATE.xml'
|
|
|
|
if (Test-Path -Path $StateFilePath) {
|
|
|
|
$StateContent = Import-Clixml -Path $StateFilePath
|
|
|
|
$StateContent.Steps += $StepName
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$StateContent = [PSCustomObject]@{
|
|
|
|
Steps = @($StepName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Export-Clixml -Path $StateFilePath -InputObject $StateContent -Force -Encoding utf8NoBOM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Invoke-BuildState {
|
|
|
|
[CmdletBinding()]
|
|
|
|
param ()
|
|
|
|
|
|
|
|
process {
|
|
|
|
$StateFilePath = Join-Path -Path $PWD -ChildPath './STATE.xml'
|
|
|
|
if ( Test-Path -Path $StateFilePath ) {
|
|
|
|
throw 'One one more pipeline steps failed. Marking the pipeline as failed!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|