]> source.dussan.org Git - sonar-scanner-cli.git/commitdiff
Do not log encoding when non-project task
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 5 Apr 2013 21:30:42 +0000 (23:30 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 5 Apr 2013 21:30:42 +0000 (23:30 +0200)
sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java
sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java
sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java
sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java
sonar-runner-api/src/main/java/org/sonar/runner/api/Utils.java
sonar-runner-api/src/test/java/org/sonar/runner/api/SourceEncodingTest.java
sonar-runner-api/src/test/java/org/sonar/runner/api/UtilsTest.java
sonar-runner-dist/src/main/java/org/sonar/runner/Main.java

index a831d0504969aaef8ecfa22856f54be87ff2acdf..b1ab426d6cf67dfd7ba65d6ac4cc711775a86f5a 100644 (file)
@@ -26,7 +26,7 @@ import java.io.File;
 class Dirs {
 
   void init(Runner runner) {
-    boolean onProject = ScanProperties.SCAN_TASK.equals(runner.property(RunnerProperties.TASK, null));
+    boolean onProject = Utils.taskRequiresProject(runner.properties());
     if (onProject) {
       initProjectDirs(runner);
     } else {
index 80c4a0ca841bca9f6f9f01d2458e8c5662beafd2..999ac076eca44b71577c5b806666e9c3d842434e 100644 (file)
@@ -36,11 +36,6 @@ public interface RunnerProperties {
    */
   String TASK = "sonar.task";
 
-  /**
-   * Encoding of source and test files. By default it's the platform encoding.
-   */
-  String SOURCE_ENCODING = "sonar.sourceEncoding";
-
   /**
    * Working directory containing generated reports and temporary data.
    */
index f41459bf5b816eb7b453d8fc7a9914ca3b2fe012..91e68b822f5680e5aaff75dcf71e4d04672bcaee 100644 (file)
@@ -29,6 +29,7 @@ public interface ScanProperties {
 
   /**
    * Default task
+   *
    * @see RunnerProperties#TASK
    */
   String SCAN_TASK = "scan";
@@ -80,4 +81,10 @@ public interface ScanProperties {
    * Property used to specify the base directory of the project to analyse. Default is ".".
    */
   String PROJECT_BASEDIR = "sonar.projectBaseDir";
+
+  /**
+   * Encoding of source and test files. By default it's the platform encoding.
+   */
+  String PROJECT_SOURCE_ENCODING = "sonar.sourceEncoding";
+
 }
index 8782ebd0c7e76e38fc1172edf874d1a8c29a8bd3..660e929ee6880dbd0b970346e87fb8134fcf41af 100644 (file)
@@ -27,15 +27,17 @@ import java.util.Locale;
 class SourceEncoding {
 
   void init(Runner runner) {
-    String sourceEncoding = runner.property(RunnerProperties.SOURCE_ENCODING, "");
-    boolean platformDependent = false;
-    if ("".equals(sourceEncoding)) {
-      sourceEncoding = Charset.defaultCharset().name();
-      platformDependent = true;
-      runner.setProperty(RunnerProperties.SOURCE_ENCODING, sourceEncoding);
+    boolean onProject = Utils.taskRequiresProject(runner.properties());
+    if (onProject) {
+      String sourceEncoding = runner.property(ScanProperties.PROJECT_SOURCE_ENCODING, "");
+      boolean platformDependent = false;
+      if ("".equals(sourceEncoding)) {
+        sourceEncoding = Charset.defaultCharset().name();
+        platformDependent = true;
+        runner.setProperty(ScanProperties.PROJECT_SOURCE_ENCODING, sourceEncoding);
+      }
+      Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\""
+          + (platformDependent ? " (analysis is platform dependent)" : ""));
     }
-    Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\""
-        + (platformDependent ? " (analysis is platform dependent)" : ""));
   }
-
 }
index 046c1dda0887c30c90fe193b992b8ff9046a0cdb..a9c18ebb0ddb50f3733d5b07d837800943710ebb 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.runner.api;
 
 import java.util.Arrays;
 import java.util.Iterator;
+import java.util.Properties;
 
 class Utils {
   private Utils() {
@@ -42,4 +43,8 @@ class Utils {
     }
     return sb.toString();
   }
+
+  static boolean taskRequiresProject(Properties props) {
+    return ScanProperties.SCAN_TASK.equals(props.get(RunnerProperties.TASK));
+  }
 }
index b7270e96d72818cdb287346ebc1e411386da148f..3cce220a9b105f8e02bdc01db41aecaec8e4b0ff 100644 (file)
@@ -32,15 +32,23 @@ public class SourceEncodingTest {
 
   @Test
   public void should_set_default_platform_encoding() throws Exception {
+    runner.setProperty("sonar.task", "scan");
     encoding.init(runner);
     assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo(Charset.defaultCharset().name());
   }
 
   @Test
   public void should_use_parameterized_encoding() throws Exception {
+    runner.setProperty("sonar.task", "scan");
     runner.setProperty("sonar.sourceEncoding", "THE_ISO_1234");
     encoding.init(runner);
     assertThat(runner.property("sonar.sourceEncoding", null)).isEqualTo("THE_ISO_1234");
   }
 
+  @Test
+  public void should_not_init_encoding_if_not_project_task() throws Exception {
+    runner.setProperty("sonar.task", "views");
+    encoding.init(runner);
+    assertThat(runner.property("sonar.sourceEncoding", null)).isNull();
+  }
 }
index f956c755334083a208a31233dfcec5b2d09a3e79..8bb4ec229eb487a2699e440c1dd7b2030aac3d16 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.runner.api;
 
 import org.junit.Test;
 
+import java.util.Properties;
+
 import static org.fest.assertions.Assertions.assertThat;
 
 public class UtilsTest {
@@ -30,4 +32,19 @@ public class UtilsTest {
     assertThat(Utils.join(new String[]{"foo"}, ",")).isEqualTo("foo");
     assertThat(Utils.join(new String[]{"foo", "bar"}, ",")).isEqualTo("foo,bar");
   }
+
+  @Test
+  public void task_should_require_project() {
+    Properties props = new Properties();
+    props.setProperty("sonar.task", "scan");
+    assertThat(Utils.taskRequiresProject(props)).isTrue();
+  }
+
+  @Test
+  public void task_should_not_require_project() {
+    Properties props = new Properties();
+    assertThat(Utils.taskRequiresProject(props)).isFalse();
+    props.setProperty("sonar.task", "views");
+    assertThat(Utils.taskRequiresProject(props)).isFalse();
+  }
 }
index a1585624acee01e400a9f34d6b98aab98e0e1bba..69022f3f430afb21b843ea3f2ccac1c9f231bb4c 100644 (file)
@@ -78,7 +78,6 @@ public class Main {
 //      Logs.info("Server: " + runner.getSonarServerURL());
 //      try {
 //        Logs.info("Work directory: " + runner.getWorkDir().getCanonicalPath());
-      //Logs.info("Cache directory: " + runner.getCache().getCacheLocation());
 //      } catch (IOException e) {
 //        throw new RunnerException("Unable to resolve directory", e);
 //      }