desruisseaux commented on code in PR #2236: URL: https://github.com/apache/maven/pull/2236#discussion_r2029900609
########## impl/maven-impl/src/test/java/org/apache/maven/impl/PathSelectorTest.java: ########## @@ -0,0 +1,95 @@ +/* + * 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.assertTrue; + +public class PathSelectorTest { + /** + * The temporary directory where are the files to test. + */ + private Path directory; + + /** + * The filtered set of paths. creates by {@link #filter()}. + */ + private Set<Path> filtered; + + /** + * Creates a temporary directory and checks its list of content based on patterns. + * + * @param tempDir 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 tempDir) throws IOException { + directory = tempDir; + Path foo = Files.createDirectory(tempDir.resolve("foo")); + Path bar = Files.createDirectory(foo.resolve("bar")); + Path biz = Files.createDirectory(tempDir.resolve("biz")); + Files.createFile(tempDir.resolve("root.txt")); + Files.createFile(bar.resolve("leaf.txt")); + Files.createFile(biz.resolve("excluded.txt")); + + filter(""); + assertContains("root.txt"); + assertContains("foo/bar/leaf.txt"); + assertTrue(filtered.isEmpty(), filtered.toString()); + + filter("glob:"); + assertContains("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 syntax syntax to test, either an empty string of {@code "glob:"} + * @throws IOException if an error occurred while listing the files. + */ + private void filter(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); + filtered = 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 is no unexpected elements remaining + * after all expected elements have been removed. + * + * @param path the path to test + */ + private void assertContains(String path) { Review Comment: That line has some degrees of freedom (the collection, the base directory, the message in case of failure), even if I agree that there is not much (yet) real freedom because there is only one collection and one directory. A purpose of using a method is that, when many calls to `assertContains` are repeated, we know that those things are the same for every lines without having to paid a closer look to the lines. I will rename as `assertFilteredFilesContains` for more clarity. -- 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