]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3698 API : ability to execute scripts without enough permissions
authorSimon Brandhof <simon.brandhof@gmail.com>
Wed, 25 Jul 2012 21:18:14 +0000 (23:18 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Wed, 25 Jul 2012 21:18:14 +0000 (23:18 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java

index 257a8209387a9d1668135a45cb9dd46eb900a667..801df4bb9e35f9ed8157bdc03b3a43345b7bb5ab 100644 (file)
@@ -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()]);
index cd47f03703aca6b8be13740699052c0747c35cd6..b57a671ee0bba544315b6abc55ad64bd1b3a6e0b 100644 (file)
@@ -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");
+    }
+  }
 }