ソースを参照

SONAR-7647 Improve error log when there is a failure in a ProjectBuilder

tags/6.0-RC1
Duarte Meneses 8年前
コミット
9cb0a45cbf

+ 7
- 1
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();
}

}
}

+ 63
- 1
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,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();

読み込み中…
キャンセル
保存