aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine
diff options
context:
space:
mode:
authorJanos Gyerik <janos.gyerik@sonarsource.com>2017-11-30 16:26:49 +0100
committerJanos Gyerik <janos.gyerik@sonarsource.com>2017-12-05 10:47:46 +0100
commit734a658fc6fb58de2c807c646eaae5eb008adf69 (patch)
treee8d328a274f2846292f91eb9b04a4c5e42190d9c /sonar-scanner-engine
parent5a455faaea8e112b6d8c1500cfcbfeccb9c0f9d6 (diff)
downloadsonarqube-734a658fc6fb58de2c807c646eaae5eb008adf69.tar.gz
sonarqube-734a658fc6fb58de2c807c646eaae5eb008adf69.zip
SONAR-10152 Add revision id to scanner report
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java95
1 files changed, 91 insertions, 4 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
index ce68ecc39a4..320db4b83ad 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
@@ -22,6 +22,8 @@ package org.sonar.scanner.report;
import com.google.common.collect.ImmutableMap;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Date;
import org.junit.Before;
import org.junit.Rule;
@@ -32,10 +34,10 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.core.config.ScannerProperties;
import org.sonar.scanner.ProjectAnalysisInfo;
-import org.sonar.scanner.analysis.DefaultAnalysisMode;
import org.sonar.scanner.bootstrap.ScannerPlugin;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cpd.CpdSettings;
@@ -46,11 +48,13 @@ import org.sonar.scanner.rule.ModuleQProfiles;
import org.sonar.scanner.rule.QProfile;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.branch.BranchType;
+import org.sonar.scanner.scm.ScmConfiguration;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -66,9 +70,10 @@ public class MetadataPublisherTest {
private ProjectAnalysisInfo projectAnalysisInfo;
private CpdSettings cpdSettings;
private InputModuleHierarchy inputModuleHierarchy;
- private DefaultAnalysisMode analysisMode;
private ScannerPluginRepository pluginRepository;
private BranchConfiguration branches;
+ private ScmConfiguration scmConfiguration;
+ private ScmProvider scmProvider;
@Before
public void prepare() throws IOException {
@@ -78,6 +83,11 @@ public class MetadataPublisherTest {
settings = new MapSettings();
qProfiles = mock(ModuleQProfiles.class);
pluginRepository = mock(ScannerPluginRepository.class);
+
+ scmProvider = mock(ScmProvider.class);
+ when(scmProvider.relativePathFromScmRoot(any(Path.class))).thenReturn(Paths.get("dummy/path"));
+ when(scmProvider.revisionId(any(Path.class))).thenReturn("dummy-sha1");
+
createPublisher(ProjectDefinition.create().setKey("foo"));
when(pluginRepository.getPluginsByKey()).thenReturn(emptyMap());
}
@@ -86,10 +96,11 @@ public class MetadataPublisherTest {
rootModule = new DefaultInputModule(def.setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()), TestInputFileBuilder.nextBatchId());
inputModuleHierarchy = mock(InputModuleHierarchy.class);
when(inputModuleHierarchy.root()).thenReturn(rootModule);
- analysisMode = mock(DefaultAnalysisMode.class);
branches = mock(BranchConfiguration.class);
+ scmConfiguration = mock(ScmConfiguration.class);
+ when(scmConfiguration.provider()).thenReturn(scmProvider);
underTest = new MetadataPublisher(projectAnalysisInfo, inputModuleHierarchy, settings.asConfig(), qProfiles, cpdSettings,
- pluginRepository, branches);
+ pluginRepository, branches, scmConfiguration);
}
@Test
@@ -196,4 +207,80 @@ public class MetadataPublisherTest {
assertThat(metadata.getMergeBranchName()).isEqualTo(branchTarget);
}
+ @Test
+ public void write_project_basedir() throws Exception {
+ String path = "some/dir";
+ Path relativePathFromScmRoot = Paths.get(path);
+ when(scmProvider.relativePathFromScmRoot(any(Path.class))).thenReturn(relativePathFromScmRoot);
+
+ File outputDir = temp.newFolder();
+ underTest.publish(new ScannerReportWriter(outputDir));
+
+ ScannerReportReader reader = new ScannerReportReader(outputDir);
+ ScannerReport.Metadata metadata = reader.readMetadata();
+ assertThat(metadata.getRelativePathFromScmRoot()).isEqualTo(path);
+ }
+
+ @Test
+ public void write_revision_id() throws Exception {
+ String revisionId = "some-sha1";
+ when(scmProvider.revisionId(any(Path.class))).thenReturn(revisionId);
+
+ File outputDir = temp.newFolder();
+ underTest.publish(new ScannerReportWriter(outputDir));
+
+ ScannerReportReader reader = new ScannerReportReader(outputDir);
+ ScannerReport.Metadata metadata = reader.readMetadata();
+ assertThat(metadata.getScmRevisionId()).isEqualTo(revisionId);
+ }
+
+ @Test
+ public void should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot() throws IOException {
+ String revisionId = "some-sha1";
+
+ ScmProvider fakeScmProvider = new ScmProvider() {
+ @Override
+ public String key() {
+ return "foo";
+ }
+
+ @Override
+ public String revisionId(Path path) {
+ return revisionId;
+ }
+ };
+ when(scmConfiguration.provider()).thenReturn(fakeScmProvider);
+
+ File outputDir = temp.newFolder();
+ underTest.publish(new ScannerReportWriter(outputDir));
+
+ ScannerReportReader reader = new ScannerReportReader(outputDir);
+ ScannerReport.Metadata metadata = reader.readMetadata();
+ assertThat(metadata.getScmRevisionId()).isEqualTo(revisionId);
+ }
+
+ @Test
+ public void should_not_crash_when_scm_provider_does_not_support_revisionId() throws IOException {
+ String relativePathFromScmRoot = "some/path";
+
+ ScmProvider fakeScmProvider = new ScmProvider() {
+ @Override
+ public String key() {
+ return "foo";
+ }
+
+ @Override
+ public Path relativePathFromScmRoot(Path path) {
+ return Paths.get(relativePathFromScmRoot);
+ }
+ };
+ when(scmConfiguration.provider()).thenReturn(fakeScmProvider);
+
+ File outputDir = temp.newFolder();
+ underTest.publish(new ScannerReportWriter(outputDir));
+
+ ScannerReportReader reader = new ScannerReportReader(outputDir);
+ ScannerReport.Metadata metadata = reader.readMetadata();
+ assertThat(metadata.getRelativePathFromScmRoot()).isEqualTo(relativePathFromScmRoot);
+ }
}