Skip to content

Commit

Permalink
Removed the resetting of permissions in tests, relying on JUnit @tempdir
Browse files Browse the repository at this point in the history
 to take care of permissions issues when clearing the temporary directory
  • Loading branch information
peterdemaeyer committed Nov 8, 2024
1 parent 5f801d4 commit 1616a1b
Showing 1 changed file with 32 additions and 49 deletions.
81 changes: 32 additions & 49 deletions src/test/java/org/apache/maven/plugins/clean/CleanerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,18 @@ void deleteFailsWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws Excep
createFile(basedir.resolve("file"));
final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
// Prevent directory listing, which will result in a DirectoryNotEmptyException.
// 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__);
setPosixFilePermissions(basedir, permissions);
try {
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
final IOException exception =
assertThrows(IOException.class, () -> cleaner.delete(basedir.toFile(), null, false, true, false));
verify(log, never()).warn(any(CharSequence.class), any(Throwable.class));
assertEquals("Failed to delete " + basedir, exception.getMessage());
final DirectoryNotEmptyException cause =
assertInstanceOf(DirectoryNotEmptyException.class, exception.getCause());
assertEquals(basedir.toString(), cause.getMessage());
} finally {
// Allow the tempDir to be cleared by the @TempDir extension.
setPosixFilePermissions(basedir, initialPermissions);
}
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
final IOException exception =
assertThrows(IOException.class, () -> cleaner.delete(basedir.toFile(), null, false, true, false));
verify(log, never()).warn(any(CharSequence.class), any(Throwable.class));
assertEquals("Failed to delete " + basedir, exception.getMessage());
final DirectoryNotEmptyException cause =
assertInstanceOf(DirectoryNotEmptyException.class, exception.getCause());
assertEquals(basedir.toString(), cause.getMessage());
}

@Test
Expand All @@ -105,22 +100,17 @@ void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Excepti
createFile(basedir.resolve("file"));
final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
// Prevent directory listing, which will result in a DirectoryNotEmptyException.
// 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__);
setPosixFilePermissions(basedir, permissions);
try {
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
final IOException exception =
assertThrows(IOException.class, () -> cleaner.delete(basedir.toFile(), null, false, true, true));
assertEquals("Failed to delete " + basedir, exception.getMessage());
final DirectoryNotEmptyException cause =
assertInstanceOf(DirectoryNotEmptyException.class, exception.getCause());
assertEquals(basedir.toString(), cause.getMessage());
} finally {
// Allow the tempDir to be cleared by the @TempDir extension.
setPosixFilePermissions(basedir, initialPermissions);
}
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
final IOException exception =
assertThrows(IOException.class, () -> cleaner.delete(basedir.toFile(), null, false, true, true));
assertEquals("Failed to delete " + basedir, exception.getMessage());
final DirectoryNotEmptyException cause =
assertInstanceOf(DirectoryNotEmptyException.class, exception.getCause());
assertEquals(basedir.toString(), cause.getMessage());
}

@Test
Expand All @@ -131,24 +121,20 @@ void deleteLogsWarningWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws
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 a AccessDeniedException.
final String r_xr_xr_x = rwxrwxr_x.replace('w', '-');
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(r_xr_xr_x);
setPosixFilePermissions(basedir, permissions);
try {
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false));
verify(log, times(2)).warn(any(CharSequence.class), any(Throwable.class));
InOrder inOrder = inOrder(log);
ArgumentCaptor<AccessDeniedException> cause1 = ArgumentCaptor.forClass(AccessDeniedException.class);
inOrder.verify(log).warn(eq("Failed to delete " + file), cause1.capture());
assertEquals(file.toString(), cause1.getValue().getMessage());
ArgumentCaptor<DirectoryNotEmptyException> cause2 =
ArgumentCaptor.forClass(DirectoryNotEmptyException.class);
inOrder.verify(log).warn(eq("Failed to delete " + basedir), cause2.capture());
assertEquals(basedir.toString(), cause2.getValue().getMessage());
} finally {
setPosixFilePermissions(basedir, initialPermissions);
}
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false));
verify(log, times(2)).warn(any(CharSequence.class), any(Throwable.class));
InOrder inOrder = inOrder(log);
ArgumentCaptor<AccessDeniedException> cause1 = ArgumentCaptor.forClass(AccessDeniedException.class);
inOrder.verify(log).warn(eq("Failed to delete " + file), cause1.capture());
assertEquals(file.toString(), cause1.getValue().getMessage());
ArgumentCaptor<DirectoryNotEmptyException> cause2 = ArgumentCaptor.forClass(DirectoryNotEmptyException.class);
inOrder.verify(log).warn(eq("Failed to delete " + basedir), cause2.capture());
assertEquals(basedir.toString(), cause2.getValue().getMessage());
}

@Test
Expand All @@ -159,15 +145,12 @@ void deleteDoesNotLogAnythingWhenNoPermissionAndWarnDisabled(@TempDir Path tempD
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 a AccessDeniedException.
final String r_xr_xr_x = rwxrwxr_x.replace('w', '-');
final Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(r_xr_xr_x);
setPosixFilePermissions(basedir, permissions);
try {
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false));
verify(log, never()).warn(any(CharSequence.class), any(Throwable.class));
} finally {
setPosixFilePermissions(basedir, initialPermissions);
}
final Cleaner cleaner = new Cleaner(null, log, false, null, null);
assertDoesNotThrow(() -> cleaner.delete(basedir.toFile(), null, false, false, false));
verify(log, never()).warn(any(CharSequence.class), any(Throwable.class));
}
}

0 comments on commit 1616a1b

Please sign in to comment.