Skip to content

Commit 00ddcdc

Browse files
committed
Refactoring
1 parent e65f83d commit 00ddcdc

7 files changed

+144
-100
lines changed

Tests/Remove-CrashDump.Tests.ps1

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Import-Module .\WindowsDiskCleanup\WindowsDiskCleanup.psm1 -Force
2+
3+
Describe 'Remove-CrashDump.Tests' {
4+
BeforeAll {
5+
$LOCALAPPDATA = "$PSScriptRoot\LocalAppData"
6+
$env:LOCALAPPDATA = $LOCALAPPDATA
7+
$crashDumpPath = [System.IO.Path]::Combine($env:LOCALAPPDATA, 'CrashDumps')
8+
$testDumpFilePath = [System.IO.Path]::Combine($crashDumpPath, 'test.dmp')
9+
10+
if (-not (Test-Path $crashDumpPath)) {
11+
New-Item -Path $crashDumpPath -ItemType Directory -Force | Out-Null
12+
}
13+
}
14+
15+
BeforeEach {
16+
$file = New-Item -Path $testDumpFilePath -ItemType File -Force
17+
$file.CreationTimeUtc = (Get-Date).AddDays(-10)
18+
}
19+
20+
AfterEach {
21+
if (Test-Path $testDumpFilePath) {
22+
Remove-Item -Path $testDumpFilePath -Force
23+
}
24+
}
25+
26+
AfterAll {
27+
if (Test-Path $LOCALAPPDATA) {
28+
Remove-Item -Path $LOCALAPPDATA -Recurse -Force
29+
}
30+
}
31+
32+
It 'should remove crash dump files older than specified days' {
33+
Remove-CrashDump -Days 5 -DryRun:$false
34+
Test-Path $testDumpFilePath | Should -Be $false
35+
}
36+
37+
It 'should not remove crash dump files in dry run mode' {
38+
Remove-CrashDump -Days 5 -DryRun:$true
39+
Test-Path $testDumpFilePath | Should -Be $true
40+
}
41+
42+
It 'should do nothing if crash dump path does not exist' {
43+
Remove-Item -Path $crashDumpPath -Recurse -Force
44+
{ Remove-CrashDump -Days 5 -DryRun:$false } | Should -Not -Throw
45+
}
46+
}

Tests/Remove-OldItem.Tests.ps1

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

Tests/WindowsDiskCleanup.Tests.ps1

-96
This file was deleted.

WindowsDiskCleanup/Public/Remove-CrashDump.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function Remove-CrashDump {
1010
process {
1111
$crashDumpPath = [System.IO.Path]::Combine($env:LOCALAPPDATA, 'CrashDumps')
1212
if (Test-Path $crashDumpPath) {
13-
Get-ChildItem $crashDumpPath -Filter "*.dmp" | Remove-OldFile -Days $Days -DryRun:$DryRun
13+
Get-ChildItem $crashDumpPath -Filter "*.dmp" | Remove-OldItem -Days $Days -DryRun:$DryRun
1414
}
1515
}
1616
}

WindowsDiskCleanup/Public/Remove-IISLog.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Remove-IISLog {
1111

1212
process {
1313
if (Test-Path $LogFilesFolder) {
14-
Get-ChildItem $LogFilesFolder -Recurse -Filter *.log | Remove-OldFile -Days $Days -DryRun:$DryRun
14+
Get-ChildItem $LogFilesFolder -Recurse -Filter *.log | Remove-OldItem -Days $Days -DryRun:$DryRun
1515
}
1616
}
1717
}

WindowsDiskCleanup/Public/Remove-OldFile.ps1 renamed to WindowsDiskCleanup/Public/Remove-OldItem.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Remove-OldFile {
1+
function Remove-OldItem {
22
[CmdletBinding()]
33
param(
44
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = "File")]

WindowsDiskCleanup/Public/Remove-TempASPNETFile.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Remove-TempASPNETFile {
1111

1212
process {
1313
if (Test-Path $FolderPath) {
14-
Get-ChildItem $FolderPath | Remove-OldFile -Days $Days -DryRun:$DryRun
14+
Get-ChildItem $FolderPath | Remove-OldItem -Days $Days -DryRun:$DryRun
1515
}
1616
}
1717
}

0 commit comments

Comments
 (0)