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

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