From: Fabrice Bellingard Date: Thu, 19 Jul 2012 14:39:01 +0000 (+0000) Subject: SONARPLUGINS-1230 Configure the location of working directory X-Git-Tag: 2.5-rc1~295 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=18ce02ea44a241694377ebd1dd36350c4220e2b6;p=sonar-scanner-cli.git SONARPLUGINS-1230 Configure the location of working directory --- diff --git a/pom.xml b/pom.xml index 1953a11..91e9404 100644 --- a/pom.xml +++ b/pom.xml @@ -86,6 +86,12 @@ ${sonar.buildVersion} test + + org.easytesting + fest-assert + 1.4 + test + diff --git a/src/main/java/org/sonar/runner/Main.java b/src/main/java/org/sonar/runner/Main.java index 009bf0e..aedfc8a 100644 --- a/src/main/java/org/sonar/runner/Main.java +++ b/src/main/java/org/sonar/runner/Main.java @@ -170,7 +170,7 @@ public final class Main { printUsage(); } else if ("-X".equals(arg) || "--debug".equals(arg)) { - props.setProperty(Runner.VERBOSE, "true"); + props.setProperty(Runner.PROPERTY_VERBOSE, "true"); debugMode = true; } else if ("-D".equals(arg) || "--define".equals(arg)) { diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java index 65fdaf3..b4cb17e 100644 --- a/src/main/java/org/sonar/runner/Runner.java +++ b/src/main/java/org/sonar/runner/Runner.java @@ -42,21 +42,27 @@ public final class Runner { */ @Deprecated public static final String DEBUG_MODE = "runner.debug"; - + /** * @since 1.2 */ - public static final String VERBOSE = "sonar.verbose"; + public static final String PROPERTY_VERBOSE = "sonar.verbose"; + + /** + * @since 1.4 + */ + public static final String PROPERTY_WORK_DIRECTORY = "sonar.working.directory"; + public static final String DEF_VALUE_WORK_DIRECTORY = ".sonar"; /** * Array of prefixes of versions of Sonar without support of this runner. */ - private static final String[] UNSUPPORTED_VERSIONS = { "1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5" }; + private static final String[] UNSUPPORTED_VERSIONS = {"1", "2.0", "2.1", "2.2", "2.3", "2.4", "2.5"}; /** * Array of all mandatory properties required to execute runner. */ - private static final String[] MANDATORY_PROPERTIES = { "sonar.projectKey", "sonar.projectName", "sonar.projectVersion", "sources" }; + private static final String[] MANDATORY_PROPERTIES = {"sonar.projectKey", "sonar.projectName", "sonar.projectVersion", "sources"}; private File projectDir; private File workDir; @@ -103,7 +109,7 @@ public final class Runner { if (!projectDir.isDirectory() || !projectDir.exists()) { throw new IllegalArgumentException("Project home must be an existing directory: " + path); } - workDir = new File(projectDir, ".sonar"); + workDir = new File(projectDir, properties.getProperty(PROPERTY_WORK_DIRECTORY, DEF_VALUE_WORK_DIRECTORY)); } public File getProjectDir() { @@ -125,7 +131,7 @@ public final class Runner { } public boolean isDebug() { - return Boolean.parseBoolean(properties.getProperty(VERBOSE, properties.getProperty(DEBUG_MODE, "false"))); + return Boolean.parseBoolean(properties.getProperty(PROPERTY_VERBOSE, properties.getProperty(DEBUG_MODE, "false"))); } public String getRunnerVersion() { @@ -146,14 +152,14 @@ public final class Runner { String serverVersion = bootstrapper.getServerVersion(); if (isUnsupportedVersion(serverVersion)) { throw new BootstrapException("Sonar " + serverVersion - + " does not support Standalone Runner. Please upgrade Sonar to version 2.6 or more."); + + " does not support Standalone Runner. Please upgrade Sonar to version 2.6 or more."); } } private BootstrapClassLoader createClassLoader(Bootstrapper bootstrapper) { URL url = Main.class.getProtectionDomain().getCodeSource().getLocation(); return bootstrapper.createClassLoader( - new URL[]{url}, // Add JAR with Sonar Runner - it's a Jar which contains this class + new URL[] {url}, // Add JAR with Sonar Runner - it's a Jar which contains this class getClass().getClassLoader()); } diff --git a/src/test/java/org/sonar/runner/MainTest.java b/src/test/java/org/sonar/runner/MainTest.java index ab324cb..3696091 100644 --- a/src/test/java/org/sonar/runner/MainTest.java +++ b/src/test/java/org/sonar/runner/MainTest.java @@ -49,13 +49,13 @@ public class MainTest { @Test public void shouldEnableDebugMode() { Properties props = Main.parseArguments(new String[] { "-X" }); - assertThat(props.getProperty(Runner.VERBOSE), is("true")); + assertThat(props.getProperty(Runner.PROPERTY_VERBOSE), is("true")); } @Test public void shouldDisableDebugModeByDefault() { Properties props = Main.parseArguments(new String[] {}); - assertThat(props.getProperty(Runner.VERBOSE), nullValue()); + assertThat(props.getProperty(Runner.PROPERTY_VERBOSE), nullValue()); } @Test diff --git a/src/test/java/org/sonar/runner/RunnerTest.java b/src/test/java/org/sonar/runner/RunnerTest.java index 42c3dc9..d7fde1c 100644 --- a/src/test/java/org/sonar/runner/RunnerTest.java +++ b/src/test/java/org/sonar/runner/RunnerTest.java @@ -20,6 +20,12 @@ package org.sonar.runner; +import org.junit.Test; + +import java.io.File; +import java.util.Properties; + +import static org.fest.assertions.Assertions.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.greaterThanOrEqualTo; import static org.hamcrest.Matchers.is; @@ -27,11 +33,6 @@ import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; -import java.io.File; -import java.util.Properties; - -import org.junit.Test; - public class RunnerTest { @Test @@ -95,7 +96,7 @@ public class RunnerTest { Properties properties = new Properties(); Runner runner = Runner.create(properties); assertThat("Default value", runner.isDebug(), is(false)); - properties.setProperty(Runner.VERBOSE, "true"); + properties.setProperty(Runner.PROPERTY_VERBOSE, "true"); assertThat(runner.isDebug(), is(true)); } @@ -126,4 +127,15 @@ public class RunnerTest { assertThat(runner.getProjectDir().exists(), is(true)); } + @Test + public void shouldSpecifyWorkingDirectory() { + Properties properties = new Properties(); + Runner runner = Runner.create(properties); + assertThat(runner.getWorkDir()).isEqualTo(new File(".", ".sonar")); + + properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, "temp-dir"); + runner = Runner.create(properties); + assertThat(runner.getWorkDir()).isEqualTo(new File(".", "temp-dir")); + } + }