Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestDrive behaves like Context in nested Describe #1837

Closed
Axiometry opened this issue Feb 4, 2021 · 5 comments
Closed

TestDrive behaves like Context in nested Describe #1837

Axiometry opened this issue Feb 4, 2021 · 5 comments
Labels

Comments

@Axiometry
Copy link

General summary of the issue

I nested a Describe block in another Describe block, with the intention of having a new TestDrive set up for the nested Describe block (while leaving the main Describe block's TestDrive intact). However, what I found was that the nested Describe block was given the same TestDrive folder as the main Describe block. I also observed the rest of the behavior usually associated with Context blocks, as described here: https://pester-docs.netlify.app/docs/usage/testdrive

Describe your environment

Pester version     : 5.1.1
PowerShell version : 7.1.0
OS version         : Microsoft Windows NT 10.0.19042.0

Steps to reproduce

Describe "Parent" {
    It "BeforeChild" {
        Write-Host "Parent Initial TestDrive: $TestDrive"
        Set-Content -Path "$TestDrive\file1.txt" -Value "parent"
    }

    Describe "Child" {
        It "RunChild" {
            Write-Host "Child TestDrive: $TestDrive"
            if (Test-Path "$TestDrive\file1.txt") {
                Write-Host "Preexisting file1.txt: $(Get-Content -Path "$TestDrive\file1.txt")"
            } else {
                Write-Host "No preexisting file1.txt"
            }
            Set-Content -Path "$TestDrive\file1.txt" -Value "child"
            Set-Content -Path "$TestDrive\file2.txt" -Value "child"
        }
    }

    It "AfterChild" {
        Write-Host "Parent Final TestDrive: $TestDrive"
        if (Test-Path "$TestDrive\file1.txt") {
            Write-Host "Final file1.txt: $(Get-Content -Path "$TestDrive\file1.txt")"
        } else {
            Write-Host "Final file1.txt not found"
        }

        if (Test-Path "$TestDrive\file2.txt") {
            Write-Host "Final file2.txt: $(Get-Content -Path "$TestDrive\file2.txt")"
        } else {
            Write-Host "Final file2.txt not found"
        }
    }
}

Expected Behavior

The documentation doesn't state that a nested Describe block functions like a Context block. As a result, I expected a nested Describe block to function just like a top-level Describe block.

Specifically:

  1. A new TestDrive is created for the nested Describe block, separate from the TestDrive for the top-level Describe block.
  2. The TestDrive for the nested Describe block is cleaned up when it finishes.
  3. The top-level Describe block retains its TestDrive, which is untouched by the execution of the nested Describe block (unless the nested Describe block explicitly makes changes in the top-level Describe block's TestDrive).

Example of output from above, if it worked as I'd expect:

Describing Parent
Parent Initial TestDrive: C:\Users\user\AppData\Local\Temp\efe71c15-97a2-4c2b-ba29-4f6a3009e013
  [+] BeforeChild 89ms (58ms|31ms)
 Describing Child
Child TestDrive: C:\Users\user\AppData\Local\Temp\02998c99-1b35-41e7-90b7-4f52a9ce36ae
No preexisting file1.txt
   [+] RunChild 47ms (43ms|4ms)
Parent Final TestDrive: C:\Users\user\AppData\Local\Temp\efe71c15-97a2-4c2b-ba29-4f6a3009e013
Final file1.txt: parent
Final file2.txt not found
  [+] AfterChild 15ms (14ms|1ms)

Current Behavior

Describing Parent
Parent Initial TestDrive: C:\Users\user\AppData\Local\Temp\efe71c15-97a2-4c2b-ba29-4f6a3009e013
  [+] BeforeChild 89ms (58ms|31ms)
 Describing Child
Child TestDrive: C:\Users\user\AppData\Local\Temp\efe71c15-97a2-4c2b-ba29-4f6a3009e013
Preexisting file1.txt: parent
   [+] RunChild 47ms (43ms|4ms)
Parent Final TestDrive: C:\Users\user\AppData\Local\Temp\efe71c15-97a2-4c2b-ba29-4f6a3009e013
Final file1.txt: child
Final file2.txt not found
  [+] AfterChild 15ms (14ms|1ms)

Possible Solution? (optional)

@nohwnd
Copy link
Member

nohwnd commented Feb 4, 2021

This is by design. TestDrive is setup only for the top-level block, be it Describe or Context and re-used all the way down. Describe and Context are interchangeable 99% of the time, except for Mock -Scope Describe vs Mock -Scope Context. So in this case it does not matter if you put Context in Describe or Describe in Context. It will setup TestDrive only for the top-level block.

@Axiometry
Copy link
Author

Understood, thanks for your time. In that case, I see two sub-issues from this:

  1. Bug Report: The documentation could be clearer about this. (It explicitly says Context block when it's actually any nested Context or Describe block.)
  2. Feature Request: The behavior I described in the "Expected Behavior" section would be very useful.

What are your thoughts?

@nohwnd
Copy link
Member

nohwnd commented Mar 4, 2021

@Axiometry I will stick with the current behavior. The proposed behavior sounds nice for some cases, but would be complicated to implement, full of edge cases, and file copying. And because TestDrive is a PSDrive, talking with it is not the fastest. Additionally it should also work the same way for TestRegistry. So there is a lot of thinking to be done, and backwards compatibility. If you want to PR it I will by happy to review you code and help you steer it, but can't commit to implementing it myself.

If you need a clean test drive for a block, I would suggest you add BeforeAll and create a directory in the TestDrive and give it a random name, and save that path into a variable, and use that in your tests.

@nohwnd nohwnd added the Feature label Mar 4, 2021
@fflaten
Copy link
Collaborator

fflaten commented May 19, 2021

@Axiometry See updated Scoping-docs for Testdrive, https://pester-docs.netlify.app/docs/usage/testdrive

Is it clearer now?

@fflaten
Copy link
Collaborator

fflaten commented Jul 16, 2022

Closing as won't fix. TestDrive is created per container and creating subfolders is a good solution for this. See nohwnd's suggestion and docs.

@fflaten fflaten closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants