aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJanos Gyerik <janos.gyerik@sonarsource.com>2018-09-10 17:48:53 +0200
committersonartech <sonartech@sonarsource.com>2018-09-19 10:51:42 +0200
commitadb7939d170496d34efe09c71daceecaedd42e8c (patch)
tree64a4a6374fa5657ef76908809fe172e61e9ea3fd /sonar-scanner-engine
parent3e857050df9d80e3e1e2cf703f4b30d85489590b (diff)
downloadsonarqube-adb7939d170496d34efe09c71daceecaedd42e8c.tar.gz
sonarqube-adb7939d170496d34efe09c71daceecaedd42e8c.zip
SONAR-11125 Publish all components with measures but without sources of unchanged files
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java12
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/report/SourcePublisher.java2
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java16
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java9
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java10
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java18
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java24
9 files changed, 47 insertions, 48 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java
index 1413a7a6bfa..5a60eeb5290 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ChangedLinesPublisher.java
@@ -72,10 +72,16 @@ public class ChangedLinesPublisher implements ReportPublisherStep {
}
private int writeChangedLines(ScmProvider provider, ScannerReportWriter writer) {
+ String targetBranchName = branchConfiguration.branchTarget();
+ if (targetBranchName == null) {
+ return 0;
+ }
+
Path rootBaseDir = inputModuleHierarchy.root().getBaseDir();
- Map<Path, DefaultInputFile> allPublishedFiles = StreamSupport.stream(inputComponentStore.allFilesToPublish().spliterator(), false)
+ Map<Path, DefaultInputFile> changedFiles = StreamSupport.stream(inputComponentStore.allChangedFilesToPublish().spliterator(), false)
.collect(Collectors.toMap(DefaultInputFile::path, f -> f));
- Map<Path, Set<Integer>> pathSetMap = provider.branchChangedLines(branchConfiguration.branchTarget(), rootBaseDir, allPublishedFiles.keySet());
+
+ Map<Path, Set<Integer>> pathSetMap = provider.branchChangedLines(targetBranchName, rootBaseDir, changedFiles.keySet());
int count = 0;
if (pathSetMap == null) {
@@ -84,7 +90,7 @@ public class ChangedLinesPublisher implements ReportPublisherStep {
return count;
}
- for (Map.Entry<Path, DefaultInputFile> e : allPublishedFiles.entrySet()) {
+ for (Map.Entry<Path, DefaultInputFile> e : changedFiles.entrySet()) {
Set<Integer> changedLines = pathSetMap.getOrDefault(e.getKey(), Collections.emptySet());
if (changedLines.isEmpty()) {
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java
index 8ec6f7af82a..2ad95729c13 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java
@@ -165,7 +165,7 @@ public class ComponentsPublisher implements ReportPublisherStep {
} else if (component instanceof DefaultInputFile) {
// skip files not marked for publishing
DefaultInputFile inputFile = (DefaultInputFile) component;
- return !inputFile.isPublished() || (branchConfiguration.isShortOrPullRequest() && inputFile.status() == Status.SAME);
+ return !inputFile.isPublished();
}
return false;
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/SourcePublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/SourcePublisher.java
index acb37b393fa..95cfc041b1e 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/SourcePublisher.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/SourcePublisher.java
@@ -44,7 +44,7 @@ public class SourcePublisher implements ReportPublisherStep {
@Override
public void publish(ScannerReportWriter writer) {
- for (final DefaultInputFile inputFile : componentCache.allFilesToPublish()) {
+ for (final DefaultInputFile inputFile : componentCache.allChangedFilesToPublish()) {
File iofile = writer.getSourceFile(inputFile.batchId());
try (OutputStream output = new BufferedOutputStream(new FileOutputStream(iofile));
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
index e6d96e51733..0d4388e3dd7 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/InputComponentStore.java
@@ -31,12 +31,12 @@ import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
+import java.util.stream.Stream;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.InputFile.Status;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
@@ -76,11 +76,19 @@ public class InputComponentStore {
return inputComponents.values();
}
- public Iterable<DefaultInputFile> allFilesToPublish() {
+ private Stream<DefaultInputFile> allFilesToPublishStream() {
return inputFileCache.values().stream()
.map(f -> (DefaultInputFile) f)
- .filter(DefaultInputFile::isPublished)
- .filter(f -> !branchConfiguration.isShortOrPullRequest() || f.status() != Status.SAME)
+ .filter(DefaultInputFile::isPublished);
+ }
+
+ public Iterable<DefaultInputFile> allFilesToPublish() {
+ return allFilesToPublishStream()::iterator;
+ }
+
+ public Iterable<DefaultInputFile> allChangedFilesToPublish() {
+ return allFilesToPublishStream()
+ .filter(f -> !branchConfiguration.isShortOrPullRequest() || f.status() != InputFile.Status.SAME)
::iterator;
}
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
index 1d568737d7c..859cda8b75d 100644
--- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
+++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java
@@ -231,9 +231,6 @@ public class DefaultSensorStorage implements SensorStorage {
public void store(Measure newMeasure) {
if (newMeasure.inputComponent() instanceof DefaultInputFile) {
DefaultInputFile defaultInputFile = (DefaultInputFile) newMeasure.inputComponent();
- if (shouldSkipStorage(defaultInputFile)) {
- return;
- }
defaultInputFile.setPublished(true);
}
saveMeasure(newMeasure.inputComponent(), (DefaultMeasure<?>) newMeasure);
@@ -248,9 +245,6 @@ public class DefaultSensorStorage implements SensorStorage {
private void saveMeasure(InputComponent component, DefaultMeasure<?> measure) {
if (component.isFile()) {
DefaultInputFile defaultInputFile = (DefaultInputFile) component;
- if (shouldSkipStorage(defaultInputFile)) {
- return;
- }
defaultInputFile.setPublished(true);
}
@@ -471,9 +465,6 @@ public class DefaultSensorStorage implements SensorStorage {
@Override
public void store(DefaultCoverage defaultCoverage) {
DefaultInputFile inputFile = (DefaultInputFile) defaultCoverage.inputFile();
- if (shouldSkipStorage(inputFile)) {
- return;
- }
inputFile.setPublished(true);
if (defaultCoverage.linesToCover() > 0) {
saveCoverageMetricInternal(inputFile, LINES_TO_COVER, new DefaultMeasure<Integer>().forMetric(LINES_TO_COVER).withValue(defaultCoverage.linesToCover()));
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
index 28d4b5e0d77..efc99a743f8 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
@@ -70,7 +70,7 @@ public class BranchMediumTest {
}
@Test
- public void should_skip_report_for_unchanged_files_in_short_branch() {
+ public void should_not_skip_report_for_unchanged_files_in_short_branch() {
// sanity check, normally report gets generated
TaskResult result = getResult(tester);
assertThat(getResult(tester).getReportComponent(result.inputFile(FILE_PATH).key())).isNotNull();
@@ -79,12 +79,12 @@ public class BranchMediumTest {
assertThat(result.getReportReader().hasCoverage(fileId)).isTrue();
assertThat(result.getReportReader().readFileSource(fileId)).isNotNull();
- // file is skipped for short branches (no report, no coverage, no duplications)
+ // file is not skipped for short branches (need coverage, duplications coming soon)
TaskResult result2 = getResult(tester.setBranchType(BranchType.SHORT));
- assertThat(result2.getReportComponent(result2.inputFile(FILE_PATH).key())).isNull();
+ assertThat(result2.getReportComponent(result2.inputFile(FILE_PATH).key())).isNotNull();
assertThat(result2.getReportReader().readChangesets(fileId)).isNull();
- assertThat(result2.getReportReader().hasCoverage(fileId)).isFalse();
- assertThat(result.getReportReader().readFileSource(fileId)).isNull();
+ assertThat(result2.getReportReader().hasCoverage(fileId)).isTrue();
+ assertThat(result2.getReportReader().readFileSource(fileId)).isNull();
}
@Test
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
index 4f73d5894cf..6bb328a0222 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
@@ -119,7 +119,7 @@ public class ChangedLinesPublisherTest {
Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
Set<Integer> lines = new HashSet<>(Arrays.asList(1, 10));
when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
- when(inputComponentStore.allFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
+ when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
publisher.publish(writer);
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
index 1e84cff4434..da85993c73a 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
@@ -309,7 +309,7 @@ public class ComponentsPublisherTest {
}
@Test
- public void skip_unchanged_components_in_short_branches() throws IOException {
+ public void do_not_skip_unchanged_components_in_short_branches() throws IOException {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class);
when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
@@ -373,13 +373,14 @@ public class ComponentsPublisherTest {
assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue();
assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue();
- assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isFalse();
- assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isFalse();
- assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse();
+ // do not skip, needed for computing overall coverage
+ assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue();
+ assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue();
+ assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isTrue();
}
@Test
- public void skip_unchanged_components_in_pull_requests() throws IOException {
+ public void do_not_skip_unchanged_components_in_pull_requests() throws IOException {
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class);
when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12"));
@@ -443,9 +444,10 @@ public class ComponentsPublisherTest {
assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue();
assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue();
- assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isFalse();
- assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isFalse();
- assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse();
+ // do not skip, needed for computing overall coverage
+ assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue();
+ assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue();
+ assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isTrue();
}
@Test
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
index 27b67e50220..813c6310e5b 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
@@ -86,6 +86,7 @@ public class DefaultSensorStorageTest {
MetricFinder metricFinder = mock(MetricFinder.class);
when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC);
when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION);
+ when(metricFinder.<Integer>findByKey(CoreMetrics.LINES_TO_COVER_KEY)).thenReturn(CoreMetrics.LINES_TO_COVER);
settings = new MapSettings();
moduleIssues = mock(ModuleIssues.class);
@@ -191,29 +192,20 @@ public class DefaultSensorStorageTest {
}
@Test
- public void should_skip_file_measure_on_short_branch_when_file_status_is_SAME() {
- InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
- when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
-
- underTest.store(new DefaultMeasure()
- .on(file)
- .forMetric(CoreMetrics.NCLOC)
- .withValue(10));
-
- verifyZeroInteractions(measureCache);
- }
-
- @Test
- public void should_skip_file_measure_on_pull_request_when_file_status_is_SAME() {
+ public void should_not_skip_file_measures_on_short_lived_branch_or_pull_request_when_file_status_is_SAME() {
InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
when(branchConfiguration.isShortOrPullRequest()).thenReturn(true);
+ ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
+ when(measureCache.put(eq(file.key()), eq(CoreMetrics.LINES_TO_COVER_KEY), argumentCaptor.capture())).thenReturn(null);
underTest.store(new DefaultMeasure()
.on(file)
- .forMetric(CoreMetrics.NCLOC)
+ .forMetric(CoreMetrics.LINES_TO_COVER)
.withValue(10));
- verifyZeroInteractions(measureCache);
+ DefaultMeasure m = argumentCaptor.getValue();
+ assertThat(m.value()).isEqualTo(10);
+ assertThat(m.metric()).isEqualTo(CoreMetrics.LINES_TO_COVER);
}
@Test