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;
*/
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);
+ }
}
}
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;
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();
+ }
+
}
--- /dev/null
+# 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
--- /dev/null
+# 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
+
--- /dev/null
+<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>
--- /dev/null
+# 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
+
--- /dev/null
+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
--- /dev/null
+<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>
--- /dev/null
+public class Hellob