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

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