diff options
12 files changed, 152 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(); + } + } 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 index 00000000000..8e9516ae86b --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample2/sonar-project.properties @@ -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 index 00000000000..e69de29bb2d --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/a.xoo 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 index 00000000000..32c30f6c20f --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/aaasonar-project.properties.bk @@ -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 index 00000000000..a588e760f94 --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/buildpom.xml @@ -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 index 00000000000..5d916ab417e --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/sonar-project.properties @@ -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 index 00000000000..74d29a4fa08 --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo @@ -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 index 00000000000..9339e13010d --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_a/src2/main/xoo/com/sonar/it/samples/modules/a1/HelloA1.xoo @@ -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 index 00000000000..e69de29bb2d --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/b.xoo 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 index 00000000000..9f4e04bc53e --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/pom.xml @@ -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 index 00000000000..b3267d7b012 --- /dev/null +++ b/sonar-scanner-engine/test-resources/mediumtest/xoo/multi-modules-sample3/module_b/src/main/xoo/com/sonar/it/samples/modules/a1/HelloB.xoo @@ -0,0 +1 @@ +public class Hellob |