]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7647 Improve error log when there is a failure in a ProjectBuilder
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Mon, 4 Jul 2016 14:15:54 +0000 (16:15 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 7 Jul 2016 07:34:33 +0000 (09:34 +0200)
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectBuildersExecutor.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java

index b0165b6f0b30cb0292bebb9894994f4cd0a20500..c7ba292f8769c222328515e97182ffa0ef75aec8 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.scanner.scan;
 import org.sonar.api.batch.bootstrap.ProjectBuilder;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.api.batch.bootstrap.internal.ProjectBuilderContext;
+import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.log.Logger;
 import org.sonar.api.utils.log.Loggers;
 import org.sonar.api.utils.log.Profiler;
@@ -46,9 +47,14 @@ public class ProjectBuildersExecutor {
       ProjectBuilderContext context = new ProjectBuilderContext(reactor);
 
       for (ProjectBuilder projectBuilder : projectBuilders) {
-        projectBuilder.build(context);
+        try {
+          projectBuilder.build(context);
+        } catch (Exception e) {
+          throw MessageException.of("Failed to execute project builder: " + projectBuilder, e);
+        }
       }
       profiler.stopInfo();
     }
+
   }
 }
index b59787880a8ae26d0bbdeb9169f939152e04f7bc..3dd0661bcdf769e88a44fdab7016abddceaef75d 100644 (file)
@@ -25,12 +25,20 @@ import java.io.IOException;
 import java.util.Date;
 import java.util.List;
 import org.apache.commons.io.FileUtils;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.bootstrap.ProjectBuilder;
 import org.sonar.api.utils.MessageException;
 import org.sonar.scanner.mediumtest.BatchMediumTester;
 import org.sonar.scanner.mediumtest.TaskResult;
@@ -49,8 +57,10 @@ public class ProjectBuilderMediumTest {
   @Rule
   public ExpectedException exception = ExpectedException.none();
 
+  private ProjectBuilder projectBuilder = mock(ProjectBuilder.class);
+
   public BatchMediumTester tester = BatchMediumTester.builder()
-    .registerPlugin("xoo", new XooPlugin())
+    .registerPlugin("xoo", new XooPluginWithBuilder(projectBuilder))
     .addRules(new XooRulesDefinition())
     .addDefaultQProfile("xoo", "Sonar Way")
     .setPreviousAnalysisDate(new Date())
@@ -62,11 +72,63 @@ public class ProjectBuilderMediumTest {
     tester.start();
   }
 
+  private class XooPluginWithBuilder extends XooPlugin {
+    private ProjectBuilder builder;
+
+    XooPluginWithBuilder(ProjectBuilder builder) {
+      this.builder = builder;
+    }
+
+    @Override
+    public void define(Context context) {
+      super.define(context);
+      context.addExtension(builder);
+    }
+  }
+
   @After
   public void stop() {
     tester.stop();
   }
 
+  @Test
+  public void testProjectReactorValidation() throws IOException {
+    File baseDir = prepareProject();
+
+    doThrow(new IllegalStateException("My error message")).when(projectBuilder).build(any(ProjectBuilder.Context.class));
+    exception.expectMessage("Failed to execute project builder");
+    exception.expect(MessageException.class);
+    exception.expectCause(new BaseMatcher<Throwable>() {
+
+      @Override
+      public boolean matches(Object item) {
+        if (!(item instanceof IllegalStateException)) {
+          return false;
+        }
+        IllegalStateException e = (IllegalStateException) item;
+        return "My error message".equals(e.getMessage());
+      }
+
+      @Override
+      public void describeTo(Description description) {
+      }
+    });
+
+    tester.newTask()
+      .properties(ImmutableMap.<String, String>builder()
+        .put("sonar.task", "scan")
+        .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+        .put("sonar.projectKey", "com.foo.project")
+        .put("sonar.projectName", "Foo Project")
+        .put("sonar.projectVersion", "1.0-SNAPSHOT")
+        .put("sonar.projectDescription", "Description of Foo Project")
+        .put("sonar.sources", ".")
+        .put("sonar.xoo.enableProjectBuilder", "true")
+        .build())
+      .start();
+
+  }
+
   @Test
   public void testProjectBuilder() throws IOException {
     File baseDir = prepareProject();