]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16554 fix scanner failing to create report for multi-modules projects with...
authorPhilippe Perrin <philippe.perrin@sonarsource.com>
Mon, 27 Jun 2022 12:44:33 +0000 (14:44 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 12 Jul 2022 14:30:04 +0000 (14:30 +0000)
12 files changed:
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample2/sonar-project.properties [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/a.xoo [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/aaasonar-project.properties.bk [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/buildpom.xml [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/sonar-project.properties [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src2/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/b.xoo [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/pom.xml [new file with mode: 0644]
sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/src/main/xoo/com/sonar/it/samples/modules/a1/HelloB.xoo [new file with mode: 0644]

index 11552502e00f67bcc0aafc2cb88cf05e5cd2e72c..eda4ba28e716bd6f0552f4346cdfb96b54798dc8 100644 (file)
@@ -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);
+      }
     }
   }
 
index 180466e355c0601e7f1047cf78563743386b1375..81c308fabcbfa02a75305ff20f59a2229d5943b1 100644 (file)
@@ -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();
+  }
+
 }
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample2/sonar-project.properties b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample2/sonar-project.properties
new file mode 100644 (file)
index 0000000..8e9516a
--- /dev/null
@@ -0,0 +1,22 @@
+# Root project information
+sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample
+sonar.projectName=Sonar :: Integration Tests :: Multi-modules Sample
+sonar.projectVersion=1.0-SNAPSHOT
+
+# Some properties that will be inherited by the modules
+#sonar.sources=src/main
+# List of the module identifiers
+sonar.modules=module_a,module_b
+
+module_a.sonar.projectKey=module_a
+module_a.sonar.projectName=Module A
+
+
+module_b.sonar.projectKey=module_b
+module_b.sonar.projectName=Module B
+
+
+sonar.working.directory=module_a/target
+module_a.sonar.working.directory=target
+module_b.sonar.working.directory=target
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/a.xoo b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/a.xoo
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/aaasonar-project.properties.bk b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/aaasonar-project.properties.bk
new file mode 100644 (file)
index 0000000..32c30f6
--- /dev/null
@@ -0,0 +1,22 @@
+# Root project information
+sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample
+sonar.projectName=Sonar :: Integration Tests :: Multi-modules Sample
+sonar.projectVersion=1.0-SNAPSHOT
+
+# Some properties that will be inherited by the modules
+
+
+
+# List of the module identifiers
+sonar.modules=., ../module_b
+
+sonar.working.directory=target
+..sonar.working.directory=target
+../module_b.working.directory=../module_b/targetC
+
+..sonar.projectKey=
+..sonar.projectName=Module A
+
+../module_b.sonar.projectKey=module_b
+../module_b.sonar.projectName=Module B
+
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/buildpom.xml b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/buildpom.xml
new file mode 100644 (file)
index 0000000..a588e76
--- /dev/null
@@ -0,0 +1,16 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>xyz.test.project</groupId>
+       <artifactId>test-project</artifactId>
+       <version>1.0.LOCAL.RELEASE</version>
+       <name>test-project</name>
+       <description>test-project</description>
+       <packaging>pom</packaging>
+
+       <modules>
+               <module>../module_b</module>
+               <module>.</module>
+       </modules>
+
+</project>
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/sonar-project.properties b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/sonar-project.properties
new file mode 100644 (file)
index 0000000..5d916ab
--- /dev/null
@@ -0,0 +1,24 @@
+# Root project information
+sonar.projectKey=com.sonarsource.it.samples:multi-modules-sample
+sonar.projectName=Sonar :: Integration Tests :: Multi-modules Sample
+sonar.projectVersion=1.0-SNAPSHOT
+
+# Some properties that will be inherited by the modules
+sonar.sources=module_a/src2/main/xoo
+module_a.sonar.sources=src/main/xoo
+module_b.sonar.sources=src/main/xoo
+
+
+# List of the module identifiers
+sonar.modules=module_a, module_b
+
+sonar.working.directory=module_a/target/sonar
+module_a.sonar.working.directory=target/sonar
+module_b.sonar.working.directory=targetC/sonar
+
+module_a.sonar.projectKey=
+module_a.sonar.projectName=Module A
+
+module_b.sonar.projectKey=module_b
+module_b.sonar.projectName=Module B
+
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo
new file mode 100644 (file)
index 0000000..74d29a4
--- /dev/null
@@ -0,0 +1,16 @@
+package com.sonar.it.samples.modules.a1;
+
+public class HelloA1 {
+       private int i;
+       private HelloA1() {
+               
+       }
+       
+       public void hello() {
+               System.out.println("hello" + " xoo");
+       }
+       
+       protected String getHello() {
+               return "hello";
+       }
+}
\ No newline at end of file
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src2/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src2/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo
new file mode 100644 (file)
index 0000000..9339e13
--- /dev/null
@@ -0,0 +1 @@
+Root
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/b.xoo b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/b.xoo
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/pom.xml b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/pom.xml
new file mode 100644 (file)
index 0000000..9f4e04b
--- /dev/null
@@ -0,0 +1,9 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+       <modelVersion>4.0.0</modelVersion>
+       <groupId>xyz.test.war</groupId>
+       <artifactId>module_b</artifactId>
+       <version>1.0.1.RELEASE</version>
+       <packaging>war</packaging>
+       
+</project>
diff --git a/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/src/main/xoo/com/sonar/it/samples/modules/a1/HelloB.xoo b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/src/main/xoo/com/sonar/it/samples/modules/a1/HelloB.xoo
new file mode 100644 (file)
index 0000000..b3267d7
--- /dev/null
@@ -0,0 +1 @@
+public class Hellob