diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-07-04 16:15:54 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2016-07-07 09:34:33 +0200 |
commit | 9cb0a45cbf5c1b2964cb7c095b9f87d94eb59c26 (patch) | |
tree | a4d5961e783599c79123fb97f5097654dd6619bb | |
parent | 811b439aab032a3d2e831c62190aeee55bdd8626 (diff) | |
download | sonarqube-9cb0a45cbf5c1b2964cb7c095b9f87d94eb59c26.tar.gz sonarqube-9cb0a45cbf5c1b2964cb7c095b9f87d94eb59c26.zip |
SONAR-7647 Improve error log when there is a failure in a ProjectBuilder
2 files changed, 70 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectBuildersExecutor.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectBuildersExecutor.java index b0165b6f0b3..c7ba292f876 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectBuildersExecutor.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/ProjectBuildersExecutor.java @@ -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(); } + } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java index b59787880a8..3dd0661bcdf 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/ProjectBuilderMediumTest.java @@ -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,12 +72,64 @@ 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(); |