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.

GlobalTempFolderProviderTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.bootstrap;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.nio.file.Files;
  25. import java.nio.file.Path;
  26. import java.nio.file.attribute.BasicFileAttributeView;
  27. import java.nio.file.attribute.FileTime;
  28. import java.util.Map;
  29. import java.util.concurrent.TimeUnit;
  30. import org.junit.jupiter.api.Test;
  31. import org.junit.jupiter.api.io.TempDir;
  32. import org.sonar.api.CoreProperties;
  33. import org.sonar.api.utils.System2;
  34. import org.sonar.api.utils.TempFolder;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.junit.jupiter.api.Assumptions.assumeTrue;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. class GlobalTempFolderProviderTest {
  40. private final SonarUserHome sonarUserHome = mock(SonarUserHome.class);
  41. private final GlobalTempFolderProvider underTest = new GlobalTempFolderProvider();
  42. @Test
  43. void createTempFolderProps(@TempDir Path workingDir) throws Exception {
  44. Files.delete(workingDir);
  45. var tempFolder = underTest.provide(
  46. new ScannerProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.toAbsolutePath().toString())), sonarUserHome);
  47. tempFolder.newDir();
  48. tempFolder.newFile();
  49. assertThat(workingDir).isDirectory();
  50. assertThat(workingDir.toFile().list()).hasSize(1);
  51. var rootTmpDir = workingDir.toFile().listFiles()[0];
  52. assertThat(rootTmpDir.list()).hasSize(2);
  53. }
  54. @Test
  55. void cleanUpOld(@TempDir Path workingDir) throws IOException {
  56. long creationTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(100);
  57. for (int i = 0; i < 3; i++) {
  58. Path tmp = workingDir.resolve(".sonartmp_" + i);
  59. Files.createDirectories(tmp);
  60. setFileCreationDate(tmp, creationTime);
  61. }
  62. underTest.provide(
  63. new ScannerProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.toAbsolutePath().toString())), sonarUserHome);
  64. // this also checks that all other temps were deleted
  65. assertThat(workingDir.toFile().list()).hasSize(1);
  66. }
  67. @Test
  68. void createTempFolderFromSonarHome(@TempDir Path sonarUserHomePath) throws Exception {
  69. // with sonar home, it will be in {sonar.home}/.sonartmp
  70. when(sonarUserHome.getPath()).thenReturn(sonarUserHomePath);
  71. var expectedWorkingDir = sonarUserHomePath.resolve(CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE);
  72. TempFolder tempFolder = underTest.provide(new ScannerProperties(Map.of()), sonarUserHome);
  73. tempFolder.newDir();
  74. tempFolder.newFile();
  75. assertThat(expectedWorkingDir).isDirectory();
  76. assertThat(expectedWorkingDir.toFile().list()).hasSize(1);
  77. var rootTmpDir = expectedWorkingDir.toFile().listFiles()[0];
  78. assertThat(rootTmpDir.list()).hasSize(2);
  79. }
  80. @Test
  81. void dotWorkingDir(@TempDir Path sonarUserHomePath) {
  82. when(sonarUserHome.getPath()).thenReturn(sonarUserHomePath);
  83. String globalWorkDir = ".";
  84. ScannerProperties globalProperties = new ScannerProperties(
  85. ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, globalWorkDir));
  86. var tempFolder = underTest.provide(globalProperties, sonarUserHome);
  87. File newFile = tempFolder.newFile();
  88. assertThat(newFile.getParentFile().getParentFile().toPath()).isEqualTo(sonarUserHomePath);
  89. assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_");
  90. }
  91. @Test
  92. void homeIsSymbolicLink(@TempDir Path realSonarHome, @TempDir Path symlink) throws IOException {
  93. assumeTrue(!System2.INSTANCE.isOsWindows());
  94. symlink.toFile().delete();
  95. Files.createSymbolicLink(symlink, realSonarHome);
  96. when(sonarUserHome.getPath()).thenReturn(symlink);
  97. ScannerProperties globalProperties = new ScannerProperties(Map.of());
  98. TempFolder tempFolder = underTest.provide(globalProperties, sonarUserHome);
  99. File newFile = tempFolder.newFile();
  100. assertThat(newFile.getParentFile().getParentFile().toPath().toAbsolutePath()).isEqualTo(symlink);
  101. assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_");
  102. }
  103. private void setFileCreationDate(Path f, long time) throws IOException {
  104. BasicFileAttributeView attributes = Files.getFileAttributeView(f, BasicFileAttributeView.class);
  105. FileTime creationTime = FileTime.fromMillis(time);
  106. attributes.setTimes(creationTime, creationTime, creationTime);
  107. }
  108. }