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 directory cannot be deleted on Windows with Java 11 #2811
Comments
JUnit Jupiter 5.7.0 is no longer supported, and changes have been made to the So, please try out JUnit Jupiter 5.8.2 and report back here if you still encounter issues with |
Sorry, I should've checked my versions (the IDE even warned me about it). Unfortunately, the error persists: |
That test was failing a lot but not always :( I got rid of this annotation and now the test passes consistently: |
I'll try to reproduce it. |
This bug hit me yesterday, and I was able to reduce my failing test to a very small example: import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class FailedTest {
@TempDir
File tmp;
@Test
void test() throws IOException {
final File f = new File(tmp, "input");
f.mkdir();
Files.list(f.toPath());
}
} Using: JUnit 5.8.2 (but also fails on all versions >= 5.7.1) Unfortunately the original test only fails on 5.8.x but passes on 5.7.x (I cannot publish that one). |
@kgeilmann |
I can confirm. |
EDIT: see #2811 (comment) Adding another diagnostic set, for what I expect is the same root issue: JUnit version: 5.9.0-RC1
/cc @jkschneider EDIT: see #2811 (comment) |
Happened to me as well. |
I also met the same problem, is there any progress? |
I'm seeing this in In the meantime I've been doing: private static Path tempDir;
@BeforeAll
public static void before() throws IOException {
tempDir = Files.createTempDirectory(null);
}
@AfterAll
public static void after() {
tempDir.toFile().delete();
} |
JUnit's @tempdir doesn't work properly on Windows in CI Refs: junit-team/junit5#2811 Refs: e14c3b8
@tempdir breaks build on Windows, see junit-team/junit5#2811
JUnit's `@TempDir` annotation has some issues on Windows, see junit-team/junit5#2811 Where applicable, manual temporary directory management is used instead. --------- Co-authored-by: Jean-Baptiste Lievremont <jeanbaptiste.lievremont@sonarsource.com>
@nmck257 The resource returned by |
You are right. It sounds like my previous report was in line with the documented behavior of the feature, given an unclosed resource in the client code before the test method finished. That said, is there an opportunity to refine the exception thrown in this scenario? Consider this test case: @Test
void foo(@TempDir Path tempDir) throws Exception {
Path filePath = tempDir.resolve("foo.txt");
Files.writeString(filePath, "foo");
Files.newInputStream(filePath);
} It yields:
It tells you that the directory is not empty, but does not tell you the logical root cause: that a specific file in that directory could not be deleted during cleanup. That information would likely be more useful for identifying that there is an unclosed resource. |
@marcphilipp I actually still get this same behavior on 5.9.3. In the debugger, I can see this line getting called, and trace it all the way down to a The file is deleted after the process terminates, though. Maybe the windows filesystem is "accepting" the delete command but deferring it until IO handles are released? Naively, you could add an My environment:
|
The Windows fileapi.h docs say this:
So if a file is still open during So when It would be very useful if |
Encountered a similar issue. Interestingly, tests were green when setting a break point in the last line of the test (just some random assert). However, I then tried out to do a My fix now is:
My naive explanation: Some resource that went already out of scope still has to be |
I have a test that has been failing on
windows-latest
with Java 11 on GitHub Actions CI, but not on Java 17.This is my test:
I added those
deleteOnExit
calls to try to "help" JUnit to get rid of that temp dir, to no avail.Link to GitHub Actions CI Log
Link to JUnit report
The error it shows:
Notice that this same test passes on Java 11 on MacOS and Linux, and on Java 17 on MacOS, Linux and Windows. It seems to fail very consistently on Java 11/Windows.
Steps to reproduce
It should be very easy to reproduce the problem as long as you can run your tests on the specific Windows and Java versions being used by GitHub Actions CI. The code under test will basically just copy a file from one location to another.
Feel free to fork my project to make it easy to run the tests.
GitHub Environment:
Context
Link to build file
Gradle downloaded from (see the wrapper properties):
The text was updated successfully, but these errors were encountered: