summaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-07-25 23:18:14 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-07-25 23:18:14 +0200
commit054f94941fb53ef4b976131d00b94a9065204640 (patch)
tree5fc586ef3a8e977c89d7b0c52ee5c10b5ebb581a /sonar-plugin-api
parenta2ec541cb1c6bc4f71b3b5aa96df955608aacea1 (diff)
downloadsonarqube-054f94941fb53ef4b976131d00b94a9065204640.tar.gz
sonarqube-054f94941fb53ef4b976131d00b94a9065204640.zip
SONAR-3698 API : ability to execute scripts without enough permissions
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java26
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java26
2 files changed, 46 insertions, 6 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
index 257a8209387..801df4bb9e3 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
@@ -23,6 +23,7 @@ import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.SystemUtils;
import java.io.File;
import java.util.Arrays;
@@ -39,6 +40,7 @@ public class Command {
private List<String> arguments = Lists.newArrayList();
private File directory;
private Map<String, String> env = Maps.newHashMap(System.getenv());
+ private boolean newShell = false;
private Command(String executable) {
this.executable = executable;
@@ -99,8 +101,32 @@ public class Command {
return Collections.unmodifiableMap(env);
}
+ /**
+ * @since 3.3
+ */
+ public boolean isNewShell() {
+ return newShell;
+ }
+
+ /**
+ * Set to true if the script to execute has not enough rights (+x on unix).
+ * @since 3.3
+ */
+ public Command setNewShell(boolean b) {
+ this.newShell = b;
+ return this;
+ }
+
String[] toStrings() {
List<String> command = Lists.newArrayList();
+ if (newShell) {
+ if (SystemUtils.IS_OS_WINDOWS) {
+ command.add("cmd");
+ command.add("/C");
+ } else {
+ command.add("sh");
+ }
+ }
command.add(executable);
command.addAll(arguments);
return command.toArray(new String[command.size()]);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
index cd47f03703a..b57a671ee0b 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java
@@ -19,6 +19,7 @@
*/
package org.sonar.api.utils.command;
+import org.apache.commons.lang.SystemUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -35,19 +36,19 @@ public class CommandTest {
public ExpectedException thrown = ExpectedException.none();
@Test
- public void shouldFailWhenBlankExecutable() throws Exception {
+ public void fail_if_blank_executable() {
thrown.expect(IllegalArgumentException.class);
Command.create(" ");
}
@Test
- public void shouldFailWhenNullExecutable() throws Exception {
+ public void fail_if_null_executable() {
thrown.expect(IllegalArgumentException.class);
Command.create(null);
}
@Test
- public void shouldCreateCommand() throws Exception {
+ public void create_command() {
Command command = Command.create("java");
command.addArgument("-Xmx512m");
command.addArguments(Arrays.asList("-a", "-b"));
@@ -65,7 +66,7 @@ public class CommandTest {
}
@Test
- public void shouldSetWorkingDirectory() throws Exception {
+ public void working_directory() {
Command command = Command.create("java");
assertThat(command.getDirectory()).isNull();
@@ -75,15 +76,28 @@ public class CommandTest {
}
@Test
- public void initialize_with_current_env() throws Exception {
+ public void initialize_with_current_env() {
Command command = Command.create("java");
assertThat(command.getEnvironmentVariables()).isNotEmpty();
}
@Test
- public void override_env_variables() throws Exception {
+ public void override_env_variables() {
Command command = Command.create("java");
command.setEnvironmentVariable("JAVA_HOME", "/path/to/java");
assertThat(command.getEnvironmentVariables().get("JAVA_HOME")).isEqualTo("/path/to/java");
}
+
+ @Test
+ public void use_new_shell() {
+ if (SystemUtils.IS_OS_WINDOWS) {
+ Command command = Command.create("foo.bat");
+ command.setNewShell(true);
+ assertThat(command.toCommandLine()).isEqualTo("cmd /C foo.bat");
+ } else {
+ Command command = Command.create("foo.sh");
+ command.setNewShell(true);
+ assertThat(command.toCommandLine()).isEqualTo("sh foo.sh");
+ }
+ }
}