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.

ScmConfigurationTest.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * SonarQube
  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 org.sonar.scanner.scm;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import java.util.Optional;
  25. import org.hamcrest.BaseMatcher;
  26. import org.hamcrest.Description;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.junit.runner.RunWith;
  31. import org.mockito.Answers;
  32. import org.sonar.api.CoreProperties;
  33. import org.sonar.api.batch.AnalysisMode;
  34. import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
  35. import org.sonar.api.batch.scm.ScmProvider;
  36. import org.sonar.api.config.Configuration;
  37. import org.sonar.api.notifications.AnalysisWarnings;
  38. import org.sonar.api.utils.MessageException;
  39. import org.sonar.api.utils.log.LogTester;
  40. import org.sonar.core.config.ScannerProperties;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. import static org.mockito.Mockito.any;
  43. import static org.mockito.Mockito.anyString;
  44. import static org.mockito.Mockito.mock;
  45. import static org.mockito.Mockito.verify;
  46. import static org.mockito.Mockito.verifyZeroInteractions;
  47. import static org.mockito.Mockito.when;
  48. import static org.mockito.Mockito.withSettings;
  49. import static org.sonar.scanner.scm.ScmConfiguration.MESSAGE_SCM_EXLUSIONS_IS_DISABLED_BY_CONFIGURATION;
  50. import static org.sonar.scanner.scm.ScmConfiguration.MESSAGE_SCM_STEP_IS_DISABLED_BY_CONFIGURATION;
  51. @RunWith(DataProviderRunner.class)
  52. public class ScmConfigurationTest {
  53. private final InputModuleHierarchy inputModuleHierarchy = mock(InputModuleHierarchy.class, withSettings().defaultAnswer(Answers.RETURNS_MOCKS));
  54. private final AnalysisMode analysisMode = mock(AnalysisMode.class);
  55. private final AnalysisWarnings analysisWarnings = mock(AnalysisWarnings.class);
  56. private final Configuration settings = mock(Configuration.class);
  57. private final String scmProviderKey = "dummyScmProviderKey";
  58. private final ScmProvider scmProvider = mock(ScmProvider.class);
  59. private final ScmConfiguration underTest;
  60. @Rule
  61. public LogTester logTester = new LogTester();
  62. @Rule
  63. public ExpectedException thrown = ExpectedException.none();
  64. public ScmConfigurationTest() {
  65. when(analysisMode.isIssues()).thenReturn(false);
  66. when(scmProvider.key()).thenReturn(scmProviderKey);
  67. underTest = new ScmConfiguration(inputModuleHierarchy, analysisMode, settings, analysisWarnings, scmProvider);
  68. }
  69. @Test
  70. public void do_not_register_warning_when_success_to_autodetect_scm_provider() {
  71. when(scmProvider.supports(any())).thenReturn(true);
  72. underTest.start();
  73. assertThat(underTest.provider()).isNotNull();
  74. verifyZeroInteractions(analysisWarnings);
  75. }
  76. @Test
  77. public void register_warning_when_fail_to_detect_scm_provider() {
  78. underTest.start();
  79. assertThat(underTest.provider()).isNull();
  80. verify(analysisWarnings).addUnique(anyString());
  81. }
  82. @Test
  83. public void log_when_disabled() {
  84. when(settings.getBoolean(CoreProperties.SCM_DISABLED_KEY)).thenReturn(Optional.of(true));
  85. underTest.start();
  86. assertThat(logTester.logs()).contains(MESSAGE_SCM_STEP_IS_DISABLED_BY_CONFIGURATION);
  87. }
  88. @Test
  89. public void log_when_exclusion_is_disabled() {
  90. when(settings.getBoolean(CoreProperties.SCM_EXCLUSIONS_DISABLED_KEY)).thenReturn(Optional.of(true));
  91. underTest.start();
  92. assertThat(logTester.logs()).contains(MESSAGE_SCM_EXLUSIONS_IS_DISABLED_BY_CONFIGURATION);
  93. }
  94. @Test
  95. @UseDataProvider("scmDisabledProperty")
  96. public void exclusion_is_disabled_by_property(boolean scmDisabled, boolean scmExclusionsDisabled, boolean isScmExclusionDisabled) {
  97. when(settings.getBoolean(CoreProperties.SCM_DISABLED_KEY)).thenReturn(Optional.of(scmDisabled));
  98. when(settings.getBoolean(CoreProperties.SCM_EXCLUSIONS_DISABLED_KEY)).thenReturn(Optional.of(scmExclusionsDisabled));
  99. underTest.start();
  100. assertThat(underTest.isExclusionDisabled()).isEqualTo(isScmExclusionDisabled);
  101. }
  102. @DataProvider
  103. public static Object[][] scmDisabledProperty() {
  104. return new Object[][] {
  105. {true, true, true},
  106. {true, false, true},
  107. {false, true, true},
  108. {false, false, false}
  109. };
  110. }
  111. @Test
  112. public void return_early_from_start_in_issues_mode() {
  113. // return early = doesn't reach the logging when disabled
  114. when(settings.getBoolean(CoreProperties.SCM_DISABLED_KEY)).thenReturn(Optional.of(true));
  115. when(analysisMode.isIssues()).thenReturn(true);
  116. underTest.start();
  117. assertThat(logTester.logs()).isEmpty();
  118. }
  119. @Test
  120. public void fail_when_multiple_scm_providers_claim_support() {
  121. when(scmProvider.supports(any())).thenReturn(true);
  122. when(scmProvider.key()).thenReturn("key1", "key2");
  123. ScmProvider[] providers = {scmProvider, scmProvider};
  124. ScmConfiguration underTest = new ScmConfiguration(inputModuleHierarchy, analysisMode, settings, analysisWarnings, providers);
  125. thrown.expect(MessageException.class);
  126. thrown.expectMessage(
  127. new BaseMatcher<String>() {
  128. @Override
  129. public void describeTo(Description description) {
  130. }
  131. @Override
  132. public boolean matches(Object item) {
  133. return ((String) item).matches("SCM provider autodetection failed. "
  134. + "Both .* and .* claim to support this project. "
  135. + "Please use \"sonar.scm.provider\" to define SCM of your project.");
  136. }
  137. });
  138. underTest.start();
  139. }
  140. @Test
  141. public void fail_when_considerOldScmUrl_finds_invalid_provider_in_link() {
  142. when(settings.get(ScannerProperties.LINKS_SOURCES_DEV)).thenReturn(Optional.of("scm:invalid"));
  143. thrown.expect(IllegalArgumentException.class);
  144. thrown.expectMessage("no SCM provider found for this key");
  145. underTest.start();
  146. }
  147. @Test
  148. public void set_provider_from_valid_link() {
  149. when(settings.get(ScannerProperties.LINKS_SOURCES_DEV)).thenReturn(Optional.of("scm:" + scmProviderKey));
  150. underTest.start();
  151. assertThat(underTest.provider()).isSameAs(scmProvider);
  152. }
  153. @Test
  154. @UseDataProvider("malformedScmLinks")
  155. public void dont_set_provider_from_links_if_malformed(String link) {
  156. when(settings.get(ScannerProperties.LINKS_SOURCES_DEV)).thenReturn(Optional.of(link));
  157. underTest.start();
  158. assertThat(underTest.provider()).isNull();
  159. }
  160. @DataProvider
  161. public static Object[][] malformedScmLinks() {
  162. return new Object[][] {
  163. {"invalid prefix"},
  164. {"scm"},
  165. {"scm:"}
  166. };
  167. }
  168. }