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

TempDir in trait not initialized #1409

Closed
thecodesmith opened this issue Dec 31, 2021 · 2 comments
Closed

TempDir in trait not initialized #1409

thecodesmith opened this issue Dec 31, 2021 · 2 comments
Labels

Comments

@thecodesmith
Copy link

thecodesmith commented Dec 31, 2021

Describe the bug

The @TempDir annotation does not work when defined in a trait.

To Reproduce

Adding the following trait and test class in TempDirExtensionSpec reproduces the problem:

trait TempDirTrait {
  @TempDir Path tmp
  @TempDir @Shared Path sharedTmp
}

class TempDirTraitSpec extends Specification implements TempDirTrait {
  void "TempDir works for inherited fields"() {
    expect:
    tmp != null
  }

  void "TempDir works for inherited shared fields"() {
    expect:
    sharedTmp != null
  }
}

Expected behavior

The temporary directory is initialized and the above tests pass.

Actual behavior

Condition not satisfied:

tmp != null
|   |
|   false
null

Condition not satisfied:

tmp != null
|   |
|   false
null

Java version

openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment Corretto-11.0.13.8.1 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.13.8.1 (build 11.0.13+8-LTS, mixed mode)

Buildtool version

------------------------------------------------------------
Gradle 7.3.3
------------------------------------------------------------

Build time:   2021-12-22 12:37:54 UTC
Revision:     6f556c80f945dc54b50e0be633da6c62dbe8dc71

Kotlin:       1.5.31
Groovy:       3.0.9
Ant:          Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM:          11.0.13 (Amazon.com Inc. 11.0.13+8-LTS)
OS:           Mac OS X 12.1 x86_64

What operating system are you using

Mac

Dependencies

N/A - test created in Spock codebase

Additional context

This approach using a trait with a temporary directory previously worked in Spock 1.x using a JUnit @Rule TemporaryFolder. I am updating my test utils library for Spock 2 and I'd like to use the same approach if possible.

This issue is similar to the one here (#1229) for using @TempDir in a base class, which was solved.

@leonard84
Copy link
Member

Unfortunately field extensions don't work in traits, as Groovy doesn't support traits with AST some necessary metadata isn't generated for fields contributed by traits.

@thecodesmith
Copy link
Author

Ah, that is unfortunate. Thanks for the explanation. I will find another way to implement it then.

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

2 participants