-
Notifications
You must be signed in to change notification settings - Fork 52
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
[MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure #60
Merged
elharo
merged 6 commits into
apache:maven-clean-plugin-3.x
from
peterdemaeyer:MCLEAN-124
Nov 9, 2024
+163
−4
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f1885c
MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate r…
peterdemaeyer e951a26
Use Mockito + fix unit tests for Windows
peterdemaeyer 5f801d4
Renamed variable error -> failure
peterdemaeyer 1616a1b
Removed the resetting of permissions in tests, relying on JUnit @Temp…
peterdemaeyer 813acd6
Fixed typo a -> an
peterdemaeyer f35036f
Simplified the setting of permissions in tests
peterdemaeyer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.plugins.clean; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.AccessDeniedException; | ||
import java.nio.file.DirectoryNotEmptyException; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.PosixFilePermission; | ||
import java.nio.file.attribute.PosixFilePermissions; | ||
import java.util.Set; | ||
|
||
import org.apache.maven.plugin.logging.Log; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.InOrder; | ||
|
||
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.setPosixFilePermissions; | ||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assumptions.assumeTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
class CleanerTest { | ||
|
||
private static final boolean POSIX_COMPLIANT = | ||
FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); | ||
|
||
private final Log log = mock(); | ||
|
||
@Test | ||
void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception { | ||
assumeTrue(POSIX_COMPLIANT); | ||
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); | ||
final Path file = createFile(basedir.resolve("file")); | ||
final Cleaner cleaner = new Cleaner(null, log, false, null, null); | ||
cleaner.delete(basedir.toFile(), null, false, true, false); | ||
assertFalse(exists(basedir)); | ||
assertFalse(exists(file)); | ||
} | ||
|
||
@Test | ||
void deleteFailsWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws Exception { | ||
assumeTrue(POSIX_COMPLIANT); | ||
when(log.isWarnEnabled()).thenReturn(true); | ||
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); | ||
createFile(basedir.resolve("file")); | ||
// Remove the executable flag to prevent directory listing, which will result in a DirectoryNotEmptyException. | ||
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 = | ||
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 | ||
void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception { | ||
assumeTrue(POSIX_COMPLIANT); | ||
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); | ||
createFile(basedir.resolve("file")); | ||
// Remove the executable flag to prevent directory listing, which will result in a DirectoryNotEmptyException. | ||
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 = | ||
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 | ||
void deleteLogsWarningWithoutRetryWhenNoPermission(@TempDir Path tempDir) throws Exception { | ||
assumeTrue(POSIX_COMPLIANT); | ||
when(log.isWarnEnabled()).thenReturn(true); | ||
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); | ||
final Path file = createFile(basedir.resolve("file")); | ||
// Remove the writable flag to prevent deletion of the file, which will result in an AccessDeniedException. | ||
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)); | ||
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 | ||
void deleteDoesNotLogAnythingWhenNoPermissionAndWarnDisabled(@TempDir Path tempDir) throws Exception { | ||
assumeTrue(POSIX_COMPLIANT); | ||
when(log.isWarnEnabled()).thenReturn(false); | ||
final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath(); | ||
createFile(basedir.resolve("file")); | ||
// Remove the writable flag to prevent deletion of the file, which will result in an AccessDeniedException. | ||
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)); | ||
verify(log, never()).warn(any(CharSequence.class), any(Throwable.class)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an attempt to fix the tests so that they also work on Windows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to do this in a separate pr first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I can't. This code doesn't exist outside of this PR, so I can't create a separate PR for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do the tests work now on windows?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I disabled the tests on non-POSIX-compliant OSes using JUnit 5
Assumptions
. I deemed it acceptable, because this is not a test for specific features and failures on all OSes, it's a test forCleaner
providing it with a variety of failures. It doesn't matter if these failure are exactly the same on all OSes, as long as there is at least one POSIX-compliant OS in the test suite, which is the case, it's enough to cover the feature.