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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * SonarSource :: IT :: SonarScanner CLI
  3. * Copyright (C) 2009-2022 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.Rule;
  30. import org.junit.Test;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonarqube.ws.Measures.Measure;
  33. import static java.lang.Integer.parseInt;
  34. import static org.assertj.core.api.Assertions.assertThat;
  35. public class ScannerTest extends ScannerTestCase {
  36. @Rule
  37. public TemporaryFolder temp = new TemporaryFolder();
  38. @Test
  39. public void basedir_contains_sources() {
  40. SonarScanner build = newScanner(new File("projects/basedir-with-source"));
  41. orchestrator.executeBuild(build);
  42. Map<String, Measure> projectMeasures = getMeasures(
  43. "java:basedir-with-source", "files", "ncloc");
  44. assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(1);
  45. assertThat(parseInt(projectMeasures.get("ncloc").getValue()))
  46. .isGreaterThan(1);
  47. }
  48. /**
  49. * Replace the maven format groupId:artifactId by a single key
  50. */
  51. @Test
  52. public void should_support_simple_project_keys() {
  53. SonarScanner build = newScanner(new File("projects/simple-sample"))
  54. .setProjectKey("SAMPLE");
  55. orchestrator.executeBuild(build);
  56. Map<String, Measure> projectMeasures = getMeasures("SAMPLE", "files",
  57. "ncloc");
  58. assertThat(parseInt(projectMeasures.get("files").getValue())).isEqualTo(2);
  59. assertThat(parseInt(projectMeasures.get("ncloc").getValue()))
  60. .isGreaterThan(1);
  61. }
  62. /**
  63. * SONARPLUGINS-1230
  64. */
  65. @Test
  66. public void should_override_working_dir_with_relative_path() {
  67. SonarScanner build = newScanner(new File("projects/override-working-dir"))
  68. .setProperty("sonar.working.directory", ".overridden-relative-sonar");
  69. orchestrator.executeBuild(build);
  70. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  71. assertThat(
  72. new File("projects/override-working-dir/.overridden-relative-sonar"))
  73. .exists().isDirectory();
  74. }
  75. /**
  76. * SONARPLUGINS-1230
  77. */
  78. @Test
  79. public void should_override_working_dir_with_absolute_path() {
  80. File projectHome = new File("projects/override-working-dir");
  81. SonarScanner build = newScanner(projectHome)
  82. .setProperty("sonar.working.directory",
  83. new File(projectHome, ".overridden-absolute-sonar").getAbsolutePath());
  84. orchestrator.executeBuild(build);
  85. assertThat(new File("projects/override-working-dir/.sonar")).doesNotExist();
  86. assertThat(
  87. new File("projects/override-working-dir/.overridden-absolute-sonar"))
  88. .exists().isDirectory();
  89. }
  90. /**
  91. * SONARPLUGINS-1856
  92. */
  93. @Test
  94. public void should_fail_if_source_dir_does_not_exist() {
  95. SonarScanner build = newScanner(new File("projects/bad-source-dirs"));
  96. BuildResult result = orchestrator.executeBuildQuietly(build);
  97. assertThat(result.getStatus()).isNotEqualTo(0);
  98. // with the following message
  99. assertThat(result.getLogs())
  100. .contains("Invalid value of sonar.sources for bad-source-dirs");
  101. }
  102. /**
  103. * SONARPLUGINS-2256
  104. */
  105. @Test
  106. public void should_warn_when_analysis_is_platform_dependent() {
  107. SonarScanner build = newScanner(new File("projects/simple-sample"))
  108. // ORCH-243
  109. .setSourceEncoding("");
  110. String log = orchestrator.executeBuild(build).getLogs();
  111. // 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
  112. // used to launch the tests. But we can check that the analysis is platform dependent (i.e. "sonar.sourceEncoding" hasn't been set).
  113. assertThat(log).contains("Default locale:");
  114. assertThat(log).contains(", source code encoding:");
  115. assertThat(log).contains("(analysis is platform dependent)");
  116. }
  117. /**
  118. * SONARUNNER-153
  119. */
  120. @Test
  121. public void should_enable_verbose() {
  122. // this line should appear in all versions (LTS-DEV) in debug only
  123. String expectedLog = "Available languages:";
  124. SonarScanner build = newScanner(new File("projects/simple-sample"))
  125. .setProperty("sonar.verbose", "true");
  126. String logs = orchestrator.executeBuild(build).getLogs();
  127. assertThat(logs).contains(expectedLog);
  128. }
  129. @Test
  130. public void should_use_json_environment_props() {
  131. SonarScanner build = newScanner(
  132. new File("projects/simple-sample-no-properties"))
  133. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  134. + "\"sonar.projectKey\" : \"sample\"," +
  135. "\"sonar.projectName\" : \"Sample, with comma\"," +
  136. "\"sonar.projectDescription\" : \"This is a sample\"," +
  137. "\"sonar.projectVersion\" : \"1.2.3\"," +
  138. "\"sonar.sources\" : \"src\" }");
  139. orchestrator.executeBuild(build);
  140. }
  141. @Test
  142. public void should_use_environment_prop() {
  143. SonarScanner build = newScanner(new File("projects/simple-sample"))
  144. .setEnvironmentVariable("SONAR_HOST_URL", "http://from-env.org");
  145. BuildRunner runner = new BuildRunner(orchestrator.getConfiguration());
  146. BuildResult buildResult = runner.runQuietly(null, build);
  147. assertThat(buildResult.isSuccess()).isFalse();
  148. assertThat(buildResult.getLogs())
  149. .contains("SonarQube server [http://from-env.org] can not be reached");
  150. }
  151. @Test
  152. public void should_skip_analysis() {
  153. SonarScanner build = newScanner(new File("projects/simple-sample"))
  154. .setProperty("sonar.host.url", "http://foo")
  155. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS",
  156. "{ \"sonar.scanner.skip\":\"true\" }");
  157. BuildResult result = orchestrator.executeBuild(build);
  158. assertThat(result.getLogs()).contains("SonarScanner analysis skipped");
  159. }
  160. @Test
  161. public void should_fail_if_unable_to_connect() {
  162. SonarScanner build = newScanner(new File("projects/simple-sample"))
  163. //env property should be overridden
  164. .setEnvironmentVariable("SONAR_HOST_URL", "http://from-env.org")
  165. .setProperty("sonar.host.url", "http://foo");
  166. BuildResult result = orchestrator.executeBuildQuietly(build);
  167. // expect build failure
  168. assertThat(result.isSuccess()).isFalse();
  169. // with the following message
  170. assertThat(result.getLogs())
  171. .contains("SonarQube server [http://foo] can not be reached");
  172. }
  173. // SONARPLUGINS-3574
  174. @Test
  175. public void run_from_external_location() throws IOException {
  176. File tempDir = temp.newFolder();
  177. SonarScanner build = newScanner(tempDir)
  178. .setProperty("sonar.projectBaseDir",
  179. new File("projects/simple-sample").getAbsolutePath())
  180. .addArguments("-e");
  181. orchestrator.executeBuild(build);
  182. assertThat(getComponent("sample").getDescription())
  183. .isEqualTo("This is a sample");
  184. Map<String, Measure> projectMeasures = getMeasures("sample", "files",
  185. "ncloc", "violations");
  186. assertThat(projectMeasures.values().stream()
  187. .filter(measure -> measure.getValue() != null)
  188. .collect(Collectors.toList())).hasSize(3);
  189. }
  190. @Test
  191. public void verify_scanner_opts_env_variable_passed_as_jvm_argument() {
  192. SonarScanner build = newScanner(new File("projects/simple-sample"))
  193. .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx1k");
  194. BuildResult executeBuild = orchestrator.executeBuildQuietly(build);
  195. assertThat(executeBuild.getLastStatus()).isNotEqualTo(0);
  196. String logs = executeBuild.getLogs();
  197. assertThat(logs).contains("Error occurred during initialization of VM");
  198. // Not the same message with JRE 8 and 11
  199. assertThat(logs).containsPattern("Too small (initial|maximum) heap");
  200. }
  201. // SQSCANNER-24
  202. @Test
  203. public void should_override_project_settings_path() {
  204. File projectHome = new File("projects/override-project-settings-path");
  205. SonarScanner build = newScanner(projectHome)
  206. .setProperty("project.settings",
  207. new File(projectHome, "conf/sq-project.properties").getAbsolutePath());
  208. orchestrator.executeBuild(build);
  209. assertThat(getComponent("sample-with-custom-settings-path").getName())
  210. .isEqualTo("Test with custom settings location");
  211. }
  212. // SQSCANNER-61
  213. @Test
  214. public void should_override_project_settings_path_using_env_variable() {
  215. File projectHome = new File("projects/override-project-settings-path");
  216. SonarScanner build = newScanner(projectHome)
  217. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  218. + "\"project.settings\" : \"" + StringEscapeUtils.escapeJavaScript(
  219. new File(projectHome, "conf/sq-project.properties").getAbsolutePath())
  220. + "\"}");
  221. orchestrator.executeBuild(build);
  222. assertThat(getComponent("sample-with-custom-settings-path").getName())
  223. .isEqualTo("Test with custom settings location");
  224. }
  225. }