]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11566 Sub modules don't always have a valid relative path from the root
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Fri, 7 Dec 2018 21:29:06 +0000 (15:29 -0600)
committerSonarTech <sonartech@sonarsource.com>
Mon, 10 Dec 2018 19:21:00 +0000 (20:21 +0100)
sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ComponentsPublisher.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java

index 5cf6649d310417d16a2f706cce9798559ff54c7a..5e1a68031be4a38172bc70788cbc91d028c4bec3 100644 (file)
@@ -74,7 +74,8 @@ public class ComponentsPublisher implements ReportPublisherStep {
   }
 
   /**
-   * Writes the tree of components recursively, deep-first. 
+   * Writes the tree of components recursively, deep-first.
+   *
    * @return true if component was written (not skipped)
    */
   private boolean recursiveWriteComponent(DefaultInputComponent component) {
@@ -124,11 +125,11 @@ public class ComponentsPublisher implements ReportPublisherStep {
     String path = getPath(component);
     if (path != null) {
       builder.setPath(path);
+    }
 
-      String projectRelativePath = getProjectRelativePath(component);
-      if (projectRelativePath != null) {
-        builder.setProjectRelativePath(projectRelativePath);
-      }
+    String projectRelativePath = getProjectRelativePath(component);
+    if (projectRelativePath != null) {
+      builder.setProjectRelativePath(projectRelativePath);
     }
 
     for (InputComponent child : children) {
index e6bcc93c5b3387c3bd5896411748851dae875aba..9d3f7126fe2f33d9269c6e65a5499cca4e95052a 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.scanner.report;
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.Collections;
@@ -194,6 +195,54 @@ public class ComponentsPublisherTest {
     assertThat(rootProtobuf.getName()).isEqualTo("foo my_branch");
   }
 
+  @Test
+  public void should_write_relative_path_to_root_for_modules_in_flat_hierarchy() throws IOException {
+    Path rootBaseDir = temp.newFolder().toPath();
+    Path module1BaseDir = rootBaseDir.resolve("module1");
+    Path module2BaseDir = rootBaseDir.resolve("module2");
+    Files.createDirectories(module1BaseDir);
+    Files.createDirectories(module2BaseDir);
+
+    ProjectDefinition rootDef = ProjectDefinition.create()
+      .setKey("foo")
+      .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0")
+      .setName("Root project")
+      .setDescription("Root description")
+      .setBaseDir(rootBaseDir.toFile())
+      .setWorkDir(temp.newFolder());
+    DefaultInputModule root = new DefaultInputModule(rootDef, 1);
+
+    ProjectDefinition module1Def = ProjectDefinition.create()
+      .setKey("module1k")
+      .setName("Module1")
+      .setDescription("Module description")
+      .setBaseDir(module1BaseDir.toFile())
+      .setWorkDir(temp.newFolder());
+    rootDef.addSubProject(module1Def);
+    DefaultInputModule module1 = new DefaultInputModule(module1Def, 2);
+
+    ProjectDefinition module2Def = ProjectDefinition.create()
+      .setKey("module2k")
+      .setName("Module2")
+      .setDescription("Module description")
+      .setBaseDir(module2BaseDir.toFile())
+      .setWorkDir(temp.newFolder());
+    module1Def.addSubProject(module2Def);
+    DefaultInputModule module2 = new DefaultInputModule(module2Def, 3);
+
+    Map<DefaultInputModule, DefaultInputModule> modules = new HashMap<>();
+    modules.put(module2, module1);
+    modules.put(module1, root);
+    moduleHierarchy = new DefaultInputModuleHierarchy(modules);
+    tree.index(module2, module1);
+    tree.index(module1, root);
+    ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration);
+    publisher.publish(writer);
+
+    assertThat(reader.readComponent(2).getProjectRelativePath()).isEqualTo("module1");
+    assertThat(reader.readComponent(3).getProjectRelativePath()).isEqualTo("module2");
+  }
+
   @Test
   public void should_skip_dir_without_published_files() throws IOException {
     ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class);