From 2feaf823d3f2d611d6309ff3ee8762b10143a86e Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Wed, 5 Sep 2012 16:40:09 +0200 Subject: [PATCH] SONARPLUGINS-1856 Log message if sources directory does not exist => When launching the Sonar Runner, a meaningful error message must be thrown when one source directory doesn't exist --- .../runner/model/SonarProjectBuilder.java | 26 ++++++++++++++----- .../runner/model/SonarProjectBuilderTest.java | 19 ++++++++------ .../sonar-project.properties | 6 +++++ 3 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/simple-project-with-unexisting-source-dir/sonar-project.properties diff --git a/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java b/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java index d1c5fde..3bd4b02 100644 --- a/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java +++ b/src/main/java/org/sonar/runner/model/SonarProjectBuilder.java @@ -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()); } diff --git a/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java b/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java index 931e535..5495a6b 100644 --- a/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java +++ b/src/test/java/org/sonar/runner/model/SonarProjectBuilderTest.java @@ -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 index 0000000..3ce2bb0 --- /dev/null +++ b/src/test/resources/org/sonar/runner/model/SonarProjectBuilderTest/simple-project-with-unexisting-source-dir/sonar-project.properties @@ -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 -- 2.39.5