Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When working with SQ >= 6.4 sensors are now global #409

Merged
merged 1 commit into from
Jun 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.plugins.csharp;

import org.sonar.api.SonarQubeVersion;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Qualifiers;
Expand Down Expand Up @@ -134,8 +135,8 @@ public CSharpCoverageAggregator(Settings settings) {

public static class CSharpCoverageReportImportSensor extends CoverageReportImportSensor {

public CSharpCoverageReportImportSensor(CSharpCoverageAggregator coverageAggregator) {
super(COVERAGE_CONF, coverageAggregator, CSharpPlugin.LANGUAGE_NAME, false);
public CSharpCoverageReportImportSensor(CSharpCoverageAggregator coverageAggregator, SonarQubeVersion sonarQubeVersion) {
super(COVERAGE_CONF, coverageAggregator, CSharpPlugin.LANGUAGE_KEY, CSharpPlugin.LANGUAGE_NAME, sonarQubeVersion, false);
}

}
Expand All @@ -150,8 +151,9 @@ public CSharpIntegrationCoverageAggregator(Settings settings) {

public static class CSharpIntegrationCoverageReportImportSensor extends CoverageReportImportSensor {

public CSharpIntegrationCoverageReportImportSensor(CSharpIntegrationCoverageAggregator coverageAggregator) {
super(IT_COVERAGE_CONF, coverageAggregator, CSharpPlugin.LANGUAGE_NAME, true);
public CSharpIntegrationCoverageReportImportSensor(CSharpIntegrationCoverageAggregator coverageAggregator,
SonarQubeVersion sonarQubeVersion) {
super(IT_COVERAGE_CONF, coverageAggregator, CSharpPlugin.LANGUAGE_KEY, CSharpPlugin.LANGUAGE_NAME, sonarQubeVersion, true);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.plugins.csharp;

import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.Settings;
Expand Down Expand Up @@ -74,8 +75,9 @@ public CSharpUnitTestResultsAggregator(Settings settings) {

public static class CSharpUnitTestResultsImportSensor extends UnitTestResultsImportSensor {

public CSharpUnitTestResultsImportSensor(CSharpUnitTestResultsAggregator unitTestResultsAggregator, ProjectDefinition projectDef) {
super(unitTestResultsAggregator, projectDef, CSharpPlugin.LANGUAGE_NAME);
public CSharpUnitTestResultsImportSensor(CSharpUnitTestResultsAggregator unitTestResultsAggregator, ProjectDefinition projectDef,
SonarQubeVersion sonarQubeVersion) {
super(unitTestResultsAggregator, projectDef, CSharpPlugin.LANGUAGE_KEY, CSharpPlugin.LANGUAGE_NAME, sonarQubeVersion);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.sonar.api.SonarQubeVersion;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.Settings;
import org.sonar.plugins.csharp.CSharpCodeCoverageProvider.CSharpCoverageAggregator;
Expand Down Expand Up @@ -60,12 +61,13 @@ public void for_coverage() throws Exception {

@Test
public void createInstance_CoverageReport() {
new CSharpCoverageReportImportSensor(new CSharpCoverageAggregator(new Settings()));
new CSharpCoverageReportImportSensor(new CSharpCoverageAggregator(new Settings()), new SonarQubeVersion(SonarQubeVersion.V5_6));
}

@Test
public void createInstance_IntegrationCoverageReport() {
new CSharpIntegrationCoverageReportImportSensor(new CSharpIntegrationCoverageAggregator(new Settings()));
new CSharpIntegrationCoverageReportImportSensor(new CSharpIntegrationCoverageAggregator(new Settings()),
new SonarQubeVersion(SonarQubeVersion.V5_6));
}

private static Set<Object> nonProperties(List extensions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.Settings;
Expand All @@ -46,7 +47,8 @@ public void test() {

@Test
public void createInstance_CSharpUnitTestResultsImportSensor() {
new CSharpUnitTestResultsImportSensor(new CSharpUnitTestResultsAggregator(new Settings()), ProjectDefinition.create());
new CSharpUnitTestResultsImportSensor(new CSharpUnitTestResultsAggregator(new Settings()), ProjectDefinition.create(),
new SonarQubeVersion(SonarQubeVersion.V5_6));
}

private static Set<Object> nonProperties(List extensions) {
Expand Down
8 changes: 4 additions & 4 deletions sonar-dotnet-tests-library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
</parent>

<artifactId>sonar-dotnet-tests-library</artifactId>

<name>SonarQube .NET Tests Library</name>
<description />
<url />

<developers>
<developer>
<id>dbolkensteyn</id>
<name>Dinesh Bolkensteyn</name>
<organization>SonarSource</organization>
</developer>
</developers>

<properties>
<sonarqube.version>5.6</sonarqube.version>
<sonarqube.version>6.4</sonarqube.version>
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>
<gitRepositoryName>sonar-dotnet-tests-library</gitRepositoryName>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.io.File;
import java.util.Map;

import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputFile.Type;
Expand All @@ -29,6 +31,8 @@
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.resources.Language;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove this unused import 'org.sonar.api.resources.Language'. rule

import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

Expand All @@ -40,13 +44,19 @@ public class CoverageReportImportSensor implements Sensor {
private final CoverageConfiguration coverageConf;
private final CoverageAggregator coverageAggregator;
private final boolean isIntegrationTest;
private final String languageKey;
private final String languageName;
private final SonarQubeVersion sonarQubeVersion;

public CoverageReportImportSensor(CoverageConfiguration coverageConf, CoverageAggregator coverageAggregator, String languageName, boolean isIntegrationTest) {
public CoverageReportImportSensor(CoverageConfiguration coverageConf, CoverageAggregator coverageAggregator,
String languageKey, String languageName, SonarQubeVersion sonarQubeVersion,
boolean isIntegrationTest) {
this.coverageConf = coverageConf;
this.coverageAggregator = coverageAggregator;
this.isIntegrationTest = isIntegrationTest;
this.languageKey = languageKey;
this.languageName = languageName;
this.sonarQubeVersion = sonarQubeVersion;
}

@Override
Expand All @@ -56,6 +66,11 @@ public void describe(SensorDescriptor descriptor) {
} else {
descriptor.name(this.languageName + " Unit Tests Coverage Report Import");
}
descriptor.onlyOnLanguage(this.languageKey);

if (sonarQubeVersion.isGreaterThanOrEqual(Version.create(6,4))) {
descriptor.global();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
package org.sonar.plugins.dotnet.tests;

import java.io.File;

import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.Language;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MINOR Remove this unused import 'org.sonar.api.resources.Language'. rule

import org.sonar.api.utils.Version;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

Expand All @@ -35,18 +39,28 @@ public class UnitTestResultsImportSensor implements Sensor {
private final WildcardPatternFileProvider wildcardPatternFileProvider = new WildcardPatternFileProvider(new File("."), File.separator);
private final UnitTestResultsAggregator unitTestResultsAggregator;
private final ProjectDefinition projectDef;
private final String languageKey;
private final String languageName;
private final SonarQubeVersion sonarQubeVersion;

public UnitTestResultsImportSensor(UnitTestResultsAggregator unitTestResultsAggregator, ProjectDefinition projectDef, String languageName) {
public UnitTestResultsImportSensor(UnitTestResultsAggregator unitTestResultsAggregator, ProjectDefinition projectDef,
String languageKey, String languageName, SonarQubeVersion sonarQubeVersion) {
this.unitTestResultsAggregator = unitTestResultsAggregator;
this.projectDef = projectDef;
this.languageKey = languageKey;
this.languageName = languageName;
this.sonarQubeVersion = sonarQubeVersion;
}

@Override
public void describe(SensorDescriptor descriptor) {
String name = String.format("%s Unit Test Results Import", this.languageName);
descriptor.name(name);

if (sonarQubeVersion.isGreaterThanOrEqual(Version.create(6,4))) {
descriptor.global();
descriptor.onlyOnLanguage(this.languageKey);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.plugins.dotnet.tests;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -28,6 +29,7 @@
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.Settings;

import static java.util.Arrays.asList;
Expand Down Expand Up @@ -89,24 +91,21 @@ public void hasCoverageProperty() {
public void aggregate() {
CoverageCache cache = mock(CoverageCache.class);
when(cache.readCoverageFromCacheOrParse(Mockito.any(CoverageParser.class), Mockito.any(File.class))).thenAnswer(
new Answer<Coverage>() {
@Override
public Coverage answer(InvocationOnMock invocation) throws Throwable {
invocation -> {
CoverageParser parser = (CoverageParser) invocation.getArguments()[0];
File reportFile = (File) invocation.getArguments()[1];
Coverage coverage = new Coverage();
parser.accept(reportFile, coverage);
return coverage;
}
});
});

WildcardPatternFileProvider wildcardPatternFileProvider = mock(WildcardPatternFileProvider.class);

CoverageConfiguration coverageConf = new CoverageConfiguration("", "ncover", "opencover", "dotcover", "visualstudio");
Settings settings = new Settings();
MapSettings settings = new MapSettings();

settings.setProperty("ncover", "foo.nccov");
when(wildcardPatternFileProvider.listFiles("foo.nccov")).thenReturn(new HashSet<>(asList(new File("foo.nccov"))));
when(wildcardPatternFileProvider.listFiles("foo.nccov")).thenReturn(new HashSet<>(Collections.singletonList(new File("foo.nccov"))));
NCover3ReportParser ncoverParser = mock(NCover3ReportParser.class);
OpenCoverReportParser openCoverParser = mock(OpenCoverReportParser.class);
DotCoverReportsAggregator dotCoverParser = mock(DotCoverReportsAggregator.class);
Expand All @@ -125,7 +124,7 @@ public Coverage answer(InvocationOnMock invocation) throws Throwable {
settings.clear();

settings.setProperty("opencover", "bar.xml");
when(wildcardPatternFileProvider.listFiles("bar.xml")).thenReturn(new HashSet<>(asList(new File("bar.xml"))));
when(wildcardPatternFileProvider.listFiles("bar.xml")).thenReturn(new HashSet<>(Collections.singletonList(new File("bar.xml"))));
ncoverParser = mock(NCover3ReportParser.class);
openCoverParser = mock(OpenCoverReportParser.class);
dotCoverParser = mock(DotCoverReportsAggregator.class);
Expand All @@ -143,7 +142,7 @@ public Coverage answer(InvocationOnMock invocation) throws Throwable {

settings.clear();
settings.setProperty("dotcover", "baz.html");
when(wildcardPatternFileProvider.listFiles("baz.html")).thenReturn(new HashSet<>(asList(new File("baz.html"))));
when(wildcardPatternFileProvider.listFiles("baz.html")).thenReturn(new HashSet<>(Collections.singletonList(new File("baz.html"))));
ncoverParser = mock(NCover3ReportParser.class);
openCoverParser = mock(OpenCoverReportParser.class);
dotCoverParser = mock(DotCoverReportsAggregator.class);
Expand All @@ -161,7 +160,7 @@ public Coverage answer(InvocationOnMock invocation) throws Throwable {

settings.clear();
settings.setProperty("visualstudio", "qux.coveragexml");
when(wildcardPatternFileProvider.listFiles("qux.coveragexml")).thenReturn(new HashSet<>(asList(new File("qux.coveragexml"))));
when(wildcardPatternFileProvider.listFiles("qux.coveragexml")).thenReturn(new HashSet<>(Collections.singletonList(new File("qux.coveragexml"))));
ncoverParser = mock(NCover3ReportParser.class);
openCoverParser = mock(OpenCoverReportParser.class);
dotCoverParser = mock(DotCoverReportsAggregator.class);
Expand All @@ -181,10 +180,10 @@ public Coverage answer(InvocationOnMock invocation) throws Throwable {

settings.clear();
settings.setProperty("ncover", ",*.nccov ,bar.nccov");
when(wildcardPatternFileProvider.listFiles("*.nccov")).thenReturn(new HashSet<>(asList(new File("foo.nccov"))));
when(wildcardPatternFileProvider.listFiles("bar.nccov")).thenReturn(new HashSet<>(asList(new File("bar.nccov"))));
when(wildcardPatternFileProvider.listFiles("*.nccov")).thenReturn(new HashSet<>(Collections.singletonList(new File("foo.nccov"))));
when(wildcardPatternFileProvider.listFiles("bar.nccov")).thenReturn(new HashSet<>(Collections.singletonList(new File("bar.nccov"))));
settings.setProperty("opencover", "bar.xml");
when(wildcardPatternFileProvider.listFiles("bar.xml")).thenReturn(new HashSet<>(asList(new File("bar.xml"))));
when(wildcardPatternFileProvider.listFiles("bar.xml")).thenReturn(new HashSet<>(Collections.singletonList(new File("bar.xml"))));
settings.setProperty("dotcover", "baz.html");
when(wildcardPatternFileProvider.listFiles("baz.html")).thenReturn(new HashSet<>(asList(new File("baz.html"))));
settings.setProperty("visualstudio", "qux.coveragexml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;
import org.sonar.api.SonarQubeVersion;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.coverage.CoverageType;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.batch.sensor.internal.SensorContextTester;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;

public class CoverageReportImportSensorTest {

Expand All @@ -48,30 +47,32 @@ public void coverage() {

CoverageAggregator coverageAggregator = mock(CoverageAggregator.class);

new CoverageReportImportSensor(coverageConf, coverageAggregator, "C#", false).describe(new DefaultSensorDescriptor());
SonarQubeVersion sonarQubeVersion = new SonarQubeVersion(SonarQubeVersion.V5_6);
new CoverageReportImportSensor(coverageConf, coverageAggregator, "cs", "C#", sonarQubeVersion, false)
.describe(new DefaultSensorDescriptor());
}

@Test
public void analyze() throws Exception {
SensorContextTester context = computeCoverageMeasures(false);
assertThat(context.lineHits("foo:Foo.cs", CoverageType.UNIT, 2)).isEqualTo(1);
assertThat(context.lineHits("foo:Foo.cs", CoverageType.UNIT, 4)).isEqualTo(0);
assertThat(context.lineHits("foo:Foo.cs", 2)).isEqualTo(1);
assertThat(context.lineHits("foo:Foo.cs", 4)).isEqualTo(0);
}

@Test
public void analyzeIntegrationTests() throws Exception {
SensorContextTester context = computeCoverageMeasures(true);
assertThat(context.lineHits("foo:Foo.cs", CoverageType.IT, 2)).isEqualTo(1);
assertThat(context.lineHits("foo:Foo.cs", CoverageType.IT, 4)).isEqualTo(0);
assertThat(context.lineHits("foo:Foo.cs", 2)).isEqualTo(1);
assertThat(context.lineHits("foo:Foo.cs", 4)).isEqualTo(0);
}

private SensorContextTester computeCoverageMeasures(boolean isIntegrationTest) throws Exception {
File baseDir = temp.newFolder();

Coverage coverage = mock(Coverage.class);
String fooPath = new File(baseDir, "Foo.cs").getAbsolutePath();
String bazPath = new File(baseDir, "Baz.java").getAbsolutePath();
String barPath = new File(baseDir, "Bar.cs").getAbsolutePath();
String fooPath = new File(baseDir, "Foo.cs").getCanonicalPath();
String bazPath = new File(baseDir, "Baz.java").getCanonicalPath();
String barPath = new File(baseDir, "Bar.cs").getCanonicalPath();
when(coverage.files()).thenReturn(new HashSet<>(asList(fooPath, barPath, bazPath)));
when(coverage.hits(fooPath)).thenReturn(ImmutableMap.<Integer, Integer>builder()
.put(2, 1)
Expand All @@ -88,15 +89,18 @@ private SensorContextTester computeCoverageMeasures(boolean isIntegrationTest) t

SensorContextTester context = SensorContextTester.create(baseDir);

DefaultInputFile inputFile = new DefaultInputFile("foo", "Foo.cs")
DefaultInputFile inputFile = new TestInputFileBuilder("foo", baseDir, new File(baseDir, "Foo.cs"))
.setLanguage("cs")
.initMetadata("a\na\na\na\na\na\na\na\na\na\n");
.initMetadata("a\na\na\na\na\na\na\na\na\na\n")
.build();
context.fileSystem().add(inputFile);
context.fileSystem().add(new DefaultInputFile("foo", "Baz.java").setLanguage("java"));
context.fileSystem().add(new TestInputFileBuilder("foo", "Baz.java").setLanguage("java").build());

CoverageConfiguration coverageConf = new CoverageConfiguration("cs", "", "", "", "");

new CoverageReportImportSensor(coverageConf, coverageAggregator, "C#", isIntegrationTest).analyze(context, coverage);
SonarQubeVersion sonarQubeVersion = new SonarQubeVersion(SonarQubeVersion.V5_6);
new CoverageReportImportSensor(coverageConf, coverageAggregator,"cs", "C#", sonarQubeVersion, isIntegrationTest)
.analyze(context, coverage);

verify(coverageAggregator).aggregate(Mockito.any(WildcardPatternFileProvider.class), Mockito.eq(coverage));

Expand Down
Loading