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;
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;
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()]);
*/
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;
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"));
}
@Test
- public void shouldSetWorkingDirectory() throws Exception {
+ public void working_directory() {
Command command = Command.create("java");
assertThat(command.getDirectory()).isNull();
}
@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");
+ }
+ }
}