Skip to content

Commit f9982e7

Browse files
tablackburnclaude
andauthored
fix: Validate PSScriptAnalyzer availability and harden WhatIf tests (#36)
* fix: Skip PSScriptAnalyzer install on cache hit The lint job was unconditionally running Install-Module even when the module was already restored from cache. This caused Install-Module -Force to conflict with the already-loaded cached module, triggering a NullReferenceException in PSScriptAnalyzer. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: Validate PSScriptAnalyzer availability and harden WhatIf tests Replace GitHub Actions cache-hit conditional with runtime module availability check to handle corrupt or empty caches gracefully. Refactor WhatIf integration tests to assert on specific collection titles and IDs instead of counts to prevent flaky failures from concurrent server state changes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: Address review feedback from Copilot Remove unused cache step ID, fix misleading log message, and use GUID instead of Get-Random for WhatIf test collection title. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: Validate PSScriptAnalyzer importability beyond module discovery Strengthen the cache validation by attempting a full Import-Module and verifying Invoke-ScriptAnalyzer is available, rather than relying solely on Get-Module -ListAvailable which only checks file presence. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * fix: Create dedicated collection for Remove WhatIf test The test was picking an arbitrary existing collection which could be deleted by concurrent test cleanup. Create a dedicated collection, verify it survives the WhatIf, then clean it up in a finally block. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1cfb5cb commit f9982e7

2 files changed

Lines changed: 33 additions & 16 deletions

File tree

.github/workflows/CI.yaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,24 @@ jobs:
3030
if: steps.cache-lint-modules.outputs.cache-hit != 'true'
3131
shell: pwsh
3232
run: |
33-
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
34-
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
33+
$moduleFound = Get-Module -ListAvailable -Name PSScriptAnalyzer
34+
if ($moduleFound) {
35+
try {
36+
Import-Module PSScriptAnalyzer -Force -ErrorAction Stop
37+
Get-Command Invoke-ScriptAnalyzer -ErrorAction Stop | Out-Null
38+
Write-Host 'PSScriptAnalyzer already available'
39+
}
40+
catch {
41+
Write-Host 'PSScriptAnalyzer cache appears invalid; reinstalling...'
42+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
43+
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
44+
}
45+
}
46+
else {
47+
Write-Host 'Installing PSScriptAnalyzer...'
48+
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
49+
Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
50+
}
3551
3652
- name: Run PSScriptAnalyzer
3753
shell: pwsh

tests/Integration/Public/Collection.Integration.tests.ps1

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -415,32 +415,33 @@ Describe 'Collection WhatIf Integration Tests' -Skip:(-not $script:integrationEn
415415
return
416416
}
417417

418-
$countBefore = (Get-PatCollection -LibraryId $script:testLibrary.key).Count
418+
$whatIfTitle = "WhatIf-Test-Collection-$([Guid]::NewGuid())"
419419
$ratingKey = [int]$script:testMediaItem.ratingKey
420420

421-
New-PatCollection -Title 'WhatIf-Test-Collection' -LibraryId $script:testLibrary.key -RatingKey $ratingKey -WhatIf
421+
New-PatCollection -Title $whatIfTitle -LibraryId $script:testLibrary.key -RatingKey $ratingKey -WhatIf
422422

423-
$countAfter = (Get-PatCollection -LibraryId $script:testLibrary.key).Count
424-
$countAfter | Should -Be $countBefore
423+
$collections = Get-PatCollection -LibraryId $script:testLibrary.key
424+
$collections.Title | Should -Not -Contain $whatIfTitle
425425
}
426426

427427
It 'Remove-PatCollection WhatIf does not delete collection' {
428-
if (-not $script:testLibrary) {
429-
Set-ItResult -Skipped -Because 'No library available for testing'
428+
if (-not $script:testLibrary -or -not $script:testMediaItem) {
429+
Set-ItResult -Skipped -Because 'No library or media item available for testing'
430430
return
431431
}
432432

433-
$collections = Get-PatCollection -LibraryId $script:testLibrary.key
434-
if ($collections) {
435-
$countBefore = $collections.Count
433+
$whatIfRemoveTitle = "WhatIf-Remove-Test-$([Guid]::NewGuid())"
434+
$ratingKey = [int]$script:testMediaItem.ratingKey
435+
$created = New-PatCollection -Title $whatIfRemoveTitle -LibraryId $script:testLibrary.key -RatingKey $ratingKey -PassThru
436436

437-
Remove-PatCollection -CollectionId $collections[0].CollectionId -WhatIf
437+
try {
438+
Remove-PatCollection -CollectionId $created.CollectionId -WhatIf
438439

439-
$countAfter = (Get-PatCollection -LibraryId $script:testLibrary.key).Count
440-
$countAfter | Should -Be $countBefore
440+
$collectionsAfter = Get-PatCollection -LibraryId $script:testLibrary.key
441+
$collectionsAfter.CollectionId | Should -Contain $created.CollectionId
441442
}
442-
else {
443-
Set-ItResult -Skipped -Because 'No collections exist to test WhatIf'
443+
finally {
444+
Remove-PatCollection -CollectionId $created.CollectionId -Confirm:$false -ErrorAction SilentlyContinue
444445
}
445446
}
446447
}

0 commit comments

Comments
 (0)