Skip to content

Commit

Permalink
Simplified the setting of permissions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdemaeyer committed Nov 9, 2024
1 parent 813acd6 commit f35036f
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/test/java/org/apache/maven/plugins/clean/CleanerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static java.nio.file.Files.createDirectory;
import static java.nio.file.Files.createFile;
import static java.nio.file.Files.exists;
import static java.nio.file.Files.getPosixFilePermissions;
import static java.nio.file.Files.setPosixFilePermissions;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -77,11 +76,8 @@ void deleteFailsWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws Excep
when(log.isWarnEnabled()).thenReturn(true);
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();
createFile(basedir.resolve("file"));
final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
// Remove the executable flag to prevent directory listing, which will result in a DirectoryNotEmptyException.
final String rw_rw_r__ = rwxrwxr_x.replace('x', '-');
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(rw_rw_r__);
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-rw-r--");
setPosixFilePermissions(basedir, permissions);
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
final IOException exception =
Expand All @@ -98,11 +94,8 @@ void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Excepti
assumeTrue(POSIX_COMPLIANT);
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();
createFile(basedir.resolve("file"));
final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
// Remove the executable flag to prevent directory listing, which will result in a DirectoryNotEmptyException.
final String rw_rw_r__ = rwxrwxr_x.replace('x', '-');
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(rw_rw_r__);
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-rw-r--");
setPosixFilePermissions(basedir, permissions);
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
final IOException exception =
Expand All @@ -119,11 +112,8 @@ void deleteLogsWarningWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws
when(log.isWarnEnabled()).thenReturn(true);
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();
final Path file = createFile(basedir.resolve("file"));
final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
// Remove the writable flag to prevent deletion of the file, which will result in an AccessDeniedException.
final String r_xr_xr_x = rwxrwxr_x.replace('w', '-');
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(r_xr_xr_x);
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("r-xr-xr-x");
setPosixFilePermissions(basedir, permissions);
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false));
Expand All @@ -143,11 +133,8 @@ void deleteDoesNotLogAnythingWhenNoPermissionAndWarnDisabled(@TempDir Path tempD
when(log.isWarnEnabled()).thenReturn(false);
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();
createFile(basedir.resolve("file"));
final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
// Remove the writable flag to prevent deletion of the file, which will result in an AccessDeniedException.
final String r_xr_xr_x = rwxrwxr_x.replace('w', '-');
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(r_xr_xr_x);
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("r-xr-xr-x");
setPosixFilePermissions(basedir, permissions);
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false));
Expand Down

0 comments on commit f35036f

Please sign in to comment.