Skip to content
Open
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
29 changes: 29 additions & 0 deletions its/src/test/java/org/sonar/plugins/jacoco/its/JacocoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.sonarqube.ws.Ce;
import org.sonarqube.ws.Measures;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.ce.AnalysisStatusRequest;
import org.sonarqube.ws.client.measures.ComponentRequest;

import java.io.File;
Expand Down Expand Up @@ -331,6 +333,33 @@ void kotlin_files_should_be_located_and_covered() {
checkCoveredKotlinFile();
}

/**
* Reproduces JACOCO-241: SonarScanner for Gradle adds the Kotlin build scripts of the whole build to the sources of the root module, which also inherits
* sonar.coverage.jacoco.xmlReportPaths. A Gradle script can't appear in a JaCoCo report, so matching nothing is expected and must not raise a warning.
*/
@Test
void should_not_warn_when_the_only_kotlin_files_are_gradle_scripts() throws IOException {
String projectKey = "kotlin-gradle-scripts-project";
SonarScanner build = SonarScanner.create()
.setProjectKey(projectKey)
.setDebugLogs(true)
.setSourceDirs("build.gradle.kts,settings.gradle.kts")
.setProperty("sonar.coverage.jacoco.xmlReportPaths", "jacoco.xml")
.setProjectDir(prepareProject("kotlin-gradle-scripts-project"));
BuildResult result = orchestrator.executeBuild(build);

assertThat(result.getLogs())
.contains("which has no source file to analyze")
.doesNotContain("could be matched to the analysed sources");
List<String> warnings = getWsClient().ce().analysisStatus(new AnalysisStatusRequest().setComponent(projectKey))
.getComponent()
.getWarningsList()
.stream()
.map(Ce.AnalysisStatusWsResponse.Warning::getMessage)
.collect(Collectors.toList());
assertThat(warnings).noneMatch(warning -> warning.contains("JaCoCo"));
}

private void checkNoJacocoCoverage() {
Map<String, Double> measures = getCoverageMeasures(FILE_KEY);
assertThat(measures.get("line_coverage")).isEqualTo(0.0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
java
}

repositories {
mavenCentral()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN" "report.dtd"><report name="test-jacoco-plugin"><sessioninfo id="meneses-5df0b336" start="1533041905630" dump="1533041905970"/><package name="org/sonarsource/test"><class name="org/sonarsource/test/Calc"><method name="&lt;init&gt;" desc="(I)V" line="6"><counter type="INSTRUCTION" missed="0" covered="6"/><counter type="LINE" missed="0" covered="3"/><counter type="COMPLEXITY" missed="0" covered="1"/><counter type="METHOD" missed="0" covered="1"/></method><method name="add" desc="(I)I" line="11"><counter type="INSTRUCTION" missed="0" covered="15"/><counter type="BRANCH" missed="0" covered="2"/><counter type="LINE" missed="0" covered="4"/><counter type="COMPLEXITY" missed="0" covered="2"/><counter type="METHOD" missed="0" covered="1"/></method><method name="subtract" desc="(I)I" line="19"><counter type="INSTRUCTION" missed="3" covered="12"/><counter type="BRANCH" missed="1" covered="1"/><counter type="LINE" missed="1" covered="3"/><counter type="COMPLEXITY" missed="1" covered="1"/><counter type="METHOD" missed="0" covered="1"/></method><counter type="INSTRUCTION" missed="3" covered="33"/><counter type="BRANCH" missed="1" covered="3"/><counter type="LINE" missed="1" covered="10"/><counter type="COMPLEXITY" missed="1" covered="4"/><counter type="METHOD" missed="0" covered="3"/><counter type="CLASS" missed="0" covered="1"/></class><class name="org/sonarsource/test/CalcNoCoverage"><method name="&lt;init&gt;" desc="(I)V" line="6"><counter type="INSTRUCTION" missed="6" covered="0"/><counter type="LINE" missed="3" covered="0"/><counter type="COMPLEXITY" missed="1" covered="0"/><counter type="METHOD" missed="1" covered="0"/></method><method name="add" desc="(I)I" line="11"><counter type="INSTRUCTION" missed="15" covered="0"/><counter type="BRANCH" missed="2" covered="0"/><counter type="LINE" missed="4" covered="0"/><counter type="COMPLEXITY" missed="2" covered="0"/><counter type="METHOD" missed="1" covered="0"/></method><counter type="INSTRUCTION" missed="21" covered="0"/><counter type="BRANCH" missed="2" covered="0"/><counter type="LINE" missed="7" covered="0"/><counter type="COMPLEXITY" missed="3" covered="0"/><counter type="METHOD" missed="2" covered="0"/><counter type="CLASS" missed="1" covered="0"/></class><sourcefile name="CalcNoCoverage.java"><line nr="6" mi="2" ci="0" mb="0" cb="0"/><line nr="7" mi="3" ci="0" mb="0" cb="0"/><line nr="8" mi="1" ci="0" mb="0" cb="0"/><line nr="11" mi="6" ci="0" mb="0" cb="0"/><line nr="12" mi="3" ci="0" mb="2" cb="0"/><line nr="13" mi="3" ci="0" mb="0" cb="0"/><line nr="15" mi="3" ci="0" mb="0" cb="0"/><counter type="INSTRUCTION" missed="21" covered="0"/><counter type="BRANCH" missed="2" covered="0"/><counter type="LINE" missed="7" covered="0"/><counter type="COMPLEXITY" missed="3" covered="0"/><counter type="METHOD" missed="2" covered="0"/><counter type="CLASS" missed="1" covered="0"/></sourcefile><sourcefile name="Calc.java"><line nr="6" mi="0" ci="2" mb="0" cb="0"/><line nr="7" mi="0" ci="3" mb="0" cb="0"/><line nr="8" mi="0" ci="1" mb="0" cb="0"/><line nr="11" mi="0" ci="6" mb="0" cb="0"/><line nr="12" mi="0" ci="3" mb="0" cb="2"/><line nr="13" mi="0" ci="3" mb="0" cb="0"/><line nr="15" mi="0" ci="3" mb="0" cb="0"/><line nr="19" mi="0" ci="6" mb="0" cb="0"/><line nr="20" mi="0" ci="3" mb="1" cb="1"/><line nr="21" mi="3" ci="0" mb="0" cb="0"/><line nr="23" mi="0" ci="3" mb="0" cb="0"/><counter type="INSTRUCTION" missed="3" covered="33"/><counter type="BRANCH" missed="1" covered="3"/><counter type="LINE" missed="1" covered="10"/><counter type="COMPLEXITY" missed="1" covered="4"/><counter type="METHOD" missed="0" covered="3"/><counter type="CLASS" missed="0" covered="1"/></sourcefile><counter type="INSTRUCTION" missed="24" covered="33"/><counter type="BRANCH" missed="3" covered="3"/><counter type="LINE" missed="8" covered="10"/><counter type="COMPLEXITY" missed="4" covered="4"/><counter type="METHOD" missed="2" covered="3"/><counter type="CLASS" missed="1" covered="1"/></package><counter type="INSTRUCTION" missed="24" covered="33"/><counter type="BRANCH" missed="3" covered="3"/><counter type="LINE" missed="8" covered="10"/><counter type="COMPLEXITY" missed="4" covered="4"/><counter type="METHOD" missed="2" covered="3"/><counter type="CLASS" missed="1" covered="1"/></report>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "kotlin-gradle-scripts-project"
15 changes: 14 additions & 1 deletion src/main/java/org/sonar/plugins/jacoco/FileLocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.StreamSupport;
Expand All @@ -31,6 +32,7 @@
public abstract class FileLocator {
public static final String SEPARATOR_REGEX = Pattern.quote(File.separator);
private static final Set<String> COVERED_LANGUAGES = Set.of("java", "kotlin");
private static final String KOTLIN_SCRIPT_EXTENSION = ".kts";

protected final ReversePathTree tree = new ReversePathTree();
protected final KotlinFileLocator kotlinFileLocator;
Expand All @@ -55,7 +57,18 @@ protected FileLocator(List<InputFile> inputFiles, @Nullable KotlinFileLocator ko
}

private static boolean isCoverableByJacoco(InputFile inputFile) {
return inputFile.type() == InputFile.Type.MAIN && inputFile.language() != null && COVERED_LANGUAGES.contains(inputFile.language());
return inputFile.type() == InputFile.Type.MAIN
&& inputFile.language() != null
&& COVERED_LANGUAGES.contains(inputFile.language())
&& !isKotlinScript(inputFile);
}

/**
* Kotlin scripts, such as the Gradle build scripts that the SonarScanner for Gradle adds to the sources of the root module, are not compiled
* into classes, so they never appear in a JaCoCo report.
*/
private static boolean isKotlinScript(InputFile inputFile) {
return inputFile.filename().toLowerCase(Locale.ROOT).endsWith(KOTLIN_SCRIPT_EXTENSION);
}

public boolean hasFilesCoverableByJacoco() {
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/sonar/plugins/jacoco/FileLocatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ void should_not_have_files_coverable_by_jacoco_for_files_without_language() {
assertThat(locator.hasFilesCoverableByJacoco()).isFalse();
}

@Test
void should_not_have_files_coverable_by_jacoco_for_kotlin_scripts() {
InputFile buildScript = inputFile("build.gradle.kts", "kotlin", InputFile.Type.MAIN);
InputFile settingsScript = inputFile("settings.gradle.kts", "kotlin", InputFile.Type.MAIN);
InputFile upperCaseScript = inputFile("buildSrc/Script.KTS", "kotlin", InputFile.Type.MAIN);
ModuleFileLocator locator = new ModuleFileLocator(Arrays.asList(buildScript, settingsScript, upperCaseScript), kotlinFileLocator);
assertThat(locator.hasFilesCoverableByJacoco()).isFalse();
}

@Test
void should_have_files_coverable_by_jacoco_for_kotlin_files_next_to_kotlin_scripts() {
InputFile buildScript = inputFile("build.gradle.kts", "kotlin", InputFile.Type.MAIN);
InputFile mainFile = inputFile("src/main/kotlin/File.kt", "kotlin", InputFile.Type.MAIN);
ModuleFileLocator locator = new ModuleFileLocator(Arrays.asList(buildScript, mainFile), kotlinFileLocator);
assertThat(locator.hasFilesCoverableByJacoco()).isTrue();
}

@Test
void should_not_have_files_coverable_by_jacoco_when_no_file_is_indexed() {
ModuleFileLocator locator = new ModuleFileLocator(Collections.emptyList(), kotlinFileLocator);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/sonar/plugins/jacoco/SensorUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.sonar.plugins.jacoco;

import com.sonarsource.scanner.engine.sensor.test.fixtures.TestInputFileBuilder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
Expand Down Expand Up @@ -253,4 +254,43 @@ void do_not_warn_when_the_module_has_no_indexed_file() {
assertThat(logTester.logs(Level.WARN)).isEmpty();
verifyNoInteractions(analysisWarnings);
}

@Test
void do_not_warn_when_the_only_kotlin_files_of_the_module_are_gradle_scripts() {
logTester.setLevel(Level.DEBUG);
ModuleFileLocator locator = new ModuleFileLocator(Arrays.asList(
kotlinMainFile("build.gradle.kts"),
kotlinMainFile("settings.gradle.kts")), new KotlinFileLocator(null));
ReportImporter importer = mock(ReportImporter.class);
AnalysisWarnings analysisWarnings = mock(AnalysisWarnings.class);

SensorUtils.importReports(Collections.singletonList(MANY_FILES_REPORT), locator, importer, LOG, analysisWarnings, "my-project");

assertThat(logTester.logs(Level.WARN)).isEmpty();
assertThat(logTester.logs(Level.DEBUG)).contains("Coverage report '" + MANY_FILES_REPORT
+ "' was not imported into 'my-project', which has no source file to analyze.");
verifyNoInteractions(analysisWarnings);
verifyNoInteractions(importer);
}

@Test
void warn_when_nothing_matches_in_a_module_with_kotlin_sources_and_gradle_scripts() {
ModuleFileLocator locator = new ModuleFileLocator(Arrays.asList(
kotlinMainFile("build.gradle.kts"),
kotlinMainFile("src/main/kotlin/org/example/App.kt")), new KotlinFileLocator(null));
ReportImporter importer = mock(ReportImporter.class);
AnalysisWarnings analysisWarnings = mock(AnalysisWarnings.class);

SensorUtils.importReports(Collections.singletonList(MANY_FILES_REPORT), locator, importer, LOG, analysisWarnings, "my-project");

assertThat(logTester.logs(Level.WARN)).containsExactly("None of the 36 files in coverage report '" + MANY_FILES_REPORT
+ "' could be matched to the analysed sources of 'my-project'."
+ " No coverage was imported from this report.");
verify(analysisWarnings).addUnique(SensorUtils.NOTHING_MATCHED_ANALYSIS_WARNING);
verifyNoInteractions(importer);
}

private static InputFile kotlinMainFile(String relativePath) {
return new TestInputFileBuilder("my-project", relativePath).setLanguage("kotlin").setType(InputFile.Type.MAIN).build();
}
}
Loading