You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScannerTest.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * SonarSource :: IT :: SonarQube Scanner
  3. * Copyright (C) 2009-2020 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package com.sonarsource.scanner.it;
  21. import com.sonar.orchestrator.build.BuildResult;
  22. import com.sonar.orchestrator.build.BuildRunner;
  23. import com.sonar.orchestrator.build.SonarScanner;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.Map;
  27. import java.util.stream.Collectors;
  28. import org.apache.commons.lang.StringEscapeUtils;
  29. import org.junit.After;
  30. import org.junit.Rule;
  31. import org.junit.Test;
  32. import org.junit.rules.TemporaryFolder;
  33. import org.sonarqube.ws.Measures.Measure;
  34. import static java.lang.Integer.parseInt;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. public class ScannerTest extends ScannerTestCase {
  37. @Rule
  38. public TemporaryFolder temp = new TemporaryFolder();
  39. @After
  40. public void cleanup() {
  41. orchestrator.resetData();
  42. }
  43. @Test
  44. public void basedir_contains_sources() {
  45. SonarScanner build = newScanner(new File("projects/basedir-with-source"));
  46. orchestrator.executeBuild(build);
  47. Map<String, Measure> projectMeasures = getMeasures(
  48. "java:basedir-with-source", "files", "ncloc");
  49. assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1);
  50. assertThat(parseInt(projectMeasures.get("ncloc").getValue()))
  51. .isGreaterThan(1);
  52. }
  53. /**
  54. * Replace the maven format groupId:artifactId by a single key
  55. */
  56. @Test
  57. public void should_support_simple_project_keys() {
  58. SonarScanner build = newScanner(new File("projects/simple-sample"))
  59. .setProjectKey("SAMPLE");
  60. orchestrator.executeBuild(build);
  61. Map<String, Measure> projectMeasures = getMeasures("SAMPLE", "files",
  62. "ncloc");
  63. assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2);
  64. assertThat(parseInt(projectMeasures.get("ncloc").getValue()))
  65. .isGreaterThan(1);
  66. }
  67. /**
  68. * SONARPLUGINS-1230
  69. */
  70. @Test
  71. public void should_override_working_dir_with_relative_path() {
  72. SonarScanner build = newScanner(new File("projects/override-working-dir"))
  73. .setProperty("sonar.working.directory", ".overridden-relative-sonar");
  74. orchestrator.executeBuild(build);
  75. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  76. assertThat(
  77. new File("projects/override-working-dir/.overridden-relative-sonar"))
  78. .exists().isDirectory();
  79. }
  80. /**
  81. * SONARPLUGINS-1230
  82. */
  83. @Test
  84. public void should_override_working_dir_with_absolute_path() {
  85. File projectHome = new File("projects/override-working-dir");
  86. SonarScanner build = newScanner(projectHome)
  87. .setProperty("sonar.working.directory",
  88. new File(projectHome, ".overridden-absolute-sonar").getAbsolutePath());
  89. orchestrator.executeBuild(build);
  90. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  91. assertThat(
  92. new File("projects/override-working-dir/.overridden-absolute-sonar"))
  93. .exists().isDirectory();
  94. }
  95. /**
  96. * SONARPLUGINS-1856
  97. */
  98. @Test
  99. public void should_fail_if_source_dir_does_not_exist() {
  100. SonarScanner build = newScanner(new File("projects/bad-source-dirs"));
  101. BuildResult result = orchestrator.executeBuildQuietly(build);
  102. assertThat(result.getStatus()).isNotEqualTo(0);
  103. // with the following message
  104. assertThat(result.getLogs())
  105. .contains("Invalid value of sonar.sources for bad-source-dirs");
  106. }
  107. /**
  108. * SONARPLUGINS-2256
  109. */
  110. @Test
  111. public void should_warn_when_analysis_is_platform_dependent() {
  112. SonarScanner build = newScanner(new File("projects/simple-sample"))
  113. // ORCH-243
  114. .setSourceEncoding("");
  115. String log = orchestrator.executeBuild(build).getLogs();
  116. // Note: we can't really check the locale value and the charset because the ones used during the Sonar analysis may not be the ones
  117. // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set).
  118. assertThat(log).contains("Default locale:");
  119. assertThat(log).contains(", source code encoding:");
  120. assertThat(log).contains("(analysis is platform dependent)");
  121. }
  122. /**
  123. * SONARUNNER-153
  124. */
  125. @Test
  126. public void should_enable_verbose() {
  127. // this line should appear in all versions (LTS-DEV) in debug only
  128. String expectedLog = "Available languages:";
  129. SonarScanner build = newScanner(new File("projects/simple-sample"))
  130. .setProperty("sonar.verbose", "true");
  131. String logs = orchestrator.executeBuild(build).getLogs();
  132. assertThat(logs).contains(expectedLog);
  133. }
  134. @Test
  135. public void should_use_json_environment_props() {
  136. SonarScanner build = newScanner(
  137. new File("projects/simple-sample-no-properties"))
  138. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  139. + "\"sonar.projectKey\" : \"sample\"," +
  140. "\"sonar.projectName\" : \"Sample, with comma\"," +
  141. "\"sonar.projectDescription\" : \"This is a sample\"," +
  142. "\"sonar.projectVersion\" : \"1.2.3\"," +
  143. "\"sonar.sources\" : \"src\" }");
  144. orchestrator.executeBuild(build);
  145. }
  146. @Test
  147. public void should_use_environment_prop() {
  148. SonarScanner build = newScanner(new File("projects/simple-sample"))
  149. .setEnvironmentVariable("SONAR_HOST_URL", "http://from-env.org");
  150. BuildRunner runner = new BuildRunner(orchestrator.getConfiguration());
  151. BuildResult buildResult = runner.runQuietly(null, build);
  152. assertThat(buildResult.isSuccess()).isFalse();
  153. assertThat(buildResult.getLogs())
  154. .contains("SonarQube server [http://from-env.org] can not be reached");
  155. }
  156. @Test
  157. public void should_skip_analysis() {
  158. SonarScanner build = newScanner(new File("projects/simple-sample"))
  159. .setProperty("sonar.host.url", "http://foo")
  160. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS",
  161. "{ \"sonar.scanner.skip\":\"true\" }");
  162. BuildResult result = orchestrator.executeBuild(build);
  163. assertThat(result.getLogs()).contains("SonarScanner analysis skipped");
  164. }
  165. @Test
  166. public void should_fail_if_unable_to_connect() {
  167. SonarScanner build = newScanner(new File("projects/simple-sample"))
  168. //env property should be overridden
  169. .setEnvironmentVariable("SONAR_HOST_URL", "http://from-env.org")
  170. .setProperty("sonar.host.url", "http://foo");
  171. BuildResult result = orchestrator.executeBuildQuietly(build);
  172. // expect build failure
  173. assertThat(result.isSuccess()).isFalse();
  174. // with the following message
  175. assertThat(result.getLogs())
  176. .contains("SonarQube server [http://foo] can not be reached");
  177. }
  178. // SONARPLUGINS-3574
  179. @Test
  180. public void run_from_external_location() throws IOException {
  181. File tempDir = temp.newFolder();
  182. SonarScanner build = newScanner(tempDir)
  183. .setProperty("sonar.projectBaseDir",
  184. new File("projects/simple-sample").getAbsolutePath())
  185. .addArguments("-e");
  186. orchestrator.executeBuild(build);
  187. assertThat(getComponent("sample").getDescription())
  188. .isEqualTo("This is a sample");
  189. Map<String, Measure> projectMeasures = getMeasures("sample", "files",
  190. "ncloc", "classes", "violations");
  191. assertThat(projectMeasures.values().stream()
  192. .filter(measure -> measure.getValue() != null)
  193. .collect(Collectors.toList())).hasSize(4);
  194. }
  195. @Test
  196. public void verify_scanner_opts_env_variable_passed_as_jvm_argument() {
  197. SonarScanner build = newScanner(new File("projects/simple-sample"))
  198. .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx1k");
  199. BuildResult executeBuild = orchestrator.executeBuildQuietly(build);
  200. assertThat(executeBuild.getLastStatus()).isNotEqualTo(0);
  201. String logs = executeBuild.getLogs();
  202. assertThat(logs).contains("Error occurred during initialization of VM");
  203. // Not the same message with JRE 8 and 11
  204. assertThat(logs).containsPattern("Too small (initial|maximum) heap");
  205. }
  206. // SQSCANNER-24
  207. @Test
  208. public void should_override_project_settings_path() {
  209. File projectHome = new File("projects/override-project-settings-path");
  210. SonarScanner build = newScanner(projectHome)
  211. .setProperty("project.settings",
  212. new File(projectHome, "conf/sq-project.properties").getAbsolutePath());
  213. orchestrator.executeBuild(build);
  214. assertThat(getComponent("sample-with-custom-settings-path").getName())
  215. .isEqualTo("Test with custom settings location");
  216. }
  217. // SQSCANNER-61
  218. @Test
  219. public void should_override_project_settings_path_using_env_variable() {
  220. File projectHome = new File("projects/override-project-settings-path");
  221. SonarScanner build = newScanner(projectHome)
  222. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  223. + "\"project.settings\" : \"" + StringEscapeUtils.escapeJavaScript(
  224. new File(projectHome, "conf/sq-project.properties").getAbsolutePath())
  225. + "\"}");
  226. orchestrator.executeBuild(build);
  227. assertThat(getComponent("sample-with-custom-settings-path").getName())
  228. .isEqualTo("Test with custom settings location");
  229. }
  230. }