Repository: vackosar/gitflow-incremental-builder
Pull request #53 (2018-10-09 22:43:02)

Fix issue #52: Abort as early as possible if ProjectDependencyGraph is missing


Patch:
diff --git a/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipant.java b/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipant.java
index c5c7fe6..aad10dc 100644
--- a/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipant.java
+++ b/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipant.java
@@ -20,15 +20,24 @@ public class MavenLifecycleParticipant extends AbstractMavenLifecycleParticipant
 
     @Override
     public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
+        mergeCurrentProjectProperties(session);
+
+        if (!Boolean.valueOf(Property.enabled.getValue())) {
+            logger.info("gitflow-incremental-builder is disabled.");
+            return;
+        }
+
+        // check prerequisites
+        if (session.getProjectDependencyGraph() == null) {
+            logger.warn("Execution of gitflow-incremental-builder is not supported in this environment: "
+                    + "Current MavenSession does not provide a ProjectDependencyGraph. "
+                    + "Consider disabling gitflow-incremental-builder via property: " + Property.enabled.fullName());
+            return;
+        }
+
+        logger.info("gitflow-incremental-builder starting..."); //TODO Print version.
         try {
-            mergeCurrentProjectProperties(session);
-            if (Boolean.valueOf(Property.enabled.getValue())) {
-                logger.info("gitflow-incremental-builder starting..."); //TODO Print version.
-                execute(session);
-                logger.info("gitflow-incremental-builder exiting...");
-            } else {
-                logger.info("gitflow-incremental-builder is disabled.");
-            }
+            execute(session);
         } catch (Exception e) {
             Boolean failOnError = Boolean.valueOf(Property.failOnError.getValue());
             if (! failOnError || (e.getMessage() != null && e.getMessage().contains(SkipExecutionException.class.getCanonicalName()))) {
@@ -38,6 +47,7 @@ public class MavenLifecycleParticipant extends AbstractMavenLifecycleParticipant
                 throw new MavenExecutionException("Exception during gitflow-incremental-builder execution occurred.", e);
             }
         }
+        logger.info("gitflow-incremental-builder exiting...");
     }
 
     private void execute(MavenSession session) throws GitAPIException, IOException {


Test patch:
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java
index 6fe0b1d..0fbbba3 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/MavenLifecycleParticipantTest.java
@@ -8,12 +8,16 @@ import org.codehaus.plexus.logging.Logger;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.stubbing.Answer;
 
 import java.lang.reflect.Field;
 
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
 
 public class MavenLifecycleParticipantTest extends BaseRepoTest {
@@ -30,15 +34,22 @@ public class MavenLifecycleParticipantTest extends BaseRepoTest {
         loggerField.setAccessible(true);
         loggerField.set(participant, logger);
         builder = new StringBuilder();
-        doAnswer(invocation -> builder.append(invocation.getArguments()[0]).append("\n")).when(logger).info(anyString());
+        Answer<?> loggingAnswer = invocation -> builder.append(invocation.getArguments()[0]).append("\n");
+        doAnswer(loggingAnswer).when(logger).warn(anyString());
+        doAnswer(loggingAnswer).when(logger).info(anyString());
         doAnswer(invocation -> builder).when(logger).debug(anyString());
     }
 
     @Test
     public void disabled() throws Exception {
         Property.enabled.setValue("false");
-        participant.afterProjectsRead(getMavenSessionMock());
+        MavenSession session = getMavenSessionMock();
+
+        participant.afterProjectsRead(session);
+
         Assert.assertTrue(builder.toString().contains("gitflow-incremental-builder is disabled."));
+        verify(session).getTopLevelProject();
+        verifyNoMoreInteractions(session);
     }
 
     @Test
@@ -46,8 +57,12 @@ public class MavenLifecycleParticipantTest extends BaseRepoTest {
         Property.enabled.setValue("false");
         MavenSession session = getMavenSessionMock();
         session.getTopLevelProject().getProperties().setProperty(Property.enabled.fullName(), "true");
+
         participant.afterProjectsRead(session);
+
         Assert.assertTrue(builder.toString().contains("gitflow-incremental-builder is disabled."));
+        verify(session, times(2)).getTopLevelProject();
+        verifyNoMoreInteractions(session);
     }
 
     @Test
@@ -55,14 +70,20 @@ public class MavenLifecycleParticipantTest extends BaseRepoTest {
         Property.enabled.setValue(null);
         MavenSession session = getMavenSessionMock();
         session.getTopLevelProject().getProperties().setProperty(Property.enabled.fullName(), "false");
+
         participant.afterProjectsRead(session);
+
         Assert.assertTrue(builder.toString().contains("gitflow-incremental-builder is disabled."));
+        verify(session, times(2)).getTopLevelProject();
+        verifyNoMoreInteractions(session);
     }
 
     @Test
     public void defaultlyEnabled() throws Exception {
         MavenSession session = getMavenSessionMock();
+
         participant.afterProjectsRead(session);
+
         Assert.assertTrue(builder.toString().contains("gitflow-incremental-builder starting..."));
     }
 
@@ -71,7 +92,9 @@ public class MavenLifecycleParticipantTest extends BaseRepoTest {
         Property.failOnError.setValue("false");
         MavenSession session = getMavenSessionMock();
         when(session.getCurrentProject()).thenThrow(new RuntimeException("FAIL !!!"));
+
         participant.afterProjectsRead(session);
+
         Assert.assertTrue(builder.toString().contains("gitflow-incremental-builder execution skipped:"));
         Assert.assertTrue(builder.toString().contains("FAIL !!!"));
     }
@@ -80,10 +103,24 @@ public class MavenLifecycleParticipantTest extends BaseRepoTest {
     public void skipOnSkippable() throws Exception {
         MavenSession session = getMavenSessionMock();
         when(session.getCurrentProject()).thenThrow(new SkipExecutionException("FAIL !!!"));
+
         participant.afterProjectsRead(session);
+
         Assert.assertTrue(builder.toString().contains("gitflow-incremental-builder execution skipped:"));
         Assert.assertTrue(builder.toString().contains("SkipExecutionException"));
         Assert.assertTrue(builder.toString().contains("FAIL !!!"));
     }
 
+    @Test
+    public void projectDependencyGraphMissing() throws Exception {
+        MavenSession session = getMavenSessionMock();
+        when(session.getProjectDependencyGraph()).thenReturn(null);
+
+        participant.afterProjectsRead(session);
+
+        Assert.assertTrue(builder.toString().contains("ProjectDependencyGraph"));
+        verify(session).getTopLevelProject();
+        verify(session).getProjectDependencyGraph();
+        verifyNoMoreInteractions(session);
+    }
 }