Skip to content

Commit eb34ed7

Browse files
authored
Set readonly attribute to false if DosFileAttributeView is supported
Fixes #3352.
1 parent 8514156 commit eb34ed7

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.10.0-RC1.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ JUnit repository on GitHub.
6464
the documentation for extension registration via `@ExtendWith` on fields.
6565
* The scope of applicability for `TestWatcher` implementations is now more extensively
6666
documented in the User Guide and Javadoc.
67+
* `@TempDir` now successfully cleans up files and directories on Windows that are set to
68+
read-only.
6769

6870

6971
[[release-notes-5.10.0-RC1-junit-vintage]]

junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.Paths;
3434
import java.nio.file.SimpleFileVisitor;
3535
import java.nio.file.attribute.BasicFileAttributes;
36+
import java.nio.file.attribute.DosFileAttributeView;
3637
import java.util.Collections;
3738
import java.util.HashSet;
3839
import java.util.Set;
@@ -390,6 +391,15 @@ private static void tryToResetPermissions(Path path) {
390391
if (Files.isDirectory(path)) {
391392
file.setExecutable(true);
392393
}
394+
DosFileAttributeView dos = Files.getFileAttributeView(path, DosFileAttributeView.class);
395+
if (dos != null) {
396+
try {
397+
dos.setReadOnly(false);
398+
}
399+
catch (IOException ignore) {
400+
// nothing we can do
401+
}
402+
}
393403
}
394404

395405
private IOException createIOExceptionWithAttachedFailures(SortedMap<Path, IOException> failures) {

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TempDirectoryPerContextTests.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.io.IOException;
3131
import java.nio.file.Files;
3232
import java.nio.file.Path;
33+
import java.nio.file.attribute.DosFileAttributeView;
3334
import java.util.Deque;
3435
import java.util.LinkedList;
3536
import java.util.function.Supplier;
@@ -47,8 +48,6 @@
4748
import org.junit.jupiter.api.TestInfo;
4849
import org.junit.jupiter.api.TestInstance;
4950
import org.junit.jupiter.api.TestMethodOrder;
50-
import org.junit.jupiter.api.condition.DisabledOnOs;
51-
import org.junit.jupiter.api.condition.OS;
5251
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
5352
import org.junit.jupiter.api.extension.ExtensionContext;
5453
import org.junit.jupiter.api.extension.ParameterResolutionException;
@@ -126,15 +125,13 @@ void nonMintPermissionsDoNotCauseFailure() {
126125
}
127126

128127
@Test
129-
@DisabledOnOs(OS.WINDOWS)
130128
@DisplayName("is capable of removing a read-only file in a read-only dir")
131129
void readOnlyFileInReadOnlyDirDoesNotCauseFailure() {
132130
executeTestsForClass(ReadOnlyFileInReadOnlyDirDoesNotCauseFailureTestCase.class).testEvents()//
133131
.assertStatistics(stats -> stats.started(1).succeeded(1));
134132
}
135133

136134
@Test
137-
@DisabledOnOs(OS.WINDOWS)
138135
@DisplayName("is capable of removing a read-only file in a dir in a read-only dir")
139136
void readOnlyFileInNestedReadOnlyDirDoesNotCauseFailure() {
140137
executeTestsForClass(ReadOnlyFileInDirInReadOnlyDirDoesNotCauseFailureTestCase.class).testEvents()//
@@ -942,8 +939,8 @@ static class ReadOnlyFileInReadOnlyDirDoesNotCauseFailureTestCase {
942939
void createReadOnlyFileInReadOnlyDir(@TempDir File tempDir) throws IOException {
943940
File file = tempDir.toPath().resolve("file").toFile();
944941
assumeTrue(file.createNewFile());
945-
assumeTrue(tempDir.setReadOnly());
946-
assumeTrue(file.setReadOnly());
942+
assumeTrue(makeReadOnly(tempDir));
943+
assumeTrue(makeReadOnly(file));
947944
}
948945

949946
}
@@ -956,13 +953,22 @@ void createReadOnlyFileInReadOnlyDir(@TempDir File tempDir) throws IOException {
956953
File file = tempDir.toPath().resolve("dir").resolve("file").toFile();
957954
assumeTrue(file.getParentFile().mkdirs());
958955
assumeTrue(file.createNewFile());
959-
assumeTrue(tempDir.setReadOnly());
960-
assumeTrue(file.getParentFile().setReadOnly());
961-
assumeTrue(file.setReadOnly());
956+
assumeTrue(makeReadOnly(tempDir));
957+
assumeTrue(makeReadOnly(file.getParentFile()));
958+
assumeTrue(makeReadOnly(file));
962959
}
963960

964961
}
965962

963+
private static boolean makeReadOnly(File file) throws IOException {
964+
var dos = Files.getFileAttributeView(file.toPath(), DosFileAttributeView.class);
965+
if (dos != null) {
966+
dos.setReadOnly(true);
967+
return true;
968+
}
969+
return file.setReadOnly();
970+
}
971+
966972
// https://github.com/junit-team/junit5/issues/2609
967973
@SuppressWarnings("ResultOfMethodCallIgnored")
968974
static class NonMintPermissionContentInTempDirectoryDoesNotCauseFailureTestCase {

0 commit comments

Comments
 (0)