]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-1230 Work dir can be absolute or relative
authorFabrice Bellingard <bellingard@gmail.com>
Thu, 26 Jul 2012 15:18:10 +0000 (15:18 +0000)
committerFabrice Bellingard <bellingard@gmail.com>
Thu, 26 Jul 2012 15:18:10 +0000 (15:18 +0000)
src/main/java/org/sonar/runner/Runner.java
src/test/java/org/sonar/runner/RunnerTest.java

index 60b6a454901d6cbe712e18ea8420e5075bf251a3..1a51496aae9c72e3ff5990c5fdaf0db5b320b560 100644 (file)
@@ -109,7 +109,22 @@ public final class Runner {
     if (!projectDir.isDirectory() || !projectDir.exists()) {
       throw new IllegalArgumentException("Project home must be an existing directory: " + path);
     }
-    workDir = new File(projectDir, properties.getProperty(PROPERTY_WORK_DIRECTORY, DEF_VALUE_WORK_DIRECTORY));
+    workDir = initWorkDir();
+  }
+
+  private File initWorkDir() {
+    String customWorkDir = properties.getProperty(PROPERTY_WORK_DIRECTORY);
+    if (customWorkDir == null || customWorkDir.trim().length() == 0) {
+      return new File(projectDir, DEF_VALUE_WORK_DIRECTORY);
+    }
+    return defineCustomizedWorkDir(new File(customWorkDir));
+  }
+
+  private File defineCustomizedWorkDir(File customWorkDir) {
+    if (customWorkDir.isAbsolute()) {
+      return customWorkDir;
+    }
+    return new File(projectDir, customWorkDir.getPath());
   }
 
   public File getProjectDir() {
index c0495fc1a570b3b84bededc74554c08f1c397b4b..877e85d203629add8a26cf456ad6e3c2c34e6e8d 100644 (file)
@@ -153,9 +153,20 @@ public class RunnerTest {
     Runner runner = Runner.create(properties);
     assertThat(runner.getWorkDir()).isEqualTo(new File(".", ".sonar"));
 
+    // empty string
+    properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, "    ");
+    runner = Runner.create(properties);
+    assertThat(runner.getWorkDir()).isEqualTo(new File(".", ".sonar"));
+
+    // real relative path
     properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, "temp-dir");
     runner = Runner.create(properties);
     assertThat(runner.getWorkDir()).isEqualTo(new File(".", "temp-dir"));
+
+    // real asbolute path
+    properties.setProperty(Runner.PROPERTY_WORK_DIRECTORY, new File("target").getAbsolutePath());
+    runner = Runner.create(properties);
+    assertThat(runner.getWorkDir()).isEqualTo(new File("target").getAbsoluteFile());
   }
 
   @Test