Browse Source

SONAR-21972 Fix tests to not depend on the operating system's locale

tags/10.5.0.89998
Julien HENRY 1 month ago
parent
commit
369eb731d4

+ 4
- 0
build.gradle View File

jvmArgs '-Dfile.encoding=UTF8' jvmArgs '-Dfile.encoding=UTF8'
maxHeapSize = '1500m' maxHeapSize = '1500m'
systemProperty 'java.awt.headless', true 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 { testLogging {
events "skipped", "failed" // verbose log for failed and skipped tests (by default the name of the tests are not logged) 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) exceptionFormat 'full' // log the full stack trace (default is the 1st line of the stack trace)

+ 11
- 2
sonar-scanner-engine/src/main/java/org/sonar/scm/git/ProcessWrapperFactory.java View File

import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
import java.util.function.Consumer; import java.util.function.Consumer;
import javax.annotation.Nullable; import javax.annotation.Nullable;
} }


public ProcessWrapper create(@Nullable Path baseDir, Consumer<String> stdOutLineConsumer, String... command) { 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 { static class ProcessWrapper {
private final Path baseDir; private final Path baseDir;
private final Consumer<String> stdOutLineConsumer; private final Consumer<String> stdOutLineConsumer;
private final String[] command; 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.baseDir = baseDir;
this.stdOutLineConsumer = stdOutLineConsumer; this.stdOutLineConsumer = stdOutLineConsumer;
this.envVariables.putAll(envVariables);
this.command = command; this.command = command;
} }


ProcessBuilder pb = new ProcessBuilder() ProcessBuilder pb = new ProcessBuilder()
.command(command) .command(command)
.directory(baseDir != null ? baseDir.toFile() : null); .directory(baseDir != null ? baseDir.toFile() : null);
envVariables.forEach(pb.environment()::put);


Process p = pb.start(); Process p = pb.start();
try { try {

+ 3
- 2
sonar-scanner-engine/src/test/java/org/sonar/scm/git/ProcessWrapperFactoryTest.java View File

package org.sonar.scm.git; package org.sonar.scm.git;


import java.io.IOException; import java.io.IOException;
import java.util.Map;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;
public void should_log_error_output_in_debug_mode() throws IOException { public void should_log_error_output_in_debug_mode() throws IOException {
logTester.setLevel(Level.DEBUG); logTester.setLevel(Level.DEBUG);
var root = temp.newFolder().toPath(); 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); .isInstanceOf(IllegalStateException.class);


assertThat(logTester.logs(Level.DEBUG).get(0)).startsWith("fatal:"); assertThat(logTester.logs(Level.DEBUG).get(0)).startsWith("fatal:");

Loading…
Cancel
Save