Create-Directory in tools won't create directory if at least one exists
See original GitHub issue- Is this issue blocking (yes/no) no
- Is this issue causing unreasonable pain (yes/no) no
Create-Directory
in tools.ps1
won’t create non-existing directories when given at least one existing path. The reason is that it takes string array, but then does not iterate over it, which due to powershell casting rules will return $true, $false
in Test-Path
which casts to $true
in if
and skips the folder creation.
There is no need to check paths when creating foders on the FileSystem provider when using -Force. It wil only create folders that do not exist.
function Create-Directory([string[]] $path) {
if (!(Test-Path $path)) {
New-Item -path $path -force -itemType 'Directory' | Out-Null
}
}
# Pester tests for repro
Describe 'Create-Directory broken' {
It 'returns false given at least one non-existing path' {
$existingPath = [IO.Path]::GetTempPath()
$nonExistingPath = "TestDrive:/thisPathDoesNotExist"
[string[]] $path = $existingPath, $nonExistingPath
[bool] $actual = Test-Path $path
$actual | Should -BeFalse
}
It 'creates folder given one existing path and one non existing path' {
$existingPath = [IO.Path]::GetTempPath()
$nonExistingPath = "TestDrive:/thisPathDoesNotExist"
[string[]] $path = $existingPath, $nonExistingPath
Create-Directory $path
$nonExistingPath | Should -Exist
}
}
# fixed
function Create-Directory([string[]] $Path) {
# no need to check path, New-Item will only create non-existing items on FileSystem provider
New-Item -Path $path -Force -ItemType 'Directory' | Out-Null
}
Describe 'Create-Directory fixed' {
It 'creates folder given one existing path and one non existing path' {
$existingPath = [IO.Path]::GetTempPath()
$nonExistingPath = "TestDrive:/thisPathDoesNotExist"
[string[]] $path = $existingPath, $nonExistingPath
Create-Directory $path
$nonExistingPath | Should -Exist
}
}
Describing Create-Directory broken
[-] returns false given at least one non-existing path 21ms
Expected $false, but got $true.
15: $actual | Should -BeFalse
[-] creates folder given one existing path and one non existing path 47ms
Expected path 'TestDrive:/thisPathDoesNotExist' to exist, but it did not exist.
26: $nonExistingPath | Should -Exist
Describing Create-Directory fixed
[+] creates folder given one existing path and one non existing path 13ms
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Create a directory if it does not exist and then ...
The Files.createDirectories() creates a new directory and parent directories that do not exist. This method does not throw an exception if ...
Read more >How to Create Directory If it Does Not Exist using Python?
In this article, We will learn how to create a Directory if it Does Not Exist using Python. Method 1: Using os.path.exists() and ......
Read more >Directory.CreateDirectory Method (System.IO)
If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory....
Read more >Solved: Dynamic Create File Directory
Solved: Is there a tool to dynamically create a new file directory on the hard drive (or network drive); whereby the file directory...
Read more >AssetDatabase create folders recursively?
IO.Directory.CreateDirectory, which will automatically create folders recursively. You can e.g. immediately create an asset inside that folder ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sure.
@garath Sorry. Done now. 🙂