From: Simon Brandhof Date: Wed, 25 Jul 2012 21:18:14 +0000 (+0200) Subject: SONAR-3698 API : ability to execute scripts without enough permissions X-Git-Tag: 3.3~360 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=054f94941fb53ef4b976131d00b94a9065204640;p=sonarqube.git SONAR-3698 API : ability to execute scripts without enough permissions --- 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 arguments = Lists.newArrayList(); private File directory; private Map 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 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"); + } + } }