Last active
March 7, 2021 15:11
-
-
Save neil-sabol/76e0f74e9477652b9b7334ee0af60ec9 to your computer and use it in GitHub Desktop.
See https://blog.neilsabol.site/post/deleting-specific-log-files-by-extension-older-than-days/. This Pester snippet demonstrates the use of -Path vs. -LiteralPath with the Get-ChildItem PowerShell cmdlet. Prerequisites: Pester 5+ (Install-Module -Name Pester -Force -SkipPublisherCheck), PowerShell 5+, Windows 2012+. Once the prerequisites are in…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ############################### | |
# Get-ChildItem-Path.Tests.ps1 | |
# https://blog.neilsabol.site/post/deleting-specific-log-files-by-extension-older-than-days/ | |
# Neil Sabol | |
# neil.sabol@gmail.com | |
# ############################### | |
# Define test parameters | |
$script:testPath = "C:\Temp" | |
$script:testLiteralPath = "\\?\C:\Temp" | |
$script:testPathDepth = 6 | |
$script:testPathDirNameLength = 52 | |
$script:testPathFileCount = 10 | |
$script:testPathFileNameLength = 255 | |
# Print test parameters | |
write-host "" | |
write-host "Directory Path Length: $($testPathDepth*$testPathDirNameLength)" | |
write-host "File Name Length: $($testPathFileNameLength)" | |
write-host "Total Length: $($testPathDepth*$testPathDirNameLength+$testPathFileNameLength)" | |
write-host "" | |
# Begin Pester tests | |
Describe 'Get-ChildItem Path Testing' { | |
BeforeAll { | |
# Create the test path if it does not exist | |
If(-Not (Test-Path "$testPath")) { New-Item -Path "$testPath" -ItemType "Directory" } | |
# Create a long directory path | |
for($i=0;$i -lt $testPathDepth;$i++) { | |
$longPath += "$(-join (((65..90) + (97..122)) * 100 | Get-Random -Count $testPathDirNameLength | % {[char]$_}))\" | |
} | |
New-Item -Path "$testLiteralPath" -Name "$longPath" -ItemType "Directory" -Force | |
# Get the root of the long path | |
$rootTestDirectory = $longPath[0..$($testPathDirNameLength-1)] -join "" | |
# Create some test files under the long path | |
for($i=0;$i -lt $testPathFileCount;$i++) { | |
New-Item -Path "$testLiteralPath\$longPath" -Name "$(-join ((48..57) * 100 | Get-Random -Count $($testPathFileNameLength-4) | % {[char]$_})).txt" -ItemType "File" -Value "$(-join ((65..90) + (97..122) | Get-Random -Count 40 | % {[char]$_}))" -Force | |
} | |
} | |
Context "Get-ChildItem with -Path" { | |
It 'Should throw exception when listing long path contents' { | |
{ Get-ChildItem -Path "$testPath\$rootTestDirectory" -Recurse -ErrorAction Stop | Where-Object { $_.Name -like '*.txt' } } | Should -Throw | |
} | |
It 'Should throw exception when deleting all files under a long path' { | |
{ Get-ChildItem -Path "$testPath\$rootTestDirectory" -Recurse -ErrorAction Stop | Where-Object { $_.Name -like '*.txt' } | Remove-Item } | Should -Throw | |
} | |
} | |
Context "Get-ChildItem with -LiteralPath" { | |
It 'Should NOT throw exception when listing long path contents' { | |
{ Get-ChildItem -LiteralPath "$testLiteralPath\$rootTestDirectory" -Recurse -ErrorAction Stop | Where-Object { $_.Name -like '*.txt' } } | Should -Not -Throw | |
} | |
It 'Should NOT throw exception when deleting all files under a long path' { | |
{ Get-ChildItem -LiteralPath "$testLiteralPath\$rootTestDirectory" -Recurse -ErrorAction Stop | Where-Object { $_.Name -like '*.txt' } | Remove-Item -ErrorAction Stop } | Should -Not -Throw | |
} | |
It "Should delete all files under a long path" { | |
Get-ChildItem -LiteralPath "$testLiteralPath\$rootTestDirectory" -Recurse -ErrorAction Stop | Where-Object { $_.Name -like '*.txt' } | Measure-Object -ErrorAction Stop | select -ExpandProperty Count | Should -Be 0 | |
} | |
} | |
AfterAll { | |
# Clean up | |
Remove-Item -Path "$script:testLiteralPath\$rootTestDirectory" -Recurse -Force | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment