aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-07-05 10:01:55 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2011-07-05 10:01:55 +0200
commit83f27436880ed453c4c367e8c39625e09982a624 (patch)
tree45ccf1ff535b083181f91537146be056c0e3ca3a /sonar-plugin-api/src
parentd7c0d144d27ac2b7cefb60b0de0c6e82eff28441 (diff)
downloadsonarqube-83f27436880ed453c4c367e8c39625e09982a624.tar.gz
sonarqube-83f27436880ed453c4c367e8c39625e09982a624.zip
SONAR-2574 API: configure the working directory used by org.sonar.api.utils.command.Command
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/Command.java11
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java7
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java28
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandTest.java14
-rwxr-xr-xsonar-plugin-api/src/test/scripts/echo.bat1
-rwxr-xr-xsonar-plugin-api/src/test/scripts/echo.sh3
6 files changed, 55 insertions, 9 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 74d6a75de9e..9ae6e59e7a9 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 org.apache.commons.lang.StringUtils;
+import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -34,6 +35,7 @@ public final class Command {
private String executable;
private List<String> arguments = Lists.newArrayList();
+ private File directory;
private Command(String executable) {
this.executable = executable;
@@ -62,6 +64,15 @@ public final class Command {
return this;
}
+ public File getDirectory() {
+ return directory;
+ }
+
+ public Command setDirectory(File d) {
+ this.directory = d;
+ return this;
+ }
+
String[] toStrings() {
List<String> command = Lists.newArrayList();
command.add(executable);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
index 168c42acee3..4b8e4c93e4c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/command/CommandExecutor.java
@@ -54,6 +54,9 @@ public final class CommandExecutor {
try {
LoggerFactory.getLogger(getClass()).debug("Executing command: " + command);
ProcessBuilder builder = new ProcessBuilder(command.toStrings());
+ if (command.getDirectory() != null) {
+ builder.directory(command.getDirectory());
+ }
process = builder.start();
// consume and display the error and output streams
@@ -75,9 +78,7 @@ public final class CommandExecutor {
return ft.get(timeoutMilliseconds, TimeUnit.MILLISECONDS);
} catch (TimeoutException te) {
- if (process != null) {
- process.destroy();
- }
+ process.destroy();
throw new CommandException(command, "Timeout exceeded: " + timeoutMilliseconds + " ms", te);
} catch (Exception e) {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java
index d346550ac83..b0276fecdf6 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/command/CommandExecutorTest.java
@@ -19,35 +19,51 @@
*/
package org.sonar.api.utils.command;
+import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import java.io.File;
+import java.io.IOException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.number.OrderingComparisons.greaterThanOrEqualTo;
-import static org.hamcrest.number.OrderingComparisons.lessThan;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
+import static org.junit.matchers.JUnitMatchers.containsString;
public class CommandExecutorTest {
@Test
- public void shouldEchoArguments() {
+ public void shouldEchoArguments() throws IOException {
String executable = getScript("echo");
int exitCode = CommandExecutor.create().execute(Command.create(executable), 1000L);
assertThat(exitCode, is(0));
}
@Test
- public void shouldStopWithTimeout() {
+ public void shouldConfigureWorkingDirectory() throws IOException {
+ String executable = getScript("echo");
+ File dir = new File("target/tmp/CommandExecutorTest/shouldConfigureWorkingDirectory");
+ FileUtils.forceMkdir(dir);
+ FileUtils.cleanDirectory(dir);
+
+ int exitCode = CommandExecutor.create().execute(Command.create(executable).setDirectory(dir), 1000L);
+ assertThat(exitCode, is(0));
+
+ File log = new File(dir, "echo.log");
+ assertThat(FileUtils.readFileToString(log), containsString(dir.getCanonicalPath()));
+ }
+
+ @Test
+ public void shouldStopWithTimeout() throws IOException {
String executable = getScript("forever");
long start = System.currentTimeMillis();
try {
CommandExecutor.create().execute(Command.create(executable), 300L);
fail();
} catch (CommandException e) {
- long duration = System.currentTimeMillis()-start;
+ long duration = System.currentTimeMillis() - start;
// should test >= 300 but it strangly fails during build on windows.
// The timeout is raised after 297ms (??)
assertThat(e.getMessage(), duration, greaterThanOrEqualTo(290L));
@@ -59,13 +75,13 @@ public class CommandExecutorTest {
CommandExecutor.create().execute(Command.create("notfound"), 1000L);
}
- private String getScript(String name) {
+ private String getScript(String name) throws IOException {
String filename;
if (SystemUtils.IS_OS_WINDOWS) {
filename = name + ".bat";
} else {
filename = name + ".sh";
}
- return new File("src/test/scripts/" + filename).getPath();
+ return new File("src/test/scripts/" + filename).getCanonicalPath();
}
}
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 b32d8946440..71d9b74cd6d 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,9 +19,13 @@
*/
package org.sonar.api.utils.command;
+import org.hamcrest.CoreMatchers;
import org.junit.Test;
+import java.io.File;
+
import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
public class CommandTest {
@@ -45,4 +49,14 @@ public class CommandTest {
assertThat(command.getArguments().size(), is(2));
assertThat(command.toCommandLine(), is("java -Xmx512m -Dfoo=bar"));
}
+
+ @Test
+ public void shouldSetWorkingDirectory() throws Exception {
+ Command command = Command.create("java");
+ assertThat(command.getDirectory(), nullValue());
+
+ File working = new File("working");
+ command = Command.create("java").setDirectory(working);
+ assertThat(command.getDirectory(), is(working));
+ }
}
diff --git a/sonar-plugin-api/src/test/scripts/echo.bat b/sonar-plugin-api/src/test/scripts/echo.bat
index 4c84e2dc8a9..0f2436ceb71 100755
--- a/sonar-plugin-api/src/test/scripts/echo.bat
+++ b/sonar-plugin-api/src/test/scripts/echo.bat
@@ -1,2 +1,3 @@
@ECHO OFF
+@ECHO %CD% > echo.log
@ECHO "Parameter: " + %1
diff --git a/sonar-plugin-api/src/test/scripts/echo.sh b/sonar-plugin-api/src/test/scripts/echo.sh
index eebd2bd2fe6..3e45899f469 100755
--- a/sonar-plugin-api/src/test/scripts/echo.sh
+++ b/sonar-plugin-api/src/test/scripts/echo.sh
@@ -1,2 +1,5 @@
#!/bin/sh
+
+WORKING_DIR=`pwd`
+echo $WORKING_DIR > echo.log
echo "Parameter: " + $1 \ No newline at end of file