Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ScannerTest.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * SonarSource :: IT :: SonarScanner CLI
  3. * Copyright (C) 2009-2024 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. * SONARUNNER-153
  121. */
  122. @Test
  123. public void should_enable_verbose() {
  124. // this line should appear in all versions (LTS-DEV) in debug only
  125. String expectedLog = "Available languages:";
  126. SonarScanner build = newScanner(new File("projects/simple-sample"))
  127. .setProperty("sonar.verbose", "true");
  128. String logs = orchestrator.executeBuild(build).getLogs();
  129. assertThat(logs).contains(expectedLog);
  130. }
  131. @Test
  132. public void should_use_json_environment_props() {
  133. SonarScanner build = newScanner(
  134. new File("projects/simple-sample-no-properties"))
  135. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  136. + "\"sonar.projectKey\" : \"sample\"," +
  137. "\"sonar.projectName\" : \"Sample, with comma\"," +
  138. "\"sonar.projectDescription\" : \"This is a sample\"," +
  139. "\"sonar.projectVersion\" : \"1.2.3\"," +
  140. "\"sonar.sources\" : \"src\" }");
  141. orchestrator.executeBuild(build);
  142. }
  143. @Test
  144. public void should_use_environment_prop() {
  145. SonarScanner build = newScanner(new File("projects/simple-sample"))
  146. .setEnvironmentVariable("SONAR_HOST_URL", "http://from-env.org");
  147. BuildRunner runner = new BuildRunner(orchestrator.getConfiguration());
  148. BuildResult buildResult = runner.runQuietly(null, build);
  149. assertThat(buildResult.isSuccess()).isFalse();
  150. assertThat(buildResult.getLogs())
  151. .containsAnyOf(
  152. "No such host is known (from-env.org)", // Windows
  153. "from-env.org: Name or service not known" // Linux
  154. );
  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 CLI 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. .containsAnyOf(
  177. "No such host is known (foo)", // Windows
  178. "foo: No address associated with hostname" // Linux
  179. );
  180. }
  181. // SONARPLUGINS-3574
  182. @Test
  183. public void run_from_external_location() throws IOException {
  184. File tempDir = temp.newFolder();
  185. SonarScanner build = newScanner(tempDir)
  186. .setProperty("sonar.projectBaseDir",
  187. new File("projects/simple-sample").getAbsolutePath())
  188. .addArguments("-e");
  189. orchestrator.executeBuild(build);
  190. assertThat(getComponent("sample").getDescription())
  191. .isEqualTo("This is a sample");
  192. Map<String, Measure> projectMeasures = getMeasures("sample", "files",
  193. "ncloc", "violations");
  194. assertThat(projectMeasures.values().stream()
  195. .filter(measure -> measure.getValue() != null)
  196. .collect(Collectors.toList())).hasSize(3);
  197. }
  198. @Test
  199. public void verify_scanner_opts_env_variable_passed_as_jvm_argument() {
  200. SonarScanner build = newScanner(new File("projects/simple-sample"))
  201. .setEnvironmentVariable("SONAR_SCANNER_OPTS", "-Xmx1k");
  202. BuildResult executeBuild = orchestrator.executeBuildQuietly(build);
  203. assertThat(executeBuild.getLastStatus()).isNotZero();
  204. String logs = executeBuild.getLogs();
  205. assertThat(logs).contains("Error occurred during initialization of VM")
  206. // Not the same message with JRE 8 and 11
  207. .containsPattern("Too small (initial|maximum) heap");
  208. }
  209. // SQSCANNER-24
  210. @Test
  211. public void should_override_project_settings_path() {
  212. File projectHome = new File("projects/override-project-settings-path");
  213. SonarScanner build = newScanner(projectHome)
  214. .setProperty("project.settings",
  215. new File(projectHome, "conf/sq-project.properties").getAbsolutePath());
  216. orchestrator.executeBuild(build);
  217. assertThat(getComponent("sample-with-custom-settings-path").getName())
  218. .isEqualTo("Test with custom settings location");
  219. }
  220. // SQSCANNER-61
  221. @Test
  222. public void should_override_project_settings_path_using_env_variable() {
  223. File projectHome = new File("projects/override-project-settings-path");
  224. SonarScanner build = newScanner(projectHome)
  225. .setEnvironmentVariable("SONARQUBE_SCANNER_PARAMS", "{"
  226. + "\"project.settings\" : \"" + StringEscapeUtils.escapeJavaScript(
  227. new File(projectHome, "conf/sq-project.properties").getAbsolutePath())
  228. + "\"}");
  229. orchestrator.executeBuild(build);
  230. assertThat(getComponent("sample-with-custom-settings-path").getName())
  231. .isEqualTo("Test with custom settings location");
  232. }
  233. }