elharo commented on code in PR #2236:
URL: https://github.com/apache/maven/pull/2236#discussion_r2075537304


##########
impl/maven-impl/src/test/java/org/apache/maven/impl/PathSelectorTest.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.impl;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class PathSelectorTest {
+    /**
+     * Creates a temporary directory and checks its list of content based on 
patterns.
+     *
+     * @param directory temporary directory where to create a tree
+     * @throws IOException if an error occurred while creating a temporary 
file or directory
+     */
+    @Test
+    public void testTree(final @TempDir Path directory) throws IOException {
+        Path foo = Files.createDirectory(directory.resolve("foo"));
+        Path bar = Files.createDirectory(foo.resolve("bar"));
+        Path biz = Files.createDirectory(directory.resolve("biz"));
+        Files.createFile(directory.resolve("root.txt"));
+        Files.createFile(bar.resolve("leaf.txt"));
+        Files.createFile(biz.resolve("excluded.txt"));
+
+        Set<Path> filtered = filter(directory, "");
+        assertFilteredFilesContains(filtered, directory, "root.txt");
+        assertFilteredFilesContains(filtered, directory, "foo/bar/leaf.txt");
+        assertTrue(filtered.isEmpty(), filtered.toString());
+
+        filtered = filter(directory, "glob:");
+        assertFilteredFilesContains(filtered, directory, "foo/bar/leaf.txt");
+        assertTrue(filtered.isEmpty(), filtered.toString());
+    }
+
+    /**
+     * Creates the filtered paths in a modifiable set.
+     * The result is assigned to {@link #filtered}.
+     *
+     * @param directory the temporary directory containing the files to test
+     * @param syntax syntax to test, either an empty string of {@code "glob:"}
+     * @return the filtered set of paths
+     * @throws IOException if an error occurred while listing the files.
+     */
+    private static Set<Path> filter(final Path directory, final String syntax) 
throws IOException {
+        var includes = List.of(syntax + "**/*.txt");
+        var excludes = List.of(syntax + "biz/**");
+        var matcher = new PathSelector(directory, includes, excludes, false);
+        return new 
HashSet<>(Files.walk(directory).filter(matcher::matches).toList());
+    }
+
+    /**
+     * Asserts that the filtered set of paths contains the given item.
+     * If present, the path is removed from the collection of filtered files.
+     * It allows caller to verify that there are no unexpected elements 
remaining
+     * after all expected elements have been removed.
+     *
+     * @param filtered  the filtered set of paths created by {@link 
#filter(String)}
+     * @param directory the temporary directory containing the files to test
+     * @param path      the path to test
+     */
+    private static void assertFilteredFilesContains(Set<Path> filtered, Path 
directory, String path) {
+        assertTrue(filtered.remove(directory.resolve(path)), path);
+    }
+
+    /**
+     * Tests the omission of unnecessary excludes.
+     *
+     * Note: at the time of writing this test (April 2025), the list of 
excludes go down from 40 to 17 elements.

Review Comment:
   go --> goes



##########
impl/maven-impl/src/test/java/org/apache/maven/impl/PathSelectorTest.java:
##########
@@ -0,0 +1,108 @@
+/*
+ * 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.impl;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class PathSelectorTest {
+    /**
+     * Creates a temporary directory and checks its list of content based on 
patterns.
+     *
+     * @param directory temporary directory where to create a tree
+     * @throws IOException if an error occurred while creating a temporary 
file or directory
+     */
+    @Test
+    public void testTree(final @TempDir Path directory) throws IOException {
+        Path foo = Files.createDirectory(directory.resolve("foo"));
+        Path bar = Files.createDirectory(foo.resolve("bar"));
+        Path biz = Files.createDirectory(directory.resolve("biz"));
+        Files.createFile(directory.resolve("root.txt"));
+        Files.createFile(bar.resolve("leaf.txt"));
+        Files.createFile(biz.resolve("excluded.txt"));
+
+        Set<Path> filtered = filter(directory, "");
+        assertFilteredFilesContains(filtered, directory, "root.txt");
+        assertFilteredFilesContains(filtered, directory, "foo/bar/leaf.txt");
+        assertTrue(filtered.isEmpty(), filtered.toString());
+
+        filtered = filter(directory, "glob:");

Review Comment:
   Try not to reuse local variables. It's quite error prone.



-- 
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-unsubscr...@maven.apache.org

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

Reply via email to