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.

MainTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * SonarScanner CLI
  3. * Copyright (C) 2011-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 org.sonarsource.scanner.cli;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. import org.junit.jupiter.api.BeforeEach;
  24. import org.junit.jupiter.api.Test;
  25. import org.junit.jupiter.api.extension.RegisterExtension;
  26. import org.mockito.ArgumentCaptor;
  27. import org.mockito.InOrder;
  28. import org.mockito.Mockito;
  29. import org.slf4j.LoggerFactory;
  30. import org.slf4j.event.Level;
  31. import org.sonar.api.utils.MessageException;
  32. import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
  33. import org.sonarsource.scanner.lib.ScannerEngineFacade;
  34. import org.sonarsource.scanner.lib.ScannerProperties;
  35. import testutils.LogTester;
  36. import static org.assertj.core.api.Assertions.assertThat;
  37. import static org.mockito.ArgumentMatchers.any;
  38. import static org.mockito.Mockito.doThrow;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.never;
  41. import static org.mockito.Mockito.times;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. class MainTest {
  45. @RegisterExtension
  46. LogTester logTester = new LogTester();
  47. private final Exit exit = mock();
  48. private final Cli cli = mock();
  49. private final Conf conf = mock();
  50. private final Properties properties = mock();
  51. private final ScannerEngineBootstrapperFactory scannerEngineBootstrapperFactory = mock();
  52. private final ScannerEngineBootstrapper bootstrapper = mock();
  53. private final ScannerEngineFacade engine = mock();
  54. @BeforeEach
  55. void setUp() {
  56. when(scannerEngineBootstrapperFactory.create(any(Properties.class), any(String.class))).thenReturn(bootstrapper);
  57. when(bootstrapper.bootstrap()).thenReturn(engine);
  58. when(engine.analyze(any())).thenReturn(true);
  59. when(conf.properties()).thenReturn(properties);
  60. }
  61. @Test
  62. void should_execute_scanner_engine() {
  63. when(cli.getInvokedFrom()).thenReturn("");
  64. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  65. main.analyze();
  66. verify(exit).exit(Exit.SUCCESS);
  67. verify(scannerEngineBootstrapperFactory).create(properties, "");
  68. verify(bootstrapper, times(1)).bootstrap();
  69. verify(engine, times(1)).analyze((Map) properties);
  70. }
  71. @Test
  72. void should_exit_with_error_on_error_during_analysis() {
  73. Exception e = new NullPointerException("NPE");
  74. e = new IllegalStateException("Error", e);
  75. doThrow(e).when(engine).analyze(any());
  76. when(cli.getInvokedFrom()).thenReturn("");
  77. when(cli.isDebugEnabled()).thenReturn(true);
  78. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  79. main.analyze();
  80. verify(exit).exit(Exit.INTERNAL_ERROR);
  81. assertThat(logTester.logs(Level.ERROR)).contains("Error during SonarScanner CLI execution");
  82. }
  83. @Test
  84. void should_exit_with_error_on_error_during_bootstrap() {
  85. Exception e = new NullPointerException("NPE");
  86. e = new IllegalStateException("Error", e);
  87. doThrow(e).when(bootstrapper).bootstrap();
  88. when(cli.getInvokedFrom()).thenReturn("");
  89. when(cli.isDebugEnabled()).thenReturn(true);
  90. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  91. main.analyze();
  92. verify(bootstrapper).bootstrap();
  93. verify(engine, never()).analyze(any());
  94. verify(exit).exit(Exit.INTERNAL_ERROR);
  95. assertThat(logTester.logs(Level.ERROR)).contains("Error during SonarScanner CLI execution");
  96. }
  97. @Test
  98. void show_stacktrace() {
  99. Exception e = createException(false);
  100. testException(e, false, false, Exit.INTERNAL_ERROR);
  101. assertThat(logTester.logs(Level.ERROR)).contains("Error during SonarScanner CLI execution");
  102. assertThat(logTester.logs(Level.ERROR)).contains("Re-run SonarScanner CLI using the -X switch to enable full debug logging.");
  103. }
  104. @Test
  105. void dont_show_MessageException_stacktrace() {
  106. Exception e = createException(true);
  107. testException(e, false, false, Exit.USER_ERROR);
  108. assertThat(logTester.logs(Level.ERROR)).containsOnly("Error during SonarScanner CLI execution",
  109. "my message",
  110. "Caused by: A functional cause",
  111. "",
  112. "Re-run SonarScanner CLI using the -X switch to enable full debug logging.");
  113. }
  114. @Test
  115. void dont_show_MessageException_stacktrace_embedded() {
  116. Exception e = createException(true);
  117. testException(e, false, true, Exit.USER_ERROR);
  118. assertThat(logTester.logs(Level.ERROR)).containsOnly("Error during SonarScanner CLI execution",
  119. "my message",
  120. "Caused by: A functional cause",
  121. "");
  122. }
  123. @Test
  124. void show_MessageException_stacktrace_in_debug() {
  125. Exception e = createException(true);
  126. testException(e, true, false, Exit.USER_ERROR);
  127. assertThat(logTester.logs(Level.ERROR)).containsOnly("Error during SonarScanner CLI execution");
  128. }
  129. @Test
  130. void show_MessageException_stacktrace_in_debug_embedded() {
  131. Exception e = createException(true);
  132. testException(e, true, true, Exit.USER_ERROR);
  133. assertThat(logTester.logs(Level.ERROR)).containsOnly("Error during SonarScanner CLI execution");
  134. }
  135. @Test
  136. void show_stacktrace_in_debug() {
  137. Exception e = createException(false);
  138. testException(e, true, false, Exit.INTERNAL_ERROR);
  139. assertThat(logTester.logs(Level.ERROR)).containsOnly("Error during SonarScanner CLI execution");
  140. }
  141. private void testException(Exception e, boolean debugEnabled, boolean isEmbedded, int expectedExitCode) {
  142. when(cli.isDebugEnabled()).thenReturn(debugEnabled);
  143. when(cli.isEmbedded()).thenReturn(isEmbedded);
  144. when(cli.getInvokedFrom()).thenReturn("");
  145. doThrow(e).when(engine).analyze(any());
  146. when(scannerEngineBootstrapperFactory.create(any(Properties.class), any(String.class))).thenReturn(bootstrapper);
  147. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  148. main.analyze();
  149. verify(exit).exit(expectedExitCode);
  150. }
  151. private Exception createException(boolean messageException) {
  152. Exception e;
  153. if (messageException) {
  154. e = new MessageException("my message", new IllegalStateException("A functional cause"));
  155. } else {
  156. e = new IllegalStateException("Error", new NullPointerException("NPE"));
  157. }
  158. return e;
  159. }
  160. @Test
  161. void should_only_display_version() {
  162. Properties p = new Properties();
  163. when(cli.isDisplayVersionOnly()).thenReturn(true);
  164. when(cli.getInvokedFrom()).thenReturn("");
  165. when(conf.properties()).thenReturn(p);
  166. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  167. main.analyze();
  168. InOrder inOrder = Mockito.inOrder(exit, scannerEngineBootstrapperFactory);
  169. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  170. inOrder.verify(scannerEngineBootstrapperFactory, times(1)).create(p, "");
  171. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  172. }
  173. @Test
  174. void should_skip() {
  175. Properties p = new Properties();
  176. p.setProperty(ScannerProperties.SKIP, "true");
  177. when(conf.properties()).thenReturn(p);
  178. when(cli.getInvokedFrom()).thenReturn("");
  179. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  180. main.analyze();
  181. assertThat(logTester.logs(Level.INFO)).contains("SonarScanner CLI analysis skipped");
  182. InOrder inOrder = Mockito.inOrder(exit, scannerEngineBootstrapperFactory);
  183. inOrder.verify(exit, times(1)).exit(Exit.SUCCESS);
  184. inOrder.verify(scannerEngineBootstrapperFactory, times(1)).create(p, "");
  185. }
  186. @Test
  187. void shouldLogServerVersion() {
  188. when(engine.isSonarCloud()).thenReturn(false);
  189. when(engine.getServerVersion()).thenReturn("5.5");
  190. Properties p = new Properties();
  191. when(cli.isDisplayVersionOnly()).thenReturn(true);
  192. when(cli.getInvokedFrom()).thenReturn("");
  193. when(conf.properties()).thenReturn(p);
  194. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  195. main.analyze();
  196. assertThat(logTester.logs(Level.INFO)).contains("Communicating with SonarQube Server 5.5");
  197. }
  198. @Test
  199. void should_log_SonarCloud_server() {
  200. when(engine.isSonarCloud()).thenReturn(true);
  201. Properties p = new Properties();
  202. when(conf.properties()).thenReturn(p);
  203. when(cli.getInvokedFrom()).thenReturn("");
  204. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  205. main.analyze();
  206. assertThat(logTester.logs(Level.INFO)).contains("Communicating with SonarCloud");
  207. }
  208. @Test
  209. void should_configure_logging() {
  210. Properties analysisProps = testLogging("sonar.verbose", "true");
  211. assertThat(analysisProps.getProperty("sonar.verbose")).isEqualTo("true");
  212. }
  213. @Test
  214. void should_configure_logging_trace() {
  215. Properties analysisProps = testLogging("sonar.log.level", "TRACE");
  216. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("TRACE");
  217. }
  218. @Test
  219. void should_set_bootstrap_start_time_in_millis() {
  220. Properties analysisProps = execute("sonar.scanner.bootstrapStartTime", "1714137496104");
  221. assertThat(analysisProps.getProperty("sonar.scanner.bootstrapStartTime")).isEqualTo("1714137496104");
  222. }
  223. @Test
  224. void should_configure_logging_debug() {
  225. Properties analysisProps = testLogging("sonar.log.level", "DEBUG");
  226. assertThat(analysisProps.getProperty("sonar.log.level")).isEqualTo("DEBUG");
  227. }
  228. private Properties testLogging(String propKey, String propValue) {
  229. Properties actualProps = execute(propKey, propValue);
  230. // Logger used for callback should have debug enabled
  231. assertThat(LoggerFactory.getLogger(getClass()).isDebugEnabled()).isTrue();
  232. return actualProps;
  233. }
  234. private Properties execute(String propKey, String propValue) {
  235. Properties p = new Properties();
  236. p.put(propKey, propValue);
  237. when(conf.properties()).thenReturn(p);
  238. when(cli.getInvokedFrom()).thenReturn("");
  239. Main main = new Main(exit, cli, conf, scannerEngineBootstrapperFactory);
  240. main.analyze();
  241. ArgumentCaptor<Properties> propertiesCapture = ArgumentCaptor.forClass(Properties.class);
  242. verify(engine).analyze((Map) propertiesCapture.capture());
  243. return propertiesCapture.getValue();
  244. }
  245. }