aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-scanner-engine/src')
-rw-r--r--sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/WorkDirectoriesInitializer.java12
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java34
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();
+ }
+
}