aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-08-07 18:11:58 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2017-08-08 10:51:13 +0200
commit45cbb08cf2cfcdbe6f755e875cf54ce1ce04c196 (patch)
tree7e3f8ec2fdcc083324d28fd10ab1f3268e12d286 /sonar-scanner-engine/src/test/java/org
parenta0f2105b46566f5bea0da5915e923fc20fd31eda (diff)
downloadsonarqube-45cbb08cf2cfcdbe6f755e875cf54ce1ce04c196.tar.gz
sonarqube-45cbb08cf2cfcdbe6f755e875cf54ce1ce04c196.zip
Fix regression introduced during previous scanner hardening
Module workDir is deleted when cleaning root module work dir when they are nested.
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java (renamed from sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoryCleanerTest.java)55
1 files changed, 39 insertions, 16 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoryCleanerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java
index 82886823190..3000f931503 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoryCleanerTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java
@@ -21,6 +21,7 @@ package org.sonar.scanner.scan;
import java.io.File;
import java.io.IOException;
+import java.util.Arrays;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -33,46 +34,68 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public class WorkDirectoryCleanerTest {
- private WorkDirectoryCleaner cleaner;
+public class WorkDirectoriesInitializerTest {
+ private WorkDirectoriesInitializer initializer;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ private File rootWorkDir;
+ private File lock;
+ private InputModuleHierarchy hierarchy;
+ private DefaultInputModule root;
+
@Before
public void setUp() throws IOException {
+ rootWorkDir = temp.newFolder();
// create files to clean
- temp.newFile();
- File newFolder = temp.newFolder();
+ new File(rootWorkDir, "foo.txt").createNewFile();
+ File newFolder = new File(rootWorkDir, "foo");
+ newFolder.mkdir();
File fileInFolder = new File(newFolder, "test");
fileInFolder.createNewFile();
- File lock = new File(temp.getRoot(), DirectoryLock.LOCK_FILE_NAME);
+ lock = new File(rootWorkDir, DirectoryLock.LOCK_FILE_NAME);
lock.createNewFile();
- // mock project
- InputModuleHierarchy hierarchy = mock(InputModuleHierarchy.class);
- DefaultInputModule root = mock(DefaultInputModule.class);
+ hierarchy = mock(InputModuleHierarchy.class);
+ root = mock(DefaultInputModule.class);
when(hierarchy.root()).thenReturn(root);
- when(root.getWorkDir()).thenReturn(temp.getRoot().toPath());
+ when(root.getWorkDir()).thenReturn(rootWorkDir.toPath());
- assertThat(temp.getRoot().list().length).isGreaterThan(1);
- cleaner = new WorkDirectoryCleaner(hierarchy);
+ assertThat(rootWorkDir.list().length).isGreaterThan(1);
+ initializer = new WorkDirectoriesInitializer(hierarchy);
}
@Test
public void testNonExisting() {
temp.delete();
- cleaner.execute();
+ initializer.execute();
}
@Test
public void testClean() {
- File lock = new File(temp.getRoot(), DirectoryLock.LOCK_FILE_NAME);
- cleaner.execute();
+ initializer.execute();
+
+ assertThat(rootWorkDir).exists();
+ assertThat(lock).exists();
+ assertThat(rootWorkDir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME);
+ }
+
+ @Test
+ public void cleaningRootModuleShouldNotDeleteChildrenWorkDir() throws IOException {
+ DefaultInputModule moduleA = mock(DefaultInputModule.class);
+ when(hierarchy.children(root)).thenReturn(Arrays.asList(moduleA));
+ File moduleAWorkdir = new File(rootWorkDir, "moduleA");
+ when(moduleA.getWorkDir()).thenReturn(moduleAWorkdir.toPath());
+ moduleAWorkdir.mkdir();
+ new File(moduleAWorkdir, "fooA.txt").createNewFile();
+
+ initializer.execute();
- assertThat(temp.getRoot()).exists();
+ assertThat(rootWorkDir).exists();
assertThat(lock).exists();
- assertThat(temp.getRoot().list()).containsOnly(DirectoryLock.LOCK_FILE_NAME);
+ assertThat(rootWorkDir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME, "moduleA");
+ assertThat(moduleAWorkdir).exists();
}
}