question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
nohwndcommented, Apr 7, 2020

Sure.

0reactions
nohwndcommented, Apr 17, 2020

@garath Sorry. Done now. 🙂

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found