tests/RegistryOrphans.Tests.ps1
|
$moduleRoot = Split-Path -Parent $PSScriptRoot Import-Module (Join-Path $moduleRoot 'Octa.psd1') -Force . (Join-Path $moduleRoot 'private\ActionHelpers.ps1') . (Join-Path $moduleRoot 'private\ElevationCheck.ps1') Get-ChildItem -Path (Join-Path $moduleRoot 'categories') -Filter '*.ps1' | ForEach-Object { . $_.FullName } $isElevated = Test-OctaElevation Describe 'Test-OctaUninstallTargetBroken' { It 'reports $false for a real, existing executable path' { Test-OctaUninstallTargetBroken -UninstallString "`"$PSHOME\powershell.exe`" /uninstall" | Should Be $false } It 'reports $true for a quoted path that does not exist' { Test-OctaUninstallTargetBroken -UninstallString '"C:\ThisPathDoesNotExist12345\fake.exe"' | Should Be $true } It 'never flags an MSI-style UninstallString - MsiExec.exe resolves via PATH, not a broken reference (research.md - the original Test-Path-only assumption was wrong for bare names, fixed during implementation)' { Test-OctaUninstallTargetBroken -UninstallString 'MsiExec.exe /X{00000000-0000-0000-0000-000000000000}' | Should Be $false } } Describe 'Get-OctaRegistryOrphansActions' { $testKeyPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\OctaPesterTestOrphan' It 'flags a synthetic entry whose UninstallString points at a nonexistent path, with RiskLevel Risky' { if (-not $isElevated) { return } Remove-Item -LiteralPath $testKeyPath -Recurse -Force -ErrorAction SilentlyContinue New-Item -Path $testKeyPath -Force | Out-Null Set-ItemProperty -Path $testKeyPath -Name 'DisplayName' -Value 'Octa Pester Test Orphan' Set-ItemProperty -Path $testKeyPath -Name 'UninstallString' -Value '"C:\ThisPathDoesNotExist12345\fake.exe"' try { $actions = @(Get-OctaRegistryOrphansActions) $match = $actions | Where-Object { $_.TargetIdentifier -like "*OctaPesterTestOrphan*" } $match | Should Not Be $null $match.RiskLevel | Should Be 'Risky' $match.Reversible | Should Be $true } finally { Remove-Item -LiteralPath $testKeyPath -Recurse -Force -ErrorAction SilentlyContinue } } It 'does not flag a synthetic entry whose UninstallString points at a real, existing path' { if (-not $isElevated) { return } Remove-Item -LiteralPath $testKeyPath -Recurse -Force -ErrorAction SilentlyContinue New-Item -Path $testKeyPath -Force | Out-Null Set-ItemProperty -Path $testKeyPath -Name 'DisplayName' -Value 'Octa Pester Test Orphan' Set-ItemProperty -Path $testKeyPath -Name 'UninstallString' -Value "`"$PSHOME\powershell.exe`"" try { $actions = @(Get-OctaRegistryOrphansActions) $match = $actions | Where-Object { $_.TargetIdentifier -like "*OctaPesterTestOrphan*" } $match | Should Be $null } finally { Remove-Item -LiteralPath $testKeyPath -Recurse -Force -ErrorAction SilentlyContinue } } } Describe 'Get-OctaRegistryOrphansCategory' { It 'requires elevation and has no irreversible actions (detection-only, reversible via the standard registry snapshot)' { $category = Get-OctaRegistryOrphansCategory $category.RequiresElevation | Should Be $true $category.ContainsIrreversibleActions | Should Be $false } } |