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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * SonarQube Scanner
  3. * Copyright (C) 2011-2016 SonarSource SA
  4. * mailto:contact 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.fest.assertions.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.TemporaryFolder;
  38. public class ConfTest {
  39. @Rule
  40. public TemporaryFolder temp = new TemporaryFolder();
  41. Map<String, String> env = new HashMap<>();
  42. Properties args = new Properties();
  43. Logs logs = new Logs(System.out, System.err);
  44. Cli cli = mock(Cli.class);
  45. Conf conf = new Conf(cli, logs, env);
  46. @Before
  47. public void initConf() {
  48. env.clear();
  49. when(cli.properties()).thenReturn(args);
  50. }
  51. @Test
  52. public void should_load_global_settings_by_home() throws Exception {
  53. Path home = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByHome/").toURI());
  54. args.setProperty("scanner.home", home.toAbsolutePath().toString());
  55. Properties properties = conf.properties();
  56. assertThat(properties.get("sonar.prop")).isEqualTo("value");
  57. }
  58. @Test
  59. public void should_not_fail_if_no_home() throws Exception {
  60. assertThat(conf.properties()).isNotEmpty();
  61. // worst case, use current path
  62. assertThat(conf.properties().getProperty("sonar.projectBaseDir")).isEqualTo(Paths.get("").toAbsolutePath().toString());
  63. }
  64. @Test
  65. public void base_dir_can_be_relative() throws URISyntaxException, IOException {
  66. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  67. args.setProperty("project.home", projectHome.getParent().toAbsolutePath().toString());
  68. args.setProperty("sonar.projectBaseDir", "project");
  69. Properties properties = conf.properties();
  70. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  71. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  72. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  73. }
  74. @Test
  75. public void should_load_conf_by_direct_path() throws Exception {
  76. Path settings = Paths.get(getClass().getResource("ConfTest/shouldLoadRunnerSettingsByDirectPath/other-conf.properties").toURI());
  77. args.setProperty("scanner.settings", settings.toAbsolutePath().toString());
  78. assertThat(conf.properties().get("sonar.prop")).isEqualTo("otherValue");
  79. }
  80. @Test
  81. public void shouldLoadCompleteConfiguration() throws Exception {
  82. Path runnerHome = Paths.get(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/runner").toURI());
  83. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadCompleteConfiguration/project").toURI());
  84. args.setProperty("scanner.home", runnerHome.toAbsolutePath().toString());
  85. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  86. Properties properties = conf.properties();
  87. assertThat(properties.getProperty("project.prop")).isEqualTo("foo");
  88. assertThat(properties.getProperty("overridden.prop")).isEqualTo("project scope");
  89. assertThat(properties.getProperty("global.prop")).isEqualTo("jdbc:mysql:localhost/sonar");
  90. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  91. }
  92. @Test
  93. public void shouldLoadModuleConfiguration() throws Exception {
  94. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  95. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  96. Properties properties = conf.properties();
  97. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  98. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  99. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  100. }
  101. @Test
  102. public void shouldLoadEnvironmentProperties() throws IOException {
  103. env.put("SONARQUBE_SCANNER_PARAMS", "{\"sonar.key1\" : \"v1\", \"sonar.key2\" : \"v2\"}");
  104. args.put("sonar.key2", "v3");
  105. Properties props = conf.properties();
  106. assertThat(props.getProperty("sonar.key1")).isEqualTo("v1");
  107. assertThat(props.getProperty("sonar.key2")).isEqualTo("v3");
  108. }
  109. @Test
  110. public void shouldSupportDeepModuleConfigurationInRoot() throws Exception {
  111. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldSupportDeepModuleConfigurationInRoot/project").toURI());
  112. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  113. Properties properties = conf.properties();
  114. assertThat(properties.getProperty("1.sonar.projectName")).isEqualTo("Module 1");
  115. assertThat(properties.getProperty("1.11.sonar.projectName")).isEqualTo("Module 11");
  116. assertThat(properties.getProperty("1.11.111.sonar.projectName")).isEqualTo("Module 111");
  117. assertThat(properties.getProperty("1.12.sonar.projectName")).isEqualTo("Module 12");
  118. assertThat(properties.getProperty("2.sonar.projectName")).isEqualTo("Module 2");
  119. // SONARUNNER-125
  120. assertThat(properties.getProperty("11.111.sonar.projectName")).isNull();
  121. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  122. }
  123. @Test
  124. public void shouldLoadModuleConfigurationOverrideBasedir() throws Exception {
  125. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  126. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  127. Properties properties = conf.properties();
  128. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  129. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  130. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  131. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  132. }
  133. @Test
  134. public void shouldCliOverrideSettingFiles() throws Exception {
  135. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  136. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  137. args.setProperty("module1.sonar.projectName", "mod1");
  138. args.setProperty("module2.sonar.projectName", "mod2");
  139. args.setProperty("module3.sonar.projectName", "mod3");
  140. Properties properties = conf.properties();
  141. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("mod1");
  142. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("mod2");
  143. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("mod3");
  144. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  145. }
  146. @Test
  147. public void shouldUseCliToDiscoverModules() throws Exception {
  148. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  149. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  150. args.setProperty("sonar.modules", "module1");
  151. args.setProperty("module1.sonar.projectBaseDir", "module_3");
  152. Properties properties = conf.properties();
  153. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 3");
  154. assertThat(properties.getProperty("module2.sonar.projectName")).isNull();
  155. assertThat(properties.getProperty("module3.sonar.projectName")).isNull();
  156. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  157. }
  158. @Test
  159. public void shouldNotUseCurrentDir() throws Exception {
  160. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationOverrideBasedir/project").toURI());
  161. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  162. Properties properties = conf.properties();
  163. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  164. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  165. assertThat(properties.getProperty("module3.sonar.projectName")).isEqualTo("Module 3");
  166. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  167. }
  168. @Test
  169. public void shouldLoadModuleConfigurationWithoutRootConf() throws Exception {
  170. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfigurationWithoutRootConf/project").toURI());
  171. args.setProperty("project.home", projectHome.toAbsolutePath().toString());
  172. args.put("sonar.modules", "module1,module2");
  173. Properties properties = conf.properties();
  174. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  175. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  176. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(projectHome.toString());
  177. }
  178. @Test
  179. public void shouldSupportSettingBaseDirFromCli() throws Exception {
  180. Path projectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  181. args.setProperty("project.home", temp.newFolder().getCanonicalPath());
  182. args.setProperty("sonar.projectBaseDir", projectHome.toAbsolutePath().toString());
  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 ignoreEmptyModule() throws Exception {
  190. Path projectHome = Paths.get(getClass().getResource("ConfTest/emptyModules/project").toURI());
  191. args.setProperty("project.home", temp.newFolder().getCanonicalPath());
  192. args.setProperty("sonar.projectBaseDir", projectHome.toAbsolutePath().toString());
  193. conf.properties();
  194. }
  195. @Test
  196. public void shouldGetList() {
  197. Properties props = new Properties();
  198. props.put("prop", " foo ,, bar , \n\ntoto,tutu");
  199. assertThat(Conf.getListFromProperty(props, "prop")).containsOnly("foo", "bar", "toto", "tutu");
  200. }
  201. @Test
  202. public void shouldNotResolveSymlinks() throws IOException, URISyntaxException {
  203. assumeTrue(SystemUtils.IS_OS_UNIX);
  204. Path root = temp.getRoot().toPath();
  205. Path realProjectHome = Paths.get(getClass().getResource("ConfTest/shouldLoadModuleConfiguration/project").toURI());
  206. Path linkProjectHome = root.resolve("link");
  207. try {
  208. Files.createSymbolicLink(linkProjectHome, realProjectHome);
  209. args.setProperty("project.home", linkProjectHome.toAbsolutePath().toString());
  210. Properties properties = conf.properties();
  211. assertThat(properties.getProperty("module1.sonar.projectName")).isEqualTo("Module 1");
  212. assertThat(properties.getProperty("module2.sonar.projectName")).isEqualTo("Module 2");
  213. assertThat(properties.getProperty("sonar.projectBaseDir")).isEqualTo(linkProjectHome.toString());
  214. assertThat(properties.getProperty("module1.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module1").toString());
  215. assertThat(properties.getProperty("module2.sonar.projectBaseDir")).isEqualTo(linkProjectHome.resolve("module2").toString());
  216. } finally {
  217. if (linkProjectHome != null) {
  218. Files.delete(linkProjectHome);
  219. }
  220. }
  221. }
  222. }