This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
commit 3fa6e918f52991792437cbd015f6e6eea79a24de Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Sun Nov 8 20:58:32 2026 +0000 Keep only the scanner path Surefire uses, fix matchPatternStart, add tests DirectoryScanner keeps setBasedir, setIncludes, setExcludes, scan and getIncludedFiles with the same recursion and predicates; the conductor hooks, the diff, the other getters, the symlink and case switches and the default excludes had no caller, nor had ScanConductor and DirectoryScanResult. MatchPattern.matchPatternStart compared the pattern with itself (altStr came from the pattern, not the path), so couldHoldIncluded was always true; plexus-utils has the path here. --- .../api/util/internal/DirectoryScanResult.java | 55 -- .../api/util/internal/DirectoryScanner.java | 770 ++------------------- .../surefire/api/util/internal/MatchPattern.java | 3 +- .../surefire/api/util/internal/ScanConductor.java | 81 --- .../api/util/internal/DirectoryScannerTest.java | 150 ++++ .../api/util/internal/MatchPatternTest.java | 41 ++ .../api/util/internal/MatchPatternsTest.java | 49 ++ .../api/util/internal/SelectorUtilsTest.java | 91 +++ 8 files changed, 384 insertions(+), 856 deletions(-) diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanResult.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanResult.java deleted file mode 100644 index 088d92e90..000000000 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanResult.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.surefire.api.util.internal; - -/** - * Scan for files in a directory at a given time and reports removed and added files - * between captures. - * - * <p>Copied from maven-shared-utils, so that surefire-api, which ends up on the test class path, needs no - * dependency for it. - */ -public class DirectoryScanResult { - private final String[] filesAdded; - - private final String[] filesRemoved; - - /** - * @param filesAdded added files - * @param filesRemoved removed files - */ - public DirectoryScanResult(String[] filesAdded, String[] filesRemoved) { - this.filesAdded = filesAdded; - this.filesRemoved = filesRemoved; - } - - /** - * @return all files which got detected as being added between 2 capture calls - */ - public String[] getFilesAdded() { - return filesAdded; - } - - /** - * @return all files which got detected as being removed between 2 capture calls - */ - public String[] getFilesRemoved() { - return filesRemoved; - } -} diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java index 8d8fbe164..6be6b8080 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/DirectoryScanner.java @@ -19,366 +19,82 @@ package org.apache.maven.surefire.api.util.internal; import java.io.File; -import java.nio.file.Files; import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; /** - * <p>Class for scanning a directory for files/directories which match certain criteria.</p> - * <p> - * These criteria consist of selectors and patterns which have been specified. With the selectors you can select which - * files you want to have included. Files which are not selected are excluded. With patterns you can include or exclude - * files based on their filename. - * </p> - * <p> - * The idea is simple. A given directory is recursively scanned for all files and directories. Each file/directory is - * matched against a set of selectors, including special support for matching against filenames with include and and - * exclude patterns. Only files/directories which match at least one pattern of the include pattern list or other file - * selector, and don't match any pattern of the exclude pattern list or fail to match against a required selector will - * be placed in the list of files/directories found. - * </p> - * <p> - * When no list of include patterns is supplied, "**" will be used, which means that everything will be matched. When no - * list of exclude patterns is supplied, an empty list is used, such that nothing will be excluded. When no selectors - * are supplied, none are applied. - * </p> - * <p> - * The filename pattern matching is done as follows: The name to be matched is split up in path segments. A path segment - * is the name of a directory or file, which is bounded by <code>File.separator</code> ('/' under UNIX, '\' under - * Windows). For example, "abc/def/ghi/xyz.java" is split up in the segments "abc", "def","ghi" and "xyz.java". The same - * is done for the pattern against which should be matched. - * </p> - * <p> - * The segments of the name and the pattern are then matched against each other. When '**' is used for a path segment in - * the pattern, it matches zero or more path segments of the name. - * </p> - * <p> - * There is a special case regarding the use of <code>File.separator</code>s at the beginning of the pattern and the - * string to match:<br> - * When a pattern starts with a <code>File.separator</code>, the string to match must also start with a - * <code>File.separator</code>. When a pattern does not start with a <code>File.separator</code>, the string to match - * may not start with a <code>File.separator</code>. When one of these rules is not obeyed, the string will not match. - * </p> - * <p> - * When a name path segment is matched against a pattern path segment, the following special characters can be used:<br> - * '*' matches zero or more characters<br> - * '?' matches one character. - * </p> - * <p> - * Examples: - * <ul> - * <li>"**\*.class" matches all .class files/dirs in a directory tree.</li> - * <li>"test\a??.java" matches all files/dirs which start with an 'a', then two more characters and then ".java", in a - * directory called test.</li> - * <li>"**" matches everything in a directory tree.</li> - * <li>"**\test\**\XYZ*" matches all files/dirs which start with "XYZ" and where there is a parent directory called test - * (e.g. "abc\test\def\ghi\XYZ123").</li> - * </ul> - * <p> - * Case sensitivity may be turned off if necessary. By default, it is turned on. - * </p> - * <p> - * Example of usage: - * </p> - * <pre> - * String[] includes = { "**\\*.class" }; - * String[] excludes = { "modules\\*\\**" }; - * ds.setIncludes( includes ); - * ds.setExcludes( excludes ); - * ds.setBasedir( new File( "test" ) ); - * ds.setCaseSensitive( true ); - * ds.scan(); - * - * System.out.println( "FILES:" ); - * String[] files = ds.getIncludedFiles(); - * for ( int i = 0; i < files.length; i++ ) - * { - * System.out.println( files[i] ); - * } - * </pre> - * <p> - * This will scan a directory called test for .class files, but excludes all files in all proper subdirectories of a - * directory called "modules" - * </p> - * <p> - * This class must not be used from multiple Threads concurrently! - * </p> - * - * @author Arnout J. Kuiper <a href="mailto:[email protected]">[email protected]</a> - * @author Magesh Umasankar - * @author <a href="mailto:[email protected]">Bruce Atherton</a> - * @author <a href="mailto:[email protected]">Antoine Levy-Lambert</a> - * <p>Copied from maven-shared-utils, so that surefire-api, which ends up on the test class path, needs no - * dependency for it. + * Scans a directory for files matching include patterns and not matching exclude patterns, in Ant syntax + * ({@code *}, {@code ?}, {@code **}) or as {@code %regex[...]}, see {@link SelectorUtils#matchPath}. Matching is + * case-sensitive and symbolic links are followed. + * <p>Reduced from the maven-shared-utils scanner to what Surefire uses, so that surefire-api, which ends up on the + * test class path, needs no dependency for it. */ public class DirectoryScanner { - /** - * Patterns which should be excluded by default. - * - * @see #addDefaultExcludes() - */ - public static final String[] DEFAULTEXCLUDES = { - // Miscellaneous typical temporary files - "**/*~", - "**/#*#", - "**/.#*", - "**/%*%", - "**/._*", - - // CVS - "**/CVS", - "**/CVS/**", - "**/.cvsignore", - - // Subversion - "**/.svn", - "**/.svn/**", - - // Arch - "**/.arch-ids", - "**/.arch-ids/**", - - // Bazaar - "**/.bzr", - "**/.bzr/**", - - // SurroundSCM - "**/.MySCMServerInfo", - - // Mac - "**/.DS_Store", - - // Serena Dimensions Version 10 - "**/.metadata", - "**/.metadata/**", - - // Mercurial - "**/.hg", - "**/.hg/**", - - // git - "**/.git", - "**/.git/**", - // BitKeeper - "**/BitKeeper", - "**/BitKeeper/**", - "**/ChangeSet", - "**/ChangeSet/**", - - // darcs - "**/_darcs", - "**/_darcs/**", - "**/.darcsrepo", - "**/.darcsrepo/**", - "**/-darcs-backup*", - "**/.darcs-temp-mail" - }; - - /** - * The base directory to be scanned. - */ private File basedir; - /** - * The patterns for the files to be included. - */ private String[] includes; - /** - * The patterns for the files to be excluded. - */ private String[] excludes; - private MatchPatterns excludesPatterns; - private MatchPatterns includesPatterns; - /** - * The files which matched at least one include and no excludes and were selected. - */ - private List<String> filesIncluded; - - /** - * The files which did not match any includes or selectors. - */ - private List<String> filesNotIncluded; - - /** - * The files which matched at least one include and at least one exclude. - */ - private List<String> filesExcluded; - - /** - * The directories which matched at least one include and no excludes and were selected. - */ - private List<String> dirsIncluded; - - /** - * The directories which were found and did not match any includes. - */ - private List<String> dirsNotIncluded; - - /** - * The directories which matched at least one include and at least one exclude. - */ - private List<String> dirsExcluded; - - /** - * Whether or not our results were built by a slow scan. - */ - private boolean haveSlowResults = false; - - /** - * Whether or not the file system should be treated as a case sensitive one. - */ - private boolean isCaseSensitive = true; - - /** - * Whether or not symbolic links should be followed. - */ - private boolean followSymlinks = true; - - /** - * A {@link ScanConductor} an control the scanning process. - */ - private ScanConductor scanConductor = null; - - /** - * The last ScanAction. We need to store this in the instance as the scan() method doesn't return. - */ - private ScanConductor.ScanAction scanAction = null; - - /** - * Sole constructor. - */ - public DirectoryScanner() {} + private MatchPatterns excludesPatterns; - /** - * Sets the base directory to be scanned. This is the directory which is scanned recursively. All '/' and '\' - * characters are replaced by <code>File.separatorChar</code>, so the separator used need not match - * <code>File.separatorChar</code>. - * - * @param basedir the base directory to scan. Must not be <code>null</code>. - */ - public void setBasedir(final String basedir) { - setBasedir(new File(basedir.replace('/', File.separatorChar).replace('\\', File.separatorChar))); - } + private List<String> filesIncluded; /** - * Sets the base directory to be scanned. This is the directory which is scanned recursively. - * - * @param basedir the base directory for scanning. Should not be <code>null</code>. + * @param basedir the directory to scan; {@link #scan()} fails if it does not exist or is not a directory */ public void setBasedir(final File basedir) { this.basedir = basedir; } - /** - * Returns the base directory to be scanned. This is the directory which is scanned recursively. - * - * @return the base directory to be scanned - */ public File getBasedir() { return basedir; } /** - * Sets whether or not the file system should be regarded as case sensitive. - * - * @param isCaseSensitiveParameter whether or not the file system should be regarded as a case sensitive one + * @param includes patterns relative to the base directory, with {@code /} or {@code \} as separator; a pattern + * ending in a separator includes everything below. {@code null} includes everything. + * @throws NullPointerException if an element is {@code null} */ - public void setCaseSensitive(final boolean isCaseSensitiveParameter) { - this.isCaseSensitive = isCaseSensitiveParameter; + public void setIncludes(final String... includes) { + this.includes = normalize(includes, "includes"); } /** - * Sets whether or not symbolic links should be followed. - * - * @param followSymlinks whether or not symbolic links should be followed + * @param excludes patterns relative to the base directory, like {@link #setIncludes}; {@code null} excludes nothing + * @throws NullPointerException if an element is {@code null} */ - public void setFollowSymlinks(final boolean followSymlinks) { - this.followSymlinks = followSymlinks; + public void setExcludes(final String... excludes) { + this.excludes = normalize(excludes, "excludes"); } - /** - * Sets the list of include patterns to use. All '/' and '\' characters are replaced by - * <code>File.separatorChar</code>, so the separator used need not match <code>File.separatorChar</code>. - * <p> - * When a pattern ends with a '/' or '\', "**" is appended. - * </p> - * - * @param includes a list of include patterns. May be <code>null</code>, indicating that all files should be - * included. If a non-<code>null</code> list is given, all elements must be non-<code>null</code>. - */ - public void setIncludes(final String... includes) { - if (includes == null) { - this.includes = null; - } else { - this.includes = new String[includes.length]; - for (int i = 0; i < includes.length; i++) { - if (includes[i] == null) { - throw new NullPointerException(messageForNullListElement("includes")); - } - String pattern; - pattern = includes[i].trim().replace('/', File.separatorChar).replace('\\', File.separatorChar); - if (pattern.endsWith(File.separator)) { - pattern += "**"; - } - this.includes[i] = pattern; - } + private static String[] normalize(final String[] patterns, final String listName) { + if (patterns == null) { + return null; } - } - - /** - * Sets the list of exclude patterns to use. All '/' and '\' characters are replaced by - * <code>File.separatorChar</code>, so the separator used need not match <code>File.separatorChar</code>. - * <p> - * When a pattern ends with a '/' or '\', "**" is appended. - * </p> - * - * @param excludes a list of exclude patterns. May be <code>null</code>, indicating that no files should be - * excluded. If a non-<code>null</code> list is given, all elements must be non-<code>null</code>. - */ - public void setExcludes(final String... excludes) { - if (excludes == null) { - this.excludes = null; - } else { - this.excludes = new String[excludes.length]; - for (int i = 0; i < excludes.length; i++) { - if (excludes[i] == null) { - throw new NullPointerException(messageForNullListElement("excludes")); - } - String pattern; - pattern = excludes[i].trim().replace('/', File.separatorChar).replace('\\', File.separatorChar); - if (pattern.endsWith(File.separator)) { - pattern += "**"; - } - this.excludes[i] = pattern; + String[] result = new String[patterns.length]; + for (int i = 0; i < patterns.length; i++) { + if (patterns[i] == null) { + throw new NullPointerException( + "If a non-null " + listName + " list is given, all elements must be" + " non-null"); + } + String pattern = patterns[i].trim().replace('/', File.separatorChar).replace('\\', File.separatorChar); + if (pattern.endsWith(File.separator)) { + pattern += "**"; } + result[i] = pattern; } - } - - private static String messageForNullListElement(String listName) { - return "If a non-null " + listName + " list is given, all elements must be non-null"; + return result; } /** - * @param scanConductor {@link #scanConductor} - */ - public void setScanConductor(final ScanConductor scanConductor) { - this.scanConductor = scanConductor; - } - - /** - * Scans the base directory for files which match at least one include pattern and don't match any exclude patterns. - * If there are selectors then the files must pass muster there, as well. + * Scans the base directory. Afterwards {@link #getIncludedFiles()} lists the files that match an include pattern + * and no exclude pattern. * - * @throws IllegalStateException if the base directory was set incorrectly (i.e. if it is <code>null</code>, - * doesn't exist, or isn't a directory). + * @throws IllegalStateException if no base directory was set, or it does not exist or is not a directory */ public void scan() throws IllegalStateException { if (basedir == null) { @@ -390,433 +106,49 @@ public void scan() throws IllegalStateException { if (!basedir.isDirectory()) { throw new IllegalStateException("basedir " + basedir + " is not a directory"); } - - setupDefaultFilters(); - setupMatchPatterns(); - + includesPatterns = + MatchPatterns.from(includes == null || includes.length == 0 ? new String[] {"**"} : includes); + excludesPatterns = MatchPatterns.from(excludes == null ? new String[0] : excludes); filesIncluded = new ArrayList<>(); - filesNotIncluded = new ArrayList<>(); - filesExcluded = new ArrayList<>(); - dirsIncluded = new ArrayList<>(); - dirsNotIncluded = new ArrayList<>(); - dirsExcluded = new ArrayList<>(); - scanAction = ScanConductor.ScanAction.CONTINUE; - - if (isIncluded("")) { - if (!isExcluded("")) { - if (scanConductor != null) { - scanAction = scanConductor.visitDirectory("", basedir); - - if (ScanConductor.ScanAction.ABORT.equals(scanAction) - || ScanConductor.ScanAction.ABORT_DIRECTORY.equals(scanAction) - || ScanConductor.ScanAction.NO_RECURSE.equals(scanAction)) { - return; - } - } - - dirsIncluded.add(""); - } else { - dirsExcluded.add(""); - } - } else { - dirsNotIncluded.add(""); - } - scandir(basedir, "", true); - } - - /** - * Determine the file differences between the currently included files and - * a previously captured list of files. - * This method will not look for a changed in content but sole in the - * list of files given. - * <p> - * The method will compare the given array of file Strings with the result - * of the last directory scan. It will execute a {@link #scan()} if no - * result of a previous scan could be found. - * </p> - * <p> - * The result of the diff can be queried by the methods - * {@link DirectoryScanResult#getFilesAdded()} and {@link DirectoryScanResult#getFilesRemoved()} - * </p> - * - * @param oldFiles the list of previously captured files names - * @return the result of the directory scan - */ - public DirectoryScanResult diffIncludedFiles(String... oldFiles) { - if (filesIncluded == null) { - // perform a scan if the directory didn't get scanned yet - scan(); - } - - return diffFiles(oldFiles, filesIncluded.toArray(new String[0])); - } - - /** - * @param oldFiles array of old files - * @param newFiles array of new files - * @return calculated difference - */ - public static DirectoryScanResult diffFiles(String[] oldFiles, String[] newFiles) { - Set<String> oldFileSet = arrayAsHashSet(oldFiles); - Set<String> newFileSet = arrayAsHashSet(newFiles); - - List<String> added = new ArrayList<>(); - List<String> removed = new ArrayList<>(); - - for (String oldFile : oldFileSet) { - if (!newFileSet.contains(oldFile)) { - removed.add(oldFile); - } - } - - for (String newFile : newFileSet) { - if (!oldFileSet.contains(newFile)) { - added.add(newFile); - } - } - - String[] filesAdded = added.toArray(new String[0]); - String[] filesRemoved = removed.toArray(new String[0]); - - return new DirectoryScanResult(filesAdded, filesRemoved); + scandir(basedir, ""); } - /** - * Take an array of type T and convert it into a HashSet of type T. - * If <code>null</code> or an empty array gets passed, an empty Set will be returned. - * - * @param array the array - * @return the filled HashSet of type T - */ - private static <T> Set<T> arrayAsHashSet(T[] array) { - if (array == null || array.length == 0) { - return Collections.emptySet(); - } - - Set<T> set = new HashSet<>(array.length); - Collections.addAll(set, array); - - return set; - } - - /** - * Top level invocation for a slow scan. A slow scan builds up a full list of excluded/included files/directories, - * whereas a fast scan will only have full results for included files, as it ignores directories which can't - * possibly hold any included files/directories. - * <p> - * Returns immediately if a slow scan has already been completed. - */ - private void slowScan() { - if (haveSlowResults) { - return; - } - - final String[] excl = dirsExcluded.toArray(new String[0]); - - final String[] notIncl = dirsNotIncluded.toArray(new String[0]); - - for (String anExcl : excl) { - if (!couldHoldIncluded(anExcl)) { - scandir(new File(basedir, anExcl), anExcl + File.separator, false); - } - } - - for (String aNotIncl : notIncl) { - if (!couldHoldIncluded(aNotIncl)) { - scandir(new File(basedir, aNotIncl), aNotIncl + File.separator, false); - } - } - - haveSlowResults = true; - } - - /** - * Scans the given directory for files and directories. Found files and directories are placed in their respective - * collections, based on the matching of includes, excludes, and the selectors. When a directory is found, it is - * scanned recursively. - * - * @param dir the directory to scan. Must not be <code>null</code>. - * @param vpath the path relative to the base directory (needed to prevent problems with an absolute path when using - * dir). Must not be <code>null</code>. - * @param fast whether this call is part of a fast scan - * @see #filesIncluded - * @see #filesNotIncluded - * @see #filesExcluded - * @see #dirsIncluded - * @see #dirsNotIncluded - * @see #dirsExcluded - * @see #slowScan - */ - private void scandir(final File dir, final String vpath, final boolean fast) { + private void scandir(final File dir, final String vpath) { String[] newfiles = dir.list(); - if (newfiles == null) { - /* - * two reasons are mentioned in the API docs for File.list (1) dir is not a directory. This is impossible as - * we wouldn't get here in this case. (2) an IO error occurred (why doesn't it throw an exception then???) - */ - - /* - * [jdcasey] (2) is apparently happening to me, as this is killing one of my tests... this is affecting the - * assembly plugin, fwiw. I will initialize the newfiles array as zero-length for now. NOTE: I can't find - * the problematic code, as it appears to come from a native method in UnixFileSystem... - */ - newfiles = new String[0]; - } - - if (!followSymlinks) { - newfiles = doNotFollowSymbolicLinks(dir, vpath, newfiles); + // the directory could not be read: nothing to scan there + return; } - for (final String newfile : newfiles) { final String name = vpath + newfile; final File file = new File(dir, newfile); if (file.isDirectory()) { - if (isIncluded(name)) { - if (!isExcluded(name)) { - if (scanConductor != null) { - scanAction = scanConductor.visitDirectory(name, file); - - if (ScanConductor.ScanAction.ABORT.equals(scanAction) - || ScanConductor.ScanAction.ABORT_DIRECTORY.equals(scanAction)) { - return; - } - } - - if (!ScanConductor.ScanAction.NO_RECURSE.equals(scanAction)) { - dirsIncluded.add(name); - if (fast) { - scandir(file, name + File.separator, fast); - - if (ScanConductor.ScanAction.ABORT.equals(scanAction)) { - return; - } - } - } - scanAction = null; - - } else { - dirsExcluded.add(name); - if (fast && couldHoldIncluded(name)) { - scandir(file, name + File.separator, fast); - if (ScanConductor.ScanAction.ABORT.equals(scanAction)) { - return; - } - scanAction = null; - } - } - } else { - if (fast && couldHoldIncluded(name)) { - if (scanConductor != null) { - scanAction = scanConductor.visitDirectory(name, file); - - if (ScanConductor.ScanAction.ABORT.equals(scanAction) - || ScanConductor.ScanAction.ABORT_DIRECTORY.equals(scanAction)) { - return; - } - } - if (!ScanConductor.ScanAction.NO_RECURSE.equals(scanAction)) { - dirsNotIncluded.add(name); - - scandir(file, name + File.separator, fast); - if (ScanConductor.ScanAction.ABORT.equals(scanAction)) { - return; - } - } - scanAction = null; - } - } - if (!fast) { - scandir(file, name + File.separator, fast); - if (ScanConductor.ScanAction.ABORT.equals(scanAction)) { - return; - } - scanAction = null; - } - } else if (file.isFile()) { - if (isIncluded(name)) { - if (!isExcluded(name)) { - if (scanConductor != null) { - scanAction = scanConductor.visitFile(name, file); - } - - if (ScanConductor.ScanAction.ABORT.equals(scanAction) - || ScanConductor.ScanAction.ABORT_DIRECTORY.equals(scanAction)) { - return; - } - - filesIncluded.add(name); - } else { - filesExcluded.add(name); - } - } else { - filesNotIncluded.add(name); - } - } - } - } - - private String[] doNotFollowSymbolicLinks(final File dir, final String vpath, final String[] newfiles) { - final List<String> noLinks = new ArrayList<>(); - for (final String newfile : newfiles) { - if (Files.isSymbolicLink(dir.toPath())) { - final String name = vpath + newfile; - final File file = new File(dir, newfile); - if (file.isDirectory()) { - dirsExcluded.add(name); - } else { - filesExcluded.add(name); + if (isIncluded(name) && !isExcluded(name) || couldHoldIncluded(name)) { + scandir(file, name + File.separator); } - } else { - noLinks.add(newfile); + } else if (file.isFile() && isIncluded(name) && !isExcluded(name)) { + filesIncluded.add(name); } } - return noLinks.toArray(new String[0]); } - /** - * Tests whether or not a name matches at least one include pattern. - * - * @param name the name to match. Must not be <code>null</code>. - * @return <code>true</code> when the name matches at least one include pattern, or <code>false</code> - * otherwise - */ private boolean isIncluded(final String name) { - return includesPatterns.matches(name, isCaseSensitive); + return includesPatterns.matches(name, true); } - /** - * Tests whether or not a name matches the start of at least one include pattern. - * - * @param name the name to match. Must not be <code>null</code>. - * @return <code>true</code> when the name matches against the start of at least one include pattern, or - * <code>false</code> otherwise - */ private boolean couldHoldIncluded(final String name) { - return includesPatterns.matchesPatternStart(name, isCaseSensitive); + return includesPatterns.matchesPatternStart(name, true); } - /** - * Tests whether or not a name matches against at least one exclude pattern. - * - * @param name the name to match. Must not be <code>null</code>. - * @return <code>true</code> when the name matches against at least one exclude pattern, or <code>false</code> - * otherwise - */ private boolean isExcluded(final String name) { - return excludesPatterns.matches(name, isCaseSensitive); + return excludesPatterns.matches(name, true); } /** - * Returns the names of the files which matched at least one of the include patterns and none of the exclude - * patterns. The names are relative to the base directory. - * - * @return the names of the files which matched at least one of the include patterns and none of the exclude - * patterns. May also contain symbolic links to files. - * @deprecated this method does not work correctly on Windows. + * @return the matched files, relative to the base directory with {@link File#separator}, in directory listing + * order; empty before {@link #scan()} */ - @Deprecated public String[] getIncludedFiles() { - if (filesIncluded == null) { - return new String[0]; - } - return filesIncluded.toArray(new String[0]); - } - - /** - * Returns the names of the files which matched none of the include patterns. The names are relative to the base - * directory. This involves performing a slow scan if one has not already been completed. - * - * @return the names of the files which matched none of the include patterns - * @see #slowScan - */ - public String[] getNotIncludedFiles() { - slowScan(); - return filesNotIncluded.toArray(new String[0]); - } - - /** - * Returns the names of the files which matched at least one of the include patterns and at least one of the exclude - * patterns. The names are relative to the base directory. This involves performing a slow scan if one has not - * already been completed. - * - * @return the names of the files which matched at least one of the include patterns and at at least one of the - * exclude patterns - * @see #slowScan - */ - public String[] getExcludedFiles() { - slowScan(); - return filesExcluded.toArray(new String[0]); - } - - /** - * Returns the names of the directories which matched at least one of the include patterns and none of the exclude - * patterns. The names are relative to the base directory. - * - * @return the names of the directories which matched at least one of the include patterns and none of the exclude - * patterns. May also contain symbolic links to directories. - * @deprecated this method is buggy. Do not depend on it. - */ - @Deprecated - public String[] getIncludedDirectories() { - return dirsIncluded.toArray(new String[0]); - } - - /** - * Returns the names of the directories which matched none of the include patterns. The names are relative to the - * base directory. This involves performing a slow scan if one has not already been completed. - * - * @return the names of the directories which matched none of the include patterns - * @see #slowScan - */ - public String[] getNotIncludedDirectories() { - slowScan(); - return dirsNotIncluded.toArray(new String[0]); - } - - /** - * Returns the names of the directories which matched at least one of the include patterns and at least one of the - * exclude patterns. The names are relative to the base directory. This involves performing a slow scan if one has - * not already been completed. - * - * @return the names of the directories which matched at least one of the include patterns and at least one of the - * exclude patterns - * @see #slowScan - */ - public String[] getExcludedDirectories() { - slowScan(); - return dirsExcluded.toArray(new String[0]); - } - - /** - * Adds default exclusions to the current exclusions set. - */ - public void addDefaultExcludes() { - final int excludesLength = excludes == null ? 0 : excludes.length; - String[] newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length]; - if (excludesLength > 0) { - System.arraycopy(excludes, 0, newExcludes, 0, excludesLength); - } - for (int i = 0; i < DEFAULTEXCLUDES.length; i++) { - newExcludes[i + excludesLength] = - DEFAULTEXCLUDES[i].replace('/', File.separatorChar).replace('\\', File.separatorChar); - } - excludes = newExcludes; - } - - private void setupDefaultFilters() { - if (includes == null) { - // No includes supplied, so set it to 'matches all' - includes = new String[1]; - includes[0] = "**"; - } - if (excludes == null) { - excludes = new String[0]; - } - } - - private void setupMatchPatterns() { - includesPatterns = MatchPatterns.from(includes); - excludesPatterns = MatchPatterns.from(excludes); + return filesIncluded == null ? new String[0] : filesIncluded.toArray(new String[0]); } } diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/MatchPattern.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/MatchPattern.java index 357d55b1b..dfcadf65b 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/MatchPattern.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/MatchPattern.java @@ -92,7 +92,8 @@ public boolean matchPatternStart(String str, boolean isCaseSensitive) { // a file to deal with, or we can definitely say this is an exclusion... return true; } else { - String altStr = source.replace('\\', '/'); + // the maven-shared-utils original derived altStr from the pattern, which made this always true + String altStr = str.replace('\\', '/'); return SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive) || SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive); diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ScanConductor.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ScanConductor.java deleted file mode 100644 index 430746b61..000000000 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/ScanConductor.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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.surefire.api.util.internal; - -import java.io.File; - -/** - * <p>Visitor pattern for the DirectoryScanner. A ScanConductor controls the scanning process.</p> - * <p>Create an instance and pass it to - * {@link org.apache.maven.surefire.api.util.internal.DirectoryScanner#setScanConductor(ScanConductor)}. - * You will get notified about every visited directory and file. You can use the {@link ScanAction} - * to control what should happen next.</p> - * <p>A ScanConductor might also store own information but users must make sure that the state gets - * cleaned between two scan() invocations.</p> - * - * @author <a href="mailto:[email protected]">Mark Struberg</a> - * <p>Copied from maven-shared-utils, so that surefire-api, which ends up on the test class path, needs no - * dependency for it. - */ -public interface ScanConductor { - enum ScanAction { - /** - * Abort the whole scanning process. The current file will not - * be added anymore. - */ - ABORT, - - /** - * Continue the scanning with the next item in the list. - */ - CONTINUE, - - /** - * This response is only valid for {@link ScanConductor#visitDirectory(String, java.io.File)}. - * Do not recurse into the current directory. The current directory will not be added - * and the processing will be continued with the next item in the list. - */ - NO_RECURSE, - - /** - * Abort processing the current directory. - * The current file will not be added. - * The processing will continue it's scan in the parent directory if any. - */ - ABORT_DIRECTORY - } - - /** - * This method will get invoked for every detected directory. - * - * @param name the directory name (contains parent folders up to the pwd) - * @param directory the directory - * @return the ScanAction to control how to proceed with the scanning - */ - ScanAction visitDirectory(String name, File directory); - - /** - * This method will get invoked for every detected file. - * - * @param name the file name (contains parent folders up to the pwd) - * @param file the file - * @return the ScanAction to control how to proceed with the scanning - */ - ScanAction visitFile(String name, File file); -} diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/DirectoryScannerTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/DirectoryScannerTest.java new file mode 100644 index 000000000..b54e0b527 --- /dev/null +++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/DirectoryScannerTest.java @@ -0,0 +1,150 @@ +/* + * 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.surefire.api.util.internal; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class DirectoryScannerTest { + + @TempDir + Path basedir; + + @BeforeEach + void createTestData() throws IOException { + for (String f : new String[] { + "file1.txt", + "file2.txt", + "folder1/file3.txt", + "folder1/file4.dat", + "folder1/sub/File5Test.class", + "folder2/File6Test.class", + "folder2/Helper.class", + ".hidden/file7.txt" + }) { + Path p = basedir.resolve(f); + Files.createDirectories(p.getParent()); + Files.write(p, new byte[0]); + } + } + + private String[] scan(String[] includes, String[] excludes) { + DirectoryScanner scanner = new DirectoryScanner(); + scanner.setBasedir(basedir.toFile()); + scanner.setIncludes(includes); + scanner.setExcludes(excludes); + scanner.scan(); + return scanner.getIncludedFiles(); + } + + private static String p(String slashPath) { + return slashPath.replace('/', File.separatorChar); + } + + @Test + void includesEverythingByDefault() { + assertThat(scan(null, null)) + .containsExactlyInAnyOrder( + p("file1.txt"), + p("file2.txt"), + p("folder1/file3.txt"), + p("folder1/file4.dat"), + p("folder1/sub/File5Test.class"), + p("folder2/File6Test.class"), + p("folder2/Helper.class"), + p(".hidden/file7.txt")); + } + + @Test + void emptyIncludesMeanEverything() { + assertThat(scan(new String[0], null)).hasSize(8); + } + + @Test + void antPatternsAcrossDirectories() { + assertThat(scan(new String[] {"**/*Test.class"}, null)) + .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), p("folder2/File6Test.class")); + assertThat(scan(new String[] {"*.txt"}, null)).containsExactlyInAnyOrder("file1.txt", "file2.txt"); + assertThat(scan(new String[] {"folder1/*"}, null)) + .containsExactlyInAnyOrder(p("folder1/file3.txt"), p("folder1/file4.dat")); + assertThat(scan(new String[] {"folder?/*.class"}, null)) + .containsExactlyInAnyOrder(p("folder2/File6Test.class"), p("folder2/Helper.class")); + } + + @Test + void excludesWin() { + assertThat(scan(new String[] {"**/*.class"}, new String[] {"**/Helper.class"})) + .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), p("folder2/File6Test.class")); + assertThat(scan(null, new String[] {"folder1/**", ".hidden/**"})) + .containsExactlyInAnyOrder( + "file1.txt", "file2.txt", p("folder2/File6Test.class"), p("folder2/Helper.class")); + } + + @Test + void trailingSeparatorMeansEverythingBelow() { + assertThat(scan(new String[] {"folder1/"}, null)) + .containsExactlyInAnyOrder( + p("folder1/file3.txt"), p("folder1/file4.dat"), p("folder1/sub/File5Test.class")); + assertThat(scan(new String[] {"folder1\\sub\\"}, null)).containsExactly(p("folder1/sub/File5Test.class")); + } + + @Test + void regexPatterns() { + // setIncludes turns \\ into the separator even inside a regex, so the pattern avoids backslashes + assertThat(scan(new String[] {"%regex[.*File[0-9]Test[.]class]"}, null)) + .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), p("folder2/File6Test.class")); + } + + @Test + void matchingIsCaseSensitive() { + assertThat(scan(new String[] {"**/*test.class"}, null)).isEmpty(); + assertThat(scan(new String[] {"FILE1.txt"}, null)).isEmpty(); + } + + @Test + void nullElementsAreRejected() { + DirectoryScanner scanner = new DirectoryScanner(); + assertThrows(NullPointerException.class, () -> scanner.setIncludes("a", null)); + assertThrows(NullPointerException.class, () -> scanner.setExcludes((String) null)); + } + + @Test + void basedirMustBeAnExistingDirectory() { + DirectoryScanner scanner = new DirectoryScanner(); + assertThrows(IllegalStateException.class, scanner::scan); + scanner.setBasedir(basedir.resolve("missing").toFile()); + assertThrows(IllegalStateException.class, scanner::scan); + scanner.setBasedir(basedir.resolve("file1.txt").toFile()); + assertThrows(IllegalStateException.class, scanner::scan); + } + + @Test + void noFilesBeforeScan() { + assertThat(new DirectoryScanner().getIncludedFiles()).isEmpty(); + } +} diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/MatchPatternTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/MatchPatternTest.java new file mode 100644 index 000000000..f4541457e --- /dev/null +++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/MatchPatternTest.java @@ -0,0 +1,41 @@ +/* + * 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.surefire.api.util.internal; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MatchPatternTest { + + @Test + void matchPath() { + MatchPattern mp = MatchPattern.fromString("ABC*"); + assertTrue(mp.matchPath("ABCD", true)); + assertFalse(mp.matchPath("XBCD", true)); + } + + @Test + void regexPattern() { + MatchPattern mp = MatchPattern.fromString("%regex[A.C]"); + assertTrue(mp.matchPath("ABC", true)); + assertFalse(mp.matchPath("ABBC", true)); + } +} diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/MatchPatternsTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/MatchPatternsTest.java new file mode 100644 index 000000000..404e9d30a --- /dev/null +++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/MatchPatternsTest.java @@ -0,0 +1,49 @@ +/* + * 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.surefire.api.util.internal; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class MatchPatternsTest { + + @Test + void matches() { + MatchPatterns from = MatchPatterns.from("ABC**", "CDE**"); + assertTrue(from.matches("ABCDE", true)); + assertTrue(from.matches("CDEF", true)); + assertFalse(from.matches("XYZ", true)); + } + + @Test + void matchesPatternStart() { + MatchPatterns from = MatchPatterns.from("ABC/**"); + assertTrue(from.matchesPatternStart("ABC", true)); + assertFalse(from.matchesPatternStart("XYZ", true)); + } + + @Test + void caseInsensitiveWhenAsked() { + MatchPatterns from = MatchPatterns.from("abc*"); + assertFalse(from.matches("ABCD", true)); + assertTrue(from.matches("ABCD", false)); + } +} diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/SelectorUtilsTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/SelectorUtilsTest.java new file mode 100644 index 000000000..4ea7f10ba --- /dev/null +++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/SelectorUtilsTest.java @@ -0,0 +1,91 @@ +/* + * 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.surefire.api.util.internal; + +import java.io.File; + +import org.junit.jupiter.api.Test; + +import static org.apache.maven.surefire.api.util.internal.SelectorUtils.ANT_HANDLER_PREFIX; +import static org.apache.maven.surefire.api.util.internal.SelectorUtils.PATTERN_HANDLER_SUFFIX; +import static org.apache.maven.surefire.api.util.internal.SelectorUtils.REGEX_HANDLER_PREFIX; +import static org.apache.maven.surefire.api.util.internal.SelectorUtils.matchPath; +import static org.apache.maven.surefire.api.util.internal.SelectorUtils.matchPatternStart; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SelectorUtilsTest { + + @Test + void matchPatternStartRejectsNull() { + assertThrows(NullPointerException.class, () -> matchPatternStart(null, null)); + } + + @Test + void emptyStrings() { + assertTrue(matchPatternStart("", "")); + } + + @Test + void regexPrefixedPatternStart() { + assertTrue(matchPatternStart(REGEX_HANDLER_PREFIX + File.separator + "aaa" + PATTERN_HANDLER_SUFFIX, "")); + } + + @Test + void antPatternStart() { + assertFalse(matchPatternStart(ant("/aaa"), "")); + assertFalse(matchPatternStart(ant("\\aaa"), "")); + assertTrue(matchPatternStart(ant("aaa"), "")); + assertTrue(matchPatternStart(ant("/aaa/bbb"), "/aaa/bbb")); + assertTrue(matchPatternStart(ant("/aaa/**"), "/aaa/bbb")); + assertFalse(matchPatternStart(ant("/aaa/**"), "/ccc/bbb")); + assertTrue(matchPatternStart(ant("/aaa/**"), "\\aaa\\bbb")); + assertFalse(matchPatternStart(ant("/aaa/**"), "\\ccc\\bbb")); + assertFalse(matchPatternStart(ant("/aaa/"), "\\aaa\\bbb")); + } + + @Test + void antPaths() { + String sep = File.separator; + assertTrue(matchPath("**" + sep + "*Test.class", "a" + sep + "b" + sep + "FooTest.class")); + assertTrue(matchPath("**" + sep + "*Test.class", "FooTest.class")); + assertFalse(matchPath("**" + sep + "*Test.class", "a" + sep + "Foo.class")); + assertTrue(matchPath("a" + sep + "?.txt", "a" + sep + "b.txt")); + assertFalse(matchPath("a" + sep + "?.txt", "a" + sep + "bb.txt")); + assertTrue(matchPath("a" + sep + "**", "a" + sep + "b" + sep + "c")); + assertFalse(matchPath("a" + sep + "**", "b" + sep + "c")); + } + + @Test + void caseSensitivity() { + assertFalse(matchPath("*.TXT", "a.txt", true)); + assertTrue(matchPath("*.TXT", "a.txt", false)); + } + + @Test + void regexPaths() { + assertTrue(matchPath(REGEX_HANDLER_PREFIX + ".*Test\\.class" + PATTERN_HANDLER_SUFFIX, "FooTest.class")); + assertFalse(matchPath(REGEX_HANDLER_PREFIX + ".*Test\\.class" + PATTERN_HANDLER_SUFFIX, "Foo.class")); + } + + private static String ant(String pattern) { + return ANT_HANDLER_PREFIX + pattern + PATTERN_HANDLER_SUFFIX; + } +}
