From b496eaaa9d71d7d6dba1f1dcb56bdc40fc08daef Mon Sep 17 00:00:00 2001 From: OCram85 Date: Tue, 28 Jun 2022 10:19:27 +0200 Subject: [PATCH] add workaround for psscript analyzer object reference error --- tools/DroneIO.psm1 | 61 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/tools/DroneIO.psm1 b/tools/DroneIO.psm1 index 4a3dd85..de8ee71 100644 --- a/tools/DroneIO.psm1 +++ b/tools/DroneIO.psm1 @@ -80,6 +80,35 @@ function Invoke-InstallDependencies { } } + +function Start-PSScriptAnalyzer { + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSUseShouldProcessForStateChangingFunctions', + '', + Justification = 'justification' + )] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute( + 'PSProvideCommentHelp', + '', + Justification = 'internal function' + )] + param () + + process { + $AnalyzerSettings = @{ + Path = './src/' + Recurse = $true + Settings = './tools/PSScriptAnalyzerSettings.psd1' + ReportSummary = $true + ErrorAction = 'Stop' + } + $AnalyzerResults = Invoke-ScriptAnalyzer @AnalyzerSettings + if ( $AnalyzerResults ) { + Write-Output -InputObject $AnalyzerSettings + } + } +} function Invoke-Linter { [CmdletBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute( @@ -90,16 +119,20 @@ function Invoke-Linter { param () process { - Import-Module -Name PSScriptAnalyzer - $AnalyzerSettings = @{ - Path = './src/' - Recurse = $true - Settings = './tools/PSScriptAnalyzerSettings.psd1' - ReportSummary = $true - ErrorAction = 'Stop' - } try { - $AnalyzerResults = Invoke-ScriptAnalyzer @AnalyzerSettings + $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 { if ( $AnalyzerResults ) { $AnalyzerResults | Sort-Object -Property @( "ScriptName", @@ -113,10 +146,6 @@ function Invoke-Linter { ) -AutoSize | Out-String | Write-Verbose -Verbose } } - catch { - Write-Debug -Message $_.Exception.Message -Debug - Write-Error -Message 'PSScriptAnalyzer failer' - } } } @@ -171,6 +200,12 @@ function Invoke-UnitTest { $PesterConf.Filter.ExcludeTag = $ExcludeTag } $TestResults = Invoke-Pester -Configuration $PesterConf -ErrorAction 'Stop' + + if ($TestResults.FailedCount -gt 0) { + Write-FailureStateFile -StepName 'Pester' + throw ('{0} tests failed!' -f $TestResults.FailedCount) + } + if ($PassThru.IsPresent) { Write-Output -InputObject $TestResults }