You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WorkDirectoriesInitializerTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.Arrays;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.TemporaryFolder;
  28. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  29. import org.sonar.scanner.fs.InputModuleHierarchy;
  30. import static org.assertj.core.api.Assertions.assertThat;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.when;
  33. public class WorkDirectoriesInitializerTest {
  34. private WorkDirectoriesInitializer initializer;
  35. @Rule
  36. public TemporaryFolder temp = new TemporaryFolder();
  37. private File rootWorkDir;
  38. private File lock;
  39. private InputModuleHierarchy hierarchy;
  40. private DefaultInputModule root;
  41. @Before
  42. public void setUp() throws IOException {
  43. rootWorkDir = temp.newFolder();
  44. // create files to clean
  45. new File(rootWorkDir, "foo.txt").createNewFile();
  46. File newFolder = new File(rootWorkDir, "foo");
  47. newFolder.mkdir();
  48. File fileInFolder = new File(newFolder, "test");
  49. fileInFolder.createNewFile();
  50. lock = new File(rootWorkDir, DirectoryLock.LOCK_FILE_NAME);
  51. lock.createNewFile();
  52. hierarchy = mock(InputModuleHierarchy.class);
  53. root = mock(DefaultInputModule.class);
  54. when(hierarchy.root()).thenReturn(root);
  55. when(root.getWorkDir()).thenReturn(rootWorkDir.toPath());
  56. assertThat(rootWorkDir.list().length).isGreaterThan(1);
  57. initializer = new WorkDirectoriesInitializer();
  58. }
  59. @Test
  60. public void testNonExisting() {
  61. temp.delete();
  62. initializer.execute(hierarchy);
  63. }
  64. @Test
  65. public void testClean() {
  66. initializer.execute(hierarchy);
  67. assertThat(rootWorkDir).exists();
  68. assertThat(lock).exists();
  69. assertThat(rootWorkDir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME);
  70. }
  71. @Test
  72. public void cleaningRootModuleShouldNotDeleteChildrenWorkDir() throws IOException {
  73. DefaultInputModule moduleA = mock(DefaultInputModule.class);
  74. when(hierarchy.children(root)).thenReturn(Arrays.asList(moduleA));
  75. File moduleAWorkdir = new File(rootWorkDir, "moduleA");
  76. when(moduleA.getWorkDir()).thenReturn(moduleAWorkdir.toPath());
  77. moduleAWorkdir.mkdir();
  78. new File(moduleAWorkdir, "fooA.txt").createNewFile();
  79. initializer.execute(hierarchy);
  80. assertThat(rootWorkDir).exists();
  81. assertThat(lock).exists();
  82. assertThat(rootWorkDir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME, "moduleA");
  83. assertThat(moduleAWorkdir).exists();
  84. }
  85. }