From: Julien HENRY Date: Fri, 4 Apr 2014 08:03:54 +0000 (+0200) Subject: SONARPLUGINS-3634 Provide a simulation mode allowing to dump input configuration X-Git-Tag: 2.5-rc1~105 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=20f838228f687a6f6cddc79838956601b23a229c;p=sonar-scanner-cli.git SONARPLUGINS-3634 Provide a simulation mode allowing to dump input configuration --- diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java index b7583de..c4013f6 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java @@ -19,10 +19,14 @@ */ package org.sonar.runner.api; +import org.apache.commons.io.IOUtils; import org.sonar.runner.impl.InternalProperties; import javax.annotation.Nullable; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; import java.util.Properties; /** @@ -87,7 +91,26 @@ public abstract class Runner { initDefaultValues(); new SourceEncoding().init(this); new Dirs().init(this); - doExecute(); + String dumpToFile = properties.getProperty(InternalProperties.RUNNER_DUMP_TO_FILE); + if (dumpToFile != null) { + File dumpFile = new File(dumpToFile); + writeProperties(dumpFile); + System.out.println("Simulation mode. Configuration written to " + dumpFile.getAbsolutePath()); + } else { + doExecute(); + } + } + + private void writeProperties(File outputFile) { + OutputStream output = null; + try { + output = new FileOutputStream(outputFile); + properties().store(output, "Generated by sonar-runner"); + } catch (Exception e) { + throw new IllegalStateException("Fail to export sonar-runner properties", e); + } finally { + IOUtils.closeQuietly(output); + } } protected abstract void doExecute(); diff --git a/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java b/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java index d573417..2a4302b 100644 --- a/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java +++ b/sonar-runner-api/src/test/java/org/sonar/runner/api/EmbeddedRunnerTest.java @@ -19,11 +19,16 @@ */ package org.sonar.runner.api; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentMatcher; import org.sonar.runner.impl.BatchLauncher; import org.sonar.runner.impl.InternalProperties; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.util.List; import java.util.Properties; @@ -33,6 +38,10 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; public class EmbeddedRunnerTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void should_create() { assertThat(EmbeddedRunner.create()).isNotNull().isInstanceOf(EmbeddedRunner.class); @@ -60,10 +69,10 @@ public class EmbeddedRunnerTest { assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isNull(); runner = EmbeddedRunner.create() - .unmask("org.slf4j.Logger") - .mask("org.slf4j.") - .mask("ch.qos.logback.") - .unmask(""); + .unmask("org.slf4j.Logger") + .mask("org.slf4j.") + .mask("ch.qos.logback.") + .unmask(""); assertThat(runner.property(InternalProperties.RUNNER_MASK_RULES, null)).isEqualTo("UNMASK|org.slf4j.Logger,MASK|org.slf4j.,MASK|ch.qos.logback.,UNMASK|"); } @@ -116,6 +125,22 @@ public class EmbeddedRunnerTest { })); } + @Test + public void should_launch_in_simulation_mode() throws IOException { + File dump = temp.newFile(); + + BatchLauncher batchLauncher = mock(BatchLauncher.class); + EmbeddedRunner runner = new EmbeddedRunner(batchLauncher); + runner.setProperty("sonar.projectKey", "foo"); + runner.setProperty("sonarRunner.dumpToFile", dump.getAbsolutePath()); + runner.execute(); + + Properties props = new Properties(); + props.load(new FileInputStream(dump)); + + assertThat(props.getProperty("sonar.projectKey")).isEqualTo("foo"); + } + static class FakeExtension { } } diff --git a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/InternalProperties.java b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/InternalProperties.java index d4fb4f5..1a856e9 100644 --- a/sonar-runner-impl/src/main/java/org/sonar/runner/impl/InternalProperties.java +++ b/sonar-runner-impl/src/main/java/org/sonar/runner/impl/InternalProperties.java @@ -23,4 +23,5 @@ public interface InternalProperties { String RUNNER_APP = "sonarRunner.app"; String RUNNER_APP_VERSION = "sonarRunner.appVersion"; String RUNNER_MASK_RULES = "sonarRunner.maskRules"; + String RUNNER_DUMP_TO_FILE = "sonarRunner.dumpToFile"; }