diff options
author | Philippe Perrin <philippe.perrin@sonarsource.com> | 2022-06-27 14:44:33 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-07-12 14:30:04 +0000 |
commit | d1f2df5d7c34fb35d00053919752cf593a05d549 (patch) | |
tree | 3e6e5883f104645c0a1637beec81926bc93b559e /sonar-scanner-engine/src | |
parent | 201d5326c5e4471a1a77243cd2bd4ac71c9d4ada (diff) | |
download | sonarqube-d1f2df5d7c34fb35d00053919752cf593a05d549.tar.gz sonarqube-d1f2df5d7c34fb35d00053919752cf593a05d549.zip |
SONAR-16554 fix scanner failing to create report for multi-modules projects with no code to analyse
Diffstat (limited to 'sonar-scanner-engine/src')
2 files changed, 41 insertions, 5 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java index 11552502e00..eda4ba28e71 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java @@ -24,6 +24,7 @@ import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.util.Iterator; +import java.util.Objects; import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.DefaultInputProject; @@ -36,12 +37,13 @@ import org.sonar.scanner.fs.InputModuleHierarchy; */ public class WorkDirectoriesInitializer { public void execute(InputModuleHierarchy moduleHierarchy) { + DefaultInputModule root = moduleHierarchy.root(); // dont apply to root. Root is done by InputProjectProvider - for (DefaultInputModule sub : moduleHierarchy.children(moduleHierarchy.root())) { - cleanAllWorkingDirs(moduleHierarchy, sub); - } - for (DefaultInputModule sub : moduleHierarchy.children(moduleHierarchy.root())) { - mkdirsAllWorkingDirs(moduleHierarchy, sub); + for (DefaultInputModule sub : moduleHierarchy.children(root)) { + if (!Objects.equals(root.getWorkDir(), sub.getWorkDir())) { + cleanAllWorkingDirs(moduleHierarchy, sub); + mkdirsAllWorkingDirs(moduleHierarchy, sub); + } } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java index 180466e355c..81c308fabcb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java @@ -21,7 +21,9 @@ package org.sonar.scanner.scan; import java.io.File; import java.io.IOException; +import java.nio.file.Path; import java.util.Arrays; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -114,4 +116,36 @@ public class WorkDirectoriesInitializerTest { assertThat(moduleBWorkdir).isEmptyDirectory(); } + @Test + public void execute_on_hierarchy_should_clean_submodules_expect_submodule_with_same_work_directory_as_root() throws IOException { + DefaultInputModule moduleA = mock(DefaultInputModule.class); + DefaultInputModule moduleB = mock(DefaultInputModule.class); + + when(hierarchy.children(root)).thenReturn(List.of(moduleA, moduleB)); + + File rootAndModuleAWorkdir = new File(rootWorkDir, "moduleA"); + File moduleBWorkdir = new File(rootAndModuleAWorkdir, "../moduleB"); + + when(root.getWorkDir()).thenReturn(rootAndModuleAWorkdir.toPath()); + when(moduleA.getWorkDir()).thenReturn(rootAndModuleAWorkdir.toPath()); + when(moduleB.getWorkDir()).thenReturn(moduleBWorkdir.toPath()); + + rootAndModuleAWorkdir.mkdir(); + createFilesToClean(rootAndModuleAWorkdir); + moduleBWorkdir.mkdir(); + + new File(rootAndModuleAWorkdir, "fooA.txt").createNewFile(); + new File(moduleBWorkdir, "fooB.txt").createNewFile(); + + initializer.execute(hierarchy); + + assertThat(rootWorkDir).exists(); + assertThat(lock).exists(); + assertThat(rootAndModuleAWorkdir).exists(); + assertThat(rootAndModuleAWorkdir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME, "fooA.txt", "foo", "foo.txt"); + assertThat(moduleBWorkdir) + .exists() + .isEmptyDirectory(); + } + } |