mirror of
https://github.com/SonarSource/sonarqube.git
synced 2024-08-03 08:18:00 +02:00
SONAR-2322 Filter unit tests files listed in surefire reports
This commit is contained in:
parent
aa5de1d8e7
commit
7d231a6776
@ -19,9 +19,13 @@
|
||||
*/
|
||||
package org.sonar.plugins.squid;
|
||||
|
||||
import org.apache.commons.configuration.Configuration;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.sonar.api.CoreProperties;
|
||||
import org.sonar.api.batch.*;
|
||||
import org.sonar.api.batch.DependedUpon;
|
||||
import org.sonar.api.batch.Phase;
|
||||
import org.sonar.api.batch.Sensor;
|
||||
import org.sonar.api.batch.SensorContext;
|
||||
import org.sonar.api.resources.*;
|
||||
import org.sonar.api.utils.SonarException;
|
||||
import org.sonar.java.api.JavaUtils;
|
||||
@ -31,15 +35,25 @@ import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
@Phase(name = Phase.Name.PRE)
|
||||
@DependsUpon(classes = SquidSensor.class)
|
||||
@DependedUpon(JavaUtils.BARRIER_AFTER_SQUID)
|
||||
@DependedUpon(JavaUtils.BARRIER_BEFORE_SQUID)
|
||||
public final class JavaSourceImporter implements Sensor {
|
||||
|
||||
private boolean importSources = false;
|
||||
|
||||
public JavaSourceImporter(Configuration conf) {
|
||||
this.importSources = conf.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
|
||||
CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
JavaSourceImporter(boolean importSources) {
|
||||
this.importSources = importSources;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean shouldExecuteOnProject(Project project) {
|
||||
return isEnabled(project) && Java.KEY.equals(project.getLanguageKey());
|
||||
return Java.KEY.equals(project.getLanguageKey());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -63,14 +77,12 @@ public final class JavaSourceImporter implements Sensor {
|
||||
|
||||
void importSource(SensorContext context, JavaFile javaFile, InputFile inputFile, Charset sourcesEncoding) {
|
||||
try {
|
||||
//if (!context.isIndexed(javaFile, true)) {
|
||||
// See http://jira.codehaus.org/browse/SONAR-791
|
||||
// Squid is the reference plugin to index files. If a file is not indexed,
|
||||
// throw new SonarException("Invalid file: " + javaFile + ". Please check that Java source directories match root directories" +
|
||||
// " as defined by packages.");
|
||||
//}
|
||||
String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name());
|
||||
context.saveSource(javaFile, source);
|
||||
context.index(javaFile);
|
||||
|
||||
if (importSources) {
|
||||
String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name());
|
||||
context.saveSource(javaFile, source);
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
throw new SonarException("Unable to read and import the source file : '" + inputFile.getFile().getAbsolutePath() + "' with the charset : '"
|
||||
@ -78,11 +90,6 @@ public final class JavaSourceImporter implements Sensor {
|
||||
}
|
||||
}
|
||||
|
||||
boolean isEnabled(Project project) {
|
||||
return project.getConfiguration().getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY,
|
||||
CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName();
|
||||
|
@ -48,7 +48,7 @@ public class JavaSourceImporterTest {
|
||||
when(inputFile.getRelativePath()).thenReturn("UndocumentedApi.java");
|
||||
when(inputFile.getFile()).thenReturn(fileToImport);
|
||||
when(inputFile.getFileBaseDir()).thenReturn(fileToImport.getParentFile());
|
||||
importer = new JavaSourceImporter();
|
||||
importer = new JavaSourceImporter(true);
|
||||
context = mock(SensorContext.class);
|
||||
}
|
||||
|
||||
@ -61,11 +61,4 @@ public class JavaSourceImporterTest {
|
||||
verify(context).saveSource(eq(javaFile), anyString());
|
||||
}
|
||||
|
||||
@Test(expected = SonarException.class)
|
||||
@Ignore("see SONAR-791")
|
||||
public void shouldFailWhenSquidDidNotIndexFile() throws IOException {
|
||||
JavaFile javaFile = new JavaFile("Bar");
|
||||
when(context.isIndexed(javaFile, true)).thenReturn(false);
|
||||
importer.importSource(context, javaFile, inputFile, Charset.defaultCharset());
|
||||
}
|
||||
}
|
@ -73,7 +73,7 @@ public abstract class AbstractSurefireParser {
|
||||
private void parseFiles(SensorContext context, File[] reports) {
|
||||
UnitTestIndex index = new UnitTestIndex();
|
||||
parseFiles(reports, index);
|
||||
sanitize(index, context);
|
||||
sanitize(index);
|
||||
save(index, context);
|
||||
|
||||
}
|
||||
@ -90,23 +90,12 @@ public abstract class AbstractSurefireParser {
|
||||
}
|
||||
}
|
||||
|
||||
private void sanitize(UnitTestIndex index, SensorContext context) {
|
||||
private void sanitize(UnitTestIndex index) {
|
||||
for (String classname : index.getClassnames()) {
|
||||
Resource resource = getUnitTestResource(classname);
|
||||
if (resource != null && context.isIndexed(resource, false)) {
|
||||
// ok
|
||||
|
||||
} else if (StringUtils.contains(classname, "$")) {
|
||||
// Java inner class
|
||||
if (StringUtils.contains(classname, "$")) {
|
||||
// Surefire reports classes whereas sonar supports files
|
||||
String parentClassName = StringUtils.substringBeforeLast(classname, "$");
|
||||
Resource parentResource = getUnitTestResource(parentClassName);
|
||||
if (parentResource != null && context.isIndexed(parentResource, false)) {
|
||||
index.merge(classname, parentClassName);
|
||||
} else {
|
||||
index.remove(classname);
|
||||
}
|
||||
} else {
|
||||
index.remove(classname);
|
||||
index.merge(classname, parentClassName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -83,29 +83,6 @@ public class AbstractSurefireParserTest {
|
||||
verify(context, never()).saveMeasure(eq(CoreMetrics.TESTS), anyDouble());
|
||||
}
|
||||
|
||||
/**
|
||||
* This use-case occurs in projects mixing Groovy and Java tests. All test files are listed in surefire reports,
|
||||
* but only Java files are indexed into sonar.
|
||||
*/
|
||||
@Test
|
||||
public void shouldIgnoreNonIndexedFiles() throws URISyntaxException {
|
||||
AbstractSurefireParser parser = newParser();
|
||||
|
||||
SensorContext context = mock(SensorContext.class);
|
||||
when(context.isIndexed(argThat(new BaseMatcher<Resource>(){
|
||||
public boolean matches(Object o) {
|
||||
return ((Resource)o).getName().startsWith("java");
|
||||
}
|
||||
public void describeTo(Description description) {
|
||||
}
|
||||
}), eq(false))).thenReturn(true);
|
||||
|
||||
parser.collect(new Project("foo"), context, getDir("groovyAndJavaTests"));
|
||||
|
||||
verify(context).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "java.Foo")), eq(CoreMetrics.TESTS), eq(6.0));
|
||||
verify(context, never()).saveMeasure(argThat(new IsResource(Scopes.FILE, Qualifiers.FILE, "groovy.Foo")), any(Metric.class), anyDouble());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMergeInnerClasses() throws URISyntaxException {
|
||||
AbstractSurefireParser parser = newParser();
|
||||
|
@ -490,7 +490,6 @@ public class DefaultIndex extends SonarIndex {
|
||||
if (lock.isFailWhenLocked()) {
|
||||
throw new SonarException("Index is locked, resource can not be indexed: " + resource);
|
||||
}
|
||||
LOG.warn("Resource will be ignored in next Sonar versions, index is locked: " + resource);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -30,26 +30,17 @@ import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A pre-implementation for a sensor that imports sources
|
||||
*
|
||||
* A pre-implementation for a sensor that imports sources.
|
||||
* It became too much ugly because of extensability. Methods can't be
|
||||
* refactored because they are heavily overridden in plugins.
|
||||
*
|
||||
* @since 1.10
|
||||
*/
|
||||
@Phase(name = Phase.Name.PRE)
|
||||
public abstract class AbstractSourceImporter implements Sensor {
|
||||
|
||||
/**
|
||||
* @deprecated in 1.11. Use {@link CoreProperties#CORE_IMPORT_SOURCES_PROPERTY} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String KEY_IMPORT_SOURCES = CoreProperties.CORE_IMPORT_SOURCES_PROPERTY;
|
||||
|
||||
/**
|
||||
* @deprecated in 1.11. Use {@link CoreProperties#CORE_IMPORT_SOURCES_DEFAULT_VALUE} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final boolean DEFAULT_IMPORT_SOURCES = CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE;
|
||||
|
||||
private Language language;
|
||||
private boolean enabled = false;
|
||||
|
||||
public AbstractSourceImporter(Language language) {
|
||||
this.language = language;
|
||||
@ -59,13 +50,14 @@ public abstract class AbstractSourceImporter implements Sensor {
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean shouldExecuteOnProject(Project project) {
|
||||
return isEnabled(project) && language.equals(project.getLanguage());
|
||||
return language.equals(project.getLanguage());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void analyse(Project project, SensorContext context) {
|
||||
enabled = isEnabled(project);
|
||||
analyse(project.getFileSystem(), context);
|
||||
onFinished();
|
||||
}
|
||||
@ -84,12 +76,14 @@ public abstract class AbstractSourceImporter implements Sensor {
|
||||
Resource resource = createResource(file, sourceDirs, unitTest);
|
||||
if (resource != null) {
|
||||
try {
|
||||
String source = FileUtils.readFileToString(file, sourcesEncoding.name());
|
||||
context.index(resource);
|
||||
context.saveSource(resource, source);
|
||||
if (enabled) {
|
||||
String source = FileUtils.readFileToString(file, sourcesEncoding.name());
|
||||
context.saveSource(resource, source);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '"
|
||||
+ sourcesEncoding.name() + "'.", e);
|
||||
+ sourcesEncoding.name() + "'. You should check the property " + CoreProperties.ENCODING_PROPERTY, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -49,6 +49,7 @@ import org.apache.commons.lang.CharEncoding;
|
||||
import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;
|
||||
import org.sonar.api.CoreProperties;
|
||||
@ -81,8 +82,9 @@ public class AbstractSourceImporterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void canBeDisabled() {
|
||||
Project pom = mock(Project.class);
|
||||
Project pom = new Project("foo");
|
||||
Configuration config = mock(Configuration.class);
|
||||
when(pom.getConfiguration()).thenReturn(config);
|
||||
when(config.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY, CoreProperties.CORE_IMPORT_SOURCES_DEFAULT_VALUE)).thenReturn(
|
||||
|
Loading…
Reference in New Issue
Block a user