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

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