Skip to content

Update MyModule.Tests.ps1 #62

Update MyModule.Tests.ps1

Update MyModule.Tests.ps1 #62

name: PowerShell CI (Lint + Syntax)
permissions:
contents: read
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
lint-and-parse:
name: Lint (PSScriptAnalyzer) + Syntax (PS 5.1 & 7)
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set ExecutionPolicy
shell: powershell
run: |
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force
- name: Install PSScriptAnalyzer
shell: pwsh
run: |
Install-Module PSScriptAnalyzer -Scope CurrentUser -Force -ErrorAction Stop
- name: Lint with PSScriptAnalyzer (default rules)
shell: pwsh
run: |
Import-Module PSScriptAnalyzer
Write-Host "Analyzing repository with ScriptAnalyzer..."
$results = Invoke-ScriptAnalyzer -Path . -Recurse -Severity Warning,Error
if ($results) {
$results | Format-Table -AutoSize | Out-String | Write-Host
}
# Fail only on Errors (warnings are acceptable for interactive scripts)
$hasBad = $results | Where-Object { $_.Severity -eq 'Error' }
if ($hasBad) { Write-Error "ScriptAnalyzer found issues."; exit 1 }
- name: Syntax check on PowerShell 5.1
shell: powershell
run: |
$errors = 0
Get-ChildItem -Recurse -Filter *.ps1 | ForEach-Object {
$null = $null
$tokens = $null
$errs = $null
[System.Management.Automation.Language.Parser]::ParseFile($_.FullName, [ref]$tokens, [ref]$errs) | Out-Null
if ($errs -and $errs.Count -gt 0) {
Write-Host "Syntax errors in $($_.FullName):"
$errs | ForEach-Object { Write-Host " $($_.Message) at $($_.Extent.Text)" }
$errors++
}
}
if ($errors -gt 0) { throw "Syntax check failed on PS 5.1" }
- name: Syntax check on PowerShell 7.x
shell: pwsh
run: |
$errors = 0
Get-ChildItem -Recurse -Filter *.ps1 | ForEach-Object {
$null = $null
$tokens = $null
$errs = $null
[System.Management.Automation.Language.Parser]::ParseFile($_.FullName, [ref]$tokens, [ref]$errs) | Out-Null
if ($errs -and $errs.Count -gt 0) {
Write-Host "Syntax errors in $($_.FullName):"
$errs | ForEach-Object { Write-Host " $($_.Message) at $($_.Extent.Text)" }
$errors++
}
}
if ($errors -gt 0) { throw "Syntax check failed on PS 7.x" }
- name: Install Pester v5
shell: pwsh
run: |
Install-Module Pester -Scope CurrentUser -Force -SkipPublisherCheck -RequiredVersion 5.0.0
- name: Run Pester smoke tests
shell: pwsh
run: |
# Only run MyModule.Tests.ps1 for focused testing
Invoke-Pester -Path .\.github\tests\MyModule.Tests.ps1