Skip to content

Commit 351de3b

Browse files
committed
Add tests for Remove-DebugDiagLogs functionality and improve log path handling
1 parent 358f5aa commit 351de3b

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

WindowsDiskCleanup/Public/Remove-DebugDiagLogs.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ function Remove-DebugDiagLogs {
88
)
99

1010
process {
11-
Get-Item "$env:SystemDrive\Program Files\DebugDiag\Logs\DbgSVC_*.txt" | Remove-OldItem -Days $Days -DryRun:$DryRun
11+
$debugDiagLogsPath = Join-Path -Path $env:SystemDrive -ChildPath 'Program Files\DebugDiag\Logs'
12+
if (Test-Path $debugDiagLogsPath) {
13+
Get-ChildItem -Path $debugDiagLogsPath -File -Filter 'DbgSVC*.txt' | Remove-OldItem -Days $Days -DryRun:$DryRun
14+
}
1215
}
1316
}

0 commit comments

Comments
 (0)