|
| 1 | +Import-Module .\WindowsDiskCleanup\WindowsDiskCleanup.psm1 -Force |
| 2 | + |
| 3 | +Describe 'Remove-DebugDiagLogs.Tests' { |
| 4 | + BeforeAll { |
| 5 | + $originalSystemDrive = $env:SystemDrive |
| 6 | + $testSystemDrive = Join-Path -Path $PSScriptRoot -ChildPath 'TestSystemDrive' |
| 7 | + $env:SystemDrive = $testSystemDrive |
| 8 | + $debugDiagLogPath = Join-Path -Path $testSystemDrive -ChildPath 'Program Files\DebugDiag\Logs' |
| 9 | + |
| 10 | + if (-not (Test-Path $debugDiagLogPath)) { |
| 11 | + New-Item -Path $debugDiagLogPath -ItemType Directory -Force | Out-Null |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + BeforeEach { |
| 16 | + $legacyPath = Join-Path -Path $debugDiagLogPath -ChildPath 'DbgSVC_legacy.txt' |
| 17 | + $datedPath = Join-Path -Path $debugDiagLogPath -ChildPath 'DbgSVC_Date__03_21_2025__Time_08_19_41AM__908__Log.txt' |
| 18 | + |
| 19 | + $legacyFile = New-Item -Path $legacyPath -ItemType File -Force |
| 20 | + $datedFile = New-Item -Path $datedPath -ItemType File -Force |
| 21 | + |
| 22 | + $legacyFile.CreationTimeUtc = (Get-Date).AddDays(-10) |
| 23 | + $datedFile.CreationTimeUtc = (Get-Date).AddDays(-10) |
| 24 | + } |
| 25 | + |
| 26 | + AfterEach { |
| 27 | + if (Test-Path $debugDiagLogPath) { |
| 28 | + Get-ChildItem -Path $debugDiagLogPath -File -Filter 'DbgSVC*.txt' | Remove-Item -Force |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + AfterAll { |
| 33 | + if (Test-Path $testSystemDrive) { |
| 34 | + Remove-Item -Path $testSystemDrive -Recurse -Force |
| 35 | + } |
| 36 | + |
| 37 | + if ($null -ne $originalSystemDrive) { |
| 38 | + $env:SystemDrive = $originalSystemDrive |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + It 'should remove matching DebugDiag files older than specified days' { |
| 43 | + Remove-DebugDiagLogs -Days 5 -DryRun:$false |
| 44 | + |
| 45 | + Test-Path (Join-Path -Path $debugDiagLogPath -ChildPath 'DbgSVC_legacy.txt') | Should -Be $false |
| 46 | + Test-Path (Join-Path -Path $debugDiagLogPath -ChildPath 'DbgSVC_Date__03_21_2025__Time_08_19_41AM__908__Log.txt') | Should -Be $false |
| 47 | + } |
| 48 | + |
| 49 | + It 'should not remove matching DebugDiag files in dry run mode' { |
| 50 | + Remove-DebugDiagLogs -Days 5 -DryRun:$true |
| 51 | + |
| 52 | + Test-Path (Join-Path -Path $debugDiagLogPath -ChildPath 'DbgSVC_legacy.txt') | Should -Be $true |
| 53 | + Test-Path (Join-Path -Path $debugDiagLogPath -ChildPath 'DbgSVC_Date__03_21_2025__Time_08_19_41AM__908__Log.txt') | Should -Be $true |
| 54 | + } |
| 55 | + |
| 56 | + It 'should do nothing if DebugDiag log path does not exist' { |
| 57 | + Remove-Item -Path $debugDiagLogPath -Recurse -Force |
| 58 | + |
| 59 | + { Remove-DebugDiagLogs -Days 5 -DryRun:$false } | Should -Not -Throw |
| 60 | + } |
| 61 | +} |
0 commit comments