#Requires -Version 5.1 <# .SYNOPSIS Pester v5 tests for scripts/Remove-BuildVM.ps1 #> BeforeAll { $script:ScriptPath = Join-Path $PSScriptRoot '..\scripts\Remove-BuildVM.ps1' # Fake vmrun: accepts any args, always exits 0 (best-effort ops don't need to fail here) $script:FakeVmrun = Join-Path $env:TEMP "fake-vmrun-remove-$PID.cmd" Set-Content $script:FakeVmrun -Value '@echo off & exit /b 0' } AfterAll { Remove-Item $script:FakeVmrun -Force -ErrorAction SilentlyContinue } # ───────────────────────────────────────────────────────────────────────────── Describe 'Remove-BuildVM — missing VMX' { It 'returns without error when VMX does not exist' { { & $script:ScriptPath -VMPath 'C:\DoesNotExist\clone.vmx' -VmrunPath $script:FakeVmrun } | Should -Not -Throw } It 'removes partial clone directory even when VMX is missing' { $partialDir = Join-Path $env:TEMP "partial-clone-$PID" New-Item -ItemType Directory -Path $partialDir -Force | Out-Null $fakeMissingVmx = Join-Path $partialDir 'missing.vmx' & $script:ScriptPath -VMPath $fakeMissingVmx -VmrunPath $script:FakeVmrun Test-Path $partialDir | Should -Be $false } } # ───────────────────────────────────────────────────────────────────────────── Describe 'Remove-BuildVM — WhatIf' { It 'does not remove clone directory when -WhatIf is passed' { $cloneDir = Join-Path $env:TEMP "whatif-clone-$PID" $cloneVmx = Join-Path $cloneDir 'whatif-clone.vmx' New-Item -ItemType Directory -Path $cloneDir -Force | Out-Null Set-Content $cloneVmx -Value 'config.version = "8"' & $script:ScriptPath -VMPath $cloneVmx -VmrunPath $script:FakeVmrun -WhatIf Test-Path $cloneDir | Should -Be $true Remove-Item $cloneDir -Recurse -Force -ErrorAction SilentlyContinue } } # ───────────────────────────────────────────────────────────────────────────── Describe 'Remove-BuildVM — successful destroy' { It 'removes the clone directory when VMX exists and vmrun succeeds' { $cloneDir = Join-Path $env:TEMP "live-clone-$PID" $cloneVmx = Join-Path $cloneDir 'live-clone.vmx' New-Item -ItemType Directory -Path $cloneDir -Force | Out-Null Set-Content $cloneVmx -Value 'config.version = "8"' & $script:ScriptPath ` -VMPath $cloneVmx ` -VmrunPath $script:FakeVmrun ` -GracefulTimeoutSeconds 1 # short timeout for test speed Test-Path $cloneDir | Should -Be $false } }