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 20ce038814cb243c973171dee30ce521df290a14 Author: Sylwester Lachiewicz <[email protected]> AuthorDate: Mon Nov 9 00:09:43 2026 +0000 Use the plugin's FileScanner in DefaultDirectoryScanner FileScanner needs nothing from Maven, so it moves to surefire-api and both the plugin and DefaultDirectoryScanner use it, with includes and excludes resolved by TestListResolver; the copied scan code goes. The collectTests() tests pass on both implementations. Only .class files are returned now; other files used to become class names that failed to load. --- .../plugin/surefire/util/DirectoryScanner.java | 1 + .../surefire/api/util/DefaultDirectoryScanner.java | 92 +++------------------- .../surefire/api/util/internal}/FileScanner.java | 8 +- .../api/util/DefaultDirectoryScannerTest.java | 85 ++++++++------------ 4 files changed, 47 insertions(+), 139 deletions(-) diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DirectoryScanner.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DirectoryScanner.java index addb4fd92..a6e182555 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DirectoryScanner.java +++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/DirectoryScanner.java @@ -24,6 +24,7 @@ import org.apache.maven.surefire.api.testset.TestListResolver; import org.apache.maven.surefire.api.util.DefaultScanResult; +import org.apache.maven.surefire.api.util.internal.FileScanner; /** * Scans directories looking for tests. diff --git a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java index 4f69d6c4b..cfe043143 100644 --- a/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/DefaultDirectoryScanner.java @@ -26,8 +26,8 @@ import java.util.Set; import org.apache.maven.surefire.api.filter.SpecificTestClassFilter; -import org.apache.maven.surefire.api.util.internal.MatchPatterns; -import org.apache.maven.surefire.api.util.internal.SelectorUtils; +import org.apache.maven.surefire.api.testset.TestListResolver; +import org.apache.maven.surefire.api.util.internal.FileScanner; /** * Scans directories looking for tests. @@ -38,8 +38,6 @@ @Deprecated public class DefaultDirectoryScanner implements DirectoryScanner { - private static final String FS = System.getProperty("file.separator"); - private static final String[] EMPTY_STRING_ARRAY = new String[0]; private static final String JAVA_SOURCE_FILE_EXTENSION = ".java"; @@ -95,85 +93,17 @@ private static Class<?> loadClass(ClassLoader classLoader, String className) { } String[] collectTests() { - String[] tests = EMPTY_STRING_ARRAY; - if (basedir.exists()) { - tests = scan( - basedir, - includes == null ? null : processIncludesExcludes(includes), - excludes == null ? null : processIncludesExcludes(excludes)); - for (int i = 0; i < tests.length; i++) { - String test = tests[i]; - test = test.substring(0, test.indexOf(".")); - tests[i] = test.replace(FS.charAt(0), '.'); - } - } - return tests; - } - - /** - * Lists the files below {@code basedir} that match an include pattern and no exclude pattern, relative to it and - * with {@link File#separator}, in directory listing order. Patterns are Ant patterns ({@code *}, {@code ?}, - * {@code **}) or {@code %regex[...]}, see {@link SelectorUtils#matchPath}; {@code /} and {@code \\} both separate, - * a pattern ending in a separator takes everything below, no includes mean everything. Matching is - * case-sensitive and symbolic links are followed. - * - * @throws IllegalStateException if {@code basedir} is not an existing directory - * @throws NullPointerException if a pattern is {@code null} - */ - static String[] scan(File basedir, String[] includes, String[] excludes) { if (!basedir.exists()) { - throw new IllegalStateException("basedir " + basedir + " does not exist"); - } - if (!basedir.isDirectory()) { - throw new IllegalStateException("basedir " + basedir + " is not a directory"); - } - String[] inc = normalize(includes, "includes"); - MatchPatterns included = MatchPatterns.from(inc == null || inc.length == 0 ? new String[] {"**"} : inc); - String[] exc = normalize(excludes, "excludes"); - MatchPatterns excluded = MatchPatterns.from(exc == null ? EMPTY_STRING_ARRAY : exc); - List<String> files = new ArrayList<>(); - scandir(basedir, "", included, excluded, files); - return files.toArray(EMPTY_STRING_ARRAY); - } - - private static String[] normalize(String[] patterns, String listName) { - if (patterns == null) { - return null; - } - 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; - } - return result; - } - - private static void scandir( - File dir, String vpath, MatchPatterns included, MatchPatterns excluded, List<String> files) { - String[] names = dir.list(); - if (names == null) { - // the directory could not be read: nothing to scan there - return; - } - for (String name : names) { - String path = vpath + name; - File file = new File(dir, name); - if (file.isDirectory()) { - if (included.matches(path, true) && !excluded.matches(path, true) - || included.matchesPatternStart(path, true)) { - scandir(file, path + File.separator, included, excluded, files); - } - } else if (file.isFile() && included.matches(path, true) && !excluded.matches(path, true)) { - files.add(path); - } + return EMPTY_STRING_ARRAY; } + List<String> tests = new ArrayList<>(); + new FileScanner(basedir, "class") + .scanTo( + tests, + new TestListResolver( + includes == null ? Collections.<String>emptyList() : includes, + excludes == null ? Collections.<String>emptyList() : excludes)); + return tests.toArray(EMPTY_STRING_ARRAY); } private static String[] processIncludesExcludes(List<String> list) { diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/FileScanner.java b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/FileScanner.java similarity index 94% rename from maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/FileScanner.java rename to surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/FileScanner.java index 3c8f25473..2a7ba6a7b 100644 --- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/util/FileScanner.java +++ b/surefire-api/src/main/java/org/apache/maven/surefire/api/util/internal/FileScanner.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.maven.plugin.surefire.util; +package org.apache.maven.surefire.api.util.internal; import java.io.File; import java.util.Collection; @@ -25,12 +25,12 @@ import static org.apache.maven.surefire.shared.lang3.StringUtils.isBlank; -final class FileScanner { +public final class FileScanner { private final File basedir; private final String ext; - FileScanner(File basedir, String ext) { + public FileScanner(File basedir, String ext) { this.basedir = basedir; ext = ext.trim(); if (isBlank(ext)) { @@ -39,7 +39,7 @@ final class FileScanner { this.ext = ext.startsWith(".") ? ext : "." + ext; } - void scanTo(Collection<String> scannedJavaClassNames, TestFilter<String, String> filter) { + public void scanTo(Collection<String> scannedJavaClassNames, TestFilter<String, String> filter) { scan(scannedJavaClassNames, filter, basedir); } diff --git a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java index dbc98ce9b..e4869c555 100644 --- a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java +++ b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/DefaultDirectoryScannerTest.java @@ -30,11 +30,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import static org.apache.maven.surefire.api.util.DefaultDirectoryScanner.scan; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; /** * Test of the directory scanner. @@ -62,14 +60,13 @@ public void testLocateTestClasses() throws IOException, TestSetFailedException { private File tree() throws IOException { for (String f : new String[] { - "file1.txt", - "file2.txt", - "folder1/file3.txt", - "folder1/file4.dat", + "TopTest.class", + "notes.txt", "folder1/sub/File5Test.class", "folder2/File6Test.class", "folder2/Helper.class", - ".hidden/file7.txt" + "folder2/Helper$1.class", + "folder2/data.properties" }) { Path p = tmp.resolve(f); Files.createDirectories(p.getParent()); @@ -78,78 +75,58 @@ private File tree() throws IOException { return tmp.toFile(); } + private String[] collect(List<String> includes, List<String> excludes) throws IOException { + return new DefaultDirectoryScanner(tree(), includes, excludes, null).collectTests(); + } + private static String p(String slashPath) { return slashPath.replace('/', File.separatorChar); } @Test - void scanIncludesEverythingByDefault() throws IOException { - File base = tree(); - assertThat(scan(base, null, null)) - .hasSize(8) - .contains(p("folder1/sub/File5Test.class"), p(".hidden/file7.txt")); - assertThat(scan(base, new String[0], null)).hasSize(8); + void collectTestsWithJavaPatterns() throws IOException { + assertThat(collect(Arrays.asList("**/*Test.java"), null)) + .containsExactlyInAnyOrder("TopTest", "folder1.sub.File5Test", "folder2.File6Test"); } @Test - void scanAntPatternsAcrossDirectories() throws IOException { - File base = tree(); - assertThat(scan(base, new String[] {"**/*Test.class"}, null)) - .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), p("folder2/File6Test.class")); - assertThat(scan(base, new String[] {"*.txt"}, null)).containsExactlyInAnyOrder("file1.txt", "file2.txt"); - assertThat(scan(base, new String[] {"folder1/*"}, null)) - .containsExactlyInAnyOrder(p("folder1/file3.txt"), p("folder1/file4.dat")); - assertThat(scan(base, new String[] {"folder?/*.class"}, null)) - .containsExactlyInAnyOrder(p("folder2/File6Test.class"), p("folder2/Helper.class")); + void collectTestsWithClassPatterns() throws IOException { + assertThat(collect(Arrays.asList("**/*Test.class"), null)) + .containsExactlyInAnyOrder("TopTest", "folder1.sub.File5Test", "folder2.File6Test"); } @Test - void scanExcludesWin() throws IOException { - File base = tree(); - assertThat(scan(base, new String[] {"**/*.class"}, new String[] {"**/Helper.class"})) - .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), p("folder2/File6Test.class")); - assertThat(scan(base, null, new String[] {"folder1/**", ".hidden/**"})) - .containsExactlyInAnyOrder( - "file1.txt", "file2.txt", p("folder2/File6Test.class"), p("folder2/Helper.class")); + void collectTestsWithExcludes() throws IOException { + assertThat(collect(Arrays.asList("**/*.java"), Arrays.asList("**/*$*", "**/Helper.java"))) + .containsExactlyInAnyOrder("TopTest", "folder1.sub.File5Test", "folder2.File6Test"); } @Test - void scanTrailingSeparatorMeansEverythingBelow() throws IOException { - File base = tree(); - assertThat(scan(base, new String[] {"folder1/"}, null)) - .containsExactlyInAnyOrder( - p("folder1/file3.txt"), p("folder1/file4.dat"), p("folder1/sub/File5Test.class")); - assertThat(scan(base, new String[] {"folder1\\sub\\"}, null)).containsExactly(p("folder1/sub/File5Test.class")); + void collectTestsWithCommaSeparatedIncludes() throws IOException { + assertThat(collect(Arrays.asList("**/File5*.java, **/Helper.java"), null)) + .containsExactlyInAnyOrder("folder1.sub.File5Test", "folder2.Helper"); } @Test - void scanRegexPatterns() throws IOException { - // both slashes become the separator, inside a regex too, so the pattern avoids backslashes - assertThat(scan(tree(), new String[] {"%regex[.*File[0-9]Test[.]class]"}, null)) - .containsExactlyInAnyOrder(p("folder1/sub/File5Test.class"), p("folder2/File6Test.class")); + void collectTestsWithDirectoryPatterns() throws IOException { + assertThat(collect(Arrays.asList("folder2/*Test.java"), null)).containsExactly("folder2.File6Test"); + assertThat(collect(Arrays.asList("folder1/**/*.java"), null)).containsExactly("folder1.sub.File5Test"); } @Test - void scanIsCaseSensitive() throws IOException { - File base = tree(); - assertThat(scan(base, new String[] {"**/*test.class"}, null)).isEmpty(); - assertThat(scan(base, new String[] {"FILE1.txt"}, null)).isEmpty(); + void collectTestsWithRegex() throws IOException { + assertThat(collect(Arrays.asList("%regex[.*File[0-9]Test.*]"), null)) + .containsExactlyInAnyOrder("folder1.sub.File5Test", "folder2.File6Test"); } @Test - void scanRejectsNullPatternsAndBadBasedir() throws IOException { - File base = tree(); - assertThrows(NullPointerException.class, () -> scan(base, new String[] {"a", null}, null)); - assertThrows(NullPointerException.class, () -> scan(base, null, new String[] {null})); - assertThrows(IllegalStateException.class, () -> scan(new File(base, "missing"), null, null)); - assertThrows(IllegalStateException.class, () -> scan(new File(base, "file1.txt"), null, null)); + void collectTestsIsCaseSensitive() throws IOException { + assertThat(collect(Arrays.asList("**/*test.java"), null)).isEmpty(); } @Test - void collectTestsTurnsClassFilesIntoClassNames() throws IOException { - File base = tree(); - DefaultDirectoryScanner scanner = new DefaultDirectoryScanner( - base, Arrays.asList("**/*Test.java,**/Helper.class"), Arrays.asList("**/File6*"), null); - assertThat(scanner.collectTests()).containsExactlyInAnyOrder("folder1.sub.File5Test", "folder2.Helper"); + void collectTestsOfMissingDirectoryIsEmpty() { + assertThat(new DefaultDirectoryScanner(new File(tmp.toFile(), "missing"), null, null, null).collectTests()) + .isEmpty(); } }
