Remove folder Pester test behavior

Welcome Forums Pester Remove folder Pester test behavior

This topic contains 4 replies, has 3 voices, and was last updated by

1 year, 8 months ago.

  • Author
  • #71678

    Points: 2
    Rank: Member


    Im currently learning Pester and cant see why this behavior is happening

    The first snippet the pester test in the gist works and it shows all green. The second one where I first takes the result to $test to be able to log how many folders and what folders are removed i get an error first

    Remove-Item : Cannot find path 'C:\Users\xxx\AppData\Local\Temp\1550f297-536e-4d0e-8138-b51f964542b4\env\empty\diff' because it does not exist.
    At E:\GitHub\Learnpester\LearnPester\File\Remove-backupfile.ps1:117 char:13
    + $test | Remove-Item -Force -Recurse
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (C:\Users\xxx\...\env\empty\diff:String) [Remove-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

    But the test then shows green.

    Remove folder

    So is this by design in Testdrive or for pester or i'm I doing something very wrong?
    And is there a better way to the pester test or to remove empty folders and get how many and what folders are removed.
    I could grab it all first to a variable but since there are many folders and files I think this method is better, but I can be wrong.

  • #71732

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    TESTDRIVE: is scoped, so you really want to keep every use of it contained within the same scope – e.g., Describe or Context block. Just scanning this, it appears like you're doing a lot of setup out-of-scope, and then expecting it to exist in-scope – is that right?

  • #71765

    Points: 2
    Rank: Member

    Thanks Don

    I have updated the gist. I made two small function of the code and then added pester for both. I changed the setup of Testdrive to the Context block from the describe block.

    The Pester test is the same for both of the functions, just changed what function they are calling remove-emptyfolders-t1 or remove-emptyfolders-t2. The function remove-emptyfolders-t1 returns all green but remove-emptyfolders-t2 I get the error above.

  • #71779

    Points: 22
    Rank: Member

    I think the problem is that in your t2 function, PowerShell removes the `env\empty` before which includes the diff subdir. It then tries to remove the `env\empty\diff` dir and it does not exist.

  • #71807

    Points: 2
    Rank: Member


    I see what you mean Adam. Since all the folders under the root folder is empty my script will remove the empty folder first. It will then try to remove the sub folders.

    Basically it does a FIFO when it removes and I need to be FILO so need to reverse the delete order.

    But isn't that the behavior of the t1 function as well?

    I changed it a bit little, maybe not the best solution, since it will have to redo it until $test is 0, but it will work.

    do {
        $test = Get-ChildItem -Path $path -Recurse -Directory -Force | 
          Where-Object {$null -eq (Get-ChildItem -Path $_.FullName -Recurse -Force)}
        write-host $test
        $test | Remove-Item -Force -Recurse
      } while ($test.count -ne 0)

    Thanks for the help!

The topic ‘Remove folder Pester test behavior’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort