aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalTempFolderProviderTest.java
blob: 22b5f531cff3fa3eb8cd2d988d439cdb93f847dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.bootstrap;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.FileTime;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.sonar.api.CoreProperties;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TempFolder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class GlobalTempFolderProviderTest {

  private final SonarUserHome sonarUserHome = mock(SonarUserHome.class);
  private final GlobalTempFolderProvider underTest = new GlobalTempFolderProvider();

  @Test
  void createTempFolderProps(@TempDir Path workingDir) throws Exception {
    Files.delete(workingDir);

    var tempFolder = underTest.provide(
      new ScannerProperties(Map.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.toAbsolutePath().toString())), sonarUserHome);
    tempFolder.newDir();
    tempFolder.newFile();

    assertThat(workingDir).isDirectory();
    assertThat(workingDir.toFile().list()).hasSize(1);
    var rootTmpDir = workingDir.toFile().listFiles()[0];
    assertThat(rootTmpDir.list()).hasSize(2);
  }

  @Test
  void cleanUpOld(@TempDir Path workingDir) throws IOException {
    long creationTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(100);

    for (int i = 0; i < 3; i++) {
      Path tmp = workingDir.resolve(".sonartmp_" + i);
      Files.createDirectories(tmp);
      setFileCreationDate(tmp, creationTime);
      assumeCorrectFileCreationDate(tmp, creationTime);
    }

    underTest.provide(
      new ScannerProperties(Map.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.toAbsolutePath().toString())), sonarUserHome);

    // this also checks that all other temps were deleted
    assertThat(workingDir.toFile().list()).hasSize(1);
  }

  // See SONAR-22159, the test started failing due to issues in setting the correct creation time on Cirrus, skipping the test if the problem
  // happens
  private void assumeCorrectFileCreationDate(Path tmp, long creationTime) throws IOException {
    FileTime fileCreationTimeSet = Files.getFileAttributeView(tmp, BasicFileAttributeView.class).readAttributes().creationTime();
    FileTime expectedCreationTime = FileTime.fromMillis(creationTime);

    assumeTrue(expectedCreationTime.compareTo(fileCreationTimeSet) >= 0,
      String.format("Incorrect creation date set on temporary file %s: %s set instead of %s", tmp.toAbsolutePath(), fileCreationTimeSet, expectedCreationTime));
  }

  @Test
  void createTempFolderFromSonarHome(@TempDir Path sonarUserHomePath) {
    // with sonar home, it will be in {sonar.home}/.sonartmp
    when(sonarUserHome.getPath()).thenReturn(sonarUserHomePath);
    
    var expectedWorkingDir = sonarUserHomePath.resolve(CoreProperties.GLOBAL_WORKING_DIRECTORY_DEFAULT_VALUE);

    TempFolder tempFolder = underTest.provide(new ScannerProperties(Map.of()), sonarUserHome);
    tempFolder.newDir();
    tempFolder.newFile();

    assertThat(expectedWorkingDir).isDirectory();
    assertThat(expectedWorkingDir.toFile().list()).hasSize(1);
    var rootTmpDir = expectedWorkingDir.toFile().listFiles()[0];
    assertThat(rootTmpDir.list()).hasSize(2);
  }

  @Test
  void dotWorkingDir(@TempDir Path sonarUserHomePath) {
    when(sonarUserHome.getPath()).thenReturn(sonarUserHomePath);
    String globalWorkDir = ".";
    ScannerProperties globalProperties = new ScannerProperties(
      Map.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, globalWorkDir));

    var tempFolder = underTest.provide(globalProperties, sonarUserHome);
    File newFile = tempFolder.newFile();
    
    assertThat(newFile.getParentFile().getParentFile().toPath()).isEqualTo(sonarUserHomePath);
    assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_");
  }

  @Test
  void homeIsSymbolicLink(@TempDir Path realSonarHome, @TempDir Path symlink) throws IOException {
    assumeTrue(!System2.INSTANCE.isOsWindows());
    symlink.toFile().delete();
    Files.createSymbolicLink(symlink, realSonarHome);
    when(sonarUserHome.getPath()).thenReturn(symlink);

    ScannerProperties globalProperties = new ScannerProperties(Map.of());

    TempFolder tempFolder = underTest.provide(globalProperties, sonarUserHome);
    File newFile = tempFolder.newFile();
    assertThat(newFile.getParentFile().getParentFile().toPath().toAbsolutePath()).isEqualTo(symlink);
    assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_");
  }

  private void setFileCreationDate(Path f, long time) throws IOException {
    BasicFileAttributeView attributes = Files.getFileAttributeView(f, BasicFileAttributeView.class);
    FileTime creationTime = FileTime.fromMillis(time);
    attributes.setTimes(creationTime, creationTime, creationTime);
  }
}