]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
SONARPLUGINS-1856 Log message if sources directory does not exist
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Wed, 5 Sep 2012 14:40:09 +0000 (16:40 +0200)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Wed, 5 Sep 2012 14:40:09 +0000 (16:40 +0200)
=> When launching the Sonar Runner, a meaningful error message must
   be thrown when one source directory doesn't exist

src/main/java/org/sonar/runner/model/SonarProjectBuilder.java
src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java
src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/simple-project-with-unexisting-source-dir/sonar-project.properties [new file with mode: 0644]

index d1c5fde77a83b8695ed4f46b0266e17d6c8aa232..3bd4b020b1f805012fc213205bf541e5b134a06f 100644 (file)
@@ -111,7 +111,7 @@ public final class SonarProjectBuilder {
     mergeParentProperties(childProps, rootProject.getProperties());
     File baseDir = null;
     if (childProps.containsKey(PROPERTY_MODULE_PATH)) {
-      baseDir = getFileFromProperty(childProps, PROPERTY_MODULE_PATH, rootProject.getBaseDir());
+      baseDir = getFileFromPath(childProps.getProperty(PROPERTY_MODULE_PATH), rootProject.getBaseDir());
     } else {
       baseDir = new File(rootProject.getBaseDir(), moduleId);
     }
@@ -123,7 +123,7 @@ public final class SonarProjectBuilder {
   }
 
   private ProjectDefinition loadChildProjectFromPropertyFile(ProjectDefinition rootProject, Properties moduleProps, String moduleId) {
-    File propertyFile = getFileFromProperty(moduleProps, PROPERTY_MODULE_FILE, rootProject.getBaseDir());
+    File propertyFile = getFileFromPath(moduleProps.getProperty(PROPERTY_MODULE_FILE), rootProject.getBaseDir());
     if (!propertyFile.isFile()) {
       throw new RunnerException("The properties file of the module '" + moduleId + "' does not exist: " + propertyFile.getAbsolutePath());
     }
@@ -189,7 +189,9 @@ public final class SonarProjectBuilder {
         .setWorkDir(initWorkDir(baseDir));
     if (!properties.containsKey(PROPERTY_SONAR_MODULES)) {
       // this is not a "aggregator" project, so let's specify its sources, tests, ...
-      definition.addSourceDirs(getListFromProperty(properties, "sources"));
+      String[] sourceDirs = getListFromProperty(properties, "sources");
+      checkExistenceOfDirectories(definition.getKey(), baseDir, sourceDirs);
+      definition.addSourceDirs(sourceDirs);
       definition.addTestDirs(getListFromProperty(properties, "tests"));
       for (String dir : getListFromProperty(properties, "binaries")) {
         definition.addBinaryDir(dir);
@@ -203,6 +205,18 @@ public final class SonarProjectBuilder {
     return definition;
   }
 
+  @VisibleForTesting
+  protected void checkExistenceOfDirectories(String projectKey, File baseDir, String[] sourceDirs) {
+    for (String path : sourceDirs) {
+      File sourceFolder = getFileFromPath(path, baseDir);
+      if (!sourceFolder.isDirectory()) {
+        throw new RunnerException("The source folder '" + path + "' does not exist for '" + projectKey +
+          "' project/module (base directory = " + baseDir.getAbsolutePath() + ")");
+      }
+    }
+
+  }
+
   @VisibleForTesting
   protected File initWorkDir(File baseDir) {
     String workDir = properties.getProperty(PROPERTY_WORK_DIRECTORY);
@@ -261,11 +275,11 @@ public final class SonarProjectBuilder {
   }
 
   /**
-   * Returns the file denoted by the given property, may it be relative to "baseDir" or absolute.
+   * Returns the file denoted by the given path, may this path be relative to "baseDir" or absolute.
    */
   @VisibleForTesting
-  protected static File getFileFromProperty(Properties properties, String key, File baseDir) {
-    File propertyFile = new File(properties.getProperty(key).trim());
+  protected static File getFileFromPath(String path, File baseDir) {
+    File propertyFile = new File(path.trim());
     if (!propertyFile.isAbsolute()) {
       propertyFile = new File(baseDir, propertyFile.getPath());
     }
index 931e53518f32ad4f9eeab0c07aed4219fae39c84..5495a6b01f54cff79e4cfc5f6c0fac4ef4a69512 100644 (file)
@@ -56,6 +56,15 @@ public class SonarProjectBuilderTest {
         TestUtils.getResource(this.getClass(), "simple-project/libs/lib2.txt").getAbsolutePath());
   }
 
+  @Test
+  public void shouldFailIfUnexistingSourceDirectory() throws IOException {
+    thrown.expect(RunnerException.class);
+    thrown.expectMessage("The source folder 'unexisting-source-dir' does not exist for 'com.foo.project' project/module (base directory = "
+      + TestUtils.getResource(this.getClass(), "simple-project-with-unexisting-source-dir") + ")");
+
+    loadProjectDefinition("simple-project-with-unexisting-source-dir");
+  }
+
   @Test
   public void shouldDefineMultiModuleProject() throws IOException {
     ProjectDefinition rootProject = loadProjectDefinition("multi-module");
@@ -220,10 +229,7 @@ public class SonarProjectBuilderTest {
 
   @Test
   public void shouldGetRelativeFile() {
-    Properties props = new Properties();
-    props.put("path", "shouldGetFile/foo.properties");
-
-    assertThat(SonarProjectBuilder.getFileFromProperty(props, "path", TestUtils.getResource(this.getClass(), "/")))
+    assertThat(SonarProjectBuilder.getFileFromPath("shouldGetFile/foo.properties", TestUtils.getResource(this.getClass(), "/")))
         .isEqualTo(TestUtils.getResource("org/sonar/runner/model/SonarProjectBuilderTest/shouldGetFile/foo.properties"));
   }
 
@@ -231,10 +237,7 @@ public class SonarProjectBuilderTest {
   public void shouldGetAbsoluteFile() {
     File file = TestUtils.getResource("org/sonar/runner/model/SonarProjectBuilderTest/shouldGetFile/foo.properties");
 
-    Properties props = new Properties();
-    props.put("path", file.getAbsolutePath());
-
-    assertThat(SonarProjectBuilder.getFileFromProperty(props, "path", TestUtils.getResource(this.getClass(), "/")))
+    assertThat(SonarProjectBuilder.getFileFromPath(file.getAbsolutePath(), TestUtils.getResource(this.getClass(), "/")))
         .isEqualTo(file);
   }
 
diff --git a/src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/simple-project-with-unexisting-source-dir/sonar-project.properties b/src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/simple-project-with-unexisting-source-dir/sonar-project.properties
new file mode 100644 (file)
index 0000000..3ce2bb0
--- /dev/null
@@ -0,0 +1,6 @@
+sonar.projectKey=com.foo.project
+sonar.projectName=Foo Project
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.projectDescription=Description of Foo Project
+
+sources=unexisting-source-dir