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 }