junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

Provide support for setting a custom parent directory for @TempDir

Open rkrisztian opened this issue 4 years ago • 10 comments

My project requires that I do not write to /tmp or whichever location it is when I use the TempDirectory extension. So right now I have no choice but to still use junit-pioneer's implementation, e.g. like this:

@RegisterExtension
Extension tempDirectory = TempDirectory.createInCustomDirectory(() -> Paths.get("build"));

@BeforeEach
void setup(@TempDir Path testProjectDir) {
   // Do something with "testProjectDir".
}

But it would be nicer if I could just use stock JUnit 5 for this. JUnit 4 supported setting the parent dir, so I find the JUnit 5 way a bit too limiting.

Thanks in advance.

rkrisztian avatar Oct 29 '19 18:10 rkrisztian

Tentatively slated for 5.6 M2 solely for the purpose of team discussion

sbrannen avatar Oct 29 '19 20:10 sbrannen

If your project requires you not to write to the default location of temporary files, you should set the java.io.tmpdir system property.

@rkrisztian Could you please try that?

marcphilipp avatar Oct 31 '19 11:10 marcphilipp

Worked, thanks! I wish I knew about this. :)

rkrisztian avatar Nov 01 '19 11:11 rkrisztian

Assigned to the general backlog and labeled as waiting for interest.

sbrannen avatar Nov 01 '19 12:11 sbrannen

FYI: A co-worker of mine suggested the use of AbstractTask#getTemporaryDir():

tasks.withType(Test) {
  systemProperty 'java.io.tmpdir', temporaryDir
}

rkrisztian avatar Jan 14 '20 07:01 rkrisztian

I'm also interested in this functionality.

In my case I only want to affect the parent directory of one @TempDir and therefore it does not make sense to use java.io.tmpdir.

dariocutillas avatar Aug 13 '20 15:08 dariocutillas

in my case junit always set it tempDir to the working directory of the project. This is annoying because it always generates a mess into my source code directories.

Additionally this mess is not deleted after the tests were executed. I mean in this case this is good, because I don't want my source code directories to be deleted. Do you think I forget to configure something, or is this function as intended?

bes1002t avatar Jan 25 '22 21:01 bes1002t

@bes1002t, are you using the @TempDir support in JUnit Jupiter?

If so, then the behavior you are describing sounds like a bug, and we would appreciate it if you would open a separate issue describing your setup and providing a failing example.

sbrannen avatar Jan 26 '22 11:01 sbrannen

@sbrannen yes I'm using JUnit 5:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>

If you don't see an issue here with my dependency declaration, I could create a new ticket with further explanation. :)

bes1002t avatar Apr 25 '22 22:04 bes1002t

in my case junit always set it tempDir to the working directory of the project. This is annoying because it always generates a mess into my source code directories.

@bes1002t, it sounds like something else is setting the temp dir system property.

What is the output of System.out.println(System.getProperty("java.io.tmpdir")) in your tests?

If it's the project's working directory, you'll need to figure out what is setting the java.io.tmpdir JVM system property to that.

sbrannen avatar Jul 27 '22 12:07 sbrannen