From ec01690ecd6928aade607950d1062763c77431b7 Mon Sep 17 00:00:00 2001 From: Jonathan Butler Date: Tue, 26 Nov 2024 12:18:19 -0500 Subject: [PATCH] Test bad results in workflow now that reverted back to yaml workflow. --- .../Invoke-GitHubPSScriptAnalyzer.ps1 | 60 ------------------- .../Invoke-PowerShellScriptAnalyzer.ps1 | 45 ++++++++------ Hawk/tests/general/Test-PreCommitHook.ps1 | 11 ++-- 3 files changed, 33 insertions(+), 83 deletions(-) delete mode 100644 Hawk/internal/scripts/git_hub_action_scripts/Invoke-GitHubPSScriptAnalyzer.ps1 diff --git a/Hawk/internal/scripts/git_hub_action_scripts/Invoke-GitHubPSScriptAnalyzer.ps1 b/Hawk/internal/scripts/git_hub_action_scripts/Invoke-GitHubPSScriptAnalyzer.ps1 deleted file mode 100644 index 3bed51f..0000000 --- a/Hawk/internal/scripts/git_hub_action_scripts/Invoke-GitHubPSScriptAnalyzer.ps1 +++ /dev/null @@ -1,60 +0,0 @@ -<# -.SYNOPSIS - Runs PSScriptAnalyzer on changed PowerShell files in a GitHub workflow. -.DESCRIPTION - This script is designed to be used in a GitHub Actions workflow to analyze changed PowerShell files - using PSScriptAnalyzer. It reads the list of changed files from a file, analyzes them using the - provided settings file, and outputs the results. -.PARAMETER SettingsPath - The path to the PSScriptAnalyzer settings file. -.PARAMETER ChangedFiles - The path to the file containing the list of changed PowerShell files. -.EXAMPLE - Invoke-GitHubPSScriptAnalyzer -SettingsPath 'Hawk/internal/configurations/PSScriptAnalyzerSettings.psd1' -ChangedFiles "$env:GITHUB_WORKSPACE/changed_files.txt" -#> -function Invoke-GitHubPSScriptAnalyzer { - [CmdletBinding()] - param ( - [Parameter(Mandatory = $true)] - [string]$SettingsPath, - [Parameter(Mandatory = $true)] - [string]$ChangedFiles - ) - - Write-Output "Using settings file: $SettingsPath" - if (-not (Test-Path $SettingsPath)) { - Write-Error "PSScriptAnalyzer settings file not found at: $SettingsPath" - exit 1 - } - - $changedFiles = Get-Content -Path $ChangedFiles - if (-not $changedFiles) { - Write-Output "No PowerShell files were changed" - $null > (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt') - exit 0 - } - - $results = @() - foreach ($file in $changedFiles) { - $fullPath = Join-Path $env:GITHUB_WORKSPACE $file - if (Test-Path $fullPath) { - Write-Output "Analyzing $fullPath" - $fileResults = Invoke-ScriptAnalyzer -Path $fullPath -Settings $SettingsPath - if ($fileResults) { - $results += $fileResults - } - } - } - - if ($results) { - Write-Output "Found $($results.Count) issues in changed files:" - $results | Format-Table -AutoSize | Out-String | Write-Output - $results | Format-Table -AutoSize | Out-File (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt') - exit 1 - } - else { - Write-Output "No PSScriptAnalyzer issues found in changed files" - $null > (Join-Path $env:GITHUB_WORKSPACE 'psscriptanalyzer-results.txt') - exit 0 - } -} \ No newline at end of file diff --git a/Hawk/internal/scripts/pre_commit_hook_scripts/Invoke-PowerShellScriptAnalyzer.ps1 b/Hawk/internal/scripts/pre_commit_hook_scripts/Invoke-PowerShellScriptAnalyzer.ps1 index db67474..cd1008b 100644 --- a/Hawk/internal/scripts/pre_commit_hook_scripts/Invoke-PowerShellScriptAnalyzer.ps1 +++ b/Hawk/internal/scripts/pre_commit_hook_scripts/Invoke-PowerShellScriptAnalyzer.ps1 @@ -1,25 +1,34 @@ -$ErrorActionPreference = 'Stop' -$settings = Join-Path (Get-Location) 'Hawk/internal/configurations/PSScriptAnalyzerSettings.psd1' +if ($MyInvocation.ScriptName -eq (Join-Path $PSScriptRoot 'Invoke-PowerShellScriptAnalyzer.ps1')) { + $excludedFiles = @( + 'C:\Users\xxbut\code\hawk\Hawk\tests\general\Test-PreCommitHook.ps1' + ) -# Define the list of files to exclude -$excludedFiles = @( - 'none' -) + $files = git diff --cached --name-only --diff-filter=AM | Where-Object { $_ -match '\.(ps1|psm1|psd1)$' } + $hasErrors = $false -$files = git diff --cached --name-only --diff-filter=AM | Where-Object { $_ -match '\.(ps1|psm1|psd1)$' } -$hasErrors = $false - -foreach ($file in $files) { - if ($excludedFiles -notcontains $file) { - Write-Output "Analyzing $file..." - $results = Invoke-ScriptAnalyzer -Path $file -Settings $settings - if ($results) { - $results | Format-Table -AutoSize - $hasErrors = $true + foreach ($file in $files) { + if ($excludedFiles -contains $file) { + Write-Output "Skipping analysis for excluded file: $file" + } + else { + Write-Output "Analyzing $file..." + $results = Invoke-ScriptAnalyzer -Path $file -Settings $settings + if ($results) { + $results | Format-Table -AutoSize + $hasErrors = $true + } } } + + if ($hasErrors) { + Write-Output "PSScriptAnalyzer found errors. Exiting with error code 1." + exit 1 + } else { - Write-Output "Skipping analysis for excluded file: $file" + Write-Output "No PSScriptAnalyzer issues found. Exiting with code 0." + exit 0 } } -if ($hasErrors) { exit 1 } \ No newline at end of file +else { + # Original script content +} \ No newline at end of file diff --git a/Hawk/tests/general/Test-PreCommitHook.ps1 b/Hawk/tests/general/Test-PreCommitHook.ps1 index 17cfe1d..83b5fc8 100644 --- a/Hawk/tests/general/Test-PreCommitHook.ps1 +++ b/Hawk/tests/general/Test-PreCommitHook.ps1 @@ -5,7 +5,7 @@ # 3. Observe the PSScriptAnalyzer warnings/errors #region Good Code Examples - These will pass PSScriptAnalyzer -# testssssssss +# test function Test-GoodFunction { [CmdletBinding()] param ( @@ -52,9 +52,10 @@ function Test-AdvancedFunction { #region Bad Code Examples - Uncomment to test PSScriptAnalyzer # NOTE: The following code is intentionally written with issues to demonstrate PSScriptAnalyzer rules -<# + # Bad function - Multiple issues -function test-badfunction { # Wrong capitalization +function test-badfunction { + # Wrong capitalization param([string]$param1) # Missing CmdletBinding $Global:badVariable = $param1 # Using global variable @@ -77,8 +78,8 @@ function Test-BadValidation { } # Aliases and positional parameters -dir C:\ | where {$_.Length -gt 1000} # Using aliases instead of full cmdlet names -#> +dir C:\ | where { $_.Length -gt 1000 } # Using aliases instead of full cmdlet names + #endregion #region Testing Instructions