Skip to content

Commit 24971b2

Browse files
committed
Fix wrong location of the sonarUserHome
1 parent a09a479 commit 24971b2

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

its/it-tests/src/test/java/com/sonar/scanner/lib/it/PropertiesTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,28 @@ public void testRuntimeEnvironmentPassedAsUserAgent() throws IOException {
5656
@Test
5757
public void passConfigurationUsingEnvVariables() throws IOException {
5858
SimpleScanner scanner = new SimpleScanner();
59-
BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), ORCHESTRATOR.getServer().getUrl(), Map.of(), Map.of("SONAR_SCANNER_JSON_PARAMS", "{\"sonar.exclusions\": \"**/Hello.js\"}"));
59+
BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), ORCHESTRATOR.getServer().getUrl(), Map.of(),
60+
Map.of("SONAR_SCANNER_JSON_PARAMS", "{\"sonar.exclusions\": \"**/Hello.js\"}"));
6061
assertThat(buildResult.getLastStatus()).isZero();
6162

6263
assertThat(buildResult.getLogs()).contains("1 file indexed");
6364
}
6465

66+
@Test
67+
public void cacheIsInUserHomeByDefault() throws IOException {
68+
SimpleScanner scanner = new SimpleScanner();
69+
BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), ORCHESTRATOR.getServer().getUrl(), Map.of(), Map.of());
70+
assertThat(buildResult.getLastStatus()).isZero();
71+
72+
assertThat(Paths.get(System.getProperty("user.home")).resolve(".sonar/cache")).isDirectoryRecursivelyContaining(("glob:**/*scanner-engine*.jar"));
73+
}
74+
6575
@Test
6676
public void overrideHomeDirectoryWithEnv() throws IOException {
6777
var userHome = temp.newFolder();
6878
SimpleScanner scanner = new SimpleScanner();
69-
BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), ORCHESTRATOR.getServer().getUrl(), Map.of(), Map.of("SONAR_USER_HOME", userHome.getAbsolutePath()));
79+
BuildResult buildResult = scanner.executeSimpleProject(project("js-sample"), ORCHESTRATOR.getServer().getUrl(), Map.of(),
80+
Map.of("SONAR_USER_HOME", userHome.getAbsolutePath()));
7081
assertThat(buildResult.getLastStatus()).isZero();
7182

7283
assertThat(userHome.toPath().resolve("cache")).isDirectoryRecursivelyContaining(("glob:**/*scanner-engine*.jar"));

lib/src/main/java/org/sonarsource/scanner/lib/ScannerEngineBootstrapper.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.HashMap;
2525
import java.util.HashSet;
2626
import java.util.Map;
27+
import java.util.Objects;
2728
import java.util.Set;
2829
import org.sonarsource.scanner.lib.internal.ClassloadRules;
2930
import org.sonarsource.scanner.lib.internal.InternalProperties;
@@ -71,7 +72,13 @@ public ScannerEngineFacade bootstrap() {
7172
ClassloadRules rules = new ClassloadRules(Collections.emptySet(), unmaskRules);
7273
var properties = Map.copyOf(bootstrapProperties);
7374
var isSonarCloud = getSonarCloudUrl().equals(properties.get(ScannerProperties.HOST_URL));
74-
var sonarUserHome = properties.getOrDefault(ScannerProperties.SONAR_USER_HOME, Paths.get("").resolve(".sonar").toString());
75+
String sonarUserHome;
76+
if (properties.containsKey(ScannerProperties.SONAR_USER_HOME)) {
77+
sonarUserHome = properties.get(ScannerProperties.SONAR_USER_HOME);
78+
} else {
79+
var userHome = Objects.requireNonNull(System.getProperty("user.home"), "The system property 'user.home' is expected to be non null");
80+
sonarUserHome = Paths.get(userHome, ".sonar").toAbsolutePath().toString();
81+
}
7582
var launcherFactory = new IsolatedLauncherFactory(new LoggerAdapter(logOutput), new SonarUserHome(Paths.get(sonarUserHome)));
7683
var launcher = launcherFactory.createLauncher(properties, rules);
7784
return new ScannerEngineFacade(properties, launcher, logOutput, isSonarCloud);

lib/src/test/java/org/sonarsource/scanner/lib/internal/http/ServerConnectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.nio.file.Files;
2525
import java.nio.file.Path;
26-
import java.nio.file.Paths;
2726
import java.util.HashMap;
2827
import java.util.Map;
2928
import org.junit.jupiter.api.Test;
@@ -87,11 +86,12 @@ void test_downloadFile(@TempDir Path tmpFolder) throws Exception {
8786
}
8887

8988
@Test
90-
void downloadFile_fails_on_url_validation() {
89+
void downloadFile_fails_on_url_validation(@TempDir Path tmpFolder) {
90+
var toFile = tmpFolder.resolve("index.txt");
9191
ServerConnection connection = create();
9292
answer(HELLO_WORLD);
9393

94-
assertThatThrownBy(() -> connection.downloadFile("should_fail", Paths.get("test-path")))
94+
assertThatThrownBy(() -> connection.downloadFile("should_fail", toFile))
9595
.isInstanceOf(IllegalArgumentException.class)
9696
.hasMessage("URL path must start with slash: should_fail");
9797
}

0 commit comments

Comments
 (0)