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/03/22 04:33:42 UTC

[maven-surefire] 01/01: [SUREFIRE-1426] Fork crash doesn't fail build with -Dmaven.test.failure.ignore=true

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

tibordigana pushed a commit to branch SUREFIRE-1426_2
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 7bd4b91dbaa62057e564b8fd606923e4a9799503
Author: tibordigana <ti...@apache.org>
AuthorDate: Tue Mar 22 05:33:32 2022 +0100

    [SUREFIRE-1426] Fork crash doesn't fail build with -Dmaven.test.failure.ignore=true
---
 .../maven/plugin/surefire/SurefireHelper.java      | 10 +++++-
 .../plugin/surefire/AbstractSurefireMojoTest.java  |  5 +--
 .../maven/plugin/surefire/SurefireHelperTest.java  | 42 ++++++++++++++++++++++
 3 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
index 1870223..5822451 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/SurefireHelper.java
@@ -27,6 +27,7 @@ import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
 import org.apache.maven.surefire.api.cli.CommandLineOption;
 import org.apache.maven.surefire.api.suite.RunResult;
 import org.apache.maven.surefire.api.testset.TestSetFailedException;
+import org.apache.maven.surefire.booter.SurefireBooterForkException;
 
 import javax.annotation.Nonnull;
 import java.io.File;
@@ -156,7 +157,14 @@ public final class SurefireHelper
 
         if ( reportParameters.isTestFailureIgnore() )
         {
-            log.error( createErrorMessage( reportParameters, result, firstForkException ) );
+            String errorMessage = createErrorMessage( reportParameters, result, firstForkException );
+
+            if ( firstForkException instanceof SurefireBooterForkException )
+            {
+                throw new MojoExecutionException( errorMessage, firstForkException );
+            }
+
+            log.error( errorMessage );
         }
         else
         {
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
index 9d89447..8bcf947 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/AbstractSurefireMojoTest.java
@@ -2000,6 +2000,7 @@ public class AbstractSurefireMojoTest
         private List<String> includes;
         private List<String> excludes;
         private String test;
+        private boolean testFailureIgnore;
 
         private JUnitPlatformProviderInfo createJUnitPlatformProviderInfo( Artifact junitPlatformArtifact,
                                                                            TestClassPath testClasspathWrapper )
@@ -2075,13 +2076,13 @@ public class AbstractSurefireMojoTest
         @Override
         public boolean isTestFailureIgnore()
         {
-            return false;
+            return testFailureIgnore;
         }
 
         @Override
         public void setTestFailureIgnore( boolean testFailureIgnore )
         {
-
+            this.testFailureIgnore = testFailureIgnore;
         }
 
         @Override
diff --git a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
index 1ecaea7..dbe2c36 100644
--- a/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
+++ b/maven-surefire-common/src/test/java/org/apache/maven/plugin/surefire/SurefireHelperTest.java
@@ -19,12 +19,18 @@ package org.apache.maven.plugin.surefire;
  * under the License.
  */
 
+import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.surefire.AbstractSurefireMojoTest.Mojo;
+import org.apache.maven.plugin.surefire.log.PluginConsoleLogger;
 import org.apache.maven.surefire.api.suite.RunResult;
+import org.apache.maven.surefire.api.testset.TestSetFailedException;
+import org.apache.maven.surefire.booter.SurefireBooterForkException;
+import org.codehaus.plexus.logging.Logger;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.mockito.ArgumentCaptor;
 
 import java.io.File;
 import java.util.ArrayList;
@@ -36,7 +42,13 @@ import static org.apache.maven.plugin.surefire.SurefireHelper.escapeToPlatformPa
 import static org.apache.maven.plugin.surefire.SurefireHelper.reportExecution;
 import static org.apache.maven.surefire.shared.lang3.SystemUtils.IS_OS_WINDOWS;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.hamcrest.Matchers.containsString;
 import static org.junit.Assume.assumeTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.powermock.api.mockito.PowerMockito.doNothing;
+import static org.powermock.api.mockito.PowerMockito.mock;
 
 /**
  * Test of {@link SurefireHelper}.
@@ -121,6 +133,36 @@ public class SurefireHelperTest
     }
 
     @Test
+    public void shouldHandleFailWithoutExitCode() throws Exception
+    {
+        RunResult summary = new RunResult( 0, 0, 0, 0 );
+        Mojo plugin = new Mojo();
+        plugin.setTestFailureIgnore( true );
+
+        Logger logger = mock( Logger.class );
+        when( logger.isErrorEnabled() ).thenReturn( true );
+        doNothing().when( logger ).error( anyString() );
+        TestSetFailedException exc = new TestSetFailedException( "failure" );
+        reportExecution( plugin, summary, new PluginConsoleLogger( logger ), exc );
+        ArgumentCaptor<String> errorMessage = ArgumentCaptor.forClass( String.class );
+        verify( logger ).error( errorMessage.capture() );
+        assertThat( errorMessage.getValue() ).contains( "failure" );
+    }
+
+    @Test
+    public void shouldHandleFailIfJvmNonZeroExitCode() throws Exception
+    {
+        RunResult summary = new RunResult( 0, 0, 0, 0 );
+        Mojo plugin = new Mojo();
+        plugin.setTestFailureIgnore( true );
+
+        SurefireBooterForkException exc = new SurefireBooterForkException( "Unrecognized option: -Xxxx" );
+        e.expect( MojoExecutionException.class );
+        e.expectMessage( containsString( "Unrecognized option: -Xxxx" ) );
+        reportExecution( plugin, summary, new PluginConsoleLogger( mock( Logger.class ) ), exc );
+    }
+
+    @Test
     public void shouldHandleFailIfNoTests() throws Exception
     {
         RunResult summary = new RunResult( 0, 0, 0, 0 );