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.

ConfTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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.io.IOException;
  22. import java.net.URISyntaxException;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.Properties;
  29. import org.apache.commons.lang3.SystemUtils;
  30. import org.junit.jupiter.api.BeforeEach;
  31. import org.junit.jupiter.api.Test;
  32. import org.junit.jupiter.api.io.TempDir;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static org.assertj.core.api.Assertions.assertThatCode;
  35. import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
  36. import static org.junit.jupiter.api.Assumptions.assumeTrue;
  37. import static org.mockito.Mockito.mock;
  38. import static org.mockito.Mockito.when;
  39. class ConfTest {
  40. private final Map<String, String> env = new HashMap<>();
  41. private final Properties args = new Properties();
  42. private final Logs logs = new Logs(System.out, System.err);
  43. private final Cli cli = mock(Cli.class);
  44. private final Conf conf = new Conf(cli, logs, env);
  45. @BeforeEach
  46. void initConf() {
  47. env.clear();
  48. when(cli.properties()).thenReturn(args);
  49. }
  50. @Test
  51. void should_load_global_settings_by_home() throws Exception {
  52. Path home = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByHome/").toURI());
  53. args.setProperty("scanner.home", home.toAbsolutePath().toString());
  54. Properties properties = conf.properties();
  55. assertThat(properties).containsEntry("sonar.prop", "value");
  56. }
  57. @Test
  58. void should_not_fail_if_no_home() {
  59. assertThat(conf.properties()).isNotEmpty();
  60. // worst case, use current path
  61. assertThat(conf.properties().getProperty("sonar.projectBaseDir")).isEqualTo(Paths.get("").toAbsolutePath().toString());
  62. }
  63. @Test
  64. void should_set_bootstrap_time_only_once() {
  65. Properties properties = conf.properties();
  66. assertThat(properties).containsKey("sonar.scanner.bootstrapStartTime");
  67. String value = properties.getProperty("sonar.scanner.bootstrapStartTime");
  68. assertThat(conf.properties())
  69. .containsEntry("sonar.scanner.bootstrapStartTime", value);
  70. }
  71. @Test
  72. void base_dir_can_be_relative() throws URISyntaxException {
  73. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  74. args.setProperty("project.home", projectHome.getParent().toAbsolutePath().toString());
  75. args.setProperty("sonar.projectBaseDir", "project");
  76. Properties properties = conf.properties();
  77. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  78. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  79. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  80. }
  81. @Test
  82. void should_load_conf_by_direct_path() throws Exception {
  83. Path settings = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
  84. args.setProperty("scanner.settings", settings.toAbsolutePath().toString());
  85. assertThat(conf.properties()).containsEntry("sonar.prop", "otherValue");
  86. }
  87. @Test
  88. void shouldLoadCompleteConfiguration() throws Exception {
  89. Path runnerHome = Paths.get(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/runner").toURI());
  90. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/project").toURI());
  91. args.setProperty("scanner.home", runnerHome.toAbsolutePath().toString());
  92. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  93. Properties properties = conf.properties();
  94. assertThat(properties.getProperty("project.prop")).isEqualTo("foo");
  95. assertThat(properties.getProperty("overridden.prop")).isEqualTo("project scope");
  96. assertThat(properties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar");
  97. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  98. }
  99. @Test
  100. void shouldLoadModuleConfiguration() throws Exception {
  101. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  102. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  103. Properties properties = conf.properties();
  104. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  105. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  106. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  107. }
  108. @Test
  109. void shouldSupportDeepModuleConfigurationInRoot() throws Exception {
  110. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI());
  111. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  112. Properties properties = conf.properties();
  113. assertThat(properties.getProperty("1.sonar.projectName")).isEqualTo("Module 1");
  114. assertThat(properties.getProperty("1.11.sonar.projectName")).isEqualTo("Module 11");
  115. assertThat(properties.getProperty("1.11.111.sonar.projectName")).isEqualTo("Module 111");
  116. assertThat(properties.getProperty("1.12.sonar.projectName")).isEqualTo("Module 12");
  117. assertThat(properties.getProperty("2.sonar.projectName")).isEqualTo("Module 2");
  118. // SONARUNNER-125
  119. assertThat(properties.getProperty("11.111.sonar.projectName")).isNull();
  120. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  121. }
  122. @Test
  123. void shouldLoadModuleConfigurationOverrideBasedir() throws Exception {
  124. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  125. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  126. Properties properties = conf.properties();
  127. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  128. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  129. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  130. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  131. }
  132. @Test
  133. void shouldCliOverrideSettingFiles() throws Exception {
  134. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  135. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  136. args.setProperty("module1.sonar.projectName", "mod1");
  137. args.setProperty("module2.sonar.projectName", "mod2");
  138. args.setProperty("module3.sonar.projectName", "mod3");
  139. Properties properties = conf.properties();
  140. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("mod1");
  141. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("mod2");
  142. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("mod3");
  143. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  144. }
  145. @Test
  146. void shouldUseCliToDiscoverModules() throws Exception {
  147. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  148. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  149. args.setProperty("sonar.modules", "module1");
  150. args.setProperty("module1.sonar.projectBaseDir", "module_3");
  151. Properties properties = conf.properties();
  152. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 3");
  153. assertThat(properties.getProperty("module2.sonar.projectName")).isNull();
  154. assertThat(properties.getProperty("module3.sonar.projectName")).isNull();
  155. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  156. }
  157. @Test
  158. void shouldNotUseCurrentDir() throws Exception {
  159. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  160. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  161. Properties properties = conf.properties();
  162. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  163. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  164. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  165. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  166. }
  167. @Test
  168. void shouldLoadModuleConfigurationWithoutRootConf() throws Exception {
  169. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationWithoutRootConf/project").toURI());
  170. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  171. args.put("sonar.modules", "module1,module2");
  172. Properties properties = conf.properties();
  173. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  174. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  175. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  176. }
  177. @Test
  178. void failModuleBaseDirDoesNotExist() {
  179. args.setProperty("sonar.modules", "module1");
  180. args.setProperty("module1.sonar.projectBaseDir", "invalid");
  181. assertThatIllegalStateException()
  182. .isThrownBy(conf::properties)
  183. .withMessageStartingWith("The base directory of the module 'module1' does not exist");
  184. }
  185. @Test
  186. void failModulePropertyFileDoesNotExist() {
  187. args.setProperty("sonar.modules", "module1");
  188. args.setProperty("module1.sonar.projectConfigFile", "invalid");
  189. assertThatIllegalStateException()
  190. .isThrownBy(conf::properties)
  191. .withMessageStartingWith("The properties file of the module 'module1' does not exist");
  192. }
  193. @Test
  194. void shouldSupportSettingBaseDirFromCli(@TempDir Path projectHome) throws Exception {
  195. Path projectBaseDir = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  196. args.setProperty("project.home", projectHome.toString());
  197. args.setProperty("sonar.projectBaseDir", projectBaseDir.toAbsolutePath().toString());
  198. Properties properties = conf.properties();
  199. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  200. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  201. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectBaseDir.toString());
  202. }
  203. @Test
  204. void ignoreEmptyModule(@TempDir Path projectHome) throws Exception {
  205. Path projectBaseDir = Paths.get(getClass().getResource("ConfTest/emptyModules/project").toURI());
  206. args.setProperty("project.home", projectHome.toString());
  207. args.setProperty("sonar.projectBaseDir", projectBaseDir.toAbsolutePath().toString());
  208. assertThatCode(conf::properties)
  209. .doesNotThrowAnyException();
  210. }
  211. @Test
  212. void shouldGetList() {
  213. Properties props = new Properties();
  214. props.put("prop", " foo ,, bar , \n\ntoto,tutu");
  215. assertThat(Conf.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
  216. }
  217. @Test
  218. void shouldNotResolveSymlinks(@TempDir Path root) throws IOException, URISyntaxException {
  219. assumeTrue(SystemUtils.IS_OS_UNIX);
  220. Path realProjectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  221. Path linkProjectHome = root.resolve("link");
  222. try {
  223. Files.createSymbolicLink(linkProjectHome, realProjectHome);
  224. args.setProperty("project.home", linkProjectHome.toAbsolutePath().toString());
  225. Properties properties = conf.properties();
  226. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  227. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  228. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(linkProjectHome.toString());
  229. assertThat(properties.getProperty("module1.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module1").toString());
  230. assertThat(properties.getProperty("module2.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module2").toString());
  231. } finally {
  232. Files.delete(linkProjectHome);
  233. }
  234. }
  235. // SQSCANNER-24
  236. @Test
  237. void should_load_project_settings_using_property() throws Exception {
  238. Path home = Paths.get(getClass().getResource("ConfTest/shouldOverrideProjectSettingsPath/").toURI());
  239. args.setProperty("project.home", home.toAbsolutePath().toString());
  240. Properties properties = conf.properties();
  241. assertThat(properties).containsEntry("sonar.prop", "default");
  242. args.setProperty("project.settings", home.resolve("conf/sq-project.properties").toAbsolutePath().toString());
  243. properties = conf.properties();
  244. assertThat(properties).containsEntry("sonar.prop", "expected");
  245. }
  246. }