Skip to content

Commit

Permalink
Set readonly attribute to false if DosFileAttributeView is supported
Browse files Browse the repository at this point in the history
Fixes #3352.
  • Loading branch information
marcphilipp authored Jul 2, 2023
1 parent 8514156 commit eb34ed7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ JUnit repository on GitHub.
the documentation for extension registration via `@ExtendWith` on fields.
* The scope of applicability for `TestWatcher` implementations is now more extensively
documented in the User Guide and Javadoc.
* `@TempDir` now successfully cleans up files and directories on Windows that are set to
read-only.


[[release-notes-5.10.0-RC1-junit-vintage]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.DosFileAttributeView;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -390,6 +391,15 @@ private static void tryToResetPermissions(Path path) {
if (Files.isDirectory(path)) {
file.setExecutable(true);
}
DosFileAttributeView dos = Files.getFileAttributeView(path, DosFileAttributeView.class);
if (dos != null) {
try {
dos.setReadOnly(false);
}
catch (IOException ignore) {
// nothing we can do
}
}
}

private IOException createIOExceptionWithAttachedFailures(SortedMap<Path, IOException> failures) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.DosFileAttributeView;
import java.util.Deque;
import java.util.LinkedList;
import java.util.function.Supplier;
Expand All @@ -47,8 +48,6 @@
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
Expand Down Expand Up @@ -126,15 +125,13 @@ void nonMintPermissionsDoNotCauseFailure() {
}

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

@Test
@DisabledOnOs(OS.WINDOWS)
@DisplayName("is capable of removing a read-only file in a dir in a read-only dir")
void readOnlyFileInNestedReadOnlyDirDoesNotCauseFailure() {
executeTestsForClass(ReadOnlyFileInDirInReadOnlyDirDoesNotCauseFailureTestCase.class).testEvents()//
Expand Down Expand Up @@ -942,8 +939,8 @@ static class ReadOnlyFileInReadOnlyDirDoesNotCauseFailureTestCase {
void createReadOnlyFileInReadOnlyDir(@TempDir File tempDir) throws IOException {
File file = tempDir.toPath().resolve("file").toFile();
assumeTrue(file.createNewFile());
assumeTrue(tempDir.setReadOnly());
assumeTrue(file.setReadOnly());
assumeTrue(makeReadOnly(tempDir));
assumeTrue(makeReadOnly(file));
}

}
Expand All @@ -956,13 +953,22 @@ void createReadOnlyFileInReadOnlyDir(@TempDir File tempDir) throws IOException {
File file = tempDir.toPath().resolve("dir").resolve("file").toFile();
assumeTrue(file.getParentFile().mkdirs());
assumeTrue(file.createNewFile());
assumeTrue(tempDir.setReadOnly());
assumeTrue(file.getParentFile().setReadOnly());
assumeTrue(file.setReadOnly());
assumeTrue(makeReadOnly(tempDir));
assumeTrue(makeReadOnly(file.getParentFile()));
assumeTrue(makeReadOnly(file));
}

}

private static boolean makeReadOnly(File file) throws IOException {
var dos = Files.getFileAttributeView(file.toPath(), DosFileAttributeView.class);
if (dos != null) {
dos.setReadOnly(true);
return true;
}
return file.setReadOnly();
}

// https://github.com/junit-team/junit5/issues/2609
@SuppressWarnings("ResultOfMethodCallIgnored")
static class NonMintPermissionContentInTempDirectoryDoesNotCauseFailureTestCase {
Expand Down

0 comments on commit eb34ed7

Please sign in to comment.