Skip to content

Commit

Permalink
SCANCLI-130 Set sonar.scanner.bootstrapStartTime
Browse files Browse the repository at this point in the history
  • Loading branch information
claire-villard-sonarsource committed Apr 29, 2024
1 parent 98f8be6 commit 773c70f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/sonarsource/scanner/cli/Conf.java
Expand Up @@ -43,15 +43,18 @@ class Conf {
private static final String PROPERTY_PROJECT_CONFIG_FILE = "sonar.projectConfigFile";
private static final String SONAR_PROJECT_PROPERTIES_FILENAME = "sonar-project.properties";
static final String PROPERTY_SONAR_HOST_URL = "sonar.host.url";
private static final String BOOTSTRAP_START_TIME = "sonar.scanner.bootstrapStartTime";

private final Cli cli;
private final Logs logger;
private final Map<String, String> env;
private final long startTimeMs;

Conf(Cli cli, Logs logger, Map<String, String> env) {
this.cli = cli;
this.logger = logger;
this.env = env;
this.startTimeMs = System.currentTimeMillis();
}

Properties properties() {
Expand All @@ -66,6 +69,8 @@ Properties properties() {
// root project base directory must be present and be absolute
result.setProperty(PROPERTY_PROJECT_BASEDIR, getRootProjectBaseDir(result).toString());
result.remove(PROJECT_HOME);

result.setProperty(BOOTSTRAP_START_TIME, String.valueOf(startTimeMs));
return result;
}

Expand Down
51 changes: 31 additions & 20 deletions src/test/java/org/sonarsource/scanner/cli/ConfTest.java
Expand Up @@ -31,11 +31,12 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonarsource.scanner.api.internal.shaded.minimaljson.Json;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assume.assumeTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -45,9 +46,6 @@ public class ConfTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();

@Rule
public ExpectedException exception = ExpectedException.none();

private final Map<String, String> env = new HashMap<>();
private final Properties args = new Properties();
private final Logs logs = new Logs(System.out, System.err);
Expand All @@ -66,7 +64,7 @@ public void should_load_global_settings_by_home() throws Exception {
args.setProperty("scanner.home", home.toAbsolutePath().toString());

Properties properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("value");
assertThat(properties).containsEntry("sonar.prop", "value");
}

@Test
Expand All @@ -76,6 +74,17 @@ public void should_not_fail_if_no_home() {
assertThat(conf.properties().getProperty("sonar.projectBaseDir")).isEqualTo(Paths.get("").toAbsolutePath().toString());
}

@Test
public void should_set_bootstrap_time_only_once() {
Properties properties = conf.properties();

assertThat(properties).containsKey("sonar.scanner.bootstrapStartTime");
String value = properties.getProperty("sonar.scanner.bootstrapStartTime");

assertThat(conf.properties())
.containsEntry("sonar.scanner.bootstrapStartTime", value);
}

@Test
public void base_dir_can_be_relative() throws URISyntaxException {
Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
Expand All @@ -94,7 +103,7 @@ public void should_load_conf_by_direct_path() throws Exception {
Path settings = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
args.setProperty("scanner.settings", settings.toAbsolutePath().toString());

assertThat(conf.properties().get("sonar.prop")).isEqualTo("otherValue");
assertThat(conf.properties()).containsEntry("sonar.prop", "otherValue");
}

@Test
Expand Down Expand Up @@ -138,9 +147,10 @@ public void shouldLoadEnvironmentProperties() {
@Test
public void shouldFailWithInvalidEnvironmentProperties() {
env.put("SONARQUBE_SCANNER_PARAMS", "{sonar.key1: \"v1\", \"sonar.key2\" : \"v2\"}");
exception.expect(IllegalStateException.class);
exception.expectMessage("JSON");
conf.properties();

assertThatIllegalStateException()
.isThrownBy(conf::properties)
.withMessage("Failed to parse JSON in SONARQUBE_SCANNER_PARAMS environment variable");
}

@Test
Expand Down Expand Up @@ -236,19 +246,19 @@ public void failModuleBaseDirDoesNotExist() {
args.setProperty("sonar.modules", "module1");
args.setProperty("module1.sonar.projectBaseDir", "invalid");

exception.expect(IllegalStateException.class);
exception.expectMessage("The base directory of the module 'module1' does not exist");
conf.properties();
assertThatIllegalStateException()
.isThrownBy(conf::properties)
.withMessageStartingWith("The base directory of the module 'module1' does not exist");
}

@Test
public void failModulePropertyFileDoesNotExist() {
args.setProperty("sonar.modules", "module1");
args.setProperty("module1.sonar.projectConfigFile", "invalid");

exception.expect(IllegalStateException.class);
exception.expectMessage("The properties file of the module 'module1' does not exist");
conf.properties();
assertThatIllegalStateException()
.isThrownBy(conf::properties)
.withMessageStartingWith("The properties file of the module 'module1' does not exist");
}

@Test
Expand All @@ -270,7 +280,8 @@ public void ignoreEmptyModule() throws Exception {
args.setProperty("project.home", temp.newFolder().getCanonicalPath());
args.setProperty("sonar.projectBaseDir", projectHome.toAbsolutePath().toString());

conf.properties();
assertThatCode(conf::properties)
.doesNotThrowAnyException();
}

@Test
Expand Down Expand Up @@ -310,12 +321,12 @@ public void should_load_project_settings_using_property() throws Exception {
args.setProperty("project.home", home.toAbsolutePath().toString());

Properties properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("default");
assertThat(properties).containsEntry("sonar.prop", "default");

args.setProperty("project.settings", home.resolve("conf/sq-project.properties").toAbsolutePath().toString());

properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("expected");
assertThat(properties).containsEntry("sonar.prop", "expected");
}

// SQSCANNER-61
Expand All @@ -325,7 +336,7 @@ public void should_load_project_settings_using_env() throws Exception {
args.setProperty("project.home", home.toAbsolutePath().toString());

Properties properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("default");
assertThat(properties).containsEntry("sonar.prop", "default");

String jsonString = Json.object()
.add("project.settings", home.resolve("conf/sq-project.properties").toAbsolutePath().toString())
Expand All @@ -334,7 +345,7 @@ public void should_load_project_settings_using_env() throws Exception {
env.put("SONARQUBE_SCANNER_PARAMS", jsonString);

properties = conf.properties();
assertThat(properties.get("sonar.prop")).isEqualTo("expected");
assertThat(properties).containsEntry("sonar.prop", "expected");
}

}
19 changes: 16 additions & 3 deletions src/test/java/org/sonarsource/scanner/cli/MainTest.java
Expand Up @@ -299,24 +299,37 @@ public void should_configure_logging_trace() {
assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
}

@Test
public void should_set_bootstrap_start_time_in_millis() {
Properties analysisProps = execute("sonar.scanner.bootstrapStartTime", "1714137496104");
assertThat(analysisProps.getProperty("sonar.scanner.bootstrapStartTime")).isEqualTo("1714137496104");
}

@Test
public void should_configure_logging_debug() {
Properties analysisProps = testLogging("sonar.log.level", "DEBUG");
assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG");
}

private Properties testLogging(String propKey, String propValue) {
Properties actualProps = execute(propKey, propValue);

// Logger used for callback should have debug enabled
verify(logs).setDebugEnabled(true);

return actualProps;
}

private Properties execute(String propKey, String propValue) {
Properties p = new Properties();
p.put(propKey, propValue);

when(conf.properties()).thenReturn(p);
when(cli.getInvokedFrom()).thenReturn("");

Main main = new Main(exit, cli, conf, scannerFactory, logs);
main.execute();

// Logger used for callback should have debug enabled
verify(logs).setDebugEnabled(true);

ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
verify(scanner).execute((Map) propertiesCapture.capture());

Expand Down

0 comments on commit 773c70f

Please sign in to comment.