You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "peterdemaeyer (via GitHub)" <gi...@apache.org> on 2024/11/07 07:47:14 UTC

[PR] MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

peterdemaeyer opened a new pull request, #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833178076


##########
src/main/java/org/apache/maven/plugins/clean/Cleaner.java:
##########
@@ -273,7 +273,8 @@ private boolean isSymbolicLink(Path path) throws IOException {
      * @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
      */
     private int delete(File file, boolean failOnError, boolean retryOnError) throws IOException {
-        if (!file.delete()) {
+        IOException error = delete(file);

Review Comment:
   Will do.
   To justify why I named it like this: it was to be consistent with the context of the existing terminology `failOnError` - it is the `error` in that context.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833215437


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -45,36 +42,22 @@
 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.Assertions.assertTrue;
+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 boolean warnEnabled;
-
-    /**
-     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
-     */
-    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
-
-    /**
-     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.
-     */
-    private final Log log = new SilentLog() {
-
-        @Override
-        public boolean isWarnEnabled() {
-            return warnEnabled;
-        }
-
-        @Override
-        public void warn(CharSequence content, Throwable error) {
-            warnings.put(content, error);
-        }
-    };
+    private final Log log = mock();
 
     @Test
     void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception {
-        final Path basedir = createDirectory(tempDir.resolve("target"));
+        final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();

Review Comment:
   do the tests work now on windows?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#issuecomment-2461559052

   Oops, I targeted the wrong branch. Not sure how to retarget, maybe I can just delete this PR and create a new one...


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "slawekjaranowski (via GitHub)" <gi...@apache.org>.
slawekjaranowski commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1832264645


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.Path;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugin.testing.SilentLog;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+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;
+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.Assertions.assertTrue;
+
+class CleanerTest {
+
+    private boolean warnEnabled;
+
+    /**
+     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
+     */
+    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
+
+    /**
+     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.

Review Comment:
   you can simply add Mockito to dependencies



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "jira-importer (via GitHub)" <gi...@apache.org>.
jira-importer commented on PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#issuecomment-2690268779

   Resolve #180


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1832408792


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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.Path;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugin.testing.SilentLog;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+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;
+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.Assertions.assertTrue;
+
+class CleanerTest {
+
+    private boolean warnEnabled;
+
+    /**
+     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
+     */
+    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
+
+    /**
+     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.

Review Comment:
   Ok, good to know. I had to settle for Mockito 4.x/Java 8 instead of 5.x/Java 11, but it's an improvement nonetheless.
   I also needed to fix the tests for Windows paths.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833389997


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   See my other answer.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834433249


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   So I don't think these are what I would call "persistent'. Either way, let's start with the assumption you don't need this. Take out the finally block and see if everything still passes.



##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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.

Review Comment:
   This might be what I'm missing. The annotation doesn't work with the permissions you set?



##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // Prevent directory listing, which will result in a DirectoryNotEmptyException.
+        final String rw_rw_r__ = rwxrwxr_x.replace('x', '-');

Review Comment:
   Is replace needed here? Could this just be a string literal?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833182279


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -45,36 +42,22 @@
 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.Assertions.assertTrue;
+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 boolean warnEnabled;
-
-    /**
-     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
-     */
-    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
-
-    /**
-     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.
-     */
-    private final Log log = new SilentLog() {
-
-        @Override
-        public boolean isWarnEnabled() {
-            return warnEnabled;
-        }
-
-        @Override
-        public void warn(CharSequence content, Throwable error) {
-            warnings.put(content, error);
-        }
-    };
+    private final Log log = mock();
 
     @Test
     void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception {
-        final Path basedir = createDirectory(tempDir.resolve("target"));
+        final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();

Review Comment:
   Hmm, I can't. This code doesn't exist outside of this PR, so I can't create a separate PR for it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833454322


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   Something's off with that. Test resources including files should not be shared between test methods. Per Junit docs "The temporary directory will be shared by all tests in a class when the annotation is present on a static field or on a parameter of a [@BeforeAll](https://junit.org/junit5/docs/5.4.1/api/org/junit/jupiter/api/BeforeAll.html) method. Otherwise — for example, when @TempDir is only used on instance fields or on parameters in test, [@BeforeEach](https://junit.org/junit5/docs/5.4.1/api/org/junit/jupiter/api/BeforeEach.html), or [@AfterEach](https://junit.org/junit5/docs/5.4.1/api/org/junit/jupiter/api/AfterEach.html) methods — each test will use its own temporary directory."



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834639200


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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.

Review Comment:
   Maybe it does, maybe it doesn't. I didn't check. I deem it good practice to reset any modified persistent state so that I don't have to make assumptions about how "powerful" the `@TempDir` extension really is.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1832410602


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -45,36 +42,22 @@
 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.Assertions.assertTrue;
+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 boolean warnEnabled;
-
-    /**
-     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
-     */
-    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
-
-    /**
-     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.
-     */
-    private final Log log = new SilentLog() {
-
-        @Override
-        public boolean isWarnEnabled() {
-            return warnEnabled;
-        }
-
-        @Override
-        public void warn(CharSequence content, Throwable error) {
-            warnings.put(content, error);
-        }
-    };
+    private final Log log = mock();
 
     @Test
     void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception {
-        final Path basedir = createDirectory(tempDir.resolve("target"));
+        final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();

Review Comment:
   This is an attempt to fix the tests so that they also work on Windows. Let's see how that plays out.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833264610


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   why do you need to reset this here? Feels like the object shouldn't be used again



##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   why reset?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833389064


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   To avoid resource exhaustion. It's good practice to reset persistent state which is modified in a test. The files I make in this test are persistent, and I rely on the `@TempDir` extension in the JUnit framework to delete them. If I leave permissions as they are, i.e. deliberately restricted so that files cannot be deleted, I imagine it could be that deleting the directory fails, resulting not per se in an exception but in lingering temporary directories. Over time, lingering temporary directories can cause "no space left on device" on test systems. By resetting the permissions, I reset the persistent state, allowing the JUnit framework to clear the temporary directory, avoiding resource exhaustion.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834125707


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   I think there is a misunderstanding. I agree that test resources including files should not be shared between test methods. That's exactly why I did _not_ share them between tests. They fall in the category "`@TempDir` is used on parameters in tests", in which case "each test will use its own temporary directory" according to your quote, which is exactly what we want, no?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834125707


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   I think there is a misunderstanding. I agree that test resources including files should not be shared between test methods. That's exactly why I did _not_ share them between tests. According to you quote of the JUnit docs, they fall in the category "`@TempDir` is used on parameters in tests", in which case "each test will use its own temporary directory", which is exactly what we want, no?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo merged PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834636224


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // Prevent directory listing, which will result in a DirectoryNotEmptyException.
+        final String rw_rw_r__ = rwxrwxr_x.replace('x', '-');

Review Comment:
   Replace is not strictly needed, it could be a string literal, but the replace reflects better the intention. I just want to remove the executable flag, and leave any other "rw" settings as they were. That's exactly what this replace expresses. If a make it a string literal, it is not clear to the reader that in fact I only mean to change the executable flag. The name of the variable `rw_rw_r__` is merely suggestive, to reflect the sort of value the string can have.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834878667


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // Prevent directory listing, which will result in a DirectoryNotEmptyException.
+        final String rw_rw_r__ = rwxrwxr_x.replace('x', '-');

Review Comment:
   This feels too clever by half. Let's just make it a string literal.



##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,156 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        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__);
+        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"));
+        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__);
+        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"));
+        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.

Review Comment:
   in a --> in an



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833170592


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -45,36 +42,22 @@
 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.Assertions.assertTrue;
+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 boolean warnEnabled;
-
-    /**
-     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
-     */
-    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
-
-    /**
-     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.
-     */
-    private final Log log = new SilentLog() {
-
-        @Override
-        public boolean isWarnEnabled() {
-            return warnEnabled;
-        }
-
-        @Override
-        public void warn(CharSequence content, Throwable error) {
-            warnings.put(content, error);
-        }
-    };
+    private final Log log = mock();
 
     @Test
     void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception {
-        final Path basedir = createDirectory(tempDir.resolve("target"));
+        final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();

Review Comment:
   might want to do this in a separate pr first



##########
src/main/java/org/apache/maven/plugins/clean/Cleaner.java:
##########
@@ -273,7 +273,8 @@ private boolean isSymbolicLink(Path path) throws IOException {
      * @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
      */
     private int delete(File file, boolean failOnError, boolean retryOnError) throws IOException {
-        if (!file.delete()) {
+        IOException error = delete(file);

Review Comment:
   this is not a Java error; please rename the variable



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1834125707


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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);
+        }
+    }
+
+    @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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        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);

Review Comment:
   I think there is a misunderstanding. I agree that test resources including files should not be shared between test methods. That's exactly why I did _not_ share them between tests. They fall in the category "`@TempDir` is used on parameters in tests", in which case "each test will use its own temporary directory" according to your quote. So I don't see the problem?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MCLEAN-124 Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1832410602


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -45,36 +42,22 @@
 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.Assertions.assertTrue;
+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 boolean warnEnabled;
-
-    /**
-     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
-     */
-    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
-
-    /**
-     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.
-     */
-    private final Log log = new SilentLog() {
-
-        @Override
-        public boolean isWarnEnabled() {
-            return warnEnabled;
-        }
-
-        @Override
-        public void warn(CharSequence content, Throwable error) {
-            warnings.put(content, error);
-        }
-    };
+    private final Log log = mock();
 
     @Test
     void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception {
-        final Path basedir = createDirectory(tempDir.resolve("target"));
+        final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();

Review Comment:
   This is an attempt to fix the tests so that they also work on Windows.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833183933


##########
src/main/java/org/apache/maven/plugins/clean/Cleaner.java:
##########
@@ -273,7 +273,8 @@ private boolean isSymbolicLink(Path path) throws IOException {
      * @throws IOException If a file/directory could not be deleted and <code>failOnError</code> is <code>true</code>.
      */
     private int delete(File file, boolean failOnError, boolean retryOnError) throws IOException {
-        if (!file.delete()) {
+        IOException error = delete(file);

Review Comment:
   I renamed it to `failure`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1833240861


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -45,36 +42,22 @@
 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.Assertions.assertTrue;
+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 boolean warnEnabled;
-
-    /**
-     * Use a {@code LinkedHashMap} to preserve the order of the logged warnings.
-     */
-    private final Map<CharSequence, Throwable> warnings = new LinkedHashMap<>();
-
-    /**
-     * Ideally we should use a mocking framework such as Mockito for this, but alas, this project has no such dependency.
-     */
-    private final Log log = new SilentLog() {
-
-        @Override
-        public boolean isWarnEnabled() {
-            return warnEnabled;
-        }
-
-        @Override
-        public void warn(CharSequence content, Throwable error) {
-            warnings.put(content, error);
-        }
-    };
+    private final Log log = mock();
 
     @Test
     void deleteSucceedsDeeply(@TempDir Path tempDir) throws Exception {
-        final Path basedir = createDirectory(tempDir.resolve("target"));
+        final Path basedir = createDirectory(tempDir.resolve("target")).toRealPath();

Review Comment:
   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 for `Cleaner` 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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MCLEAN-124] Leverage Files.delete(Path) API to provide more accurate reason in case of failure [maven-clean-plugin]

Posted by "peterdemaeyer (via GitHub)" <gi...@apache.org>.
peterdemaeyer commented on code in PR #60:
URL: https://github.com/apache/maven-clean-plugin/pull/60#discussion_r1835344517


##########
src/test/java/org/apache/maven/plugins/clean/CleanerTest.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.getPosixFilePermissions;
+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"));
+        final Set<PosixFilePermission> initialPermissions = getPosixFilePermissions(basedir);
+        final String rwxrwxr_x = PosixFilePermissions.toString(initialPermissions);
+        // 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);
+        }
+    }
+
+    @Test
+    void deleteFailsAfterRetryWhenNoPermission(@TempDir Path tempDir) throws Exception {
+        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);
+        // 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.

Review Comment:
   I checked, the `@TempDir` framework is smart enough to fix the permissions to that the tmp dir can be deleted. So I removed all the try-catches.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org