Skip to content

Commit

Permalink
Shorten TestDrive directory name (#2239)
Browse files Browse the repository at this point in the history
* 2238  Shorten TestDrive directory name.

Pester currently uses a full GUID as its temp directory name, which uses 36 characters. This contributes to test errors
when paths used in tests exceed the Windows max path of 260 characters.

The `[IO.Path]::GetTempFileName()` only uses 4 characters for the unique/random part of its filename. Follow the
same pattern in `New-RandomTempDirectory` by only using the 4 characters. A GUID, when converted to a string, only uses
hex characters: 0-9 and a-f, which gives us 65,536 unique possible values. Instead, lets use
`[IO.Path]::GetRandomFileName()`. It generates a name that uses all letters of the alphabet and the numbers 0-9, which
gives us 36^4 = 1,679,616 possible values.

* shorter name in registry

* Pester_ prefix

---------

Co-authored-by: Jakub Jareš <me@jakubjares.com>
  • Loading branch information
splatteredbits and nohwnd authored May 18, 2024
1 parent d62396a commit 87cfcea
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/functions/TestDrive.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ function Clear-TestDrive {
function New-RandomTempDirectory {
do {
$tempPath = Get-TempDirectory
$Path = [IO.Path]::Combine($tempPath, ([Guid]::NewGuid()));
$dirName = 'Pester_' + [IO.Path]::GetRandomFileName().Substring(0, 4)
$Path = [IO.Path]::Combine($tempPath, $dirName)
} until (-not [IO.Directory]::Exists($Path))

[IO.Directory]::CreateDirectory($Path).FullName
Expand Down
2 changes: 1 addition & 1 deletion src/functions/TestRegistry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Get-TestRegistryChildItem ([string]$TestRegistryPath) {
function New-RandomTempRegistry {
do {
$tempPath = Get-TempRegistry
$Path = & $SafeCommands['Join-Path'] -Path $tempPath -ChildPath ([Guid]::NewGuid())
$Path = & $SafeCommands['Join-Path'] -Path $tempPath -ChildPath ([IO.Path]::GetRandomFileName().Substring(0, 4))
} until (-not (& $SafeCommands['Test-Path'] -Path $Path -PathType Container))

try {
Expand Down

0 comments on commit 87cfcea

Please sign in to comment.