]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21972 Fix tests to not depend on the operating system's locale
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 2 Apr 2024 12:47:45 +0000 (14:47 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 3 Apr 2024 20:02:40 +0000 (20:02 +0000)
build.gradle
sonar-scanner-engine/src/main/java/org/sonar/scm/git/ProcessWrapperFactory.java
sonar-scanner-engine/src/test/java/org/sonar/scm/git/ProcessWrapperFactoryTest.java

index 55bf6c970cfa661b66d85a6aaf19f264cd329cb9..6f85b3db20eb7e728814bf6afa731c78fde9318a 100644 (file)
@@ -566,6 +566,10 @@ subprojects {
     jvmArgs '-Dfile.encoding=UTF8'
     maxHeapSize = '1500m'
     systemProperty 'java.awt.headless', true
+    // Some tests are asserting on localized messages or dates
+    systemProperty 'user.language', 'en'
+    systemProperty 'user.country', 'US'
+    environment 'LANGUAGE', 'en_US'
     testLogging {
       events "skipped", "failed" // verbose log for failed and skipped tests (by default the name of the tests are not logged)
       exceptionFormat 'full' // log the full stack trace (default is the 1st line of the stack trace)
index dfcaf24af936254b92ab8ae96b2a3dcc74f578d4..d3bf4575ccf53add913b8e45ec464dceeeb1cfd7 100644 (file)
@@ -23,6 +23,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.nio.file.Path;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Scanner;
 import java.util.function.Consumer;
 import javax.annotation.Nullable;
@@ -41,7 +43,11 @@ public class ProcessWrapperFactory {
   }
 
   public ProcessWrapper create(@Nullable Path baseDir, Consumer<String> stdOutLineConsumer, String... command) {
-    return new ProcessWrapper(baseDir, stdOutLineConsumer, command);
+    return new ProcessWrapper(baseDir, stdOutLineConsumer, Map.of(), command);
+  }
+
+  public ProcessWrapper create(@Nullable Path baseDir, Consumer<String> stdOutLineConsumer, Map<String, String> envVariables, String... command) {
+    return new ProcessWrapper(baseDir, stdOutLineConsumer, envVariables, command);
   }
 
   static class ProcessWrapper {
@@ -49,10 +55,12 @@ public class ProcessWrapperFactory {
     private final Path baseDir;
     private final Consumer<String> stdOutLineConsumer;
     private final String[] command;
+    private final Map<String, String> envVariables = new HashMap<>();
 
-    ProcessWrapper(@Nullable Path baseDir, Consumer<String> stdOutLineConsumer, String... command) {
+    ProcessWrapper(@Nullable Path baseDir, Consumer<String> stdOutLineConsumer, Map<String, String> envVariables, String... command) {
       this.baseDir = baseDir;
       this.stdOutLineConsumer = stdOutLineConsumer;
+      this.envVariables.putAll(envVariables);
       this.command = command;
     }
 
@@ -60,6 +68,7 @@ public class ProcessWrapperFactory {
       ProcessBuilder pb = new ProcessBuilder()
         .command(command)
         .directory(baseDir != null ? baseDir.toFile() : null);
+      envVariables.forEach(pb.environment()::put);
 
       Process p = pb.start();
       try {
index 1eaa3c5cff629717b7ea1446dda2092878a293b7..ed7b600cd25d98f0ea0cef1216452059f0adf4e2 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.scm.git;
 
 import java.io.IOException;
+import java.util.Map;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
@@ -41,8 +42,8 @@ public class ProcessWrapperFactoryTest {
   public void should_log_error_output_in_debug_mode() throws IOException {
     logTester.setLevel(Level.DEBUG);
     var root = temp.newFolder().toPath();
-    var processWrapper = underTest.create(root, v -> {}, "git", "blame");
-    assertThatThrownBy(() -> processWrapper.execute())
+    var processWrapper = underTest.create(root, v -> {}, Map.of("LANG", "en_US"), "git", "blame");
+    assertThatThrownBy(processWrapper::execute)
       .isInstanceOf(IllegalStateException.class);
 
     assertThat(logTester.logs(Level.DEBUG).get(0)).startsWith("fatal:");