From 1cef2be2d888440727f9c90c75b4a2d263c55702 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 7 Dec 2018 15:29:06 -0600 Subject: [PATCH] SONAR-11566 Sub modules don't always have a valid relative path from the root --- .../scanner/report/ComponentsPublisher.java | 11 +++-- .../report/ComponentsPublisherTest.java | 49 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) 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 5cf6649d310..5e1a68031be 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 @@ -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) { 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 e6bcc93c5b3..9d3f7126fe2 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 @@ -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 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); -- 2.39.5