You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2022/01/12 22:51:14 UTC

[maven-surefire] 01/01: Included Excluded Files as patterns

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch inc-exc-files
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit c09dffcbcb486e48601592d10ea39705906a81cf
Author: Tibor Digaňa <ti...@apache.org>
AuthorDate: Wed Jan 12 23:50:57 2022 +0100

    Included Excluded Files as patterns
---
 .../plugin/surefire/AbstractSurefireMojo.java      | 107 ++++++++++++---------
 .../maven/plugin/surefire/MojoMocklessTest.java    |   6 ++
 2 files changed, 68 insertions(+), 45 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index 33f83d0..e0cc4ab 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -2208,78 +2208,96 @@ public abstract class AbstractSurefireMojo
         }
     }
 
-    private void maybeAppendList( List<String> base, List<String> list )
+    @Nonnull
+    private List<String> getExcludedScanList()
+        throws MojoFailureException
     {
-        if ( list != null )
-        {
-            base.addAll( list );
-        }
+        return getExcludeList( true );
+    }
+
+    @Nonnull
+    private List<String> getExcludeList()
+        throws MojoFailureException
+    {
+        return getExcludeList( false );
     }
 
-    @Nonnull private List<String> getExcludeList()
+    @Nonnull
+    private List<String> getExcludeList( boolean asScanList )
         throws MojoFailureException
     {
-        List<String> actualExcludes = null;
+        List<String> excludes;
         if ( isSpecificTestSpecified() )
         {
-            actualExcludes = Collections.emptyList();
+            excludes = Collections.emptyList();
         }
         else
         {
-            if ( getExcludesFile() != null )
+            excludes = new ArrayList<>();
+            if ( asScanList )
             {
-                actualExcludes = readListFromFile( getExcludesFile() );
+                if ( getExcludes() != null )
+                {
+                    excludes.addAll( getExcludes() );
+                }
+                checkMethodFilterInIncludesExcludes( excludes );
             }
 
-            if ( actualExcludes == null )
-            {
-                actualExcludes = getExcludes();
-            }
-            else
+            if ( getExcludesFile() != null )
             {
-                maybeAppendList( actualExcludes, getExcludes() );
+                excludes.addAll( readListFromFile( getExcludesFile() ) );
             }
 
-            checkMethodFilterInIncludesExcludes( actualExcludes );
-
-            if ( actualExcludes == null || actualExcludes.isEmpty() )
+            if ( asScanList && excludes.isEmpty() )
             {
-                actualExcludes = Collections.singletonList( getDefaultExcludes() );
+                excludes = Collections.singletonList( getDefaultExcludes() );
             }
         }
-        return filterNulls( actualExcludes );
+        return filterNulls( excludes );
+    }
+
+    @Nonnull
+    private List<String> getIncludedScanList()
+        throws MojoFailureException
+    {
+        return getIncludeList( true );
     }
 
+    @Nonnull
     private List<String> getIncludeList()
         throws MojoFailureException
     {
-        List<String> includes = null;
+        return getIncludeList( false );
+    }
+
+    @Nonnull
+    private List<String> getIncludeList( boolean asScanList )
+        throws MojoFailureException
+    {
+        final List<String> includes = new ArrayList<>();
         if ( isSpecificTestSpecified() )
         {
-            includes = new ArrayList<>();
             addAll( includes, split( getTest(), "," ) );
         }
         else
         {
-            if ( getIncludesFile() != null )
+            if ( asScanList )
             {
-                includes = readListFromFile( getIncludesFile() );
+                if ( getIncludes() != null )
+                {
+                    includes.addAll( getIncludes() );
+                }
+                checkMethodFilterInIncludesExcludes( includes );
             }
 
-            if ( includes == null )
-            {
-                includes = getIncludes();
-            }
-            else
+            if ( getIncludesFile() != null )
             {
-                maybeAppendList( includes, getIncludes() );
+                includes.addAll( readListFromFile( getIncludesFile() ) );
             }
 
-            checkMethodFilterInIncludesExcludes( includes );
-
-            if ( includes == null || includes.isEmpty() )
+            if ( asScanList && includes.isEmpty() )
             {
-                includes = asList( getDefaultIncludes() );
+                addAll( includes, getDefaultIncludes() );
             }
         }
 
@@ -2289,16 +2307,13 @@ public abstract class AbstractSurefireMojo
     private void checkMethodFilterInIncludesExcludes( Iterable<String> patterns )
         throws MojoFailureException
     {
-        if ( patterns != null )
+        for ( String pattern : patterns )
         {
-            for ( String pattern : patterns )
+            if ( pattern != null && pattern.contains( "#" ) )
             {
-                if ( pattern != null && pattern.contains( "#" ) )
-                {
-                    throw new MojoFailureException( "Method filter prohibited in "
-                                                        + "includes|excludes|includesFile|excludesFile parameter: "
-                                                        + pattern );
-                }
+                throw new MojoFailureException( "Method filter prohibited in "
+                    + "includes|excludes|includesFile|excludesFile parameter: "
+                    + pattern );
             }
         }
     }
@@ -2308,16 +2323,18 @@ public abstract class AbstractSurefireMojo
     {
         if ( includedExcludedTests == null )
         {
-            includedExcludedTests = new TestListResolver( getIncludeList(), getExcludeList() );
+            includedExcludedTests = new TestListResolver( getIncludedScanList(), getExcludedScanList() );
+            getConsoleLogger().debug( "Resolved included and excluded patterns: " + includedExcludedTests );
         }
         return includedExcludedTests;
     }
 
     public TestListResolver getSpecificTests()
+        throws MojoFailureException
     {
         if ( specificTests == null )
         {
-            specificTests = new TestListResolver( getTest() );
+            specificTests = new TestListResolver( getIncludeList(), getExcludeList() );
         }
         return specificTests;
     }
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java
index c98cb75..3748d88 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/MojoMocklessTest.java
@@ -32,6 +32,7 @@ import org.apache.maven.surefire.extensions.ForkNodeFactory;
 import org.apache.maven.surefire.api.suite.RunResult;
 import org.apache.maven.surefire.api.util.DefaultScanResult;
 import org.apache.maven.toolchain.Toolchain;
+import org.codehaus.plexus.logging.Logger;
 import org.junit.Test;
 
 import java.io.File;
@@ -44,6 +45,7 @@ import static java.util.Arrays.asList;
 import static java.util.Collections.singletonList;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
 import static org.powermock.reflect.Whitebox.invokeMethod;
 import static org.powermock.reflect.Whitebox.setInternalState;
 
@@ -243,6 +245,7 @@ public class MojoMocklessTest
         List<Artifact> projectTestArtifacts = singletonList( testDeps );
         String[] dependenciesToScan = { "g:a" };
         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
+        mojo.setLogger( mock( Logger.class ) );
         DefaultScanResult result = mojo.scanDependencies();
 
         assertThat( result )
@@ -275,6 +278,7 @@ public class MojoMocklessTest
         List<Artifact> projectTestArtifacts = singletonList( testDeps );
         String[] dependenciesToScan = { "g:a" };
         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
+        mojo.setLogger( mock( Logger.class ) );
         DefaultScanResult result = mojo.scanDependencies();
 
         assertThat( result )
@@ -306,6 +310,7 @@ public class MojoMocklessTest
         List<Artifact> projectTestArtifacts = singletonList( testDeps );
         String[] dependenciesToScan = { "g:a" };
         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
+        mojo.setLogger( mock( Logger.class ) );
         DefaultScanResult result = mojo.scanDependencies();
 
         assertThat( result )
@@ -352,6 +357,7 @@ public class MojoMocklessTest
         List<Artifact> projectTestArtifacts = asList( testDep1, testDep2 );
         String[] dependenciesToScan = { "g:a" };
         Mojo mojo = new Mojo( projectTestArtifacts, dependenciesToScan );
+        mojo.setLogger( mock( Logger.class ) );
         DefaultScanResult result = mojo.scanDependencies();
 
         assertThat( result )