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

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