1+ Import-Module .\WindowsDiskCleanup\WindowsDiskCleanup.psm1 - Force
2+
3+ Describe ' Remove-OldItem.Tests' {
4+ BeforeAll {
5+ $path = " $PSScriptRoot \test.txt"
6+ }
7+
8+ BeforeEach {
9+ $file = New-Item - Path $path - ItemType File - Force
10+ $file.CreationTimeUtc = (Get-Date ).AddDays(-10 )
11+ }
12+
13+ AfterEach {
14+ if (Test-Path $path ) {
15+ Remove-Item - Path $path - Force
16+ }
17+ }
18+
19+ It ' should remove old file by Path (parameter)' {
20+ Remove-OldItem - Path $file.FullName - Days 5 - DryRun:$false
21+ Test-Path $path | Should - Be $false
22+ }
23+
24+ It ' should remove old file by Path (pipeline)' {
25+ $file.FullName | Remove-OldItem - Days 5 - DryRun:$false
26+ Test-Path $path | Should - Be $false
27+ }
28+
29+ It ' should remove old file by File (parameter)' {
30+ Remove-OldItem - File $file - Days 5 - DryRun:$false
31+ Test-Path $path | Should - Be $false
32+ }
33+
34+ It ' should remove old file by File (pipeline)' {
35+ $file | Remove-OldItem - Days 5 - DryRun:$false
36+ Test-Path $path | Should - Be $false
37+ }
38+
39+ It ' should not remove files in dry run mode' {
40+ Remove-OldItem - File $file - Days 5 - DryRun:$true
41+ Test-Path $path | Should - Be $true
42+ }
43+
44+ It ' should not remove files that are not old enough' {
45+ $file.CreationTimeUtc = (Get-Date ).AddDays(-3 )
46+ Remove-OldItem - File $file - Days 5 - DryRun:$false
47+ Test-Path $path | Should - Be $true
48+ }
49+ }
50+
51+ Context ' Remove-CrashDump Tests' {
52+ BeforeAll {
53+ $LOCALAPPDATA = " $PSScriptRoot \LocalAppData"
54+ $env: LOCALAPPDATA = $LOCALAPPDATA
55+ $crashDumpPath = [System.IO.Path ]::Combine($env: LOCALAPPDATA , ' CrashDumps' )
56+ $testDumpFilePath = [System.IO.Path ]::Combine($crashDumpPath , ' test.dmp' )
57+
58+ if (-not (Test-Path $crashDumpPath )) {
59+ New-Item - Path $crashDumpPath - ItemType Directory - Force | Out-Null
60+ }
61+ }
62+
63+ BeforeEach {
64+ $file = New-Item - Path $testDumpFilePath - ItemType File - Force
65+ $file.CreationTimeUtc = (Get-Date ).AddDays(-10 )
66+ }
67+
68+ AfterEach {
69+ if (Test-Path $testDumpFilePath ) {
70+ Remove-Item - Path $testDumpFilePath - Force
71+ }
72+ }
73+
74+ AfterAll {
75+ if (Test-Path $LOCALAPPDATA ) {
76+ Remove-Item - Path $LOCALAPPDATA - Recurse - Force
77+ }
78+ }
79+
80+ It ' should remove crash dump files older than specified days' {
81+ Remove-CrashDump - Days 5 - DryRun:$false
82+ Test-Path $testDumpFilePath | Should - Be $false
83+ }
84+
85+ It ' should not remove crash dump files in dry run mode' {
86+ Remove-CrashDump - Days 5 - DryRun:$true
87+ Test-Path $testDumpFilePath | Should - Be $true
88+ }
89+
90+ It ' should do nothing if crash dump path does not exist' {
91+ Remove-Item - Path $crashDumpPath - Recurse - Force
92+ { Remove-CrashDump - Days 5 - DryRun:$false } | Should -Not - Throw
93+ }
94+ }
0 commit comments