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 10.0KB

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